logologologologo
  • home
  • learn to code
    • Space Invaders – Beginner’s Course
    • Asteroids – Beginner’s Level 2
    • Space Commander – Beginners Level 3
    • Snake – a Beginner’s Challenge
  • learn to make
    • Arduino Tutorials
    • Raspberry Retro Games Console
  • blog
    • Computer Science
    • Coding
      • Collision Detection
    • Retro Gaming
    • Computer Games for Vision Therapy
  • contact me
Learn to Code Space Invaders – Lesson 7 – Functions
27th July 2019
Learn to Code Space Invaders – Lesson 9 – Arrays and Loops (part 1)
31st July 2019
Published by Bob at 30th July 2019
Categories
  • Learn to Code Space Invaders
Tags

Space Invaders Lesson 8 – Debugging the Firing Code

Code for Lesson 8

-- title:  Space Invaders
-- author: Bob Grant
-- desc:   Lesson 7
-- script: lua

playerShip = {

	position = {
		x = 120,
		y = 128
	},
	
	spriteNum = 0,
	minX = 0,
	maxX = 232,
	speed = 1,
	
	bulletOffset = {
		x = 4,
		y = 4
	}
}

playerBullet = {

	position = {
		x = 0,
		y = 0
	},
	
	length = 5,
	colour = 14,
	speed = 2,
	active = false
	
}

function TIC()
	cls()
	
	movePlayerShip()
	
	checkPlayerFire()
	movePlayerBullet()
	
	drawPlayerBullet()
	
	drawPlayerShip()
	
	
end -- end TIC

function movePlayerShip()

	-- check move right button
	if(btn(2)) then
	 playerShip.position.x = 
			playerShip.position.x - playerShip.speed
	end
	-- check move left button
	if(btn(3)) then
	 playerShip.position.x = 
			playerShip.position.x + playerShip.speed
	end
	
	playerShip.position.x = checkLimits(
		playerShip.position.x,
		playerShip.minX,
		playerShip.maxX )

end -- movePlayerShip

function checkLimits(value, min, max)

	if (value > max) then
		value = max
	elseif (value < min) then
		value = min
	else
		value = value
	end
	
	return value

end -- checkLimits

function checkPlayerFire()

	if (not playerBullet.active) then
		-- check if the fire button is pressed
		if (btnp(4)) then
		-- if pressed make a bullet appear
		-- at the player ship
			playerBullet.position = {
				x = playerShip.position.x + playerShip.bulletOffset.x,
				y = playerShip.position.y + playerShip.bulletOffset.y
			}
			playerBullet.active = true
		end
	end -- not active
	
end -- checkPlayerFire

function movePlayerBullet()

	if (playerBullet.active) then
		-- move the bullet up the screen
		playerBullet.position.y = 
			playerBullet.position.y - playerBullet.speed
			
		if (playerBullet.position.y < 0) then
			playerBullet.active = false
		end
	end
		
end -- movePlayerBullet

function drawPlayerBullet()

	if (playerBullet.active) then
		-- draw player bullet
		-- line(startX, StartY, endX, endY, colour)
		line(
			playerBullet.position.x,
			playerBullet.position.y,
			playerBullet.position.x,
			playerBullet.position.y + playerBullet.length,
			playerBullet.colour
		)
	end
	
end -- drawPlayerBullet

function drawPlayerShip()

	spr(playerShip.spriteNum, 
		playerShip.position.x, 
		playerShip.position.y, 0)


end -- drawPlayerShip
Share
4
Bob
Bob

Related posts

17th November 2019

Learn to Code Space Invaders – Lesson 25 – Level Up!


Read more
17th November 2019

Learn to Code Space Invaders – Lesson 24 – The Aliens Have Landed


Read more
16th November 2019

Learn to Code Space Invaders – Lesson 23 – Player Lives and Game States


Read more

Comments are closed.

  • Privacy Policy
  • Contact Me
© 2020 Bytes N Bits