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
Write Your First Game in Only 25 Minutes!
26th August 2019
Learn to Code Space Invaders – Lesson 16 – Alien Speed Control
28th August 2019
Published by Bob at 28th August 2019
Categories
  • Learn to Code Space Invaders
Tags

Space Invaders Lesson 15 – Aliens on the Move

Code for Lesson 15

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

playerShip = {

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

playerBullets = {}
maxPlayerBullets = 5

aliens = {}
alienRows = 6
alienColumns = 8
alienHorzSpacing = 12
alienVertSpacing = 12
alienDirection = 1
alienSpeed = 4
alienVertSpeed = 4
alienMaxX = 232
alienMinX = 0




gameNeedsToBeInitialised = true

function TIC()

 -- game initialisation
 -- check if game needs to be initialised
	if (gameNeedsToBeInitialised) then
		initialiseGame()
		gameNeedsToBeInitialised = false
	end
	
	-- updating
	
	movePlayerShip()
	
	checkPlayerFire()
	movePlayerBullet()
	moveAliens()
	
	-- drawing / rendering
	
	cls()
	
	drawPlayerBullet()
	
	drawPlayerShip()
	
	drawAliens()
	
	
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()

	local bulletFired = false
	local bullet = 1
	
	-- if fire button is pressed then
	if (btnp(4)) then
		--  find a bullet that's ready to fire
		
	 while (bullet <= maxPlayerBullets) and (not bulletFired) do
				if (not playerBullets[bullet].active) then
						--   initialise bullet
						playerBullets[bullet].position = {
							x = playerShip.position.x + playerShip.bulletOffset.x,
							y = playerShip.position.y + playerShip.bulletOffset.y
						}
						--   mark bullet as active
						playerBullets[bullet].active = true
						--   stop other bullets from firing
						bulletFired = true
				end -- if not active
			bullet = bullet + 1
	
		end -- while

	end -- if button pressed
	
end -- checkPlayerFire

function movePlayerBullet()

	for bullet = 1, maxPlayerBullets do
	
		if (playerBullets[bullet].active) then
			-- move the bullet up the screen
			playerBullets[bullet].position.y = 
			playerBullets[bullet].position.y - playerBullets[bullet].speed
			
			if (playerBullets[bullet].position.y < 0) then
				playerBullets[bullet].active = false
			end
		end
		
	end
		
end -- movePlayerBullet

function drawPlayerBullet()

	for bullet = 1, maxPlayerBullets do
	
		if (playerBullets[bullet].active) then
		
			-- draw player bullet
			-- line(startX, StartY, endX, endY, colour)
			line(
				playerBullets[bullet].position.x,
				playerBullets[bullet].position.y,
				playerBullets[bullet].position.x,
				playerBullets[bullet].position.y + playerBullets[bullet].length,
				playerBullets[bullet].colour
			)
		
		end -- if
	
	end -- for

end -- drawPlayerBullet

function drawPlayerShip()

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


end -- drawPlayerShip

function initialiseGame()

	initPlayerBulletsArray()
	initAliens()

end -- initialiseGame

function initPlayerBulletsArray()

	for bullet = 1, maxPlayerBullets do
		playerBullets[bullet] = {
			position = {
				x = 0,
				y = 0
			},
	
			length = 5,
			colour = 14,
			speed = 2,
			active = false
		}
	end -- for

end -- initPlayerBulletsArray

function initAliens()

	-- create alien grid
	for row = 1, alienRows do
		-- create row of aliens
		aliens[row] = {}
		for column = 1,alienColumns do  
			-- create alien
			aliens[row][column] = {
				position = {
					x = (column - 1) * alienHorzSpacing,
					y = (row - 1) * alienVertSpacing
				},
				alive = true
			}
		-- end row
		end
	-- end alien grid
	end
	

end -- initAliens

function drawAliens()

	for row = 1, alienRows do
	
		for column = 1, alienColumns do
		
			if (aliens[row][column].alive) then
				spr(16, aliens[row][column].position.x,
					aliens[row][column].position.y)
			end -- if
		
		end -- columns
	
	end -- rows

end -- drawAliens

function moveAliens()
	
	if aliensAtEdge() then
	
		-- move down
		for row = 1, alienRows do
			for column = 1, alienColumns do	
				if (aliens[row][column].alive) then		
					aliens[row][column].position.y = 
						aliens[row][column].position.y +
						(alienVertSpeed)		
				end -- if	
			end -- columns
		end -- rows
		
		alienDirection = -alienDirection
		
	else
		for row = 1, alienRows do
			for column = 1, alienColumns do	
				if (aliens[row][column].alive) then		
					aliens[row][column].position.x = 
						aliens[row][column].position.x +
						(alienSpeed * alienDirection)		
				end -- if	
			end -- columns
		end -- rows
		
	end -- if


end -- moveAliens

function aliensAtEdge()

	-- for each alien
	for row = 1, alienRows do
		for column = 1, alienColumns do
			-- if alien is alive then
			if (aliens[row][column].alive) then
		 	-- if alien going right +1
				if (alienDirection == 1) then
					-- if alien x + step > MaxX then
					if (aliens[row][column].position.x + alienSpeed) >
						alienMaxX then
						-- return true
						return true
					-- end if
					end
				-- else going left -1
				else
					if (aliens[row][column].position.x - alienSpeed) <
						alienMinX then
						-- return true
						return true
					-- end if
					end
				-- end if - going left right
				end
			-- end if alive
			end
		end -- for columns
	end -- for rows	
	-- end for
	
	-- return false
	return false
		
end -- aliensAtEdge
Share
5
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