贪吃蛇自运行 AI 三条命

贪吃蛇自运行AI 三条命

  #!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@File  : 1.py
@Author:   BC
@Date  : 2023-04-20 17:25
贪吃蛇自运行AI
三条命
'''
import pygame
import random

# 初始化Pygame
pygame.init()

# 游戏设置
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("贪吃蛇")

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

# 蛇的方向定义
UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4


# 定义蛇类
class Snake:
    def __init__(self):
        self.x = screen_width / 2
        self.y = screen_height / 2
        self.width = 20
        self.height = 20
        self.color = white
        self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
        self.body = [(self.x, self.y)]
        self.score = 0
        self.lives = 3  # 添加三条命

    def move(self):
        if self.direction == UP:
            self.y -= self.height
        elif self.direction == DOWN:
            self.y += self.height
        elif self.direction == LEFT:
            self.x -= self.width
        elif self.direction == RIGHT:
            self.x += self.width

        # 检查是否吃到食物
        if self.body[0] == food.position:
            food.reset()
            self.score += 1
            self.body.insert(0, (self.x, self.y))
        else:
            self.body.insert(0, (self.x, self.y))
            self.body.pop()

        # 检查是否碰到墙壁或自己的身体
        if self.x < 0 or self.x > screen_width - self.width or \
                self.y < 0 or self.y > screen_height - self.height or \
                self.body[0] in self.body[1:]:
            self.lives -= 1  # 减少一条命
            if self.lives == 0:  # 如果没有生命了,游戏结束
                pygame.quit()
                quit()
            else:
                self.reset()  # 否则重新开始游戏

    def decision(self):
        # 获取蛇头和食物的位置
        head_x, head_y = self.body[0]
        food_x, food_y = food.position

        # 判断上下左右哪个方向离食物最近,并决定移动的方向
        if food_x < head_x:
            self.direction = LEFT
        elif food_x > head_x:
            self.direction = RIGHT
        elif food_y < head_y:
            self.direction = UP
        elif food_y > head_y:
            self.direction = DOWN

    def reset(self):
        self.x = screen_width / 2
        self.y = screen_height / 2
        self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
        self.body = [(self.x, self.y)]
        self.score = 0

    def draw(self):
        for pos in self.body:
            pygame.draw.rect(screen, self.color, (pos[0], pos[1], self.width, self.height))


# 定义食物类
class Food:
    def __init__(self):
        self.width = 20
        self.height = 20
        self.color = red
        self.reset()

    def reset(self):
        self.position = (random.randint(0, (screen_width - self.width) / self.width) * self.width,
                         random.randint(0, (screen_height - self.height) / self.height) * self.height)

    def draw(self):
        pygame.draw.rect(screen, self.color, (self.position[0], self.position[1], self.width, self.height))


# 初始化蛇和食物
snake = Snake()
food = Food()

# 游戏循环
clock = pygame.time.Clock()
while True:
    # 监听事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    # AI决策
    snake.decision()

    # 移动蛇
    snake.move()

    # 绘制蛇和食物
    screen.fill(black)
    snake.draw()
    food.draw()

    # 判断是否填满屏幕
    if snake.score == (screen_width * screen_height) / (snake.width * snake.height):
        print("You win!")
        pygame.quit()
        quit()

    # 刷新屏幕
    pygame.display.update()

    # 控制帧率
    clock.tick(10)

你可能感兴趣的:(pygame,python,开发语言)