Python贪吃蛇游戏详细代码和注释
-07-30 03:12
阅读数 28
在写Python游戏项目时,最重要的时python中的pygame库。安装pygame库和用法在我CSDN博客另一篇文章上。这里就不详细说了。下边时运行游戏界面。
下边是详细的代码和注释
import pygame,sys,random,time from pygame.locals import * #从pygame模块导入常用的函数和常量
#定义颜色变量 black_colour = pygame.Color(0,0,0) white_colour =
pygame.Color(255,255,255) red_colour = pygame.Color(255,0,0) grey_colour =
pygame.Color(150,150,150) #定义游戏结束函数 def GameOver(gamesurface): #设置提示字体的格式
GameOver_font = pygame.font.SysFont("MicrosoftYaHei", 16) #设置提示字体的颜色
GameOver_colour = GameOver_font.render(Game Over,True,grey_colour) #设置提示位置
GameOver_location = GameOver_colour.get_rect() GameOver_location.midtop =
(320,10) #绑定以上设置到句柄 gamesurface.blit(GameOver_colour,GameOver_location) #提示运行信息
pygame.display.flip() #休眠5秒 time.sleep(5) #退出游戏 pygame.quit() #退出程序 sys.exit()
#定义主函数 def main(): #初始化pygam