Understanding classes in Python Arcade
我对编程很陌生,希望能得到指导/反馈。
以下是完整的工作脚本:
我已经设法让玩家精灵被WASD控制,小行星精灵现在也在屏幕上渲染,用一些物理方法来移动它。它也会从墙壁上反弹,但不会。但出于某种原因,更新功能没有正确调用小行星类,我相信-除非它有其他问题。
非常感谢所有的帮助,所以AR和未来的指导!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | 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() |
请告诉我们具体的问题好吗(例外,精灵没有按预期移动,…)但是,您提供的代码给了我以下想法:
调用继承类的构造函数
1
2
3
4
5class Asteroid(arcade.Sprite):
def __init__(self):
super(Asteroid, self).__init__()
# or
arcade.Sprite.__init__(self)这里有更多(这很奇怪,但是我在官方文档中的类继承部分下没有发现任何关于调用基构造函数的内容,也许有人可以提供一些
使用不存在或在下面创建的变量
1
2self.center_x += self.change_x * delta_time
self.center_y += self.change_y * delta_time号
self.change_x是在下面创建的(因此它在当时不存在),delta_time不在其他任何地方(可能只是一个不完整的片段?)
您真的想在创建自己的sprite(小行星)子类后创建sprite实例吗?
1self.asteroid_sprite = Asteroid("asteroid.gif", SPRITE_SCALING_ASTEROID)而不是
1self.asteroid_sprite = arcade.Sprite("asteroid.gif", SPRITE_SCALING_ASTEROID)。
小精灵
编辑:在为小行星构造器中的变量赋值之前,请尝试这样做:
1 2 | def __init__(self, *args, **kwargs): super(Asteroid, self).__init__(*args, **kwargs) |