python贪吃蛇小游戏

贪吃蛇小游戏 python

使用python代码写了一个贪吃蛇小游戏,闲来无事。纯属瞎玩

import pygame
import random

# 初始化 Pygame
pygame.init()

# 设置游戏窗口大小
window_width = 800
window_height = 600
game_display = pygame.display.set_mode((window_width, window_height))

# 设置游戏标题
pygame.display.set_caption('贪吃蛇游戏')

# 设置颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)

# 设置蛇的大小和速度
block_size = 10
snake_speed = 7

# 设置字体
font_style = pygame.font.SysFont(None, 50)


# 绘制蛇
def draw_snake(block_size, snake_list):
    for x in snake_list:
        pygame.draw.rect(game_display, black, [x[0], x[1], block_size, block_size])


# 显示消息
def message(msg, color):
    message_text = font_style.render(msg, True, color)
    game_display.blit(message_text, [window_width/6, window_height/3])


# 游戏循环
def game_loop():
    game_over = False
    game_close = False

    # 初始化蛇的位置
    x1 = window_width / 2
    y1 = window_height / 2
    x1_change = 0
    y1_change = 0

    # 初始化食物的位置
    foodx = round(random.randrange(0, window_width - block_size) / 10.0) * 10.0
    foody = round(random.randrange(0, window_height - block_size) / 10.0) * 10.0

    # 初始化蛇的长度
    snake_list = []
    length_of_snake = 1

    # 游戏循环
    while not game_over:

        # 游戏结束
        while game_close:
            game_display.fill(white)
            message("game over!enter Q quit,enter C restart.", red)
            pygame.display.update()

            # 处理按键事件
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        game_over = True
                        game_close = False
                    if event.key == pygame.K_c:
                        game_loop()

        # 处理按键事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x1_change = -block_size
                    y1_change = 0
                elif event.key == pygame.K_RIGHT:
                    x1_change = block_size
                    y1_change = 0
                elif event.key == pygame.K_UP:
                    y1_change = -block_size
                    x1_change = 0
                elif event.key == pygame.K_DOWN:
                    y1_change = block_size
                    x1_change = 0

        # 判断蛇是否超出边界
        if x1 >= window_width or x1 < 0 or y1 >= window_height or y1 < 0:
            game_close = True

        # 更新蛇的位置
        x1 += x1_change
        y1 += y1_change
        print(x1, y1)

        game_display.fill(white)
        pygame.draw.rect(game_display, red, [foodx, foody, block_size, block_size])

        # 更新蛇的长度
        snake_head = []
        snake_head.append(x1)
        snake_head.append(y1)
        snake_list.append(snake_head)
        if len(snake_list) > length_of_snake:
            del snake_list[0]

        # 判断是否吃到食物
        for segment in snake_list[:-1]:
            if segment == snake_head:
                game_close = True

        draw_snake(block_size, snake_list)
        pygame.display.update()

        # 判断是否吃到食物
        if x1 == foodx and y1 == foody:
            foodx = round(random.randrange(0, window_width - block_size) / 10.0) * 10.0
            foody = round(random.randrange(0, window_height - block_size) / 10.0) * 10.0
            length_of_snake += 1

        # 设置游戏速度
        clock = pygame.time.Clock()
        clock.tick(snake_speed)

    # 退出 Pygame
    pygame.quit()

    # 退出 Python
    quit()


# 开始游戏
game_loop()

运行结果:

在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胖羊驼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值