【Python】最简单的贪吃蛇

【Python】最简单的贪吃蛇

图片[1]-【Python】最简单的贪吃蛇-云港网络

自己写了一个贪吃蛇,号称全站最简洁!

源代码如下:

import pygame
import random

# 游戏窗口尺寸
window_width = 400
window_height = 400

# 蛇身和食物的尺寸
snake_size = 20
food_size = 20

# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)

# 初始化pygame
pygame.init()

# 创建游戏窗口
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("贪吃蛇")

clock = pygame.time.Clock()

# 定义贪吃蛇类
class Snake:
    def __init__(self):
        self.snake = [(8, 8), (7, 8), (6, 8)]
        self.direction = "right"

    def move(self):
        head = self.snake[0]
        x, y = head
        if self.direction == "up":
            y -= 1
        elif self.direction == "down":
            y += 1
        elif self.direction == "left":
            x -= 1
        elif self.direction == "right":
            x += 1

        new_head = (x, y)
        self.snake.insert(0, new_head)
        self.snake.pop()

    def change_direction(self, direction):
        if (
            (self.direction == "up" and direction != "down") or
            (self.direction == "down" and direction != "up") or
            (self.direction == "left" and direction != "right") or
            (self.direction == "right" and direction != "left")
        ):
            self.direction = direction

    def check_collision(self):
        head = self.snake[0]
        if (
            head[0] < 0 or head[0] >= window_width // snake_size or
            head[1] < 0 or head[1] >= window_height // snake_size or
            head in self.snake[1:]
        ):
            return True
        return False

    def eat_food(self, food):
        head = self.snake[0]
        if head == food:
            self.snake.append(food)
            return True
        return False

# 定义食物类
class Food:
    def __init__(self):
        self.position = self.generate_food()

    def generate_food(self):
        x = random.randint(0, window_width // snake_size - 1)
        y = random.randint(0, window_height // snake_size - 1)
        return (x, y)

# 游戏主循环
def game_loop():
    game_over = False
    snake = Snake()
    food = Food()

    while not game_over:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    snake.change_direction("up")
                elif event.key == pygame.K_DOWN:
                    snake.change_direction("down")
                elif event.key == pygame.K_LEFT:
                    snake.change_direction("left")
                elif event.key == pygame.K_RIGHT:
                    snake.change_direction("right")

        snake.move()

        if snake.check_collision():
            game_over = True

        if snake.eat_food(food.position):
            food.position = food.generate_food()

        # 绘制游戏窗口
        window.fill(black)

        for body in snake.snake:
            pygame.draw.rect(window, green, (body[0] * snake_size, body[1] * snake_size, snake_size, snake_size))
        pygame.draw.rect(window, red, (food.position[0] * snake_size, food.position[1] * snake_size, food_size, food_size))

        pygame.display.update()
        clock.tick(10)

    pygame.quit()

# 运行游戏循环
game_loop()

软件给大家打包好了,在下面:

 
贪吃蛇.zip
zip文件
1.2K

 

© 版权声明
THE END
喜欢就支持一下吧
点赞23打赏 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容