-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9539076
commit 87339f0
Showing
11 changed files
with
904 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,67 @@ | ||
--[[ | ||
GD50 2018 | ||
Pong Remake | ||
-- Ball Class -- | ||
Author: Colton Ogden | ||
[email protected] | ||
Represents a ball which will bounce back and forth between paddles | ||
and walls until it passes a left or right boundary of the screen, | ||
scoring a point for the opponent. | ||
]] | ||
|
||
Ball = Class{} | ||
|
||
function Ball:init(x, y, w, h) | ||
function Ball:init(x, y, width, height) | ||
self.x = x | ||
self.y = y | ||
self.w = w | ||
self.h = h | ||
self.positionX = math.random(-50, 50) | ||
self.positionY = math.random(2) == 1 and -100 or 100 | ||
self.width = width | ||
self.height = height | ||
|
||
-- 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 | ||
end | ||
|
||
function Ball:update(dt) | ||
self.x = self.x + self.positionX *dt | ||
self.y = self.y + self.positionY *dt | ||
--[[ | ||
Expects a paddle as an argument and returns true or false, depending | ||
on whether their rectangles overlap. | ||
]] | ||
function Ball:collides(paddle) | ||
-- 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 > paddle.x + paddle.width or paddle.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 > paddle.y + paddle.height or paddle.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.dy = math.random(2) == 1 and -100 or 100 | ||
self.dx = math.random(-50, 50) | ||
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 | ||
end | ||
|
||
function Ball:render() | ||
love.graphics.rectangle('fill', self.x, self.y, self.w, self.y) | ||
love.graphics.rectangle('fill', self.x, self.y, self.width, self.height) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
--[[ | ||
GD50 2018 | ||
Pong Remake | ||
-- Paddle Class -- | ||
Author: Colton Ogden | ||
[email protected] | ||
Represents a paddle that can move up and down. Used in the main | ||
program to deflect the ball back toward the opponent. | ||
]] | ||
|
||
Paddle = Class{} | ||
|
||
--[[ | ||
The `init` function on our class is called just once, when the object | ||
is first created. Used to set up all variables in the class and get it | ||
ready for use. | ||
Our Paddle should take an X and a Y, for positioning, as well as a width | ||
and height for its dimensions. | ||
Note that `self` is a reference to *this* object, whichever object is | ||
instantiated at the time this function is called. Different objects can | ||
have their own x, y, width, and height values, thus serving as containers | ||
for data. In this sense, they're very similar to structs in C. | ||
]] | ||
function Paddle:init(x, y, width, height) | ||
self.x = x | ||
self.y = y | ||
self.width = width | ||
self.height = height | ||
self.dy = 0 | ||
end | ||
|
||
function Paddle:update(dt, ball) | ||
-- math.max here ensures that we're the greater of 0 or the player's | ||
-- current calculated Y position when pressing up so that we don't | ||
-- go into the negatives; the movement calculation is simply our | ||
-- previously-defined paddle speed scaled by dt | ||
if self.dy < 0 then | ||
self.y = math.max(0, self.y + self.dy * dt) | ||
-- similar to before, this time we use math.min to ensure we don't | ||
-- go any farther than the bottom of the screen minus the paddle's | ||
-- height (or else it will go partially below, since position is | ||
-- based on its top left corner) | ||
else | ||
self.y = math.min(VIRTUAL_HEIGHT - self.height, self.y + self.dy * dt) | ||
end | ||
end | ||
|
||
--[[ | ||
To be called by our main function in `love.draw`, ideally. Uses | ||
LÖVE2D's `rectangle` function, which takes in a draw mode as the first | ||
argument as well as the position and dimensions for the rectangle. To | ||
change the color, one must call `love.graphics.setColor`. As of the | ||
newest version of LÖVE2D, you can even draw rounded rectangles! | ||
]] | ||
function Paddle:render() | ||
love.graphics.rectangle('fill', self.x, self.y, self.width, self.height) | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.