收藏 分销(赏)

贪吃蛇游戏用python总结_结对-贪吃蛇游戏-结对项目总结.pdf

上传人:鱼** 文档编号:851941 上传时间:2024-03-29 格式:PDF 页数:5 大小:283.85KB
下载 相关 举报
贪吃蛇游戏用python总结_结对-贪吃蛇游戏-结对项目总结.pdf_第1页
第1页 / 共5页
贪吃蛇游戏用python总结_结对-贪吃蛇游戏-结对项目总结.pdf_第2页
第2页 / 共5页
点击查看更多>>
资源描述
贪吃蛇游戏python总结_结对-贪吃蛇游戏-结对项总结经过个多的时间,结对编程项已经接近了尾声,通过软件程这门课,让我和我的搭档学会了如何合作,如何起处理bug,如何结对编程。我们所做的项是利python带的pygame来编写个程序贪吃蛇,这个游戏我们概分为了以下个步骤,并且逐实现,现在就来总结下:1.窗和块:先每个游戏必备的步骤就是主循环以及个背景,所以我们先做个背景以及蛇头的初步实现,代码如下:game_screen=pygame.display.set_mode(game_screen_width,game_screen_height)game_playing=Truegame_bgcolor=33,66,33square_color=33,255,33square_x,square_y=0,0square_size=20while 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=Falseelif event.type=pygame.KEYDOWN:if event.key=pygame.K_UP:square_speed_x=0square_speed_y=-square_speedelif event.key=pygame.K_DOWN:square_speed_x=0square_speed_y=square_speedelif event.key=pygame.K_LEFT:square_speed_x=-square_speedsquare_speed_y=0elif event.key=pygame.K_RIGHT:square_speed_x=square_speedsquare_speed_y=0square_x+=square_speed_xsquare_y+=square_speed_yif square_x game_screen_width-square_size:square_x=game_screen_width-square_sizeif square_y game_screen_height-square_size:square_y=game_screen_height-square_sizeprint 坐标: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_turnsquare_rect=square_rect.move(square_direction)if square_rect.left game_screen_width:square_rect.right=game_screen_widthif square_rect.top game_screen_height:square_rect.bottom=game_screen_heightprint 坐标:(%3d,%3d)速度:(%2d,%2d)%(square_rect.x,square_rect.y,square_direction0,square_direction1)4.进步调整块,在背景上画出横线和纵线,也就是格,我们发现虽然块得到了很好的控制,但是不是我们想要的,我们希望其每次运动都按照格,不是到了格上,所以我们进了调整,调整代码如下:square_speed=5#每秒格square_delay=1000/square_speed#蛇每次运动的间隔if pygame.time.get_ticks()=square_time2move:square_time2move=pygame.time.get_ticks()+square_delaysquare_direction=square_turnsquare_rect=square_rect.move(square_direction)output=坐标:%r 速度:%r 范围:%r FPS:%0.2f 时间:%rprint 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_delaysquare_body=square_rect+square_body#增加节体square_body.pop()#截取尾部square_direction=square_turnsquare_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=Falseif 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.fieldself.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,yself.field.put_cell(self)break9.贪吃蛇已经初步完成了,接下来是对于游戏的完善,所以我们加了对字的显,对游戏的暂停以及重新开始的功能。以上是我们对于这次结对编程的总价,当然我们遇到了些困难,如对蛇头的控制,始终没有达到要求,后来通过在上学习,找到了法,还有就是在把代码分解成类的时候我们发了分歧,意见不统觉得分解那么多件没有必要,且很烦,但是再慢慢的调节下最后决定分解。通过此次结对编程,让我受益匪浅,且加强了的专业知识,很感谢师助教以及我的搭档。
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 通信科技 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服