logologologologo
  • home
  • learn to code
    • Space Invaders – Beginner’s Course
    • Asteroids – Beginner’s Level 2
    • Snake – a Beginner’s Challenge
  • learn to make
    • Arduino Tutorials
  • blog
    • Computer Science
    • Coding
      • Collision Detection
    • Retro Gaming
    • Computer Games for Vision Therapy
  • contact me
Learn to Code Space Invaders – Lesson 14 – The Aliens Are Here!
9th August 2019
Learn to Code Space Invaders – Lesson 15 – Aliens on the Move
28th August 2019
Published by Bob at 26th August 2019
Categories
  • Learn To Code
Tags

Write Your First Computer Game in Only 25 Minutes!

Starting with just a standard computer we’ll install our games development system and write a computer game from scratch in just 25 minutes!

Follow me as I build the game live on screen so you can see just how easy it is to get set up and coding with my ‘Learn To Code’ courses.

Make learning to code fun by building your own games!

Anyone can learn to code so click play below to get started right now!

Code Listing

-- title:  Pong
-- author: Bob Grant
-- desc:   Pong Game
-- script: lua

screenMaxX = 232
screenMaxY = 120

ball = {
	x = 120,
	y = 0,
	xSpeed = 2,
	ySpeed = 1
}

bat = {
	x = 114,
	y = 120,
	speed = 4
}

score = 0

function TIC()

	moveBat()
	moveBall()
	checkHit()
	
	cls()
	
	print("score = "..score,3,3)
	
	drawWalls()
	drawBat()
	drawBall()

end

function moveBat()

	if (btn(2)) then
		bat.x = bat.x - bat.speed
	end
	if (btn(3)) then
		bat.x = bat.x + bat.speed
	end
	if (bat.x > (screenMaxX - 8)) then
		bat.x = screenMaxX - 8
	elseif (bat.x < 0) then
		bat.x = 0
	end

end

function drawBat()

	spr(0, bat.x, bat.y, 0,1,0,0,2,1)

end

function drawWalls()

	line(0,127,0,0,15)
	line(0,0,239,0,15)
	line(239,0,239,127,15)

end

function moveBall()

	ball.x = ball.x + ball.xSpeed
	ball.y = ball.y + ball.ySpeed
	
	if (ball.x > screenMaxX) then
		ball.x = screenMaxX
		ball.xSpeed = -ball.xSpeed
		sfx(0,40,5)
	elseif (ball.x < 0) then
		ball.x = 0
		ball.xSpeed = -ball.xSpeed
		sfx(0,40,5)
	end
	
	if (ball.y < 0) then
		ball.y = 0
		ball.ySpeed = -ball.ySpeed
		sfx(0,50,5)
	end		

end

function drawBall()

	spr(16, ball.x, ball.y)

end

function checkHit()

	if (ball.ySpeed > 0) then
	
		if (ball.y > (bat.y - 8))
			and (ball.y < (bat.y)) then
			
				if (ball.x > (bat.x - 6))
				 and (ball.x < (bat.x + 22)) then
				
					ball.ySpeed = -ball.ySpeed
					ball.xSpeed = ball.xSpeed +
						math.random(-1,1)
					score = score + 1
					sfx(0,20,5)
					
				end
				
			
		end
	
	
	end

end
Share
1
Bob
Bob

Related posts

Learn to code Snake
8th October 2020

Learn to Code Snake in TIC80 – Code Walkthrough


Read more
Learn to code snake
8th October 2020

Learn to Code Snake in TIC80 – Game Overview


Read more
30th December 2018

Getting Ready to Learn To Code


Read more

Comments are closed.

  • Privacy Policy
  • Contact Me
© 2020 Bytes N Bits
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok