2
votes

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
Fix your indention. It's currently way too indented. It's important for us that you do it for us to answer your question properly, as the error looks like to be a problem with indention. Edit your question and make sure your question accurately reflects your actual code. - Ted Klein Bergman
"but instead nothing happens at all" do you see any errors, or does it just do nothing? also, did you accidently cut off you code at the last line where it shows ai.run_game? - Axiumin_

2 Answers

0
votes

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'))
0
votes
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()