0

我是编程新手,希望得到指导/反馈。

下面是一个完整的工作脚本:

我已经设法让玩家精灵由 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()
4

1 回答 1

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)
    

编辑: 在 Asteroid 构造函数中分配变量之前尝试这样做:

def __init__(self, *args, **kwargs):
    super(Asteroid, self).__init__(*args, **kwargs)

args并且kwargs是你在下面传递的东西的占位符,比如图像路径

于 2018-10-22T20:46:11.580 回答