Python贪吃蛇

好久不见. 提交于 2019-12-01 05:03:36

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!