Shuratt
Shuratt

Reputation: 89

Side-scrolling background

I'm trying to replicate those old arcade games where the background continuously slides left (or up or down..) but I'm looking for some simple lines of code to achieve that goal. Something that will shift the background image left and then reset adding another copy of the same image. Could you suggest me an easy way to add this feature to my code?

import pygame
import os

size = width, height = 750, 422
screen = pygame.display.set_mode(size)

img_path = os.path.join(os.getcwd())
background_image = pygame.image.load('background.jpg').convert()

pygame.display.set_caption("BallGame")

class Ball(object):
    def __init__(self):
        self.image = pygame.image.load("ball.png")
        self.image_rect = self.image.get_rect()
        self.image_rect.x
        self.image_rect.y
        self.facing = 'LEFT'

    def handle_keys(self):
        key = pygame.key.get_pressed()
        dist = 5
        if key[pygame.K_DOWN] and self.image_rect.y < 321:
            self.facing = 'DOWN'
            self.image_rect.y += dist
        elif key[pygame.K_UP] and self.image_rect.y > 0:
            self.facing = 'UP'
            self.image_rect.y -= dist
        if key[pygame.K_RIGHT] and self.image_rect.x < 649:
            self.facing = 'RIGHT'
            self.image_rect.x += dist
        elif key[pygame.K_LEFT] and self.image_rect.x > 0:
            self.facing = 'LEFT'
            self.image_rect.x -= dist

    def draw(self, surface):
        if self.facing == "RIGHT":
            surface.blit(pygame.transform.flip(self.image, True, False),(self.image_rect.x,self.image_rect.y))
        elif self.facing == "DOWN":
            surface.blit(pygame.image.load("ball_down.png"),(self.image_rect.x,self.image_rect.y))
        if self.facing == "UP":
            surface.blit(pygame.image.load("ball_up.png"),(self.image_rect.x,self.image_rect.y))
        elif self.facing == "LEFT":
            surface.blit(self.image,(self.image_rect.x,self.image_rect.y))

pygame.init()
screen = pygame.display.set_mode((750, 422))

ball = Ball()
clock = pygame.time.Clock()

pygame.mixer.music.load("bg_music.mp3")
pygame.mixer.music.play(-1, 0.0)

running = True
while running:
    esc_key = pygame.key.get_pressed()
    for event in pygame.event.get():
        if esc_key[pygame.K_ESCAPE]:
            pygame.display.quit()
            pygame.quit()
            running = False

    ball.handle_keys()

    screen.blit(background_image, [0, 0])
    ball.draw(screen)
    pygame.display.update()

    clock.tick(40)

Upvotes: 1

Views: 753

Answers (1)

sloth
sloth

Reputation: 101032

Here's a simple example.

Note the comments for an explanation.

import pygame

pygame.init()

screen = pygame.display.set_mode((640, 480))

clock = pygame.time.Clock()

# this is the background image
# we need to blit it two times, let's call them "left" and "right"
back_image = pygame.image.load('background.png')

# this rect will store the position of the "left"-part of the background
back_rect = back_image.get_rect()

while True:
    if pygame.event.get(pygame.QUIT):
        break

    for e in pygame.event.get():
        # your event handling here
        pass

    # this is the "left" image
    screen.blit(back_image, back_rect)
    # this is the "right" image, next to the left one
    # we calculate the "right" position by simply moving the rect by the width of the image
    screen.blit(back_image, back_rect.move(back_rect.width, 0))

    # we want a scrolling background, so we just move the position rect
    back_rect.move_ip(-2, 0)

    # it the right edge of the "left" image is zero, that means it's fully out of view
    if back_rect.right == 0:
        # so we reset the rect and start over
        back_rect.x = 0

    clock.tick(60)
    pygame.display.flip()

and here's an example image (save it as background.png)

enter image description here

The end result will look like this:

enter image description here

Upvotes: 2

Related Questions