Python中的无限滚动背景

3
我正在尝试使用Python的Arcade库为简单的“躲避飞来物体”的游戏创建一个无限滚动的背景。我已成功让背景移动,但似乎无法创建另一个背景。我查看了很多示例代码,我理解的基本思路是:当x = 0时,通过列表删除背景,然后在起始值处附加另一个背景。
但我在执行过程中遇到了问题。 :/
self.background_sprite = arcade.sprite.Sprite("resources/Background.png")
    self.background_sprite.center_x = 600
    self.background_sprite.center_y = 300
    self.background_list.append(self.background_sprite)

    for self.background_sprite in self.background_list:
        self.background_sprite.change_x -= BACKGROUND_SPEED

def update_order(self):
    self.background_update
    self.player_update()

def on_draw(self):
    """ Render the screen. """
    arcade.start_render()
    self.player_list.draw()
    for self.background_sprite in self.background_list:
        self.background_sprite.draw()


def background_update(self, delta_time):
    for self.background_sprite in self.background_list:
        x = self.background_sprite.center_x - BACKGROUND_SPEED
        self.background_list.update()
        if x == 0:
            self.background_list.remove(self.background_sprite)
            self.background_list.append(self.background_sprite)
            repeat_count_x = 2
            self.background_list.update()
1个回答

4
我解决了这个问题,所以我会为任何在之后通过谷歌搜索到这个问题的人回答它。
编辑:我认为我已经尽可能简化了代码。 我还添加了方程式,所以你只需要在顶部插入数字并在底部插入文件。 它最适用于与您的窗口大小相同的背景。
import arcade
import os

# --- Constants ---
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 400
IMAGE_WIDTH = 800
SCROLL_SPEED = 5

class MyGame(arcade.Window):
    def __init__(self, width, height):
        super().__init__(width, height)

        file_path = os.path.dirname(os.path.abspath(__file__))
        os.chdir(file_path)

    def setup(self):

        #first background image
        self.background_list = arcade.SpriteList()

        self.background_sprite = arcade.Sprite("image.file")

        self.background_sprite.center_x = IMAGE_WIDTH // 2
        self.background_sprite.center_y = SCREEN_HEIGHT // 2
        self.background_sprite.change_x = -SCROLL_SPEED

        self.background_list.append(self.background_sprite)

        #second background image
        self.background_sprite_2 = arcade.Sprite("image.file")

        self.background_sprite_2.center_x = SCREEN_WIDTH + IMAGE_WIDTH // 2
        self.background_sprite_2.center_y = SCREEN_HEIGHT // 2
        self.background_sprite_2.change_x = -SCROLL_SPEED

        self.background_list.append(self.background_sprite_2)

    def on_draw(self):
        arcade.start_render()
        self.background_list.draw()

    def update(self, delta_time):

        #reset the images when they go past the screen
        if self.background_sprite.left == -IMAGE_WIDTH:
            self.background_sprite.center_x = SCREEN_WIDTH + IMAGE_WIDTH // 2

        if self.background_sprite_2.left == -IMAGE_WIDTH:
            self.background_sprite_2.center_x = SCREEN_WIDTH + IMAGE_WIDTH // 2

        self.background_list.update()

def main():
    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT)
    window.setup()
    arcade.run()

if __name__ == "__main__":
    main()

1
这是大多数人解决问题的方式。这是最有效的解决方案之一,而且令人惊讶的是你自己也发现了它。恭喜你,你会走得很远。 - wizzwizz4
为了改进这个问题/答案对,一个 [mcve] 会有所帮助。将代码精简到最少,并确保您拥有一个完整的程序(包括 import 行)。这将对大多数人有所帮助。还要阅读 [tour]、[ask] 和 [answer]。 - wizzwizz4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接