python - arcade中的碰撞错误: When I use the arcade. python中的check_for_collision方法并发生碰撞,它给出了错误

标签 python sprite collision arcade

我在做一个贪吃蛇游戏,当我想实现蛇与苹果的碰撞时,遇到如下错误。 arcade 中的碰撞错误:当我在 python 中使用 arcade.check_for_collision 方法并发生碰撞时,它给出错误:

ValueError: Error trying to get the hit box of a sprite, when no hit box is set.
Please make sure the Sprite.texture is set to a texture before trying to draw or do collision testing.
Alternatively, manually call Sprite.set_hit_box with points for your hitbox.
import random, arcade
WIDTH_SCREEN = 500
HEIGHT_SCREEN = 500
class Game(arcade.Window):
    def __init__(self):
        super().__init__(width=WIDTH_SCREEN, height=HEIGHT_SCREEN, title='Snake')
        arcade.set_background_color(arcade.color.SAND)
        self.snake = Snake()
        self.apple = Apple()
    def on_draw(self):
        arcade.start_render()
        self.snake.draw()
        self.apple.draw_apple()
    def on_update(self, delta_time: float):
        self.snake.move()
        ***if arcade.check_for_collision(self.snake, self.apple):***
            self.snake.eat()
            self.apple = Apple()
            print(self.snake.score)
    def on_key_release(self, key: int, modifiers: int):
        if key == arcade.key.LEFT:
            self.snake.change_x = -1
            self.snake.change_y = 0
        elif key == arcade.key.RIGHT:
            self.snake.change_x = 1
            self.snake.change_y = 0
        elif key == arcade.key.UP:
            self.snake.change_x = 0
            self.snake.change_y = 1
        elif key == arcade.key.DOWN:
            self.snake.change_x = 0
            self.snake.change_y = -1         
class Snake(arcade.Sprite):
    def __init__(self):
        super().__init__()
        self.width = 16
        self.height = 16
        self.change_x = 0   
        self.change_y = 0
        self.center_x = WIDTH_SCREEN // 2
        self.center_y = HEIGHT_SCREEN //2
        self.color = arcade.color.RED
        self.score = 0
        self.speed = 1
    def move(self):
        if self.change_x > 0:
            self.center_x += self.speed
        elif self.change_x < 0:
            self.center_x -= self.speed
        elif self.change_y > 0:
            self.center_y += self.speed
        elif self.change_y < 0:
            self.center_y -= self.speed      
    def eat(self):
        self.score += 1
    def draw(self):
        arcade.draw_rectangle_filled(self.center_x, self.center_y, self.width, self.height, self.color)      
class Apple(arcade.Sprite):
    def __init__(self):
        super().__init__()
        self.radius = 10
        self.center_x = random.randint(0, WIDTH_SCREEN)
        self.center_y = random.randint(0, HEIGHT_SCREEN)
        self.color = arcade.color.YELLOW
    def draw_apple(self):
        arcade.draw_circle_filled(self.center_x, self.center_y, self.radius, self.color)
game_board = Game()
arcade.run()

最佳答案

您还应该为 Apple 类设置 widthheight 属性。

class Apple(arcade.Sprite):
    def __init__(self):
        super().__init__()
        self.radius = 10
        self.center_x = random.randint(0, WIDTH_SCREEN)
        self.center_y = random.randint(0, HEIGHT_SCREEN)
        self.color = arcade.color.YELLOW
        self.width = 20
        self.height = 20

关于python - arcade中的碰撞错误: When I use the arcade. python中的check_for_collision方法并发生碰撞,它给出了错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73383783/

相关文章:

java - Sprite 无法正常移动。 libgdx

xcode - 快速碰撞检测

javascript - 与二维碰撞有关的四叉树

collision - Godot 引擎与 KinematicBody 碰撞不起作用

java - 为什么当我尝试更改我的马里奥克隆游戏 Sprite 时,它会被切断?

java - 敌人生成 Android

python - 通过他们的 API 访问 Lending Club Loan 列表

python - 从另一个文件导入和更改变量

python - 第一次尝试从 Powershell 运行 python。我使用的命令是否正确/我需要做什么才能允许使用 Python?

python - Python C API 中的静态变量