用python怎么做贪吃蛇
用python怎么做贪吃蛇
建议你要先拥有一个第三方库:Pygame
import random,sys,pygame from pygame.locals import* FPS=5 width=640 heigh=480 cellsize=20 cellwidth=int(width/cellsize) cellheigh=int(heigh/cellsize) white=(255,255,255) black=(0,0,0) red=(255,0,0) green=(0,255,0) blue=(0,0,255) darkgreen=(0,155,0) darkgray=(40,40,40) BGcolor=white Up='up' Down='down' Left='left' Right='right' def main(): global FPSlock,screen,BASICfont pygame.init() FPSlock=pygame.time.Clock() screen=pygame.display.set_mode([width,heigh]) BASICfont=pygame.font.Font(r'C:\Windows\Fonts\simkai.ttf',20) pygame.display.set_caption('贪吃蛇') showstartscreen() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() rungame() showgameover() def showstartscreen(): width1=640 heigh1=480 titlefont=pygame.font.Font(r'C:\Windows\Fonts\simkai.ttf',100) titlesurf1=titlefont.render('贪吃蛇',True,black,darkgreen) titlesurf2=titlefont.render('贪吃蛇',True,green) degrees1=0 degrees2=0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == KEYDOWN: return screen.fill(BGcolor) surf1=pygame.transform.rotate(titlesurf1,degrees1) rect1=surf1.get_rect() rect1.center=(width1/2,heigh1/2) screen.blit(surf1,rect1) surf2=pygame.transform.rotate(titlesurf2,degrees2) rect2=surf2.get_rect() rect2.center=(width1/2,heigh1/2) screen.blit(surf2,rect2) drawpresskeymsg() pygame.display.update() FPSlock.tick(FPS) degrees1 += 3 degrees2 += 7 def drawpresskeymsg(): presskeysurf=BASICfont.render('按任意键进入游戏',True,black) presskeyrect=presskeysurf.get_rect() presskeyrect.topleft=(width-200,heigh-30) screen.blit(presskeysurf,presskeyrect) def drawgrid(): for x in range(0,width,cellsize): pygame.draw.line(screen,darkgreen,(x,0),(x,heigh)) for y in range(0,heigh,cellsize): pygame.draw.line(screen,darkgreen,(0,y),(width,y)) def getrandomlocation(): return {'x':random.randint(0,cellwidth),'y':random.randint(0,cellheigh-1)} def drawscore(score): scoresurf=BASICfont.render('分数:%s'%(score),True,black) scorerect=scoresurf.get_rect() scorerect.topleft=(width-120,10) screen.blit(scoresurf,scorerect) def rungame(): startx=random.randint(5,cellwidth-6) starty=random.randint(5,cellheigh-6) wormcoords=[{'x':startx,'y':starty},{'x':startx-1,'y':starty},{'x':startx-2,'y':starty}] direction=Right apple=getrandomlocation() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == KEYDOWN: if (event.key==K_LEFT or event.key==K_a)and direction != Right: direction=Left elif (event.key==K_RIGHT or event.key==K_d)and direction != Left: direction=Right elif (event.key==K_UP or event.key==K_w)and direction != Down: direction=Up elif (event.key==K_DOWN or event.key==K_s)and direction != Up: direction=Down elif event.key == K_ESCAPE: sys.exit() if wormcoords[0]['x'] == -1 or wormcoords[0]['x'] == cellwidth or wormcoords[0]['y'] == -1 or wormcoords[0]['y'] == cellheigh: return for wormbody in wormcoords[1:]: if wormbody['x'] == wormcoords[0]['x'] and wormbody['y'] == wormcoords[0]['y']: return if wormcoords[0]['x'] == apple['x'] and wormcoords[0]['y'] == apple['y']: apple= getrandomlocation() else: del wormcoords[-1] if direction == Up: newhead={'x':wormcoords[0]['x'],'y':wormcoords[0]['y']-1} elif direction == Down: newhead={'x':wormcoords[0]['x'],'y':wormcoords[0]['y']+1} elif direction == Left: newhead={'x':wormcoords[0]['x']-1,'y':wormcoords[0]['y']} elif direction == Right: newhead={'x':wormcoords[0]['x']+1,'y':wormcoords[0]['y']} wormcoords.insert(0,newhead) screen.fill(BGcolor) drawgrid() drawworm(wormcoords) drawapple(apple) drawscore(len(wormcoords)-3) pygame.display.update() FPSlock.tick(FPS) def drawworm(wormcoords): for coord in wormcoords: x=coord['x']*cellsize y=coord['y']*cellsize wormsegmentrect=pygame.Rect(x,y,cellsize,cellsize) pygame.draw.rect(screen,darkgreen,wormsegmentrect) worminnersegmentrect=pygame.Rect(x+4,y+4,cellsize-8,cellsize-8) pygame.draw.rect(screen,darkgreen,worminnersegmentrect) def drawapple(coord): x=coord['x']*cellsize y=coord['y']*cellsize applerect=pygame.Rect(x,y,cellsize,cellsize) pygame.draw.rect(screen,red,applerect) def showgameover(): gameoverfont=pygame.font.Font('C://Windows//Fonts//msyh.ttc',150) gamesurf=gameoverfont.render('游戏',True,black) oversurf=gameoverfont.render('失败',True,black) gamerect=gamesurf.get_rect() overrect=oversurf.get_rect() gamerect.midtop=(width/2,10) overrect.midtop=(width/2,gamerect.height+10+25) screen.blit(gamesurf,gamerect) screen.blit(oversurf,overrect) drawpresskeymsg() pygame.display.update() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() return if __name__=='__main__': main()