简介

本项目来源于哈佛大学的cs50课程,在跟随视频教程的学习过程中,对游戏开发的基本过程有一定了解,有意识好的项目架构应该遵循怎样的原则,是一个不错的Toy project,在经过自主学习之后,部分代码本人进行了中文注释.

以下选取重点功能部分的代码进行展示。

cs50games youtube地址

Ball.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
--[[
Pong

-- Ball Class --
游戏简介:一个球在屏幕中间自由移动,两个玩家控制两个类似浆的矩形,像打乒乓球一样将球打向对方
]]

Ball = Class{}

function Ball:init(x, y, width, height)
self.x = x
self.y = y
self.width = width
self.height = height

-- 随机球的初始移动速度
self.dy = math.random(2) == 1 and -100 or 100
self.dx = math.random(2) == 1 and math.random(-80, -100) or math.random(80, 100)
end

--[[
球与浆的碰撞检测
]]
function Ball:collides(paddle)


if self.x > paddle.x + paddle.width or paddle.x > self.x + self.width then
return false
end


if self.y > paddle.y + paddle.height or paddle.y > self.y + self.height then
return false
end

-- 返回false,则说明发生了碰撞
return true
end

--[[
将球重新置于中央,并且给与随机的初始速度
]]
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)
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.width, self.height)
end

paddle.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
Paddle = Class{}

--[[
初始化浆
]]
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)
if self.dy < 0 then
self.y = math.max(0,self.y + self.dy * dt)

else
self.y = math.min(VIRTUAL_HEIGHT - self.height,self.y + self.dy * dt)
end
end


--[[
渲染浆的图形
]]
function Paddle:render()
love.graphics.rectangle('fill',self.x, self.y, self.width, self.height)
end





main.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
--  pong完整版(包括ai电脑功能)

push = require 'push' --导入push库

Class = require 'class'

require 'Paddle'

require 'Ball'

WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720 --定义窗体的长度和宽度

VIRTUAL_WIDTH = 432
VIRTUAL_HEIGHT = 243 --定义虚拟窗口大小


PADDLE_SPEED = 200


function love.load() --游戏开始的初始化
love.graphics.setDefaultFilter('nearest', 'nearest') --当缩放时使用最近边缘处理模糊部分

love.window.setTitle("PongPong")

math.randomseed(os.time()) --设置随机数种子,可更好保证随机性

smallFont = love.graphics.newFont('font.ttf',8) --设置新的字体对象
largeFont = love.graphics.newFont('font.ttf', 16) --成功后的提示字体
scoreFont = love.graphics.newFont('font.ttf',32) --显示分数的字体

love.graphics.setFont(smallFont)

-- 设置游戏中的各种音效
sounds = {
['paddle_hit'] = love.audio.newSource('sounds/paddle_hit.wav','static'),
['score'] = love.audio.newSource('sounds/score.wav', 'static'),
['wall_hit'] = love.audio.newSource('sounds/wall_hit.wav', 'static'),
['win'] = love.audio.newSource('sounds/win.mp3', 'static'),
['background'] = love.audio.newSource('sounds/background.mp3', 'static')
}

push:setupScreen(VIRTUAL_WIDTH,VIRTUAL_HEIGHT,WINDOW_WIDTH,WINDOW_HEIGHT,{
fullscreen = false,
resizable = true,
vsync = true
})
--[[
初始化各种变量
]]
player1Score = 0
player2Score = 0

servingPlayer = 1

player1 = Paddle(10,30,5,20)
player2 = Paddle(VIRTUAL_WIDTH - 10, VIRTUAL_HEIGHT - 30, 5, 40)

ball = Ball(VIRTUAL_WIDTH / 2 - 2, VIRTUAL_HEIGHT / 2 - 2, 4, 4)
gameState = 'start'
end

--[[
更新窗口大小
]]
function love.resize(w,h)
push:resize(w,h)
end


function love.update(dt)
if gameState == 'serve' then

-- 在开始之前,初始化球的上下移动速度
ball.dy = math.random(-70, 70)

--当需要ai时,为player2设置初始速度
player2.dy = -200

--根据servingPlayer,确定球本次发射的初始左右移动方向
if servingPlayer == 1 then
ball.dx = math.random(140, 180)
else
ball.dx = -math.random(140, 180)
end
elseif gameState == 'play' then

sounds['background']:play()

--发生碰撞,球的x轴移动方向反向
if ball:collides(player1) then
ball.dx = -ball.dx
ball.x = player1.x + 5

if ball.dy < 0 then
ball.dy = -math.random(10,150)
else
ball.dy = math.random(10,150)
end

sounds['paddle_hit']:play()
end

if ball:collides(player2) then
ball.dx = -ball.dx
ball.x = player2.x - 4

-- keep velocity going in the same direction, but randomize it
if ball.dy < 0 then
ball.dy = -math.random(10, 150)
else
ball.dy = math.random(10, 150)
end

