snake--python语言实现

import pygame,sys,time,random
from pygame.locals import *  //引入必要模块

pygame.init()  //pygame初始化
fpsClock=pygame.time.Clock()  //创建一个控制游戏速度的变量

playSurface=pygame.display.set_mode((640,480))   //新建一个游戏的图形界面
pygame.display.set_caption('snake')  //游戏名

redColour=pygame.Color(255,0,0)
blackColour=pygame.Color(0,0,0)
whiteColour=pygame.Color(255,255,255)
greyColour=pygame.Color(150,150,150)
snakePosition=[100,100]
snakeSegments=[[100,100],[80,100],[60,100]]
raspberryPosition=[300,300]
raspberrySpawned=1
direction='right'
changeDirection=direction  //定义一些颜色和坐标供以后使用

def gameOver():
    gameOverFont=pygame.font.Font('freesansbold.ttf',72)
    gameOverSurf=gameOverFont.render('Game Over',True,greyColour)
    gameOverRect=gameOverSurf.get_rect()
    gameOverRect.midtop=(320,10)
    playSurface.bilt(gameOverSurf,gameOverRect)
    pygame.display.flip()
    time.sleep(5)
    pygame.quit()
    sys.exit()  定义一个gameOver函数,这样方便以后直接调用
    
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()  //按下ESC键就退出
        elif event.type == KEYDOWN:
            if evnet.key == K_RIGHT or event.key == ord('d'):
                changeDirection='right'
            if evnet.key == K_LEFT or event.key == ord('a'):     
                changeDirection='left'
            if evnet.key == K_UP or event.key == ord('w'):
                changeDirection='up'
            if evnet.key == K_DOWN or event.key == ord('s'):    
                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]==raspberryPosition[0] and snakePosition[1]==raspberryPosition[1]:
        raspberrySpawned=0
    else:
        snakeSegments.pop()  //当吃到草莓时,蛇的长度逐渐增加
    if raspberrySpawned == 0:
        x=random.randrange(1,32)
        y=random.randrange(1,24)
        raspberryPosition=[int(x*20),int(y*20)]
    raspberrySpawned=1  //如果草莓被吃掉,更新草莓的位置
    playSurface.fill(blackColour)  //背景色为黑色
    for position in snakeSegments:
        pygame.draw.rect(playSurface,whiteColour,Rect(position[0],position[1],20,20))  //蛇为白色
    pygame.draw.rect(playSurface,redColour,Rect(raspberryPosition[0],raspberryPosition[1],20,20))  //草莓为红色
    pygame.display.flip()
    if snakePosition[0]>620 or snakePosition[0]<0:
        gameOver()
    if snakePosition[1]>460 or snakePosition[1]<0:
        gameOver()
    for snakeBody in snakeSegments[1:]:
            if snakePosition[0]==snakeBody[0] and snakePosition[1] == snakeBody[1]:
                gameOver()  //蛇死亡的条件
fpsClock.tick(30)   //控制游戏的速度
代码写完后出现了一些非预期的错误,比如程序未响应,仍在调试中,上述代码主要是提供一个用Python写程序的思路,所以以后在写大型的Python程序时先把思路想好,按照思路一点一点实现,就可以把一个复杂的大型程序分解,效率会很高。

欢迎交流!


你可能感兴趣的:(snake--python语言实现)