python - Python中的无限滚动背景

标签 python list scroll background-image arcade

我正在尝试使用 python 的街机库为简单的“躲避传入对象”游戏创建无限滚动背景。我已经设法让背景移动,但我似乎无法创建另一个。我一直在看很多示例代码,我理解基本思想是我有一个列表,当 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()

最佳答案

我想出了如何做到这一点,所以我会为以后用谷歌搜索它的人回答我自己的问题。

编辑:我想我已经尽可能地简化了代码。我还添加了方程式,所以你所要做的就是在顶部插入你的数字,在底部插入你的文件。它非常适合与窗口大小相同的背景。

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()

关于python - Python中的无限滚动背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55167496/

相关文章:

python - 如何使用 mock 来测试用于缓存的 getter?

python - python 中 matlab (.mat) 文件 -v7 的部分加载

java - List<JSONObject> 与 JSONArray

java - 如何在java中实现抽象和泛型?

python - 在列表python中拆分Mac地址

ios - 以编程方式在 collectionView swift 中无限滚动

android - 触摸来自另一个线程的 View

Python:检查网络接口(interface)是否启动

android - 具有滚动速度控制的无限自动滚动 ListView

python - Matlab 和 Python 中的优化算法(dog-leg trust-region)