Pygame Side Scroller

Viewed 108

I've written my code and whenever I run it, I expect for it to open up the screen with the background but instead nothing happens at all. I don't know where I went wrong or what I did wrong.

My code:

import os.path
import sys
import pygame

from settings import Settings

class Slingshot_Adventures:
        def __init__(self):
                """Initialize the game, and create game resources."""
                pygame.init()

                self.screen = pygame.display.set_mode((1280, 720))
                pygame.display.set_caption('Slingshot Adventures')

                self.bg_color = (0, 0, 0)

                bg = pygame.image.load_basic(os.path.join('Final/bg.bmp'))

        def run_game(self):
            
                while True:
                # Watch for keyboard and mouse events.
                        for event in pygame.event.get():
                                if event.type == pygame.QUIT:
                                        sys.exit()

        # Redraw the screen during each pass through the loop.
                        self.screen.fill(self.bg_color)


                # Make the most recently drawn screen visible.
                        pygame.display.flip()
        
if __name__ == '__main__':
        # Make a game instance, and run the game.
        ai = Slingshot_Adventures()
        ai.run_game
2 Answers
class Slingshot_Adventures:
def __init__(self):
    """Initialize the game, and create game resources."""
    pygame.init()

    self.screen = pygame.display.set_mode((1280, 720))
    pygame.display.set_caption('Slingshot Adventures')

    self.bg_color = (0, 0, 0)
    #put your path to the image here
    self.image = pygame.image.load(r"path/to/your/image.png")
def run_game(self):

    while True:
        # Watch for keyboard and mouse events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        # Redraw the screen during each pass through the loop.
        self.screen.fill(self.bg_color)
        self.screen.blit(self.image, (0, 0))
        # Make the most recently drawn screen visible.
        pygame.display.flip()




if __name__ == '__main__':
    # Make a game instance, and run the game.
    ai = Slingshot_Adventures()
    ai.run_game()

You have set the local variable bg in the constructor of the class Slingshot_Adventures, but you did not set the attribute self.bg:

bg = pygame.image.load_basic(os.path.join('Final/bg.bmp'))

self.bg = pygame.image.load_basic(os.path.join('Final/bg.bmp'))
Related