贪吃蛇游戏⽤python总结_结对-贪吃蛇游戏-结对项⽬总结经过⼀个多⽉的时间,结对编程项⽬已经接近了尾声,通过软件⼯程这门课,让我和我的搭档学会了如何合作,如何⼀起处理bug,如何结对编程。
我们所做的项⽬是利⽤python⾃带的pygame来编写⼀个⼩程序贪吃蛇,这个游戏我们⼤概分为了以下个步骤,并且逐⼀实现,现在就来总结⼀下:
1.窗⼝和⽅块:⾸先每⼀个游戏必备的步骤就是主循环以及⼀个背景,所以我们⾸先做⼀个背景以及蛇头的初步实现,代码如下:
game_screen = pygame.display.set_mode((game_screen_width, game_screen_height))
game_playing = True
game_bgcolor = 33, 66, 33
square_color = 33, 255, 33
square_x, square_y = 0, 0
square_size = 20
while game_playing:
贪吃蛇的编程代码# ⽤户控制
for event in ():
pe == pygame.QUIT:
game_playing = False
# 更新数据
# 更新画⾯
game_screen.fill(game_bgcolor)
(game_screen, square_color,
(square_x, square_y, square_size, square_size))
pygame.display.flip()
game_clock.tick(game_speed)
pygame.quit()
2.移动⽅框,通过键盘上的上下左右四个键位,对⼩⽅块也就是蛇头进⾏移动,核⼼代码如下:
for event in ():
pe == pygame.QUIT:
game_playing = False
pe == pygame.KEYDOWN:
if event.key == pygame.K_UP:
square_speed_x = 0
square_speed_y = -square_speed
elif event.key == pygame.K_DOWN:
square_speed_x = 0
square_speed_y = square_speed
elif event.key == pygame.K_LEFT:
square_speed_x = -square_speed
square_speed_y = 0
elif event.key == pygame.K_RIGHT:
square_speed_x = square_speed
square_speed_y = 0
square_x += square_speed_x
square_y += square_speed_y
if square_x < 0:
square_x = 0
elif square_x > game_screen_width - square_size:
square_x = game_screen_width - square_size
if square_y < 0:
square_y = 0
elif square_y > game_screen_height - square_size:
square_y = game_screen_height - square_size
print "坐标:x %3d, y %3d, 速度:x %d, y %d" % (square_x, square_y,
square_speed_x,
square_speed_y)
3.调整⽅块的定位以及速度,移动的时候我们发现⽅块不受控制,并不是我们所见到的贪吃蛇⼀样,是按格⼦⾛的并且是有频率的,所以我们对代码进⾏了改变,以及把⽅块的坐标进⾏打印,以便于对贪吃蛇定位,核⼼代码如下:
if square_rect.x % CELL_SIZE == 0 and square_rect.y % CELL_SIZE == 0:
square_direction = square_turn
square_rect = ve(square_direction)
if square_rect.left < 0:
square_rect.left = 0
elif square_rect.right > game_screen_width:
square_rect.right = game_screen_width
if p < 0:
p = 0
elif square_rect.bottom > game_screen_height:
square_rect.bottom = game_screen_height
print "坐标:(%3d, %3d) 速度:(%2d, %2d)" % (square_rect.x, square_rect.y,
square_direction[0],
square_direction[1])
4.进⼀步调整⽅块,在背景上画出横线和纵线,也就是⽹格,我们发现虽然⼩⽅块得到了很好的控制,但是不是我们想要的,我们希望其每次运动都按照格⼦⾛,⽽不是⾛到了格⼦上,所以我们进⾏了调整,调整代码如下:
square_speed = 5 # 每秒⾛⼏格
square_delay = 1000 / square_speed # 蛇每次运动的间隔
if _ticks() >= square_time2move:
square_time2move = _ticks() + square_delay
square_direction = square_turn
square_rect = ve(square_direction)
output = "坐标:%r 速度:%r 范围:%r FPS:%0.2f 时间:%r"
print output % (square_rect, square_direction,
ains(square_rect), _fps(),
_ticks())
6.蛇⾝以及对碰撞边缘的判定,,如果蛇头碰到了边缘会提⽰GameOver,以及画出蛇⾝。
if _ticks() >= square_time2move:
square_time2move = _ticks() + square_delay
square_body = [square_rect] + square_body # 增加⼀节⾝体
square_body.pop() # 截取尾部
square_direction = square_turn
square_rect = ve(square_direction)
if game_playing:
# 撞墙
if not ains(square_rect):
game_playing = False
# 撞⾝体
for cell in square_body:
if square_rect == cell:
game_playing = False
if not game_playing:
print "GAME OVER "
7.现在贪吃蛇的雏形已经形成,由于代码过于多,复杂,我们决定要写成对象的形式,重新创建了⼀个Mygame类,然后写成多个⽂件。mygame类⽂件
pysanke类⽂件
settings类⽂件
实现了蛇⾝,按退出键推出,以及⼀直更新场地
8.苹果,定义苹果类,代码如下:
class Apple(Cell):
def __init__(self, game):
super(Apple, self).__init__(0, 0, APPLE_COLOR1, APPLE_COLOR2)
self.field = game.field
self.drop()
def drop(self):
while True:
x, y = randint(0, COLUMNS - 1), randint(0, ROWS - 1)
if _cell(x, y) is None:
self.x, self.y = x, y
self.field.put_cell(self)
break
9.贪吃蛇已经初步完成了,接下来是对于游戏的完善,所以我们加了对⽂字的显⽰,对游戏的暂停以及重新开始的功能。
以上是我们对于这次结对编程的总价,当然我们遇到了⼀些困难,⽐如对蛇头的控制,始终没有达到要求,后来通过在⽹上学习,到了⽅法,还有就是在把代码分解成类的时候我们发⽣了分歧,意见不统⼀觉得分解那么多⽂件没有必要,⽽且很⿇烦,但是再慢慢的调节下最后决定分解。通过此次结对编程,让我受益匪浅,⽽且加强了⾃⼰的专业知识,很感谢⽼师助教以及我的搭档。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。