pygame游戏库,sys操控Python运行时的环境
import pygame,sys,random
from pygame.locals import *
redColor=pygame.Color(255,0,0)
blackColor=pygame.Color(0,0,0)
whiteColor=pygame.Color(255,255,255)
def gameOver():
pygame.quit()
sys.exit()
def main()
#初始化
pygame.init()
#定义一个变量来控制速度
fpsClock=pygame.time.Clock()
#创建pygame显示层
playSurface=pygame.display.set_mode((640,480))
pygame.display.ser_caption('贪吃蛇')
snakePosition=[100,100]
snakeBody=[[100,100],[80,100],[60,100]]
targetPosition=[300,300]
targetflag=1
#初始化方向
direction='right'
#定义一个方向变量
changeDirection=direction
while True:
for event in pygame.event.get(): #从队列当中获取事件
if event.type==QUIT
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if (event.key == K_LEFT) and direction !=RIGHT:
changeDirection = LEFT
elif (event.key == K_RIGHT) and direction != LEFT:
changeDirection = RIGHT
elif (event.key == K_UP) and direction != DOWN:
changeDirection = UP
elif (event.key == K_DOWN) and direction != UP:
changeDirection = DOWN
elif event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
if changeDirection == 'left'
direction =changeDirection