-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-1.lua
226 lines (197 loc) · 6.92 KB
/
main-1.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
--things to adjust; bounce, AI PADDLE SPEED, ball speed
-- add option to decide number of players
--print number and add rectangle without fill around number use arrow keys to change players
W_WIDTH = 1280
W_HEIGHT = 720
V_WIDTH = 432
V_HEIGHT = 243
PADDLE_SPEED = 200
AI_PADDLE_SPEED = 200
ballSpeed = 250
push = require 'push'
Class = require 'class'
bounce = 150
require 'Paddle'
require 'Ball'
rallies = 0
sounds = {
['paddleBlip'] = love.audio.newSource('sounds/paddleBlip.wav', 'static'),
['point'] = love.audio.newSource('sounds/point.wav', 'static'),
['wallBlip'] = love.audio.newSource('sounds/wallBlip.wav', 'static'),
['win'] = love.audio.newSource('sounds/win.wav', 'static')
}
players = 0
server = math.random(2) == 1 and 1 or 2
winner = 0
function love.load(key)
love.window.setTitle('Pong')
--initializes paddles and ball
player1 = Paddle(10, 35, 5, 35, 0)
player2 = Paddle(V_WIDTH - 15, V_HEIGHT - 70, 5, 35, 0)
Ball = Ball(V_WIDTH / 2 - 2, V_HEIGHT / 2 - 2, 4, 4)
love.graphics.setDefaultFilter('nearest', 'nearest')
smallFont = love.graphics.newFont('smallFont.ttf', 8)
largeFont = love.graphics.newFont('smallFont.ttf', 32)
mediumFont = love.graphics.newFont('smallFont.ttf', 16)
--scores
push:setupScreen(V_WIDTH, V_HEIGHT, W_WIDTH, W_HEIGHT, {
fullscreen = false,
vsync = true,
resizable = true
})
--initial game state
gameState = 'start'
paused = false
end
function love.resize(w, h)
push:resize(w, h)
end
function love.update(dt)
--moves player 1 (with w and s)
math.randomseed(os.time())
if love.keyboard.isDown('w') and paused ~= true then
player1.dy = -PADDLE_SPEED
elseif love.keyboard.isDown('s') and paused ~= true then
player1.dy = PADDLE_SPEED
else
player1.dy = 0
end
--the AI
--moves player 2 (with arrow keys)
--if ball is within paddle, dont move
--0.8 so that it's not dangerously close to edge
if paused ~= true and Ball.y + 0.5 * Ball.HEIGHT > player2.y + player2.HEIGHT * 0.2 and Ball.y + 0.5 * Ball.HEIGHT < player2.y + 0.8 * player2.HEIGHT then
player2.dy = 0
elseif paused ~= true and Ball.y < player2.y + 0.5 * player2.HEIGHT then
player2.dy = -AI_PADDLE_SPEED
elseif paused ~= true and Ball.y > player2.y + 0.5 * player2.HEIGHT then
player2.dy = AI_PADDLE_SPEED
else
player2.dy = 0
end
if gameState == 'play' then
Ball:update(dt)
end
if Ball:collides(player1) then
Ball.dx = -Ball.dx
sounds['paddleBlip']:play()
rallies = rallies + 0.5
Ball.dy = math.random(-bounce, bounce)
elseif Ball:collides(player2) then
Ball.dx = -Ball.dx
rallies = rallies + 0.5
sounds['paddleBlip']:play()
Ball.dy = math.random(-bounce, bounce)
--gives the player a chance by decreasing it's speed slowly
--caps out at a percentage of the original speed
if rallies >= 4 and (AI_PADDLE_SPEED < 0.4 * PADDLE_SPEED) == false then
AI_PADDLE_SPEED = AI_PADDLE_SPEED - ((rallies / 40) * AI_PADDLE_SPEED)
end
end
Ball:screenCollide()
player1:update(dt)
player2:update(dt)
if Ball.x + Ball.WIDTH < player1.x then
player2.SCORE = player2.SCORE + 1
sounds['point']:play()
Ball:reset()
gameState = 'start'
server = 1
rallies = 0
AI_PADDLE_SPEED = PADDLE_SPEED
victory(player1, player2)
elseif Ball.x > player2.x + player2.WIDTH then
player1.SCORE = player1.SCORE + 1
sounds['point']:play()
Ball:reset()
gameState = 'start'
server = 2
rallies = 0
AI_PADDLE_SPEED = PADDLE_SPEED
victory(player1, player2)
end
end
function love.keypressed(key)
if key == 'escape' then
love.event.quit()
elseif key == 'return' then
Ball:reset()
if gameState == 'start' then
gameState = 'serve'
elseif gameState == 'play' then
gameState = 'start'
elseif gameState == 'pause' then
--sets paused to false so that you can move the paddle again
paused = false
gameState = 'start'
elseif gameState == 'serve' then
gameState = 'play'
elseif gameState == 'victory' then
gameState = 'start'
end
elseif key == 'space' then
if gameState == 'play'then
gameState = 'pause'
paused = true
elseif gameState == 'pause' then
gameState = 'play'
paused = false
end
end
end
function love.draw()
push:apply('start')
love.graphics.clear(45 / 255, 45 / 255, 52 / 255, 255 / 255)
--ball
Ball:render()
--paddles
--paddle 1
player1:render()
--paddle 2
player2:render()
love.graphics.setFont(smallFont)
if gameState == 'start' then
love.graphics.printf("Press enter to start!", 0, 20, V_WIDTH, 'center')
elseif gameState == 'serve' then
love.graphics.printf("Player " .. tostring(server) .. "'s turn to server", 0, 20, V_WIDTH, 'center')
love.graphics.printf("Press enter to serve", 0, 32, V_WIDTH, 'center')
elseif gameState == 'pause' then
love.graphics.setFont(mediumFont)
love.graphics.printf("Paused", 0, 20, V_WIDTH, 'center')
elseif gameState == 'play' then
love.graphics.printf(" ", 0, 20, V_WIDTH, 'center')
elseif gameState == 'victory' then
love.graphics.setFont(largeFont)
love.graphics.printf("Player " .. tostring(winner) .. " wins", 0, 20, V_WIDTH, 'center')
love.graphics.setFont(smallFont)
love.graphics.printf("Press enter to play again", 0, 56, V_WIDTH, 'center')
end
love.graphics.printf(rallies, 0, 70, V_WIDTH, 'center')
love.graphics.printf(AI_PADDLE_SPEED, 0, 78, V_WIDTH, 'center')
if gameState ~= 'victory' then
love.graphics.setFont(largeFont)
love.graphics.printf(player1.SCORE, 68, 40, V_WIDTH, 'center')
love.graphics.printf(player2.SCORE, -68, 40, V_WIDTH, 'center')
end
displayFPS()
push:apply('end')
end
function displayFPS()
love.graphics.setFont(smallFont)
love.graphics.setColor(0, 1, 0, 0.3)
love.graphics.printf('FPS: ' .. tostring(love.timer.getFPS()), 2, 2, V_WIDTH, 'left')
end
function victory(box1, box2)
if box1.SCORE == 3 then
gameState = 'victory'
winner = 1
elseif box2.SCORE == 3 then
gameState = 'victory'
winner = 2
end
if gameState == 'victory' then
sounds['win']:play()
box1.SCORE = 0
box2.SCORE = 0
end
end