Setting player scale -1 when going left?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
fela
Prole
Posts: 2
Joined: Fri Aug 23, 2019 8:13 pm

Setting player scale -1 when going left?

Post by fela »

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:

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
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.
User avatar
Sasha264
Party member
Posts: 131
Joined: Mon Sep 08, 2014 7:57 am

Re: Setting player scale -1 when going left?

Post by Sasha264 »

Welcome! :3

About flipping player:
I made some changes in your 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.direction = 1

	player.speed = 200

	player.img = love.graphics.newImage('gallina.png')
	player.width = player.img:getWidth()
	player.height = player.img:getHeight()

	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)
			player.direction = -1
		end
	elseif love.keyboard.isDown('a') then
		if player.x > 0 then
			player.x = player.x - (player.speed * dt)
			player.direction = 1
		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, player.direction * 0.5, 0.5, player.width / 2, 32)
  --bullets
  for i, bullet in ipairs(bullets) do
  love.graphics.draw(bullet.img, bullet.x, bullet.y)
  end
end
Here is a list:
  • added player.direction wich contains 1 or -1 and used to multiply sx parameter in your love.graphics.draw method
  • change player.direction inside love.keyboard.isDown('d') and love.keyboard.isDown('a') blocks
  • added calculation of player sprite width and height on the load
  • used half of player width as ox parameter in love.graphics.draw so now payer will flip around the center, not around the corner
Last edited by Sasha264 on Sat Aug 24, 2019 11:43 am, edited 2 times in total.
User avatar
Sasha264
Party member
Posts: 131
Joined: Mon Sep 08, 2014 7:57 am

Re: Setting player scale -1 when going left?

Post by Sasha264 »

About several conditions:
if bullet.x&bullet.y < 0 then
If you want to check several conditions in one if statement in lua you can "join" they with and or or operators, like this:

Code: Select all

if bullet.x < 0 or bullet.y < 0 then ... end

Code: Select all

if bullet.x < 0 and bullet.y < 0 then ... end
For your case maybe here will be something like this:

Code: Select all

if bullet.x < 0 or bullet.y < 0 or bullet.x > windowWidth or bullet.y > windowHeight then ... end
Where windowWidth and windowHeight calculated on the start with

Code: Select all

windowWidth, windowHeight = love.graphics.getDimensions()
But maybe here will be some improvements, because bullets must be destroyed after they completely leaves screen, not when they touchs the screen border, so you can calculate bulletSize at the start and insert it in last check like this:

Code: Select all

if bullet.x < 0 - bulletSize or bullet.y < 0 - bulletSize or bullet.x > windowWidth + bulletSize or bullet.y > windowHeight + bulletSize then ... end
User avatar
Sasha264
Party member
Posts: 131
Joined: Mon Sep 08, 2014 7:57 am

Re: Setting player scale -1 when going left?

Post by Sasha264 »

About Love2d tutorials:
Here is some youtube records like this https://www.youtube.com/watch?v=FUiz1kL0QtI
Maybe you will enjoy watching it :monocle:
User avatar
Sasha264
Party member
Posts: 131
Joined: Mon Sep 08, 2014 7:57 am

Re: Setting player scale -1 when going left?

Post by Sasha264 »

About drawing sprites:

For me in 95% cases is more conveniently to calculate and store the center of the object like bullet in bullet.x and bullet.y
But simple draw call like this

Code: Select all

love.graphics.draw(bullet.img, bullet.x, bullet.y)
will draw the bullet with top-left corner at bullet.x, bullet.y

So you can use ox and oy parameters to move sprite center to the actual center:

Code: Select all

love.graphics.draw(bullet.img, bullet.x, bullet.y, 0, 1, 1, bulletSize / 2, bulletSize / 2)
Where bulletSize calculated on the start by dimensions of the bullet sprite.

Also it really helps with rotating sprites with r parameter of love.graphics.draw
fela
Prole
Posts: 2
Joined: Fri Aug 23, 2019 8:13 pm

Re: Setting player scale -1 when going left?

Post by fela »

Thanks a lot Sasha! Though is hard for me to understand quite completly, i'll be watching the tutorials you posted. Thanks for solving multiple things in my code too, and for the quick reply.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 146 guests