1 / 6
文档名称:

使用Python编写一个贪吃蛇.pdf

格式:pdf   大小:408KB   页数:6页
下载后只包含 1 个 PDF 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

使用Python编写一个贪吃蛇.pdf

上传人:青山代下 2024/3/17 文件大小:408 KB

下载得到文件列表

使用Python编写一个贪吃蛇.pdf

相关文档

文档介绍

文档介绍:该【使用Python编写一个贪吃蛇 】是由【青山代下】上传分享,文档一共【6】页,该文档可以免费在线阅读,需要了解更多关于【使用Python编写一个贪吃蛇 】的内容,可以使用淘豆网的站内搜索功能,选择自己适合的文档,以下文字是截取该文章内的部分文字,如需要获得完整电子版,请下载此文档到您的设备,方便您编辑和打印。:..*fromosimportsystemsystem()Snakespeed=7Window_Width=800Window_Height=500Cell_Size=20#Widthandheightofthecells###%Cell_Size==0,#%Cell_Size==0,Cell_W=int(Window_Width/Cell_Size)#CellWidthCell_H=int(Window_Height/Cell_Size)#CellcHeightWhite=(255,255,255)Black=(0,0,0)Red=(255,0,0)#=(0,255,0)DARKGreen=(0,155,0)DARKGRAY=(40,40,40)YELLOW=(255,255,0)Red_DARK=(150,0,0)BLUE=(0,0,255)BLUE_DARK=(0,0,150)BGCOLOR=Black#BackgroundcolorUP='up'DOWN='down'#='left':..RIGHT='right'HEAD=0#Syntacticsugar:indexofthesnake'sheaddefmain():globalSnakespeedCLOCK,DISPLAYSURF,()SnakespeedCLOCK=()DISPLAYSURF=((Window_Width,Window_Height))BASICFONT=('',18)('Snake')showStartScreen()whileTrue:runGame()showGameOverScreen()defrunGame():#=(5,Cell_W-6)starty=(5,Cell_H-6)wormCoords=[{'x':startx,'y':starty},{'x':startx-1,'y':starty},{'x':startx-2,'y':starty}]direction=RIGHT#=getRandomLocation()whileTrue:#():#==QUIT:terminate()==KEYDOWN:if(==K_LEFT)anddirection!=RIGHT:direction=LEFTelif(==K_RIGHT)anddirection!=LEFT:direction=RIGHTelif(==K_UP)anddirection!=DOWN:direction=UP:..elif(==K_DOWN)anddirection!=UP:direction===K_ESCAPE:terminate()#checkiftheSnakehashititselfortheedgeifwormCoords[HEAD]['x']==-1orwormCoords[HEAD]['x']==Cell_WorwormCoords[HEAD]['y']==-1orwormCoords[HEAD]['y']==Cell_H:return#gameoverforwormBodyinwormCoords[1:]:ifwormBody['x']==wormCoords[HEAD]['x']andwormBody['y']==wormCoords[HEAD]['y']:return#gameover#checkifSnakehaseatenanapplyifwormCoords[HEAD]['x']==apple['x']andwormCoords[HEAD]['y']==apple['y']:#don'tremoveworm'stailsegmentapple=getRandomLocation()#setanewapplesomewhereelse:delwormCoords[-1]#removeworm'stailsegment#movethewormbyaddingasegmentinthedirectionitismovingifdirection==UP:newHead={'x':wormCoords[HEAD]['x'],'y':wormCoords[HEAD]['y']-1}elifdirection==DOWN:newHead={'x':wormCoords[HEAD]['x'],'y':wormCoords[HEAD]['y']+1}elifdirection==LEFT:newHead={'x':wormCoords[HEAD]['x']-1,'y':wormCoords[HEAD]['y']}elifdirection==RIGHT:newHead={'x':wormCoords[HEAD]['x']+1,'y':wormCoords[HEAD]['y']}(0,newHead)(BGCOLOR)drawGrid()drawWorm(wormCoords)drawApple(apple)drawScore(len(wormCoords)-3)():..(Snakespeed)defdrawPressKeyMsg():pressKeySurf=('Pressakeytoplay.',True,White)pressKeyRect=()=(Window_Width-200,Window_Height-30)(pressKeySurf,pressKeyRect)defcheckForKeyPress():iflen((QUIT))>0:terminate()keyUpEvents=(KEYUP)iflen(keyUpEvents)==0:returnNoneifkeyUpEvents[0].key==K_ESCAPE:terminate()returnkeyUpEvents[0].keydefshowStartScreen():titleFont=('',100)titleSurf1=('Snake!',True,White,DARKGreen)degrees1=0degrees2=0whileTrue:(BGCOLOR)rotatedSurf1=(titleSurf1,degrees1)rotatedRect1=()=(Window_Width/2,Window_Height/2)(rotatedSurf1,rotatedRect1)drawPressKeyMsg()ifcheckForKeyPress():()#()(Snakespeed)degrees1+=3#rotateby3degreeseachframedegrees2+=7#rotateby7degreeseachframe:..defterminate():()()defgetRandomLocation():return{'x':(0,Cell_W-1),'y':(0,Cell_H-1)}defshowGameOverScreen():gameOverFont=('',100)gameSurf=('Game',True,White)overSurf=('Over',True,White)gameRect=()overRect=()=(Window_Width/2,10)=(Window_Width/2,+10+25)(gameSurf,gameRect)(overSurf,overRect)drawPressKeyMsg()()(500)checkForKeyPress()#clearoutanykeypressesintheeventqueuewhileTrue:ifcheckForKeyPress():()#cleareventqueuereturndefdrawScore(score):scoreSurf=('Score:%s'%(score),True,White)scoreRect=()=(Window_Width-120,10)(scoreSurf,scoreRect)defdrawWorm(wormCoords):forcoordinwormCoords:x=coord['x']*Cell_Sizey=coord['y']*Cell_Size:..wormSegmentRect=(x,y,Cell_Size,Cell_Size)(DISPLAYSURF,DARKGreen,wormSegmentRect)wormInnerSegmentRect=(x+4,y+4,Cell_Size-8,Cell_Size-8)(DISPLAYSURF,Green,wormInnerSegmentRect)defdrawApple(coord):x=coord['x']*Cell_Sizey=coord['y']*Cell_SizeappleRect=(x,y,Cell_Size,Cell_Size)(DISPLAYSURF,Red,appleRect)defdrawGrid():forxinrange(0,Window_Width,Cell_Size):#(DISPLAYSURF,DARKGRAY,(x,0),(x,Window_Height))foryinrange(0,Window_Height,Cell_Size):#(DISPLAYSURF,DARKGRAY,(0,y),(Window_Width,y))if__name__=='__main__':try:main()exceptSystemExit:pass