天天达

贪吃蛇代码复制

 

    以下是一个简单的贪吃蛇游戏的Python代码,使用了pygame库: ```python # 安装pygame库 # win+r cmd pip install pygame import pygame,sys,time,random from pygame.locals import* # 定义颜色变量 redColour=pygame.Color(255,0,0) blackColour=pygame.Color(0,0,0) whiteColour=pygame.Color(255,255,255) greyColour=pygame.Color(150,150,150) def gameOver(playSurface,score): gameOverFont=pygame.font.SysFont('arial.ttf',54) # 游戏结束字体和大小 gameOverSurf=gameOverFont.render('Game Over!',True, greyColour) # 游戏结束内容显示 gameOverRect=gameOverSurf.get_rect() gameOverRect.midtop=(300,10) # 显示位置 playSurface.blit(gameOverSurf, gameOverRect) scoreFont=pygame.font.SysFont('arial.ttf',54) # 得分情况显示 scoreSurf=scoreFont.render('Score:'+str(score),True, greyColour) scoreRect=scoreSurf.get_rect() scoreRect.midtop=(300,50) playSurface.blit(scoreSurf, scoreRect) pygame.display.flip() # 刷新显示界面 time.sleep(5) # 休眠五秒钟自动退出界面 pygame.quit() sys.exit() def main(): # 初始化pygame pygame.init() fpsClock=pygame.time.Clock()