结对编程总结

  经过一个多月的时间,结对编程项目已经接近了尾声,通过软件工程这门课,让我和我的搭档学会了如何合作,如何一起处理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 pygame.event.get():
if event.type == pygame.QUIT:
game_playing = False
# 更新数据
# 更新画面
game_screen.fill(game_bgcolor)
pygame.draw.rect(game_screen, square_color,
(square_x, square_y, square_size, square_size))
pygame.display.flip()
game_clock.tick(game_speed)

pygame.quit()
sys.exit(0)

  2.移动方框,通过键盘上的上下左右四个键位,对小方块也就是蛇头进行移动,核心代码如下:

for event in pygame.event.get():
if event.type == pygame.QUIT:
game_playing = False
elif event.type == 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 = square_rect.move(square_direction)
if square_rect.left < 0:
square_rect.left = 0
elif square_rect.right > game_screen_
square_rect.right = game_screen_width
if square_rect.top < 0:
square_rect.top = 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 pygame.time.get_ticks() >= square_time2move:
square_time2move = pygame.time.get_ticks() + square_delay
square_direction = square_turn
square_rect = square_rect.move(square_direction)
output = "坐标:%r 速度:%r 范围:%r FPS:%0.2f 时间:%r"
print output % (square_rect, square_direction,
game_field.contains(square_rect), game_clock.get_fps(),
pygame.time.get_ticks())

  6.蛇身以及对碰撞边缘的判定,,如果蛇头碰到了边缘会提示GameOver,以及画出蛇身。

if pygame.time.get_ticks() >= square_time2move:
square_time2move = pygame.time.get_ticks() + square_delay
square_body = [square_rect] + square_body # 增加一节身体
square_body.pop() # 截取尾部
square_direction = square_turn
square_rect = square_rect.move(square_direction)

if game_playing:
# 撞墙
if not game_field.contains(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 self.field.get_cell(x, y) is None:
self.x, self.y = x, y
self.field.put_cell(self)
break

  9.贪吃蛇已经初步完成了,接下来是对于游戏的完善,所以我们加了对文字的显示,对游戏的暂停以及重新开始的功能。

   以上是我们对于这次结对编程的总价,当然我们遇到了一些困难,比如对蛇头的控制,始终没有达到要求,后来通过在网上学习,找到了方法,还有就是在把代码分解成类的时候我们发生了分歧,意见不统一觉得分解那么多文件没有必要,而且很麻烦,但是再慢慢的调节下最后决定分解。通过此次结对编程,让我受益匪浅,而且加强了自己的专业知识,很感谢老师助教以及我的搭档。

原文地址:https://www.cnblogs.com/npqnpq/p/7774818.html