Setting player scale -1 when going left?
Posted: Fri Aug 23, 2019 8:23 pm
Hello, i'm kinda new to the world of programming, and can't seem to get my code work. There's a couple of things i would like to do, first is the scale thing, i could't get it work with an "if love.keyboard.isDown('a') then" statement followed by "player.img = Transform:scale(-1,1)".
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:
To finish, i would like to know if anyone has good and recent resources & tutorials for Lua in Love2D, i'm pretty lost to all this. There's a couple of tutorials, but some of them are old, others are not well explained. Some tips on this code would work too, i know i'm doing everything on Main.lua and that's a bit messy.
Thanks.
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.