Problem with Space Invaders
Posted: Wed Jun 13, 2018 3:15 pm
Hi. I'm not the best programmer and im not very good at LOVE 2D. So here is the problem. Im on the way of creating a clone of Space Invaders but i encountered a problem with asynchroonus enemies movement. When any enemy touches bounds of the screen function enemies_jump() is called, which calls Enemy:jump() for every enemy. This function is reversing speed of enemies and increasing thier Y position by 64. The middle row of enemies is moving in asynchronous way (it shouldn't). The X pos of most left enemy is weird compared to the rest of the aliens. I dont know why it is happening. They should move all together in the same pattern.
Can you help me? Im using classic.lua library.
Here is the code:
main.lua
player.lua
bullet.lua
enemy.lua
Whole project is attached in zip file
Thank you in advance.
Can you help me? Im using classic.lua library.
Here is the code:
main.lua
Code: Select all
Object = require "classic"
require "player"
require "bullet"
require "enemy"
-- constants
WINDOW_WIDTH = 1024
WINDOW_HEIGHT = 768
function love.load()
-- window
love.window.setMode(WINDOW_WIDTH, WINDOW_HEIGHT, {resizable = false, vsync = true})
love.graphics.setDefaultFilter('nearest', 'nearest')
love.window.setTitle("Space Invaders")
-- graphics
background = love.graphics.newImage("background.png")
ground = love.graphics.newImage("ground.png")
spritesheet = love.graphics.newImage("sprites.png")
rocket = love.graphics.newQuad(0,0,16,16,spritesheet:getDimensions())
alien1 = love.graphics.newQuad(16,0,16,16,spritesheet:getDimensions())
alien2 = love.graphics.newQuad(32,0,16,16,spritesheet:getDimensions())
alien3 = love.graphics.newQuad(48,0,16,16,spritesheet:getDimensions())
bullet = love.graphics.newQuad(64,0,16,16,spritesheet:getDimensions())
charge = love.graphics.newQuad(80,0,16,16,spritesheet:getDimensions())
heart = love.graphics.newQuad(96,0,16,16,spritesheet:getDimensions())
-- sounds
shoot = love.audio.newSource("shoot.wav", "static")
hit = love.audio.newSource("hit.wav", "static")
gameover = love.audio.newSource("gameover.wav", "static")
-- varibles
player = Player(512, 576, 3)
bullets = {} -- table of bullets
enemies = {}
spawn_enemies()
end
function spawn_enemies()
enemy_speed = 150
table.insert(enemies, Enemy(0, 0, "alien3", enemy_speed))
table.insert(enemies, Enemy(64, 0, "alien3", enemy_speed))
table.insert(enemies, Enemy(128, 0, "alien3", enemy_speed))
table.insert(enemies, Enemy(192, 0, "alien3", enemy_speed))
table.insert(enemies, Enemy(256, 0, "alien3", enemy_speed))
table.insert(enemies, Enemy(320, 0, "alien3", enemy_speed))
table.insert(enemies, Enemy(384, 0, "alien3", enemy_speed))
table.insert(enemies, Enemy(448, 0, "alien3", enemy_speed))
table.insert(enemies, Enemy(512, 0, "alien3", enemy_speed))
table.insert(enemies, Enemy(576, 0, "alien3", enemy_speed))
table.insert(enemies, Enemy(0, 64, "alien2", enemy_speed))
table.insert(enemies, Enemy(64, 64, "alien2", enemy_speed))
table.insert(enemies, Enemy(128, 64, "alien2", enemy_speed))
table.insert(enemies, Enemy(192, 64, "alien2", enemy_speed))
table.insert(enemies, Enemy(256, 64, "alien2", enemy_speed))
table.insert(enemies, Enemy(320, 64, "alien2", enemy_speed))
table.insert(enemies, Enemy(384, 64, "alien2", enemy_speed))
table.insert(enemies, Enemy(448, 64, "alien2", enemy_speed))
table.insert(enemies, Enemy(512, 64, "alien2", enemy_speed))
table.insert(enemies, Enemy(576, 64, "alien2", enemy_speed))
table.insert(enemies, Enemy(0, 128, "alien1", enemy_speed))
table.insert(enemies, Enemy(64, 128, "alien1", enemy_speed))
table.insert(enemies, Enemy(128, 128, "alien1", enemy_speed))
table.insert(enemies, Enemy(192, 128, "alien1", enemy_speed))
table.insert(enemies, Enemy(256, 128, "alien1", enemy_speed))
table.insert(enemies, Enemy(320, 128, "alien1", enemy_speed))
table.insert(enemies, Enemy(384, 128, "alien1", enemy_speed))
table.insert(enemies, Enemy(448, 128, "alien1", enemy_speed))
table.insert(enemies, Enemy(512, 128, "alien1", enemy_speed))
table.insert(enemies, Enemy(576, 128, "alien1", enemy_speed))
end
function enemies_jump()
for i, enemy in pairs (enemies) do
enemy:jump()
print("Enemy"..i.. " x:" .. enemy.x .. " y: " .. enemy.y .. " speed: " .. enemy.x_speed)
end
end
function love.update(dt)
player:update(dt)
for index, bullet in pairs (bullets) do
bullet:update(dt)
if bullet.y + bullet.height < 0 then
table.remove(bullets, index)
end
end
for i, enemy in pairs (enemies) do
enemy:update(dt)
end
end
function love.draw()
love.graphics.draw(background, 0, 0)
love.graphics.draw(ground, 0, 640, 0, 4, 4)
player:draw()
for index,bullet in pairs (bullets) do
bullet:draw()
end
for i, enemy in pairs (enemies) do
enemy:draw()
end
end
Code: Select all
Player = Object:extend()
require "bullet"
function Player:new(x, y, hearts)
self.x = x
self.y = y
self.width = 64
self.height = 64
self.hearts = hearts
self.speed = 300
self.cooldown = 0
end
function Player:shot(self)
table.insert(bullets, Bullet(self.x, self.y - 48))
self.cooldown = 0.5
end
function Player:update(dt)
self.cooldown = self.cooldown - dt
if self.x + self.width > WINDOW_WIDTH then
self.x = WINDOW_WIDTH - self.width
end
if self.x < 0 then
self.x = 0
end
if love.keyboard.isDown("left") then
self.x = self.x - self.speed * dt
elseif love.keyboard.isDown("right") then
self.x = self.x + self.speed * dt
end
if love.keyboard.isDown("space") and self.cooldown < 0 then
Player:shot(self)
end
end
function Player:draw()
for i = 1, self.hearts do
love.graphics.draw(spritesheet, heart, (i - 1) * 64, WINDOW_HEIGHT - 64, 0, 4, 4)
end
love.graphics.draw(spritesheet, rocket, self.x, self.y, 0, 4, 4)
end
Code: Select all
Bullet = Object:extend()
function Bullet:new(x, y)
self.x = x
self.y = y
self.width = 20
self.height = 40
self.speed = 460
end
function Bullet:update(dt)
self.y = self.y - (self.speed * dt)
end
function Bullet:draw()
love.graphics.draw(spritesheet, bullet, self.x + 32, self.y + 32, 0, 4, 4, 8,8)
end
Code: Select all
Enemy = Object:extend()
enemies_count = 0
function Enemy:new(x, y, typeof, x_speed)
self.x = x
self.y = y
self.typeof = typeof
self.x_speed = x_speed
enemies_count = enemies_count + 1
end
function Enemy:jump()
self.y = self.y + 64
self.x_speed = - self.x_speed
end
function Enemy:update(dt)
self.x = self.x + (self.x_speed * dt)
if self.x <= 0 then
self.x = 0
enemies_jump()
end
if self.x + 64 >= WINDOW_WIDTH then
self.x = WINDOW_WIDTH - 64
enemies_jump()
end
end
function Enemy:draw()
if self.typeof == "alien1" then
love.graphics.draw(spritesheet, alien1, self.x, self.y, 0, 4, 4)
elseif self.typeof == "alien2" then
love.graphics.draw(spritesheet, alien2, self.x, self.y, 0, 4, 4)
else
love.graphics.draw(spritesheet, alien3, self.x, self.y, 0, 4, 4)
end
end
Thank you in advance.