Python Forum
[PyGame] vert and horz scrolling background
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] vert and horz scrolling background
#1
I'm trying to make a background that scrolls in all directions. I'm working from an example for a horizontal scrolling game and adding vertical to it, but I can't get it to work right. It works vertical and horizontally but not diagonally.

Here is the code, could someone give me a pointer on how I can make this work correctly?
def draw(self, screen):

        self.bgx -= self.player.velocity.x
        self.bgy -= self.player.velocity.y
        rel_bgx = self.bgx % self.bg.get_rect().width
        rel_bgy = self.bgy % self.bg.get_rect().height
        screen.blit(self.bg, (rel_bgx - self.bg.get_rect().width, self.bgy))
        screen.blit(self.bg, (self.bgx, rel_bgy - self.bg.get_rect().height))
        if rel_bgx < 800:
            screen.blit(self.bg, (rel_bgx, self.bgy))
        if self.bgx > 0:
            screen.blit(self.bg, (self.bgx - self.bg.get_rect().width, rel_bgy))
        if rel_bgy < 800:
            screen.blit(self.bg, (self.bgx, rel_bgy))
        if self.bgy > 0:
            screen.blit(self.bg, (rel_bgx, self.bgy - self.bg.get_rect().height))
Reply
#2
Just tile it.
import os
import pygame

class DisplayEngine:
    @staticmethod
    def center_screen():
        os.environ['SDL_VIDEO_CENTERED'] = '1'

    def __init__(self, caption, width, height, flags=0):
        pygame.display.set_caption(caption)
        self.surface = pygame.display.set_mode((width, height), flags)
        self.rect = self.surface.get_rect()
        self.clock = pygame.time.Clock()
        self.running = False
        self.delta = 0
        self.fps = 60

    def idle(self):
        self.delta = self.clock.tick(self.fps)

    def quit(self):
        self.running = False

class Scene:
    def __init__(self, display_engine):
        self.display_engine = display_engine
        self.bg = pygame.Surface((800, 800))
        self.bg.fill(pygame.Color('gray5'))
        pygame.draw.line(self.bg, pygame.Color('dodgerblue'), (0, 10), (800, 10))
        pygame.draw.line(self.bg, pygame.Color('dodgerblue'), (0, 400), (800, 400), 2)
        pygame.draw.line(self.bg, pygame.Color('dodgerblue'), (0, 750), (800, 750))
        pygame.draw.line(self.bg, pygame.Color('firebrick'), (20, 0), (20, 800), 3)
        pygame.draw.line(self.bg, pygame.Color('firebrick'), (300, 0), (300, 800))

        self.camera = pygame.Vector2()
        self.camera_speed = 140 / 1000

    # Area you are looking for.
    def tile_image(self, surface, image):
        width, height = self.display_engine.rect.size
        i_width, i_height = image.get_size()
        camera_x = int(self.camera.x % i_width)
        camera_y = int(self.camera.y % i_height)
        for x in range(-camera_x, width, i_width):
            for y in range(-camera_y, height, i_height):
                surface.blit(image, (x, y))

    def mainloop(self):
        self.display_engine.running = True
        while self.display_engine.running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.display_engine.quit()

            keys = pygame.key.get_pressed()
            if keys[pygame.K_w]:
                self.camera.y -= self.camera_speed * self.display_engine.delta

            if keys[pygame.K_s]:
                self.camera.y += self.camera_speed * self.display_engine.delta

            if keys[pygame.K_a]:
                self.camera.x -= self.camera_speed * self.display_engine.delta

            if keys[pygame.K_d]:
                self.camera.x += self.camera_speed * self.display_engine.delta


            surface = self.display_engine.surface
            surface.fill(pygame.Color("black"))

            self.tile_image(surface, self.bg)

            pygame.display.flip()
            self.display_engine.idle()

if __name__ == '__main__':
    pygame.init()
    DisplayEngine.center_screen()
    display_engine = DisplayEngine("Moving Background", 600, 600)
    scene = Scene(display_engine)
    scene.mainloop()
99 percent of computer problems exists between chair and keyboard.
Reply
#3
Your're a legend! Works like a dream.

Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] How do you change your horizontal scrolling background to a vertical one? Kalebre 0 1,101 Jul-31-2023, 02:42 PM
Last Post: Kalebre

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020