自运行贪吃蛇AI

该代码实现了一个基于Pygame的贪吃蛇游戏,蛇能够自动运行并使用简单的AI策略来决定移动方向,以接近食物。游戏包含蛇类(Snake)和食物类(Food),蛇在吃到食物后会增长,碰到边界或自身身体则游戏结束。
摘要由CSDN通过智能技术生成

自运行贪吃蛇AI

 #!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@File  : 0.py
@Author:   BC
@Date  : 2023-04-20 17:20
贪吃蛇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

    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:]:
            pygame.quit()
            quit()

    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 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()

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

    # 控制帧率
    clock.tick(10)

 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值