Python贪吃蛇源代码
Python代码狂人 Python代码大全
程序运行截图如下:
import pygame as pg
from random import randint
import sys
from pygame.locals import *
FPS = 6 # 画面帧数,代表蛇的移动速率
window_width = 600
window_height = 500
cellsize = 20
cell_width = int(window_width / cellsize)
cell_height = int(window_height / cellsize)
BGcolor = (0, 0, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
apple_color = (255, 0, 0)
snake_color = (0, 150, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
DARKGRAY = (40, 40, 40)
UP = "up"
DOWN = "down"
LEFT = "left"
RIGHT = "right"
HEAD = 0
def main(): # 有函数
global FPSclock, window, BASICFONT
pg.init()
FPSclock = pg.time.Clock()
window = pg.display.set_mode((window_width, window_height))
BASICFONT = pg.font.Font("freesansbold.ttf", 18)
pg.display.set_caption("贪吃蛇")
showStartScreen()
while True:
runGame()
showGameOverScreen()
def runGame(): # 运行游戏函数
startx = randint(5, ce