Pygame侧滚动条

2024-05-20 05:10:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了我的代码,每当我运行它时,我都希望它打开背景屏幕,但实际上什么都没有发生。我不知道我哪里做错了,也不知道我做错了什么

我的代码:

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

Tags: andtherun代码importselfeventgame
2条回答

您已在类Slingshot_Adventures的构造函数中设置了局部变量bg,但未设置属性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'))
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()

相关问题 更多 >