Skip to content

Commit 5fd2077

Browse files
committed
added pong-12
1 parent 75d5f92 commit 5fd2077

File tree

11 files changed

+803
-2
lines changed

11 files changed

+803
-2
lines changed

pong-11/main.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
GD50 2018
33
Pong Remake
44
5-
pong-10
5+
pong-11
66
"The Audio Update"
77
88
-- Main Program --

pong-12/Ball.lua

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
--[[
2+
GD50 2018
3+
Pong Remake
4+
5+
-- Ball Class --
6+
7+
Author: Colton Ogden
8+
9+
10+
Represents a ball which will bounce back and forth between paddles
11+
and walls until it passes a left or right boundary of the screen,
12+
scoring a point for the opponent.
13+
]]
14+
15+
Ball = Class{}
16+
17+
function Ball:init(x, y, width, height)
18+
self.x = x
19+
self.y = y
20+
self.width = width
21+
self.height = height
22+
23+
-- these variables are for keeping track of our velocity on both the
24+
-- X and Y axis, since the ball can move in two dimensions
25+
self.dy = math.random(2) == 1 and -100 or 100
26+
self.dx = math.random(2) == 1 and math.random(-80, -100) or math.random(80, 100)
27+
end
28+
29+
--[[
30+
Expects a paddle as an argument and returns true or false, depending
31+
on whether their rectangles overlap.
32+
]]
33+
function Ball:collides(paddle)
34+
-- first, check to see if the left edge of either is farther to the right
35+
-- than the right edge of the other
36+
if self.x > paddle.x + paddle.width or paddle.x > self.x + self.width then
37+
return false
38+
end
39+
40+
-- then check to see if the bottom edge of either is higher than the top
41+
-- edge of the other
42+
if self.y > paddle.y + paddle.height or paddle.y > self.y + self.height then
43+
return false
44+
end
45+
46+
-- if the above aren't true, they're overlapping
47+
return true
48+
end
49+
50+
--[[
51+
Places the ball in the middle of the screen, with an initial random velocity
52+
on both axes.
53+
]]
54+
function Ball:reset()
55+
self.x = VIRTUAL_WIDTH / 2 - 2
56+
self.y = VIRTUAL_HEIGHT / 2 - 2
57+
self.dy = math.random(2) == 1 and -100 or 100
58+
self.dx = math.random(-50, 50)
59+
end
60+
61+
--[[
62+
Simply applies velocity to position, scaled by deltaTime.
63+
]]
64+
function Ball:update(dt)
65+
self.x = self.x + self.dx * dt
66+
self.y = self.y + self.dy * dt
67+
end
68+
69+
function Ball:render()
70+
love.graphics.rectangle('fill', self.x, self.y, self.width, self.height)
71+
end

pong-12/Paddle.lua

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
--[[
2+
GD50 2018
3+
Pong Remake
4+
5+
-- Paddle Class --
6+
7+
Author: Colton Ogden
8+
9+
10+
Represents a paddle that can move up and down. Used in the main
11+
program to deflect the ball back toward the opponent.
12+
]]
13+
14+
Paddle = Class{}
15+
16+
--[[
17+
The `init` function on our class is called just once, when the object
18+
is first created. Used to set up all variables in the class and get it
19+
ready for use.
20+
21+
Our Paddle should take an X and a Y, for positioning, as well as a width
22+
and height for its dimensions.
23+
24+
Note that `self` is a reference to *this* object, whichever object is
25+
instantiated at the time this function is called. Different objects can
26+
have their own x, y, width, and height values, thus serving as containers
27+
for data. In this sense, they're very similar to structs in C.
28+
]]
29+
function Paddle:init(x, y, width, height)
30+
self.x = x
31+
self.y = y
32+
self.width = width
33+
self.height = height
34+
self.dy = 0
35+
end
36+
37+
function Paddle:update(dt)
38+
-- math.max here ensures that we're the greater of 0 or the player's
39+
-- current calculated Y position when pressing up so that we don't
40+
-- go into the negatives; the movement calculation is simply our
41+
-- previously-defined paddle speed scaled by dt
42+
if self.dy < 0 then
43+
self.y = math.max(0, self.y + self.dy * dt)
44+
-- similar to before, this time we use math.min to ensure we don't
45+
-- go any farther than the bottom of the screen minus the paddle's
46+
-- height (or else it will go partially below, since position is
47+
-- based on its top left corner)
48+
else
49+
self.y = math.min(VIRTUAL_HEIGHT - self.height, self.y + self.dy * dt)
50+
end
51+
end
52+
53+
--[[
54+
To be called by our main function in `love.draw`, ideally. Uses
55+
LÖVE2D's `rectangle` function, which takes in a draw mode as the first
56+
argument as well as the position and dimensions for the rectangle. To
57+
change the color, one must call `love.graphics.setColor`. As of the
58+
newest version of LÖVE2D, you can even draw rounded rectangles!
59+
]]
60+
function Paddle:render()
61+
love.graphics.rectangle('fill', self.x, self.y, self.width, self.height)
62+
end

pong-12/class.lua

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
--[[
2+
Copyright (c) 2010-2013 Matthias Richter
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
Except as contained in this notice, the name(s) of the above copyright holders
15+
shall not be used in advertising or otherwise to promote the sale, use or
16+
other dealings in this Software without prior written authorization.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
THE SOFTWARE.
25+
]]--
26+
27+
local function include_helper(to, from, seen)
28+
if from == nil then
29+
return to
30+
elseif type(from) ~= 'table' then
31+
return from
32+
elseif seen[from] then
33+
return seen[from]
34+
end
35+
36+
seen[from] = to
37+
for k,v in pairs(from) do
38+
k = include_helper({}, k, seen) -- keys might also be tables
39+
if to[k] == nil then
40+
to[k] = include_helper({}, v, seen)
41+
end
42+
end
43+
return to
44+
end
45+
46+
-- deeply copies `other' into `class'. keys in `other' that are already
47+
-- defined in `class' are omitted
48+
local function include(class, other)
49+
return include_helper(class, other, {})
50+
end
51+
52+
-- returns a deep copy of `other'
53+
local function clone(other)
54+
return setmetatable(include({}, other), getmetatable(other))
55+
end
56+
57+
local function new(class)
58+
-- mixins
59+
class = class or {} -- class can be nil
60+
local inc = class.__includes or {}
61+
if getmetatable(inc) then inc = {inc} end
62+
63+
for _, other in ipairs(inc) do
64+
if type(other) == "string" then
65+
other = _G[other]
66+
end
67+
include(class, other)
68+
end
69+
70+
-- class implementation
71+
class.__index = class
72+
class.init = class.init or class[1] or function() end
73+
class.include = class.include or include
74+
class.clone = class.clone or clone
75+
76+
-- constructor call
77+
return setmetatable(class, {__call = function(c, ...)
78+
local o = setmetatable({}, c)
79+
o:init(...)
80+
return o
81+
end})
82+
end
83+
84+
-- interface for cross class-system compatibility (see https://github.com/bartbes/Class-Commons).
85+
if class_commons ~= false and not common then
86+
common = {}
87+
function common.class(name, prototype, parent)
88+
return new{__includes = {prototype, parent}}
89+
end
90+
function common.instance(class, ...)
91+
return class(...)
92+
end
93+
end
94+
95+
96+
-- the module
97+
return setmetatable({new = new, include = include, clone = clone},
98+
{__call = function(_,...) return new(...) end})

pong-12/font.ttf

19 KB
Binary file not shown.

0 commit comments

Comments
 (0)