sounds['paddle_hit']:play()
end

-- 球碰到边界,y轴移动方向反向
if ball.y <= 0 then
ball.y = 0
ball.dy = -ball.dy
sounds['wall_hit']:play()
end

if ball.y >= VIRTUAL_HEIGHT - 4 then
ball.y = VIRTUAL_HEIGHT - 4
ball.dy = -ball.dy
sounds['wall_hit']:play()
end



-- 如果球超出左右边界,则对应玩家得分
if ball.x < 0 then
servingPlayer = 1
player2Score = player2Score + 1
sounds['score']:play()

-- 玩家得到2分,则游戏结束,取得胜利
if player2Score == 2 then
winningPlayer = 2
gameState = 'done'
sounds['win']:play()
else
gameState = 'serve'

-- 将球重新置于中央
ball:reset()
end
end

if ball.x > VIRTUAL_WIDTH then
servingPlayer = 2
player1Score = player1Score + 1
sounds['score']:play()

if player1Score == 2 then
winningPlayer = 1
gameState = 'done'
sounds['win']:play()
else
gameState = 'serve'
ball:reset()
end
end
end



-- player 1 movement
if love.keyboard.isDown('w') then
player1.dy = -PADDLE_SPEED
elseif love.keyboard.isDown('s') then
player1.dy = PADDLE_SPEED
else
player1.dy = 0
end

-- player 2 movement
if love.keyboard.isDown('up') then
player2.dy = -PADDLE_SPEED
elseif love.keyboard.isDown('down') then
player2.dy = PADDLE_SPEED
else
--AI状态下注释下一行 ,使得player2不会速度一直为0
player2.dy = 0
end

-- AI movement

if(player2.y > VIRTUAL_HEIGHT - 50) then
player2.y = VIRTUAL_HEIGHT - 50
player2.dy = - player2.dy
end

if(player2.y < 5) then
player2.y = 5
player2.dy = -player2.dy
end




if(gameState == 'play') then
ball:update(dt)
player2:update(dt)
end

player1:update(dt)
--ai状态下需要注释掉此行
--player2:update(dt)

end


function love.keypressed(key)

if key == 'escape' then --如果键盘按下esc,则退出love

love.event.quit()
-- 回车控制游戏的状态变化,进入游戏的下一个状态
elseif key == 'enter' or key == 'return' then
if gameState == 'start' then
gameState = 'serve'
elseif gameState == 'serve' then
gameState = 'play'
elseif gameState == 'done' then

-- 胜利之后,进行serve状态,游戏重新等待开始
gameState = 'serve'

ball:reset()

-- 得分重置
player1Score = 0
player2Score = 0

-- 切换发球方向
if winningPlayer == 1 then
servingPlayer = 2
else
servingPlayer = 1
end
end
end
end

function love.draw()

push:apply('start')

love.graphics.clear(40/255, 45/255, 52/255, 255/255) --清空面板,并且设置颜色

love.graphics.setFont(smallFont) --使用小字体

displayScore()

if gameState == 'start' then
love.graphics.setFont(smallFont)
love.graphics.printf('Welcome to Pong!', 0, 10, VIRTUAL_WIDTH, 'center')
love.graphics.printf('Press Enter to begin!', 0, 20, VIRTUAL_WIDTH, 'center')

elseif gameState == 'serve' then
love.graphics.setFont(smallFont)
love.graphics.printf('Player ' .. tostring(servingPlayer) .. "'s serve!",
0, 10, VIRTUAL_WIDTH, 'center')
love.graphics.printf('Press Enter to serve!', 0, 20, VIRTUAL_WIDTH, 'center')

elseif gameState == 'play' then
-- play状态下没有UI信息

elseif gameState == 'done' then

-- UI messages
love.graphics.setFont(largeFont)
love.graphics.printf('Player ' .. tostring(winningPlayer) .. ' wins!',
0, 10, VIRTUAL_WIDTH, 'center')
love.graphics.setFont(smallFont)
love.graphics.printf('Press Enter to restart!', 0, 30, VIRTUAL_WIDTH, 'center')
end


-- 渲染浆,使用他们类的函数
player1:render()
player2:render()

-- 渲染球同理
ball:render()

displayFPS() --显示fps

push:apply('end')
end

function displayFPS()

love.graphics.setFont(smallFont)
love.graphics.setColor(0,255/255,0,255/255)
love.graphics.print('FPS: ' .. tostring(love.timer.getFPS()), 10, 10)
end

function displayScore()

-- 显示玩家的分数
love.graphics.setFont(scoreFont)
love.graphics.print(tostring(player1Score), VIRTUAL_WIDTH / 2 - 50,
VIRTUAL_HEIGHT / 3)
love.graphics.print(tostring(player2Score), VIRTUAL_WIDTH / 2 + 30,
VIRTUAL_HEIGHT / 3)
end