forked from adamwiggins/lua-pong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.lua
158 lines (127 loc) · 3.14 KB
/
pong.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
math.randomseed(os.time())
Rect = {x = 0, y = 0, width = 0.3, height = 0.3, red = 0.0, green = 1.0, blue = 0.0}
function Rect:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function Rect:draw()
-- note that this is a registered C function
draw_rectangle(self.x, self.y, self.width, self.height, self.red, self.green, self.blue)
end
function Rect:intersects(other)
return not (self.x + self.width < other.x or
other.x + other.width < self.x or
self.y + self.height < other.y or
other.y + other.height < self.y)
end
---------------------------
-- side == 1: player's side
-- else : CPU's side
Paddle = {y = 0, side = 1}
function Paddle:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
o:init()
return o
end
function Paddle:init()
self.rect = Rect:new{y = y, width = 0.05, height = 0.3}
if self.side == 1 then
self.rect.x = -0.9
self.rect.green = 1.0
else
self.rect.x = 0.9
self.rect.green = 0.0
self.rect.blue = 1.0
end
end
function Paddle:draw()
self.rect.y = self.y
self.rect:draw()
end
function Paddle:move()
if self.side == 1 then
if up_pressed() then self.y = self.y + 0.01 end
if down_pressed() then self.y = self.y - 0.01 end
else
if self.y < ball.y then self.y = self.y + 0.01 end
if self.y > ball.y then self.y = self.y - 0.01 end
end
end
---------------------------
-- vx, vy is the speed
Ball = {x = 0, y = 0, vx = 0.0, vy = 0.0}
function Ball:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
o:init()
return o
end
function Ball:init()
self.rect = Rect:new{x = x, y = y, width = 0.03, height = 0.03, red = 1.0, green = 0.0, blue = 0.0}
end
function Ball:move()
newrect = Rect:new(self.rect)
newrect.x = newrect.x + self.vx
newrect.y = newrect.y + self.vy
-- detect boundary
if self.y > 1.0 or self.y < -1.0 then
self.vy = self.vy * (-1.0)
end
for _, paddle in pairs(paddles) do
if paddle.rect:intersects(newrect) then
-- bounce when meets the paddle
self.vx = self.vx * (-1.0 + math.random(-5, 5) * 0.1)
self.vy = self.vy * (-1.0 + math.random(-5, 5) * 0.1)
end
end
self.x = self.x + self.vx
self.y = self.y + self.vy
end
function Ball:draw()
self.rect.x = self.x
self.rect.y = self.y
self.rect:draw()
end
---------------------------
function describe(class, func)
print(class)
func()
print()
end
function it(descr, func)
if (func()) then
print("- ", descr)
else
print("[fail] ", descr)
end
end
function all_tests()
describe("Rect", function()
r1 = Rect:new{x = 0, y = 0, width = 3, height = 1}
r2 = Rect:new{x = 1, y = 0, width = 2, height = 2}
r3 = Rect:new{x = 4, y = 0, width = 2, height = 2}
it("intersects two rectangles", function()
return r1:intersects(r2) == true
end)
it("doesn't intersect non-intersecting rectangles", function()
return r1:intersects(r3) == false
end)
end)
end
---------------------------
-- initialize the paddle and the ball
paddles = { Paddle:new{side=1}, Paddle:new{side=2} }
ball = Ball:new{x = 0.0, y = 0.0, vx = -0.005, vy = 0.005}
function pulse()
for _, paddle in pairs(paddles) do
paddle:move()
paddle:draw()
end
ball:move()
ball:draw()
end