Issue getting objects to interact

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
jzissle
Prole
Posts: 3
Joined: Mon Mar 29, 2021 8:51 pm

Issue getting objects to interact

Post by jzissle »

Hey,
I'm trying to make my first game in which the user plays as an owl sprite and tries to dodge broccoli while trying to eat turkey as both rain down the page. The owl is controlled by the mouse.
I can't work out how to get the food to interact with the owl. I think it might be to do with how I've set up classes for the food but I'm not sure.
I've attached my work below. Sorry if it's not very good!

Code: Select all

mouse = {}
function love.load()
  Object = require "classic"
  require "owl"
  require "broccoli"
  require "turkey"
  my_background = love.graphics.newImage('colored_forest.png')
  love.mouse.setVisible(false)
  checkCollision = false
  
  owl = Owl()
  broccoli = Broccoli()
  broccoli1 = Broccoli()
  broccoli2 = Broccoli()
  turkey = Turkey()
  turkey1 = Turkey()
  turkey2 = Turkey()
end
timer = 0
function love.update(dt)
  owl:update(dt)
  timer = timer + dt
  if checkCollision == true then
    love.event.quit("restart")
  end
  if timer >= 2 then
    broccoli:update(dt)
  end
  if timer >= 4 then
    turkey:update(dt)
  end
  if timer >= 5 then
    broccoli1:update(dt)
  end
  if timer >= 6 then
    turkey1:update(dt)
  end
  if timer >= 7 then
    broccoli2:update(dt)
  end
  if timer >= 8 then
    turkey1:update(dt)
  end
end

function love.draw()
  --Load the background
  scale1 = love.graphics:getWidth() / my_background:getWidth()
  scale2 = love.graphics:getHeight() / my_background:getHeight()
  love.graphics.draw(my_background, 0, 0, 0, scale1, scale2)
  
  owl:draw()
  broccoli:draw()
  turkey:draw()
  broccoli1:draw() 
  turkey1:draw()
  broccoli2:draw()
  turkey2:draw()
  
end

  

Code: Select all

Broccoli = Object:extend()


function Broccoli:new()
  self.image = love.graphics.newImage("broccoli.png")
  self.y = -60
  self.x = love.math.random(0, love.graphics:getWidth())
  self.speed = 200
  self.width = self.image:getWidth()
  self.height = self.image:getHeight()
end

function Broccoli:update(dt)
  if self.y < love.graphics:getHeight() then
    self.y = self.y + self.speed * dt
  else
    self.y = -50 
    self.x = love.math.random(0, love.graphics:getWidth())
  end
end

function Broccoli:draw()
  love.graphics.draw(self.image, self.x, self.y, 0, 0.5, 0.5)
end

function Broccoli:checkCollision(obj)
  local self_left = self.x
  local self_right = self.x + self.width
  local self_top = self.y
  local self_bottom = self.y + self.height
  
  local obj_left = obj.x
  local obj_right = obj.x + obj.width
  local obj_top = obj.y
  local obj_bottom = obj.y + obj.height
  
  if self_right > obj_left and
  self_left < obj_right and
  self_bottom > obj_top and 
  self_top < obj_bottom then 
    return true
  else 
    return false
  end
end

Code: Select all

Owl = Object:extend()

function Owl:new()
  self.image = love.graphics.newImage("owl.png")
  self.x = 300
  self.y = 300
  self.speed = 500 
  self.width = self.image:getWidth()
  self.height = self.image:getHeight()
  
  
  
end
function Owl:update(dt)
  --get the position of the mouse
  mouse_x, mouse_y = love.mouse.getPosition()
  
  if self.x < mouse_x then
		self.x = self.x + (self.speed * 0.5 * dt)
	end
	if self.x > mouse_x then
		self.x = self.x - (self.speed * 0.5 * dt)
	end
	if self.y < mouse_y then
		self.y = self.y + (self.speed * 0.5 * dt)
	end
	if self.y > mouse_y then
		self.y = self.y - (self.speed * 0.5 * dt)
	end
  if self.x == mouse_x then
    self.x = mouse_x
  end
  if self.y == mouse_y then
    self.y = mouse_y
  end
end

function Owl:draw()
  love.graphics.draw(self.image, self.x, self.y)
end

Code: Select all

Turkey = Object:extend()

function Turkey:new()
  self.image = love.graphics.newImage("turkey.png")
  self.y = -50
  self.x = love.math.random(0, love.graphics:getWidth())
  self.speed = 200
  self.width = self.image:getWidth()
  self.height = self.image:getHeight()
end

function Turkey:update(dt)
  if self.y < love.graphics:getHeight() then
    self.y = self.y + self.speed * dt
  else
    self.y = -50 
    self.x = love.math.random(0, love.graphics:getWidth())
  end
end

function Turkey:draw()
  love.graphics.draw(self.image, self.x, self.y, 0, 0.5, 0.5)
end
Last edited by jzissle on Mon Mar 29, 2021 9:45 pm, edited 1 time in total.
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Issue getting objects to interact

Post by darkfrei »

jzissle wrote: Mon Mar 29, 2021 9:03 pm Hey,
I'm trying to make my first game in which the user plays as an owl sprite and tries to dodge broccoli while trying to eat turkey as both rain down the page. The owl is controlled by the mouse.
I can't work out how to get the food to interact with the owl. I think it might be to do with how I've set up classes for the food but I'm not sure.
I've attached my work below. Sorry if it's not very good!
Pack files, not folder.
Attachments
Screenshot_20210329-231647.png
Screenshot_20210329-231647.png (57.27 KiB) Viewed 3960 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
jzissle
Prole
Posts: 3
Joined: Mon Mar 29, 2021 8:51 pm

Re: Issue getting objects to interact

Post by jzissle »

Sorry!
User avatar
Xii
Party member
Posts: 137
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: Issue getting objects to interact

Post by Xii »

I don't see you actually calling Broccoli:checkCollision anywhere in your code.
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Issue getting objects to interact

Post by Gunroar:Cannon() »

jzissle wrote: Mon Mar 29, 2021 9:03 pm Hey,
I'm trying to make my first game in which the user plays as an owl sprite and tries to dodge broccoli while trying to eat turkey as both rain down the page. The owl is controlled by the mouse.
I can't work out how to get the food to interact with the owl. I think it might be to do with how I've set up classes for the food but I'm not sure.
You set up classes fine... :3
Since the Turkey should also be food it should have a checkCollision function too, as well as broccoli. On every food's update run checkCollision(owl) and handle the collisions(like losing when touching broccoli, and seeing your code that should be done by setting the global checkCollision to true, which in my opinion I would change the name to something like collidedWithBroccoli or owlAteBroccoli or something :) )
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
jzissle
Prole
Posts: 3
Joined: Mon Mar 29, 2021 8:51 pm

Re: Issue getting objects to interact

Post by jzissle »

Thanks guys. I was calling getCollision and not Broccoli:getCollision. :roll:
Now I can hopefully get it finished. I'll definitely be calling it owlAteBroccoli :)
Post Reply

Who is online

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