How to offset bullet object, based on player's direction - lua

I'm running lua 5.1 and love2d 11.3.0
In my demo, I have a top down shooter game. For visual purposes the character looks like so...As you can see from this top down perspective, he is holding his weapon out.
My issue is that I am having a hard time figuring out how to have his bullets start from the tip of his gun no matter what direction he is facing, or where on the screen he is positioned. Currently, the bullets always shoot from the center of this body. I have tried adjusting the bullet.x and bullet.y properties below, but that is a static change that doesn't respect the players direction...
function spawnBullet()
local bullet = {}
bullet.x = player.x
bullet.y = player.y
bullet.speed = 500
bullet.direction = playerMouseAngle()
table.insert(bullets, bullet)
end
The full game logic :
-- top down shooter game demo 2021
function love.load()
-- create table to collect sprite assets
sprites = {}
sprites.bg = love.graphics.newImage('graphics/bg_grass.jpg')
sprites.hero = love.graphics.newImage('graphics/hero.png')
-- create table for our player hero
player = {}
player.x = love.graphics.getWidth() / 2
player.y = love.graphics.getHeight() / 2
player.speed = 360
player.injured = false
player.injuredSpeed = 270
-- create table for player bullets
bullets = {}
-- game state, timer and score globals
gameState = 1
maxTime = 2
timer = maxTime
score = 0
end
function love.update(dt)
if gameState == 2 then
-- player speed will be adjusted below
local moveSpeed = player.speed
-- adjust player speed if player was injured
if player.injured then moveSpeed = player.injuredSpeed end
-- player moves right
if love.keyboard.isDown("d") and player.x < love.graphics.getWidth() -
50 then player.x = player.x + moveSpeed * dt end
-- player moves left
if love.keyboard.isDown("a") and player.x > 50 then
player.x = player.x - moveSpeed * dt
end
-- player moves up
if love.keyboard.isDown("w") and player.y > 50 then
player.y = player.y - moveSpeed * dt
end
-- player moves down
if love.keyboard.isDown("s") and player.y < love.graphics.getHeight() -
50 then player.y = player.y + moveSpeed * dt end
end
-- make bullets move in desired direction
for i, b in ipairs(bullets) do
b.x = b.x + (math.cos(b.direction) * b.speed * dt)
b.y = b.y + (math.sin(b.direction) * b.speed * dt)
end
-- remove the bullets from the game once they go off-screen
for i = #bullets, 1, -1 do
local b = bullets[i]
if b.x < 0 or b.y < 0 or b.x > love.graphics.getWidth() or b.y >
love.graphics.getHeight() then table.remove(bullets, i) end
end
-- manage game state and timer
if gameState == 2 then
timer = timer - dt
if timer <= 0 then
maxTime = 0.95 * maxTime
timer = maxTime
end
end
end
function love.draw()
-- setup background
love.graphics.push()
love.graphics.scale(0.20, 0.20)
love.graphics.draw(sprites.bg, 0, 0)
love.graphics.pop()
-- click to begin
if gameState == 1 then
love.graphics.printf('Click anywhere to begin!', 0, 50, love.graphics.getWidth(), "center")
end
-- display score
love.graphics.printf('Score: ' .. score, 0, love.graphics.getHeight() - 100, love.graphics.getWidth(), "center")
-- adjust player color if player gets injured
if player.injured then love.graphics.setColor(1, 0, 0) end
-- draw player
love.graphics.draw(sprites.hero, player.x, player.y, playerMouseAngle(),
nil, nil, sprites.hero:getWidth() / 2,
sprites.hero:getHeight() / 2)
-- display player bullets
for i, b in ipairs(bullets) do
love.graphics.circle("fill", b.x, b.y, 10, 100)
end
end
-- enable player direction based on mouse movement
function playerMouseAngle()
return
math.atan2(player.y - love.mouse.getY(), player.x - love.mouse.getX()) +
math.pi
end
-- define bullet properties
function spawnBullet()
local bullet = {}
bullet.x = player.x
bullet.y = player.y
bullet.speed = 500
bullet.direction = playerMouseAngle()
table.insert(bullets, bullet)
end
-- game state determins shooting player bullets...
function love.mousepressed(x, y, button)
if button == 1 and gameState == 2 then
spawnBullet()
-- ..or starting the game
elseif button == 1 and gameState == 1 then
gameState = 2
maxTime = 2
timer = maxTime
score = 0
end
end
Thanks in advance for any tips

