是用python写的,还包括分数获取是否打破记录和游戏难度的选择。1、pygame的安装直接在cmd中使用pipinstallpygame安装。或者在pycharm2中自动导入安装就可以了。全局变量的定义和包的导入,在代码中会用到很多此类变量的值,一开始直接定义,后面再获取变量。W=600#ScreenWidthH=400#highfps=12#frameratesize=(W,H)ROW=20#rowCOL=30#columnsnake_color=(200,200,200)#snakecolorMSGCOLOR=(3,54,73)#背景颜色MSGBGCOLOR=(255,255,193)#fontcolorblue=(64,160,171)#bluecount=0#scorepos=[160,130]#coordinatepos1=[190,200]#coordinate1importpygame,sysimportrandomimporttime3.初始化游戏开始界面,使用whileTrue循环刷新界面顺序,绘制字体和添加背景图片进去(图片可以直接选择600X400的图片),监听按下的按键,123分别表示选择难度不同,就是蛇移动的速度。#初始化pygame.init()screen=pygame.display.set_mode(size)pygame.display.set_caption("Snake")out=Truewhileout:FPSClock=pygame.time.Clock()img=pygame.image.load("bg.png")screen.blit(img,(0,0))#在字体文件中添加文字,可以使用默认的font=pygame.font.Font("STKAITI.TTF",24)fontRect=font.render("请选择游戏难度(点击键盘)",True,MSGCOLOR,MSGBGCOLOR)fontRect1=font.render("1:Easy2:Medium3:Complex",True,MSGCOLOR,MSGBGCOLOR)screen.blit(fontRect,pos)screen.blit(fontRect1,pos1)foreventinpygame.event.get():ifevent.type==pygame.QUIT:sys.exit()elifevent.type==pygame.KEYDOWN:print(event.key)ifevent.key==257:fps=8out=Falseelifevent.key==258:fps=16out=Falseelifevent.key==259:fps=24out=Falsepygame.display.update()FPSClock.tick(每秒帧数)4。游戏界面设置4.1游戏界面,使用一个Point类将整个屏幕划分为Point类可以划分多少个小格子,使用python中的类定义classPoint:row=0col=0def__init__(self,row=0,col=0):self.row=rowself.col=coldefcopy(self):returnPoint(row=self.row,col=self.col)4.2预处理给定初始化蛇头的坐标,蛇头的颜色,蛇的身体,这里都是Point对象产生的,不能出现在蛇头或者身体的位置。生产食物时,调用此方法给蛇的移动方向向左#坐标定义蛇头head=Point(row=int(ROW/2),col=int(COL/2))head_color=(80,80,128)#蛇的身体,用一个list存储对应的值组成一个数组,新的蛇初始化为3个长度的身体snakenodes=[Point(head.row,head.col),Point(head.row,head.col+1),Point(head.row,head.col+2)]#求这个顶点的坐标defrect(Point,color):left=Point.col*(W/COL)top=Point.row*(H/ROW)pygame.draw.rect(screen,color,(left,top,W/COL,H/ROW))#随机生成食物defgen_food():whileTrue:pos=Point(row=random.randint(0,ROW-1),col=random.randint(0,COL-1))#判断生产出来的食物是否与蛇碰撞is_coll=False#1.与蛇碰撞headofthesnakeifhead.row==pos.rowandhead.col==pos.col:is_coll=True#2.食物与蛇的身体碰撞fornodeinsnakenodes:ifnode.row==pos.rowandnode.col==pos.col:is_coll=Truebreakifnotis_coll:breakreturnpos#食物坐标food=gen_food()food_color=(255,255,0)#移动方向dir='left'4.3窗口实现在窗口中绘制实体,并添加逻辑,例如碰撞后,表示游戏结束,蛇头穿过食物,加点,生成新的食物再次#初始化pygame.init()screen=pygame.display.set_mode(size)pygame.display.set_caption("贪吃蛇")out=Truewhileout:FPSClock=pygame.time.Clock()img=pygame.image.load("bg.png")screen.blit(img,(0,0))#添加文字font=pygame.font.Font("STKAITI.TTF",24)fontRect=font.render("请选择游戏难度(单键盘)",True,MSGCOLOR,MSGBGCOLOR)fontRect1=font.render("1:简单2:中等3:复合",True,MSGCOLOR,MSGBGCOLOR)screen.blit(fontRect,pos)screen.blit(fontRect1,pos1)foreventinpygame.event.get():ifevent.type==pygame.QUIT:sys.exit()elifevent.type==pygame.KEYDOWN:print(event.key)ifevent.key==257:fps=8out=Falseelifevent.key==258:fps=16out=Falseelifevent.key==259:fps=24out=Falsepygame.display.update()FPSClock.tick(fps)time.sleep(1)quit=Truewhilequit:screen=pygame.display.set_mode((600,460))FPSClock=pygame.time.Clock()foreventinpygame.event.get():ifevent.type==pygame.QUIT:sys.exit()elifevent.type==pygame.KEYDOWN:#print(event.key)ifevent.key==273或event.key==pygame.K_UP:ifdir=='left'或dir=='right':dir='up'elifevent.key==274orevent.key==pygame.K_DOWN:ifdir=='left'ordir=='right':dir='down'ifevent.key==276orevent.key==pygame.K_LEFT:ifdir=='up'ordir=='down':dir='left'ifevent.key==275orevent.key==pygame.K_RIGHT:ifdir=='up'ordir=='down':dir='right'#1.画一个正方形,即整个背景正方形#pygame.draw.rect(screen,bg_c??olor,(0,0,W,H))#2.使用需要填写的图片输入方式img=pygame.image.load("bg.png")screen.blit(img,(0,0))#用白框分割pygame.draw.rect(screen,(255,255,255),(0,H,W,10))#显示下方得分框pygame.draw.rect(screen,blue,(0,H+10,W,60))#显示当前得分字体=pygame.font.Font("STKAITI.TTF",24)fontRect=font.render("当前分数:"+str(count),True,MSGCOLOR,blue)screen.blit(fontRect,(420,420))#绘制蛇头rect(head,head_color)#drawfoodrect(food,food_color)#为snakenodes中的节点绘制蛇体:rect(node,snake_color)#eatfood,eatfood当蛇头与食物重合时,此时蛇将蛇尾的长度加一,删除原蛇尾时添加判断eat=(head.row==food.rowandhead.col==food.col)#食物被再生,当食物被再生时,增加分数ifeat:food=gen_food()iffps==8:count+=3eliffps==16:count+=5eliffps==24:count+=10#处理蛇身#1.将蛇头插入数组listsnakenodes.insert(0,head.copy())#2.删除最后一个蛇尾节点,即删除尾节点ifnoteatfoodifnoteat:snakenodes.pop()#movetheheadifdir=='left':head.col-=1elifdir=='right':head.col+=1elifdir=='up':head.row-=1elifdir=='down':head.row+=1#collidedwithwalldead=Falseifhead.row<0orhead.col<0orhead.row>=ROWorhead.col>=COL:dead=Trueprint("hittingthewall")forsnakeinsnakenodes:ifhead.col==snake.col和头。row==snake.row:dead=Trueprint("打自己")breakifdead:print("gameover")quit=Falsepygame.display.update()FPSClock.tick(fps)5.选择得分的记录在前面经过不同的难度,每次吃完食物后分数加成不同。这里记录最终得分,并获取计数文件进行比对,判断记录是否被打破。打破记录并将新记录写入文件。#在计数文件中记录最高分f=open("count","r")counts=f.read()ifcount>int(counts):file=open("count","w")file.写(str(count))file.close()f.close()6.游戏结束(是否破纪录)已经得出分数,根据是否破纪录抽取不同效果。也使用这个pygame来初始化和构造一个新窗口。#分数显示pygame.init()screen=pygame.display.set_mode(size)pygame.display.set_caption("Snake")whileTrue:FPSClock=pygame.time.Clock()pygame.draw.rect(screen,MSGBGCOLOR,(0,0,W,H))#添加文本font=pygame.font.Font("STKAITI.TTF",24)ifcount<=int(counts):fontRect=font.render("yourscoreYes:"+str(count)+"Keepgoing!",True,MSGCOLOR,MSGBGCOLOR)elifcount>int(counts):fontRect=font.render("恭喜你打破记录!你的分数是:"+str(count),True,MSGCOLOR,MSGBGCOLOR)screen.blit(fontRect,(140,140))foreventinpygame.event.get():ifevent.type==pygame.QUIT:sys.exit()pygame.display.update()FPSClock.tick(fps)
