gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:27:07 25 4
gpt4 key购买 nike

我在做一个贪吃蛇游戏,当我想实现蛇与苹果的碰撞时,遇到如下错误。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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com