我是编程新手,不胜感激指导/反馈。
下面是一个完整的工作脚本:
我设法使玩家精灵受到WASD的控制,小行星精灵现在也已呈现在屏幕上,并通过一些物理方法将其移动。它也应该从墙壁反弹,但不是。但我相信,由于某种原因,更新功能未正确调用Asteroid类-除非它有其他问题。
非常感谢您提供的所有帮助以及以后的指导!
import arcade
import random
""" Universal variables """
SPRITE_SCALING_PLAYER = 0.5
SPRITE_SCALING_ASTEROID = 0.35
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
MOVEMENT_SPEED = 5
class Player(arcade.Sprite):
# PLAYER
def update(self):
# COLLISION
self.center_x += self.change_x
self.center_y += self.change_y
if self.left < 0:
self.left = 0
elif self.right > SCREEN_WIDTH - 1:
self.right = SCREEN_WIDTH - 1
if self.bottom < 0:
self.bottom = 0
elif self.top > SCREEN_HEIGHT - 1:
self.top = SCREEN_HEIGHT - 1
class Asteroid(arcade.Sprite):
# ASTEROID
def __init__(self, filename, sprite_scaling):
super().__init__(filename, sprite_scaling)
self.change_x = 0
self.change_y = 0
def update(self):
# Move the asteroid
self.center_x += self.change_x
self.center_y += self.change_y
# rebound
if self.left < 0:
self.change_x *= -1
if self.right > SCREEN_WIDTH:
self.change_x *= -1
if self.bottom < 0:
self.change_y *= -1
if self.top > SCREEN_HEIGHT:
self.change_y *= -1
# MAIN GAME CLASS
class MyGame(arcade.Window):
""" Our custom Window Class"""
def __init__(self):
""" Initializer """
# Call the parent class initializer
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, "Alien")
# Background image will be stored in this variable
self.background = ("space_bg.png")
# Variables that will hold sprite lists
self.all_sprite_list = ["ufo_sprite.png", "asteroid.gif"]
# Set up player
self.player_sprite = self.all_sprite_list[0]
# Set up asteroid
self.asteroid_sprite = self.all_sprite_list[1]
# Don't show the mouse cursor
self.set_mouse_visible(False)
# arcade.set_background_color(arcade.color.BLACK)
def setup(self):
""" Set up the game and initialize the variables. """
# background
self.background = arcade.load_texture(self.background)
# Sprite lists
self.all_sprite_list = arcade.SpriteList()
# Set up the player
self.player_sprite = Player("ufo_sprite.png", SPRITE_SCALING_PLAYER)
self.player_sprite.center_x = (SCREEN_WIDTH * 0.50)
self.player_sprite.center_y = (SCREEN_HEIGHT * 0.50)
self.all_sprite_list.append(self.player_sprite)
# Set up asteroid
self.asteroid_sprite = Asteroid("asteroid.gif", SPRITE_SCALING_ASTEROID)
Asteroid.center_x = random.randrange(SCREEN_WIDTH)
Asteroid.center_y = random.randrange(SCREEN_HEIGHT)
Asteroid.change_x = random.randrange(-4, 4)
Asteroid.change_y = random.randrange(-4, 4)
self.all_sprite_list.append(self.asteroid_sprite)
def on_draw(self):
# needed before other drawn elements
arcade.start_render()
# draw background
arcade.draw_texture_rectangle(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2,
SCREEN_WIDTH, SCREEN_HEIGHT, self.background)
# draw sprites
self.all_sprite_list.draw()
def update(self, delta_time):
""" Movement and game logic """
self.all_sprite_list.update()
def on_key_press(self, key, modifiers):
"""Called whenever a key is pressed. """
if key == arcade.key.W:
self.player_sprite.change_y = MOVEMENT_SPEED
elif key == arcade.key.S:
self.player_sprite.change_y = -MOVEMENT_SPEED
elif key == arcade.key.A:
self.player_sprite.change_x = -MOVEMENT_SPEED
elif key == arcade.key.D:
self.player_sprite.change_x = MOVEMENT_SPEED
# elif key == arcade.key.SPACE:
# self.player_sprite.change_x = MOVEMENT_SPEED
def on_key_release(self, key, modifiers):
"""Called when the user releases a key. """
if key == arcade.key.W or key == arcade.key.S:
self.player_sprite.change_y = 0
elif key == arcade.key.A or key == arcade.key.D:
self.player_sprite.change_x = 0
# elif key == arcade.key.SPACE:
# self.player_sprite.change_y = (SCREEN_HEIGHT * 0.005)
def main():
""" Main method """
window = MyGame()
window.setup()
arcade.run()
arcade.schedule(update, 1 / 80)
if __name__ == "__main__":
main()
答案 0 :(得分:1)
能否请您告诉我们确切的问题(异常,精灵未按预期移动,...) 但是,您提供的代码给了我以下想法:
调用继承的类的构造函数
class Asteroid(arcade.Sprite):
def __init__(self):
super(Asteroid, self).__init__()
# or
arcade.Sprite.__init__(self)
更多here(很奇怪,但是我在官方文档的classes-inheritance-section下调用基本构造函数一无所获,也许有人可以提供一些东西
使用不存在或在下面创建的变量
self.center_x += self.change_x * delta_time
self.center_y += self.change_y * delta_time
self.change_x是在下面创建的(因此当时不存在),并且delta_time不在其他任何地方(也许只是一个不完整的代码段?)
在创建自己的Sprite(Asteroid)子类后,您真的要创建Sprite实例吗?
self.asteroid_sprite = Asteroid("asteroid.gif", SPRITE_SCALING_ASTEROID)
代替
self.asteroid_sprite = arcade.Sprite("asteroid.gif", SPRITE_SCALING_ASTEROID)
编辑: 在分配小行星构造函数中的变量之前,请尝试执行以下操作:
def __init__(self, *args, **kwargs):
super(Asteroid, self).__init__(*args, **kwargs)
args
和kwargs
是您要在下面传递的东西(如图像路径)的占位符