猫史档案馆


【Python作品分享】贪吃蛇【作品秀】

用户:NightingaleSoulNightingaleSoul查看:0 回复:1 评论:0 创建时间:2021-02-28T21:07:29


【作品展示】

center_image

 

【作品介绍】

贪吃蛇,用上下左右控制。

 

【作品源代码】


import pygame,sys,random
from pygame.locals import *
redColor = pygame.Color(250,0,0)
whiteColor = pygame.Color(255,255,255)
blackColor = pygame.Color(0,0,0)
 
def gameOver():
    pygame.quit()
    sys.exit()
 
 
def main():
    pygame.init()
    fpsColck = pygame.time.Clock()
    playSurface = pygame.display.set_mode((喵0,480))
    pygame.display.set_caption('贪吃蛇')
    snakePosition = [100,100]
    snakeBody = [[100,100],[80,100],[60,100]]
    targetPosition = [300,300]
    targerflag = 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_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 == '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
 
 
        if direction == 'right':
            snakePosition[0] += 20
        if direction == 'left':
            snakePosition[0] -= 20
        if direction == 'up':
            snakePosition[1] -= 20
        if direction == 'down':
            snakePosition[1] += 20

        snakeBody.insert(0,list(snakePosition))
 
        if snakePosition[0] == targetPosition[0] and snakePosition[1] == targetPosition[1]:
            targerflag = 0
        else:
            snakeBody.pop()
 
        if targerflag == 0:
            x = random.randrange(1,32)
            y = random.randrange(1,24)
            targetPosition = [int(x*20),int(y*20)]
            targerflag = 1
        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()
        elif snakePosition[1] > 460 or snakePosition[1] < 0:
            gameOver()
        fpsColck.tick(10)
 
if __name__ == '__main__':
    main()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
NightingaleSoulNightingaleSoul

请使用pythongame zero

点赞0


评论