Page 1 of 1

Issue getting objects to interact

Posted: Mon Mar 29, 2021 9:03 pm
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

Re: Issue getting objects to interact

Posted: Mon Mar 29, 2021 9:17 pm
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.

Re: Issue getting objects to interact

Posted: Mon Mar 29, 2021 9:46 pm
by jzissle
Sorry!

Re: Issue getting objects to interact

Posted: Tue Mar 30, 2021 7:34 pm
by Xii
I don't see you actually calling Broccoli:checkCollision anywhere in your code.

Re: Issue getting objects to interact

Posted: Tue Mar 30, 2021 11:37 pm
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 :) )

Re: Issue getting objects to interact

Posted: Wed Mar 31, 2021 6:48 pm
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 :)