Another thing i'm unsure, if how do i make the bullets dissapear when they pass the limits of y-axis. I got the "if bullet.x < 0 then" from a tutorial, but i wanted to do something like "if bullet.x&bullet.y < 0 then", is it possible without creating another "if" statement?
The code:
Code: Select all
-- Player & enviroment
platform = {}
player = {}
--Bullets
bullets = {}
canShoot = true
canShootTimerMax = 0.2
canShootTimer = canShootTimerMax
bulletImg = nil
function love.load()
--Platform
platform.width = love.graphics.getWidth()
platform.height = love.graphics.getHeight()
platform.x = 0
platform.y = platform.height / 2
--Player
player.x = love.graphics.getWidth() / 2
player.y = love.graphics.getHeight() / 2
player.speed = 200
player.img = love.graphics.newImage('gallina.png')
player.ground = player.y
player.y_velocity = 0
player.jump_height = -300
player.gravity = -500
--Bullets
bulletImg = love.graphics.newImage('bullet.png')
end
function love.update(dt)
--Player movement
if love.keyboard.isDown('d') then
if player.x < (love.graphics.getWidth() - player.img:getWidth()) then
player.x = player.x + (player.speed * dt)
end
elseif love.keyboard.isDown('a') then
if player.x > 0 then
player.x = player.x - (player.speed * dt)
end
end
if love.keyboard.isDown('space') then
if player.y_velocity == 0 then
player.y_velocity = player.jump_height
end
end
if player.y_velocity ~= 0 then
player.y = player.y + player.y_velocity * dt
player.y_velocity = player.y_velocity - player.gravity * dt
end
if player.y > player.ground then
player.y_velocity = 0
player.y = player.ground
end
--Bullet timer
canShootTimer = canShootTimer - (1 * dt)
if canShootTimer < 0 then
canShoot = true
end
--Bullet shoot
if love.keyboard.isDown('q') and canShoot then
-- Create some bullets
newBullet = { x = player.x, y = player.y, img = bulletImg }
table.insert(bullets, newBullet)
canShoot = false
canShootTimer = canShootTimerMax
end
--Update the positions of bullets
for i, bullet in ipairs(bullets) do
bullet.x = bullet.x + (250 * dt)
if bullet.x < 0 then -- remove bullets when they pass off the screen
table.remove(bullets, i)
end
end
end
function love.draw()
--Platform
love.graphics.setColor(1, 1, 1)
love.graphics.rectangle('fill', platform.x, platform.y, platform.width, platform.height)
--Player
love.graphics.draw(player.img, player.x, player.y, 0, 0.5, 0.5, 0, 32)
--bullets
for i, bullet in ipairs(bullets) do
love.graphics.draw(bullet.img, bullet.x, bullet.y)
end
end
Thanks.