Trying to make duplicate objects from the same class interact independently

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
Deenhan
Prole
Posts: 1
Joined: Fri May 05, 2023 5:35 pm

Trying to make duplicate objects from the same class interact independently

Post by Deenhan »

Hello!

Currently working my way through CS50's Game Development course and am currently trying to recreate Breakout. My current task is to create a powerup where once you collect it, two extra balls spawn in.

I've sorted out the spawning of the powerup icon and the collection of it but it is the actual implementation of the powerup that I am struggling on.

The way that I initially wanted to go about doing it was simply calling the ball class two extra times however what didn't cross my mind was the fact that the balls would just be stuck on top of each other as they would all share the same x and y values.

Been stuck on this for a good few hours now and have made close to 0 progress. The simplest idea I had was to just make two new ball classes and have them interact the same as the original but I feel as if this is a very rudimentary way to go about it. Should I just do this or is there some way that I am missing?

Here is the code for the current working ball class

Code: Select all

--[[
    GD50
    Breakout Remake

    -- Ball Class --

    Author: Colton Ogden
    cogden@cs50.harvard.edu

    Represents a ball which will bounce back and forth between the sides
    of the world space, the player's paddle, and the bricks laid out above
    the paddle. The ball can have a skin, which is chosen at random, just
    for visual variety.
]]

Ball = Class{}

function Ball:init(skin)
    -- simple positional and dimensional variables
    self.width = 8
    self.height = 8

    -- these variables are for keeping track of our velocity on both the
    -- X and Y axis, since the ball can move in two dimensions
    self.dy = 0
    self.dx = 0

    -- this will effectively be the color of our ball, and we will index
    -- our table of Quads relating to the global block texture using this
    self.skin = skin
end

--[[
    Expects an argument with a bounding box, be that a paddle or a brick,
    and returns true if the bounding boxes of this and the argument overlap.
]]
function Ball:collides(target)
    -- first, check to see if the left edge of either is farther to the right
    -- than the right edge of the other
    if self.x > target.x + target.width or target.x > self.x + self.width then
        return false
    end

    -- then check to see if the bottom edge of either is higher than the top
    -- edge of the other
    if self.y > target.y + target.height or target.y > self.y + self.height then
        return false
    end 

    -- if the above aren't true, they're overlapping
    return true
end

--[[
    Places the ball in the middle of the screen, with no movement.
]]
function Ball:reset()
    self.x = VIRTUAL_WIDTH / 2 - 2
    self.y = VIRTUAL_HEIGHT / 2 - 2
    self.dx = 0
    self.dy = 0
end

function Ball:update(dt)
    self.x = self.x + self.dx * dt
    self.y = self.y + self.dy * dt

    -- allow ball to bounce off walls
    if self.x <= 0 then
        self.x = 0
        self.dx = -self.dx
        gSounds['wall-hit']:play()
    end

    if self.x >= VIRTUAL_WIDTH - 8 then
        self.x = VIRTUAL_WIDTH - 8
        self.dx = -self.dx
        gSounds['wall-hit']:play()
    end

    if self.y <= 0 then
        self.y = 0
        self.dy = -self.dy
        gSounds['wall-hit']:play()
    end
end

function Ball:render()
    -- gTexture is our global texture for all blocks
    -- gBallFrames is a table of quads mapping to each individual ball skin in the texture

    love.graphics.draw(gTextures['main'], gFrames['balls'][self.skin], self.x, self.y)
end
Any help is appreciated
User avatar
Xrocetoxtos
Prole
Posts: 9
Joined: Wed Apr 19, 2023 7:31 pm

Re: Trying to make duplicate objects from the same class interact independently

Post by Xrocetoxtos »

I'm not sure this is the right way to do it, but I solved a similar issue with what I found here:
https://www.lua.org/pil/16.1.html

Basically, you make two more instances of 'ball', al with the same functionality but with its own position, direction, speed etc. Those are provided by the Ball:new function and an update thing you run every frame.

Someone else will probably be able to explain either this or why I'm an idiot for suggesting. :P
User avatar
dusoft
Party member
Posts: 611
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Trying to make duplicate objects from the same class interact independently

Post by dusoft »

Either that or just you a table (array) of balls and iterate over them, e.g.:

Code: Select all

balls={}
balls[1]={ x=45, y=45}
balls[2]={ x=65, y=65}
etc.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 5 guests