You already have the required math in there during their travel...
-- make bullets move in desired direction
for i, b in ipairs(bullets) do
b.x = b.x + (math.cos(b.direction) * b.speed * dt)
b.y = b.y + (math.sin(b.direction) * b.speed * dt)
end
Just need to tweak it a bit for bullet creation
local bullet = {}
bullet.speed = 500
bullet.direction = playerMouseAngle()
bullet.x = player.x +(math.cos(bullet.direction + 0.5) * (sprites.hero:getWidth() / 2))
bullet.y = player.y +(math.sin(bullet.direction + 0.5) * (sprites.hero:getWidth() / 2))
table.insert(bullets, bullet)
Guessing on the scale. You might have to change the /2 to something else, like *0.75.

Related

lua - screen limit for characters

I have a problem where my character keeps going out of my screen.
How do I fix this problem? I know I need to put in a function to prevent the character from going out of the screen and managed to do it on the radius of the circle.
if love.keyboard.isDown("right") then
player.x = player.x + 4
elseif love.keyboard.isDown("left") then
player.x = player.x - 4
elseif love.keyboard.isDown("up") then
player.y = player.y - 4
elseif love.keyboard.isDown("down") then
player.y = player.y + 4
end
if AABB(player.x, player.y, player.w, player.h, target.x, target.y, target.raduis) then
score = score + 1
target.x = math.random(target.raduis, love.graphics.getWidth() - target.raduis)
target.y = math.random(target.raduis, love.graphics.getHeight()- 100)
end
im not sure what the "character" is, so im going to do both the player and the target
if you want the character to not go off screen, try:
if player.x < 0 then
player.x = 0
elseif player.x + player.w > love.graphics.getWidth() then
player.x = love.graphics.getWidth() - player.w
end
if player.y < 0 then
player.y = 0
elseif player.y + player.h > love.graphics.getHeight() then
player.y = love.graphics.getHeight() - player.h
end
if you want the target to stay on screen, try:
if AABB(player.x, player.y, player.w, player.h, target.x, target.y,
target.raduis) then
score = score + 1
target.x = math.random(0 + target.raduis, love.graphics.getWidth() - target.raduis)
target.y = math.random(0 + target.raduis, love.graphics.getHeight() - target.raduis)
end
hopefully this helps

Trying to add horizontal movement to my game in love2d

