python制作贪吃蛇游戏

2019年3月9日16:24:48 发表评论 1,297 views
import pygame
import sys
import random
from pygame.locals import *

redColor = pygame.Color(255,0,0)#目标方块
whiteColor = pygame.Color(255,255,255)# 贪吃蛇
blackColor = pygame.Color(0,0,0) #背景颜色

#定义游戏结束的函数
def gameOver():
    pygame.quit()
    sys.exit()

#实现工作方式
def main():

    #3.1初始化pygame
    pygame.init()

    #3.2定义一个变量来控制游戏的速度
    fpsClock = pygame.time.Clock()

    #3.3定义一个窗口
    playSurface = pygame.display.set_mode((640,480))
    pygame.display.set_caption('贪吃蛇')

    #初始化变量
    snakePosition = [100,100]#起始位置
    snakeBody = [[100,100],[80,100],[60,100]]#蛇身
    targetPosition = [300,300]#目标方块位置
    targetFlag = 1
    direction = 'right'
    changeDirection = direction

    #3.5pygame中的时间要放到一个实时循环当中来处理
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if event.key == K_RIGHT:
                    changeDirection = 'right'
                if event.key == K_LEFT:
                    changeDirection = 'left'
                if event.key == K_UP:
                    changeDirection = 'up'
                if event.key == K_DOWN:
                    changeDirection = 'down'
                if event.key == K_ESCAPE:
                    pygame.event.post(pygame.event.Event(QUIT))
        #3.6确定方向
        if changeDirection == 'left' and not direction == 'right':
            direction = changeDirection
        if changeDirection == 'right' and not direction == 'left':
            direction = changeDirection
        if changeDirection == 'up' and not direction == 'down':
            direction = changeDirection
        if changeDirection == 'down' and not direction == 'up':
            direction = changeDirection

        #3.7根据方向移动蛇头的位置
        if direction == 'right':
            snakePosition[0] += 20
        if direction == 'left':
            snakePosition[0] -= 20
        if direction =='up':
            snakePosition[1] -=20
        if direction == 'down':
            snakePosition[1] += 20
        #3.8蛇增加的长度
        snakeBody.insert(0,list(snakePosition))

# 如果贪吃蛇位置和方块位置重合 意味吃掉了方块
        if snakePosition[0] == targetPosition[0] and snakePosition[1]==targetPosition[1]:
            targetFlag = 0
        else:
            snakeBody.pop()
        if targetFlag == 0:
            x = random.randrange(1,32)
            y = random.randrange(1,24)
            targetPosition = [x*20,y*20]
            targetFlag = 1
        #3.9绘制显示层
        playSurface.fill(blackColor)
        for position in snakeBody:
            pygame.draw.rect(playSurface,whiteColor,Rect(position[0],position[1],20,20))#蛇
            pygame.draw.rect(playSurface,redColor,Rect(targetPosition[0],targetPosition[1],20,20))#目标方块
        #游戏结束
        pygame.display.flip()
        if snakePosition[0]>620 or snakePosition[0]<0:
            gameOver()
        if snakePosition[1] >460 or snakePosition[1] < 0:
            gameOver()
        #控制游戏速度,越大速度越快
        fpsClock.tick(5)

if __name__ == '__main__':
    main()











发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: