pygame做一个贪吃蛇

安装pygame

pip install pygame

写代码

代码如下

import pygame, sys, random, time
from pygame.locals import *

black_colour = pygame.Color(0, 0, 0)
white_colour = pygame.Color(255, 255, 255)
red_colour = pygame.Color(255, 0, 0)
grey_colour = pygame.Color(150, 150, 150)


def GameOver(gamesurface):
    GameOver_font = pygame.font.SysFont("MicrosoftYaHei", 16)

    GameOver_colour = GameOver_font.render('Game Over', True, grey_colour)

    GameOver_location = GameOver_colour.get_rect()
    GameOver_location.midtop = (320, 10)
    gamesurface.blit(GameOver_colour, GameOver_location)

    pygame.display.flip()

    time.sleep(5)

    pygame.quit()

    sys.exit()


def main():
    pygame.init()
    pygame.time.Clock()
    ftpsClock = pygame.time.Clock()

    gamesurface = pygame.display.set_mode((640, 480))

    pygame.display.set_caption('贪吃蛇~')

    snakeposition = [100, 100]

    snakelength = [[100, 100], [80, 100], [60, 100]]

    square_purpose = [300, 300]

    squre_posposition = 1

    derection = "right"
    change_derection = derection

    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 or event.key == ord('d'):
                    change_derection = "right"
                if event.key == K_LEFT or event.key == ord('a'):
                    change_derection = "left"
                if event.key == K_UP or event.key == ord('w'):
                    change_derection = "up"
                if event.key == K_DOWN or event.key == ord('s'):
                    change_derection = "down"
                if event.key == K_ESCAPE:
                    pygame.event.post(pygame.event.Event(QUIT))

        if change_derection == 'left' and not derection == 'right':
            derection = change_derection
        if change_derection == 'right' and not derection == 'left':
            derection = change_derection
        if change_derection == 'up' and not derection == 'down':
            derection = change_derection
        if change_derection == 'down' and not derection == 'up':
            derection = change_derection

        if derection == 'left':
            snakeposition[0] -= 20
        if derection == 'right':
            snakeposition[0] += 20
        if derection == 'up':
            snakeposition[1] -= 20
        if derection == 'down':
            snakeposition[1] += 20

        snakelength.insert(0, list(snakeposition))
        squre_posposition
        if snakeposition[0] == square_purpose[0] and snakeposition[1] == square_purpose[1]:
            squre_posposition = 0
        else:
            snakelength.pop()


        if squre_posposition == 0:
            x = random.randrange(1, 32)
            y = random.randrange(1, 24)
            square_purpose = [int(x * 20), int(y * 20)]
            squre_posposition = 1

        gamesurface.fill(black_colour)
        for position in snakelength:
            pygame.draw.rect(gamesurface, white_colour, Rect(position[0], position[1], 20, 20))
            pygame.draw.rect(gamesurface, red_colour, Rect(square_purpose[0], square_purpose[1], 20, 20))

        pygame.display.flip()

        if snakeposition[0] < 0 or snakeposition[0] > 620:
            GameOver(gamesurface)
        if snakeposition[1] < 0 or snakeposition[0] > 4600:
            GameOver(gamesurface)
        for snakebody in snakelength[1:]:
            if snakeposition[0] == snakebody[0] and snakeposition[1] == snakebody[1]:
                GameOver(gamesurface)

        ftpsClock.tick(5)


if __name__ == '__main__':
    main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值