贪食蛇

在这里插入代码片
```import pygame
import sys
import time
import random
from pygame.locals import *
#初始化python
pygame.init()
fpsClock = pygame.time.Clock()

#create pygame surface
playSurface = pygame.display.set_mode((640,480))
#set caption
pygame.display.set_caption('snake go!')
#load resource file
image=pygame.image.load('game.ico')
#set icon
pygame.display.set_icon(image)
#set color
redColor=pygame.Color(255,0,0)
blackColor=pygame.Color(0,0,0)
whiteColor=pygame.Color(255,255,255)
greyColor=pygame.Color(150,150,150)
lightGrey=pygame.Color(220,220,220)
#define gameOver function
def gameOver(playSurface,score):
    gameOverFont=pygame.font.Font('arial.ttf',72)
    gameOverSurf=gameOverFont.render('Game Over',True,greyColor)
    gameOverRect=gameOverSurf.get_rect()
    gameOverRect.midtop=(320.125)
    playSurface.blit(gameOverSurf,gameOverRect)

    scoreFont=pygame.font.Font('arial.ttf',48)
    scoreSurf=scoreFont.render('Score:'+str(score),True,greyColor)
    scoreRect.midtop=(320,225)
    playSurface.blit(scoreSurf,scoreRect)
    pygame.display.flip()#refine surface
          #休眠5秒自动关闭
    time.sleep(5)
    pygame.quit()
    sys.exit()
#定义初始位置
    snakePosition = [100,100]
    snakeSegments=[[100,100],[80,100],[60,100]]
    berryPosition=[300,300]
    berrySpawned=1
    direction='right'
    changeDirection=direction
    score=0
    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))
#限制反方向运动
                if changeDirection =='right' and not direction =='left':
                    direction=changeDirection
                if changeDirection =='left' and not direction =='right':
                    direction=changeDirection
                if changeDirection =='up' and not direction =='down':
                    direction=changeDirection
                if changeDirection =='down' and not direction =='up':
                    direction=changeDirection
#蛇头转弯
                if direction == 'right':
                    snakePosition[0]+=20
                if direction == 'left':
                    snakePosition[0]-=20
                if direction == 'up':
                    snakePosition[1]-=20
                if direction == 'down':
                    snakePosition[1]+=20
                snakeSegments.insert(0,list(snakePosition))
#是否吃掉树莓
                if snakePosition[0] == berryPosition[0] and snakePosition[1] == berryPosition[1]:
                    berrySpawned=0
                else:
                    snakeSegments.pop()
                    if berrySpawned==0:
                        x=random.randrange(1,32)
                        y=random.randrange(1,24)
                        berryPosition=[int(x*20),int(y*20)]
                        berrySpawned=1
                        score+=1
##刷新显示器
        playSurface.fill(blackColor)
        for position in snakeSegments[1:]:
            pygame.draw.rect(playSurface,whiteColor,Rect(position[0],position[1],20,20))
        pygame.draw.rect(playSurface,lightGrey,Rect(snakePosition[0],snakePosition[1],20,20))
        pygame.draw.rect(playSurface,redColor,Rect(berryPosition[0],berryPosition[1],20,20))
        pygame.display.flip()
##判断是否死亡
        if snakePosition[0] > 620 or snakePosition[0] < 0:
            gameOver(playsurface,score)
        if snakePosition[1] > 460 or snakePosition[1] < 0:
            gameOver(playsurface,score)
        for snakeBody in snakeSegments[1:]:
            if snakePosition[0]==snakeBody[0] and snakePosition[1]==snakeBody[1]:
                gameOver(playsurface,score)
##控制速度
        if len(snakeSegments)<40:
            speed =6+len(snakeSegments)//4
        else:
            speed = 16
        fpsClock.tick(speed)



你可能感兴趣的:(打卡日记)