2

I'm attempting at making a very simple game where you move up and down to dodge projectiles but I'm at a stand still where I cannot seem to figure out how to add a scrolling background.

What I'm going for is just a looped scrolling background that doesn't stop until the game ends to give the illusion of movement/flying. Any help?

import pygame
pygame.init()


screen = pygame.display.set_mode((720, 229))


clock = pygame.time.Clock()


bck = pygame.image.load('background.png')


class Box(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("goku.png")
        self.rect = self.image.get_rect()

        self.rect.centerx = 50
        self.rect.centery = 50
        self.dx = 5
        self.dy = 5

    def update(self):
        self.move()
        self.wrap()

    def move(self):
        k = pygame.key.get_pressed()
        if k[pygame.K_a]:
            self.rect.centerx -= self.dx
        if k[pygame.K_d]:
            self.rect.centerx += self.dx
        if k[pygame.K_w]:
            self.rect.centery -= self.dy
        if k[pygame.K_s]:
            self.rect.centery += self.dy

    def wrap(self):
        if self.rect.left > screen.get_width():
            self.rect.right = 0
        if self.rect.right < 0:
            self.rect.left = screen.get_width()
        if self.rect.top > screen.get_height():
            self.rect.bottom = 0
        if self.rect.bottom < 0:
            self.rect.top = screen.get_height()


def main():
    loop = True
    clock = pygame.time.Clock()

    box = Box()
    allSprites = pygame.sprite.Group(box)

    while loop:
        clock.tick(30)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                loop = False

        screen.blit(bck, (0, 0))
        allSprites.update()
        allSprites.draw(screen)
        pygame.display.flip()

if __name__ == '__main__':
    main()
  • I should note that I have barely started, just created the player sprite. I got stuck on the scrolling background issue and haven't been able to progress since. – Trevor Gallant Dec 29 '16 at 18:57
  • Perhaps this question will help: http://stackoverflow.com/questions/19576626/side-scrolling-background-python-not-100-working – Peter Hall Dec 29 '16 at 19:08

2 Answers2

0

In your code you're displayin the backgroung as follow: screen.blit(bck, (0,0) making the background sprite always at (0,0).

You should keep track of the player movement, and use it as an offset for your background sprite screen.blit(bck, (player.position_x, 0), player.position_x or whatever your variable name is. in the current context it may be box.rect.centerx, making it moving with the player.

For a better result, you would like to tweak screen.blit(bck, (player.position_x / 2, 0) for a slower moving background.

Also, after doing so, you should probably get your background to reapeat itself along the moving axis.

Usiten
  • 15
  • 5
0

I thought I'd just make my background into a sprite class itself and from there have the background continue on moving to the left.

class back(pygame.sprite.Sprite):
def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.image.load("background.png")
    self.rect = self.image.get_rect()

def update(self):
    self.rect.centerx -= 5