I have tried to add horizontal movement into my game in love, but I haven't found a way yet. This is my code.
PADDLE_SPEED = 200
player1 = Paddle(10, 30, 5, 20)
player1 = Paddle(10, 30, 5, 20)
player2 = Paddle(VIRTUAL_WIDTH - 15, VIRTUAL_HEIGHT - 30, 5, 20)
player3 = Paddle(30, 10, 20, 5)
player4 = Paddle(VIRTUAL_WIDTH - 30, VIRTUAL_HEIGHT - 15, 20, 5)
ball = Ball(VIRTUAL_WIDTH / 2 - 2, VIRTUAL_HEIGHT / 2 - 2, 4, 4)
if love.keyboard.isDown('w') then
player1.dy = -PADDLE_SPEED
elseif love.keyboard.isDown('s') then
player1.dy = PADDLE_SPEED
else
player1.dy = 0
end
if love.keyboard.isDown('a') then
player1.dx = PADDLE_SPEED
elseif love.keyboard.isDown('d') then
player1.dx = -PADDLE_SPEED
else
player1.dx = 0
end
if love.keyboard.isDown('a') then
player1.dx = -PADDLE_SPEED
elseif love.keyboard.isDown('d') then
player1.dy = PADDLE_SPEED
else
player1.dx = 0
end
if love.keyboard.isDown('up') then
player2.dy = -PADDLE_SPEED
elseif love.keyboard.isDown('down') then
player2.dy = PADDLE_SPEED
else
player2.dy = 0
end
if love.keyboard.isDown('i') then
player3.dy = -PADDLE_SPEED
elseif love.keyboard.isDown('k') then
player3.dy = PADDLE_SPEED
else
player3.dy = 0
end
if love.keyboard.isDown('g') then
player4.dy = -PADDLE_SPEED
elseif love.keyboard.isDown('b') then
player4.dy = PADDLE_SPEED
else
player4.dy = 0
end
This is my class that I call Paddle
Paddle = Class{}
function Paddle:init(x, y, width, height)
self.x = x
self.y = y
self.width = width
self.height = height
self.dy = 0
self.dx = 0
end
function Paddle:update(dt)
if self.dy < 0 then
self.y = math.max(0, self.y + self.dy * dt)
else
self.y = math.min(VIRTUAL_HEIGHT - self.height, self.y + self.dy * dt)
end
end
function Paddle:render()
love.graphics.rectangle('fill', self.x, self.y, self.width, self.height)
end
I would try drawing the rectangles individually, but I would have to remove all the code that includes player1-4. Is there any way that I can set a variable like PADDLE_SPEED to a number?
So your not quite instantiating your Paddle class correctly. Based on Lua's documentation, I changed some things in your paddle class.
Paddle.lua
Paddle={x=0,y=0,width=0,height=0}
function Paddle:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function Paddle:update(dt)
if self.dy < 0 then
self.y = math.max(0, self.y + self.dy * dt)
else
self.y = math.min(VIRTUAL_HEIGHT - self.height, self.y + self.dy * dt)
end
end
function Paddle:render()
love.graphics.rectangle('fill', self.x, self.y, self.width, self.height)
end
Also I'm not sure what you just didn't include in you question, but I set up your core 3 functions (love.load(), love.update(dt), and love.draw()), set values for VIRTUAL_WIDTH AND VIRTUAL_HEIGHT, and instantiated your player classes according to how the Lua documentation specifies
The main thing that might be your problem is that your assigning the speeds to your player objects dy and dx values. These are made up values that you've created for your player object. They don't do anything unless you later access them and use them somehow. I think what you are trying to do is change the x and y position of your player objects. You can do this by taking the players current x or y position and adding or subtracting the PADDLE_SPEED * dt(the number of seconds that have passed since the last execution of love.update(dt)
main.lua
require('Paddle')
function love.load()
--initial graphics setup
love.graphics.setBackgroundColor(0.41, 0.53, 0.97)
love.window.setMode(1000, 600)
PADDLE_SPEED = 200
VIRTUAL_WIDTH = 30
VIRTUAL_HEIGHT = 30
player1 = Paddle:new{x=10, y=30, width=5, height=20}
player2 = Paddle:new{x=VIRTUAL_WIDTH-15, y=VIRTUAL_HEIGHT-30, width=5, height=20}
player3 = Paddle:new{x=30, y=10, width=20, height=5}
player4 = Paddle:new{x=VIRTUAL_WIDTH-30, y=VIRTUAL_HEIGHT-15, width=20, height=5}
-- ball = Ball(VIRTUAL_WIDTH / 2 - 2, VIRTUAL_HEIGHT / 2 - 2, 4, 4)
end
function love.update(dt)
if love.keyboard.isDown('w') then
player1.y = player1.y + PADDLE_SPEED * dt
elseif love.keyboard.isDown('s') then
player1.y = player1.y - PADDLE_SPEED * dt
end
if love.keyboard.isDown('a') then
player1.x = player1.x + PADDLE_SPEED * dt
elseif love.keyboard.isDown('d') then
player1.x = player1.x - PADDLE_SPEED * dt
end
if love.keyboard.isDown('a') then
player2.x = player2.x - PADDLE_SPEED * dt
elseif love.keyboard.isDown('d') then
player2.x = player2.x + PADDLE_SPEED * dt
end
if love.keyboard.isDown('up') then
player2.y = player2.y - PADDLE_SPEED * dt
elseif love.keyboard.isDown('down') then
player2.y = player2.y + PADDLE_SPEED * dt
end
if love.keyboard.isDown('i') then
player3.y = player3.y - PADDLE_SPEED * dt
elseif love.keyboard.isDown('k') then
player3.y = player3.y + PADDLE_SPEED * dt
end
if love.keyboard.isDown('g') then
player4.y = player4.y - PADDLE_SPEED * dt
elseif love.keyboard.isDown('b') then
player4.y = player4.y + PADDLE_SPEED * dt
end
end
function love.draw()
player1:render()
player2:render()
player3:render()
player4:render()
end
So your ball won't work, because I don't know exactly what you want with it, and your Paddle:update(dt) still won't work because I don't know exactly what you want. When you do get Paddle:update(dt) working make sure you include player1:update(dt), player2:update(dt) ... inside your love:update(dt) function or else none of your players will update.
You should get horizontal and vertical movement of your blocks though
P.S. If you post a question a again, you should make sure that you specify exactly what isn't working. Some of your details were missing, and I wasn't sure if that was the part you were asking about, or you had those details in your original code and were asking about something else.
It seems like you probably want to deal with collisions. Here's another answer where i kind of explain collisions, but I would probably recommend love.physics for this type of scenario

Lua - Love2d not responding?

I'm building a simple shooting game on Love2d, everything seems smooth at first, but then after I add the check collision function, I tried to open the game but I got 'not responding' message. So I'm not sure what's the problem here.
I use the collision function taken from love2d forums. It keep giving message that "compare number with nil" and after I put on some game state conditions, it give me 'not responding' massage. Below is my code.
Main.lua
WIDTH = 480
HEIGHT = 800
Class = require 'Class'
require 'Player'
require 'Enemies'
PLAYER_SPEED = 150
BULLET_SPEED = 250
createEnemyTimerMax = 0.4
createEnemyTimer = createEnemyTimerMax
isAlive = true
score = 0
-- called when game starts
-- load images, sounds of the game here
function love.load(arg)
gamestate = 'play'
background = love.graphics.newImage('gfx/milkyway.png')
-- initialize player
player = Player(200, 710)
-- initialize bullet
-- render image to map
bulletimage = love.graphics.newImage('gfx/bullet.png')
-- Entity storage
bullets = {} -- array of current bullets being drawn and updated
enemy = Enemies(math.random(10, 200), math.random(10, 200))
end
function love.update(dt)
-- keypress for shooting
love.keyboard.keysPressed = {}
love.keyboard.keysReleased = {}
-- move the player
if love.keyboard.isDown('left', 'a') then
if player.x > 0 then
player.x = player.x - (PLAYER_SPEED * dt)
end
elseif love.keyboard.isDown('right', 'd') then
if player.x < (love.graphics.getWidth() - player.img:getWidth()) then
player.x = player.x + (PLAYER_SPEED * dt)
end
elseif love.keyboard.isDown('up', 'w') then
if player.y > 0 then
player.y = player.y - (PLAYER_SPEED * dt)
end
elseif love.keyboard.isDown('down', 's') then
if player.y < (love.graphics.getHeight() - player.img:getHeight()) then
player.y = player.y + (PLAYER_SPEED * dt)
end
end
-- reset the game
if gamestate == 'done' and love.keyboard.isDown('r') then
gamestate = 'play'
-- remove all the bullets and enemies from the screen
bullets = {}
enemies = {}
-- reset timers
createEnemyTimer = createEnemyTimerMax
-- move player to default position
player.x = 200
player.y = 710
-- reset gamestate
score = 0
isAlive = true
end
-- keep updating the positions of bullets when shooting
for i, bullet in ipairs(bullets) do
bullet.y = bullet.y - (BULLET_SPEED * dt)
-- remove bullets when they pass off the screen
if bullet.y < 0 then
table.remove(bullets, i)
end
end
Player:update()
Enemies:update(dt)
end
-- global key pressed function
function love.keyboard.wasPressed(key)
if (love.keyboard.keysPressed[key]) then
return true
else
return false
end
end
-- global key released function
function love.keyboard.wasReleased(key)
if (love.keyboard.keysReleased[key]) then
return true
else
return false
end
end
-- called whenever a key is released
function love.keyreleased(key)
love.keyboard.keysReleased[key] = true
end
-- called whenever a key is pressed
function love.keypressed(key)
dt = love.timer.getDelta()
-- to exit the game
if love.keyboard.isDown('escape') then
love.event.quit()
end
-- create bullets when shooting
if love.keyboard.isDown('space') then
newBullet = {x = player.x + (player.img:getWidth() / 2),
y = player.y, img = bulletimage}
table.insert(bullets, newBullet)
end
love.keyboard.keysPressed[key] = true
end
function love.draw(dt)
-- set background image
love.graphics.clear(51/255, 43/255, 68/255, 1)
drawBackground()
-- draw player
if gamestate == 'play' then
player:render()
elseif gamestate == 'done' then
love.graphics.print("Press 'R' to restart", WIDTH / 2 - 50, HEIGHT / 2 - 10)
end
love.graphics.setDefaultFilter('nearest', 'nearest')
-- draw bullets
for i, bullet in ipairs(bullets) do
love.graphics.draw(bulletimage, bullet.x, bullet.y, 3)
end
-- draw enemies
enemy:render()
end
-- background is distributed for free on pixelstalk.net
function drawBackground()
for i = 0, love.graphics.getWidth() / background:getWidth() do
for j = 0, love.graphics.getHeight() / background:getHeight() do
love.graphics.draw(background, i * background:getWidth(), j * background:getHeight())
end
end
end
Enemies.lua
Enemies = Class{}
function Enemies:init(x, y)
self.x = x
self.y = y
enemies = {} -- array of current enemies on the screen
-- render image to map
self.enemyimg = love.graphics.newImage('gfx/enemy.png')
self.width = self.enemyimg:getWidth()
self.height = self.enemyimg:getHeight()
end
function Enemies:update(dt)
createEnemyTimer = createEnemyTimer - (1 * dt)
if createEnemyTimer < 0 then
createEnemyTimer = createEnemyTimerMax
-- create an enemy
randomNumber = math.random(10, love.graphics.getWidth() - 10)
newEnemy = { x = randomNumber, y = -10, img = self.enemyimg}
table.insert(enemies, newEnemy)
end
-- keep updating the positions of enemies
for i, enemy in ipairs(enemies) do
enemy.y = enemy.y + (200 * dt)
-- remove enemies when they pass off the screen
if enemy.y > 850 then
table.remove(enemies, i)
end
end
-- run our collision detection
-- also we need to see if enemies hit our player
while gamestate == 'play' do
for i, enemy in ipairs(enemies) do
for j, bullet in ipairs(bullets) do
if CheckCollision(self.x, self.y, self.width, self.height,
bullet.x, bullet.y, bullet.bulletimage:getWidth(), bullet.bulletimage:getHeight()) then
table.remove(bullets, j)
table.remove(enemies, i)
score = score + 1
end
end
if CheckCollision(self.x, self.y, self.width, self.height,
player.x, player.y, player.width, player.height)
and isAlive == true then
table.remove(enemies, i)
isAlive = false
gamestate = 'done'
end
end
end
end
function Enemies:render()
-- pixel aircrafts created by chabull and
-- distributed for free on OpenGameArt.org
for i, enemy in ipairs(enemies) do
love.graphics.draw(self.enemyimg, self.x, self.y)
end
end
-- function to check collision
-- returns true if 2 objects overlap, false if they don't
-- x1, y1 are left-top coords of the first object, while w1, h1 are its width and height
-- x2, y2, w2, h2 are the same, but for the second object
function CheckCollision(x1, y1, w1, h1, x2, y2, w2, h2)
if x1 < x2 + w2 and x2 < x1 + w1 and y1 < y2 + h2 and y2 < y1 + h1 then
return true
else
return false
end
end
I'm not a Love2d expert but Enemies.update is probably called every frame. Running a while loop with a condition gamestate == 'play' that most likely is true all the time in that function looks problematic to me.
You're basically trapped in an infinite loop instead of updating your game state.

Lua getting (x,y) of player in game

I want to find the x and y coordinates of my player in real time so I know where to make the next level of my game. I'm currently using LÖVE 2D to run my code. When I try to print out player.x and player.y, the game runs fine but there is no text output of the coordinates. I've tried to change the position of where the text is located but that doesn't work. Any help is appreciated. Note: I just started Lua today so be blunt, please. :)
love.graphics.setDefaultFilter('nearest','nearest')
function love.load()
room1Image = love.graphics.newImage('room1.png')
room2Image = love.graphics.newImage('room2.png')
room3Image = love.graphics.newImage('room3.png')
room1 = true
room2 = false
room3 = false
player = {}
player.x = 0
player.y = 255
player.speed = 5
player.image = love.graphics.newImage('player.png')
end
function love.update(dt)
if love.keyboard.isDown("left") then
player.x = player.x - 5
end
if love.keyboard.isDown("right") then
player.x = player.x + 5
end
if love.keyboard.isDown("up") then
player.y = player.y - 5
end
if love.keyboard.isDown("down") then
player.y = player.y + 5
end
if player.y >= 600 and room1 then
room1 = false
room2 = true
player.y = 5
end
if player.y <= 0 and room2 then
room1 = true
room2 = false
player.y = 600
end
if player.y >= 600 and room2 then
room2 = false
room3 = true
player.y = 5
end
if player.y <= 0 and room3 then
room2 = true
room3 = false
player.y = 600
end
end
function love.draw()
--draw background
if room1 then
love.graphics.draw(room1Image, room1Image.x, room1Image.y)
elseif room2 then
love.graphics.draw(room2Image, room2Image.x, room2Image.y)
elseif room3 then
love.graphics.draw(room3Image, room3Image.x, room3Image.y)
end
--draw player
love.graphics.draw(player.image, player.x, player.y, 0, 5)
end
If you want to output something to the console use print(). This will NOT be visible in the game window.
If you want to show some text to the player (in game) call love.graphics.print inside love.draw():
local x,y = 0, 0 --coordinates at which the text is printed
function love.load()
end
function love.update(dt)
end
function love.draw()
love.graphics.print("This is something I want you to see.", x, y)
end

How to make love2d game compatible

I am getting the message "This game indicates it was made for version '0.9.1' of LOVE.
It may not be compatible with the running version (0.10.2)." when I try and run my game. The game still works but the message is annoying me. How do I update it to the latest version? My code is here:
debug = true
Main.lua
-- Timers
-- We declare these here so we don't have to edit them multiple places
canShoot = true
canShootTimerMax = 0.2
canShootTimer = canShootTimerMax
createEnemyTimerMax = 0.4
createEnemyTimer = createEnemyTimerMax
-- Player Object
player = { x = 200, y = 710, speed = 150, img = nil }
isAlive = true
score = 0
-- Image Storage
bulletImg = nil
enemyImg = nil
-- Entity Storage
bullets = {} -- array of current bullets being drawn and updated
enemies = {} -- array of current enemies on screen
-- 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
-- Loading
function love.load(arg)
player.img = love.graphics.newImage('assets/plane.png')
enemyImg = love.graphics.newImage('assets/enemy.png')
bulletImg = love.graphics.newImage('assets/bullet.png')
end
-- Updating
function love.update(dt)
-- I always start with an easy way to exit the game
if love.keyboard.isDown('escape') then
love.event.push('quit')
end
-- Time out how far apart our shots can be.
canShootTimer = canShootTimer - (1 * dt)
if canShootTimer < 0 then
canShoot = true
end
-- Time out enemy creation
createEnemyTimer = createEnemyTimer - (1 * dt)
if createEnemyTimer < 0 then
createEnemyTimer = createEnemyTimerMax
-- Create an enemy
randomNumber = math.random(10, love.graphics.getWidth() - 10)
newEnemy = { x = randomNumber, y = -10, img = enemyImg }
table.insert(enemies, newEnemy)
end
-- update the positions of bullets
for i, bullet in ipairs(bullets) do
bullet.y = bullet.y - (250 * dt)
if bullet.y < 0 then -- remove bullets when they pass off the screen
table.remove(bullets, i)
end
end
-- update the positions of enemies
for i, enemy in ipairs(enemies) do
enemy.y = enemy.y + (200 * dt)
if enemy.y > 850 then -- remove enemies when they pass off the screen
table.remove(enemies, i)
end
end
-- run our collision detection
-- Since there will be fewer enemies on screen than bullets we'll loop them first
-- Also, we need to see if the enemies hit our player
for i, enemy in ipairs(enemies) do
for j, bullet in ipairs(bullets) do
if CheckCollision(enemy.x, enemy.y, enemy.img:getWidth(), enemy.img:getHeight(), bullet.x, bullet.y, bullet.img:getWidth(), bullet.img:getHeight()) then
table.remove(bullets, j)
table.remove(enemies, i)
score = score + 1
end
end
if CheckCollision(enemy.x, enemy.y, enemy.img:getWidth(), enemy.img:getHeight(), player.x, player.y, player.img:getWidth(), player.img:getHeight())
and isAlive then
table.remove(enemies, i)
isAlive = false
end
end
if love.keyboard.isDown('left','a') then
if player.x > 0 then -- binds us to the map
player.x = player.x - (player.speed*dt)
end
elseif love.keyboard.isDown('right','d') then
if player.x < (love.graphics.getWidth() - player.img:getWidth()) then
player.x = player.x + (player.speed*dt)
end
end
if love.keyboard.isDown(' ', 'rctrl', 'lctrl', 'ctrl') and canShoot then
-- Create some bullets
newBullet = { x = player.x + (player.img:getWidth()/2), y = player.y, img = bulletImg }
table.insert(bullets, newBullet)
canShoot = false
canShootTimer = canShootTimerMax
end
if not isAlive and love.keyboard.isDown('r') then
-- remove all our bullets and enemies from screen
bullets = {}
enemies = {}
-- reset timers
canShootTimer = canShootTimerMax
createEnemyTimer = createEnemyTimerMax
-- move player back to default position
player.x = 50
player.y = 710
-- reset our game state
score = 0
isAlive = true
end
end
-- Drawing
function love.draw(dt)
for i, bullet in ipairs(bullets) do
love.graphics.draw(bullet.img, bullet.x, bullet.y)
end
for i, enemy in ipairs(enemies) do
love.graphics.draw(enemy.img, enemy.x, enemy.y)
end
love.graphics.setColor(255, 255, 255)
love.graphics.print("SCORE: " .. tostring(score), 400, 10)
if isAlive then
love.graphics.draw(player.img, player.x, player.y)
else
love.graphics.print("Press 'R' to restart", love.graphics:getWidth()/2-50, love.graphics:getHeight()/2-10)
end
if debug then
fps = tostring(love.timer.getFPS())
love.graphics.print("Current FPS: "..fps, 9, 10)
end
end
conf.lua
-- Configuration
function love.conf(t)
t.title = "Scrolling Shooter Tutorial" -- The title of the window the game is in (string)
t.version = "0.9.1" -- The LÖVE version this game was made for (string)
t.window.width = 480 -- we want our game to be long and thin.
t.window.height = 800
-- For Windows debugging
t.console = true
end
As noted in the comments, the problem in your case was that, in conf.lua, the version was specified as "0.9.1". In some cases, changing this value to "0.10.2" is sufficient, but a significant amount of changes occurred between 0.9.0 and 0.10.0.
Be especially aware that mouse input is definitely going to be broken because, in versions before 0.10.0, LOVE used strings to represent mouse buttons, whereas in 0.10.0 and beyond, numbers are used. To fix this, look for mouse-related functions (love.mouse.isDown, love.mousepressed, etc.) and change "l" to 1, "r" to 2, and so on. See the full list of old values and love.mousepressed for more. Additionally, mousewheel movement changed as well, with the addition of the love.wheelmoved callback and removing the strings passed to love.mousepressed.
Additionally, read through the changelog for any changes that may have affected your program.

Resources