3

I'm trying to create an infinite scrolling background for a simple 'dodge the incoming objects' game with python's arcade library. I've managed to get the background to move, but I can't seem to create another. I've been looking at a lot of example code, and I understand the basic idea is I have a list that removes the background when x = 0 and then append another at the starting value.

I have trouble with this in execution. :/

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()
Alderven
  • 7,569
  • 5
  • 26
  • 38
MadTaxi
  • 71
  • 4

1 Answers1

4

I figured out how to do this, so I'll answer my own question for anyone who googles it later.

EDIT: I think I've simplified the code as much as I know how. I also added equations, so all you have to do is plug in your numbers at the top and your files in the bottom. It works ideally for backgrounds that are the same size as your window.

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()
MadTaxi
  • 71
  • 4
  • 1
    This is how most people solve the problem. It's one of the most efficient solutions, and it's surprising that you stumbled upon it yourself. Congratulations; you will go far. – wizzwizz4 Mar 17 '19 at 20:09
  • To improve this question / answer pair, a [mcve] would help. Cut the code down to the bare minimum, and make sure that you've got a full program (including the `import` lines). That'll help the most people. Also read the [tour], [ask] and [answer]. – wizzwizz4 Mar 17 '19 at 20:10