If anyone has the time to take a quick look at this to point out the obvious much appreciated.
Ive gotten a bit farther and have enemies shooting multiple bullets in a straight line along the x and a nice scrolling background. Id like to have a second set of enemies almost the same coming from the y, but with its own set of collision so I can have them only collide with the correct color of player (Red/Blue) and give the game some tactics.
So I copied all the stuff from the enemies and enemiebullet tables with its own timers and such and renamed Red infront of them Redenenemybullet/enemybullet etc. But i was getting a bunch of errors that im calling a nil global value. Now my new enemies are scrolling down fine but my original enemies are gone and when it trys to spawn them throws a errors" Global variable enemy is not found" even though they were working previously
Code: Select all
debug = true
--Player Table
Player = { x = 20, y = 120, speed = 10, sprite = nil, HP = 10}
isAlive = false
score = 0
--
------
if Player.HP == 0 then
isAlive = false
end
--Scrolling
windowx = love.graphics.getWidth()
bg1 = {}
bg1.img = love.graphics.newImage("back1.png")
bg1.x = 0
bg1.width = bg1.img:getWidth()
speed = 30
--MOUSE STUFF
function Player.move_to(_x, _y)
dx = _x - Player.x
dy = _y - Player.y
length = math.sqrt(dx*dx+dy*dy);
dx = (dx/length)
dy = (dy/length)
Player.x = (Player.x + dx * Player.speed)
Player.y = (Player.y + dy * Player.speed)
end
------
----Bullet Stuff
-- Timers
-- We declare these here so we don't have to edit them multiple places
canShoot = true
canShootTimerMax = 0.1
canShootTimer = canShootTimerMax
---- Image Storage
bulletSprite = nil
--Red Bullet
-- Entity Storage
bullets = {}
--Red Entity Storage
----Enemy Stuff--------
--More timers
createEnemyTimerMax = .5
createEnemyTimer = createEnemyTimerMax
--Red Enemy timer
createRedEnemyTimerMax = .5
createRedEnemyTimer = createRedEnemyTimerMax
-- More storage
enemies = {}-- array of current enemies on screen
--Red Enemies
Redenemies = {}
-- More images
enemySprite = nil
--2nd enemy!!
RedenemySprite =nil
---Regular enemy shot
enemyBulletSprite = nil
enemyBullets = {}
canShootEnemy = true
canShootEnemyTimerMax = 0.3
canShootEnemyTimer = canShootEnemyTimerMax
--2nd enemy shot
RedenemyBulletSprite = nil
RedenemyBullets = {}
--RedcanShoot
RedcanShootEnemy = true
RedcanShootEnemyTimerMax = 0.3
RedcanShootEnemyTimer = RedcanShootEnemyTimerMax
--
function love.load(arg)
--Lets draw to the Top Screen
love.graphics.setScreen('top')
--
Player.sprite = love.graphics.newImage('Plane.png')
enemyBulletSprite = love.graphics.newImage('EnemyBullet.png')
bulletSprite = love.graphics.newImage('Bullet.png')
enemySprite = love.graphics.newImage('Enemy.png')
Background = love.graphics.newImage('Background.png')
BottomBG = love.graphics.newImage('bottombg.png')
WonTop = love.graphics.newImage('WonTop.png')
WonBottom = love.graphics.newImage('WonBottom.png')
--2nd enemy
RedenemySprite = love.graphics.newImage('Enemy.png')
RedenemyBulletSprite = love.graphics.newImage('Bullet.png')
end
--
function love.draw(dt)----------------------------
--
love.graphics.setScreen('bottom')
love.graphics.draw(BottomBG, 0, 0)
love.graphics.setScreen('top')
love.graphics.draw(Background, 0, 0)
love.graphics.draw(bg1.img, bg1.x, 0)
--Starting Menu
if Player.HP == 0 then
isAlive = false
end
if isAlive then
for i, enemyBullet in ipairs(enemyBullets) do
love.graphics.draw(enemyBullet.sprite, enemyBullet.x, enemyBullet.y)
end
--
--
love.graphics.draw(Player.sprite, Player.x, Player.y)
--
for i, bullet in ipairs(bullets) do
love.graphics.draw(bullet.sprite, bullet.x, bullet.y)
end
--
for i, enemy in ipairs(enemies) do
love.graphics.draw(enemy.sprite, enemy.x, enemy.y)
end
-- 2nd Enemy
for i, RedenemyBullet in ipairs(RedenemyBullets) do
love.graphics.draw(RedenemyBullet.sprite, RedenemyBullet.x, RedenemyBullet.y)
end
for i, Redenemy in ipairs(Redenemies) do
love.graphics.draw(Redenemy.sprite, Redenemy.x, Redenemy.y)
end
--
love.graphics.print("Score: " .. score, love.graphics:getWidth()-80, 15)
love.graphics.print("Lives: " .. Player.HP, 10, 15)
else
love.graphics.print("You Have Lost", love.graphics:getWidth()/2-50, love.graphics:getHeight()/2-10)
love.graphics.print("Your score is: " .. score, love.graphics:getWidth()/2-50, love.graphics:getHeight()/2-30)
love.graphics.setScreen('bottom')
love.graphics.draw(WonBottom, 0, 0)
end
----Still Updating MOuse MOvement
if love.mouse.isDown('l') then --Check every frame if left mouse button is down
local mousex, mousey = love.mouse.getPosition() --Get mousex and mousey because it's not given to us
Player.move_to(mousex, mousey)
end
end
--
function love.update(dt)
--BG SCROLLINg
bg1.x = bg1.x - speed * dt
if bg1.x < 0 then
bg1.x = bg1.x + 0
end
-- Time out enemy creation
createEnemyTimer = createEnemyTimer - (1*dt)
if createEnemyTimer < 0 then
createEnemyTimer = createEnemyTimerMax
-- Create an enemy
rand = math.random(10, love.graphics.getHeight() - 10 )
newEnemy = { x = 410, y = rand, sprite = enemySprite }
newEnemy.canShoot = true
newEnemy.timer = 0
table.insert(enemies, newEnemy)
end
-- 2nd enemy timer
createRedEnemyTimer = createRedEnemyTimer - (1*dt)
if createRedEnemyTimer < 0 then
createRedEnemyTimer = createRedEnemyTimerMax
--2nd enemy creation
-- Create an enemy
rand = math.random(10, love.graphics.getHeight() - 10 )
newRedEnemy = { x = rand, y = -10, sprite = RedenemySprite }
newRedEnemy.canShoot = true
newRedEnemy.timer = 0
table.insert(Redenemies, newRedEnemy)
end
if Player.HP == 0 then
isAlive = false
end
if isAlive == true then
--Check Shooting
for i, enemy in ipairs(enemies) do
-- Update Shooting interval timer ---
enemy.timer = enemy.timer - (1*dt)
if enemy.timer < 0 then
enemy.canShoot = true
end
end
-- Spawn Bullet --
for i, enemy in ipairs(enemies) do
if enemy.canShoot then
newEnemyBullet = { x = enemy.x + enemy.sprite:getWidth()/2 - enemyBulletSprite:getWidth()/2, y = enemy.y + enemy.sprite:getHeight() - enemyBulletSprite:getHeight(), sprite = enemyBulletSprite}
table.insert(enemyBullets, newEnemyBullet)
enemy.canShoot = false
enemy.timer = canShootEnemyTimerMax
end
end
--2nd enemy updat timer
for i, Redenemy in ipairs(Redenemies) do
Redenemy.timer = Redenemy.timer - (1*dt)
if Redenemy.timer < 0 then
Redenemy.canShoot = true
end
end
--2nd enemy spawn bullet
for i, Redenemy in ipairs(Redenemies) do
if Redenemy.canShoot then
newRedEnemyBullet = { x = Redenemy.x + Redenemy.sprite:getWidth()/2 - RedenemyBulletSprite:getWidth()/2, y = Redenemy.y + Redenemy.sprite:getHeight() - RedenemyBulletSprite:getHeight(), sprite = RedenemyBulletSprite}
table.insert(RedenemyBullets, RednewEnemyBullet)
Redenemy.canShoot = false
Redenemy.timer = canShootEnemyTimerMax
end
end
-- Check for Collisions ---- run our collision detection
for j, bullet in ipairs(bullets) do
if enemy.HP == nil then
enemy.HP = 2
end
if CheckCollision(enemy.x, enemy.y, enemy.sprite:getWidth(), enemy.sprite:getHeight(), bullet.x, bullet.y, bullet.sprite:getWidth(), bullet.sprite:getHeight()) then
enemy.HP = enemy.HP - 1
table.remove(bullets, j)
if enemy.HP == 0 then
table.remove(enemies, i)
score = score + 10
enemy.HP = 2
end
end
end
--Shooting enemies-- run our collision detection
for k, enemyBullet in ipairs(enemyBullets) do
if CheckCollision(enemyBullet.x, enemyBullet.y, enemyBullet.sprite:getWidth(), enemyBullet.sprite:getHeight(), Player.x, Player.y, Player.sprite:getWidth(), Player.sprite:getHeight())
and isAlive then
Player.HP = Player.HP - 1
table.remove(enemyBullets, k)
end
end
for k, enemyBullet in ipairs(enemyBullets) do
if CheckCollision(enemy.x, enemy.y, enemy.sprite:getWidth(), enemy.sprite:getHeight(), Player.x, Player.y, Player.sprite:getWidth(), Player.sprite:getHeight())
and isAlive then
Player.HP = Player.HP - 1
table.remove(enemies, i)
end
end
--Shooting-- Player Shots Time out how far apart our shots can be. PLayer Shots?
canShootTimer = canShootTimer - (1*dt)
if canShootTimer < 0 then
canShoot = true
end
-- PLayer Shooting
if love.keyboard.isDown('cpaddown') and canShoot then
-- Create some bullets
newBullet = { x = Player.x + (Player.sprite:getWidth()/2) - (bulletSprite:getWidth()/2), y = Player.y, sprite = bulletSprite}
table.insert(bullets, newBullet)
canShoot = false
canShootTimer = canShootTimerMax
end
--Bullets move-- update the positions of bullets
for i, bullet in ipairs(bullets) do
bullet.x = bullet.x - (350 * dt)
if bullet.y < 0 then -- remove bullets when they pass off the screen
table.remove(bullets, i)
end
end
--Enemy bullets move
for i, enemyBullet in ipairs(enemyBullets) do
enemyBullet.x = enemyBullet.x - (350 * dt)
if enemyBullet.x < 0 then
table.remove(enemyBullets, i)
end
end
--2nd enemy bullets move
for i, RedenemyBullet in ipairs(RedenemyBullets) do
RedenemyBullet.y = RedenemyBullet.y + (350 * dt)
if RedenemyBullet.x < 0 then
table.remove(RedenemyBullets, i)
end
end
--Enemies move-- update the positions of enemies
for i, enemy in ipairs(enemies) do
enemy.x = enemy.x - (200*dt)
if enemy.x < 0 then
table.remove(enemies, i)
if score > 0 then
score = score - 50
end
end
end
--2nd enemies move
for i, Redenemy in ipairs(Redenemies) do
Redenemy.y = Redenemy.y + (200*dt)
if Redenemy.x < 0 then
table.remove(Redenemies, i)
if score > 0 then
score = score - 50
end
end
end
--Moving
if love.keyboard.isDown('left') then
if Player.x > 0 then
Player.x = Player.x - (Player.speed*dt)
end
elseif love.keyboard.isDown('right') then
if Player.x < (love.graphics.getWidth() - Player.sprite:getWidth()) then
Player.x = Player.x + (Player.speed*dt)
end
end
if love.keyboard.isDown('up') then
if Player.y > 0 then
Player.y = Player.y - (Player.speed*dt)
end
end
if love.keyboard.isDown('down') then
if Player.y < (love.graphics.getHeight() - Player.sprite:getHeight()) then
Player.y = Player.y + (Player.speed*dt)
end
end
--When u dead
else
--Restarting after death
if love.keyboard.isDown('a') then
bullets = {}
enemyBullets = {}
RedenemyBullets = {}
enemies = {}
Redenemies = {}
canShootTimer = canShootTimerMax
RedcanShootTimer = RedShootTimerMax
createEnemyTimer = createEnemyTimerMax
createRedEnemyTimer = createRedEnemyTimerMax
score = 0
isAlive = true
Player.HP = 5
Player.x = 40
Player.y = 120
end
enda
end
-- Escape
-- Collision detection taken function from http://love2d.org/wiki/BoundingBox.lua
-- Returns true if two boxes overlap, false if they don't
-- x1,y1 are the left-top coords of the first box, while w1,h1 are its width and height
-- x2,y2,w2 & h2 are the same, but for the second box
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end