Python Pygame侧滚动条
我已经编写了我的代码,每当我运行它时,我都希望它打开背景屏幕,但实际上什么都没有发生。我不知道我哪里做错了,也不知道我做错了什么 我的代码:Python Pygame侧滚动条,python,pygame,Python,Pygame,我已经编写了我的代码,每当我运行它时,我都希望它打开背景屏幕,但实际上什么都没有发生。我不知道我哪里做错了,也不知道我做错了什么 我的代码: import os.path import sys import pygame from settings import Settings class Slingshot_Adventures: def __init__(self): """Initialize the ga
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
您已经在类的构造函数中设置了局部变量
bg
,但没有设置属性self.bg
:
bg=pygame.image.load\u basic(os.path.join('Final/bg.bmp'))
self.bg=pygame.image.load\u basic(os.path.join('Final/bg.bmp'))
修复您的缩进。它现在太缩进了。对于我们来说,您这样做对我们正确回答您的问题很重要,因为错误看起来是缩进的问题。并确保你的问题准确地反映了你的实际代码。“但实际上什么都没有发生”你看到了任何错误,还是什么都没做?另外,您是否在显示ai.run_game的最后一行不小心切断了代码?这实际上对我有点作用。屏幕现在弹出,但它只是一个黑屏,仍然缺少应该是背景的图像。你永远不会在屏幕上显示图像,你只是用黑色填充屏幕。在每帧用黑色填充后,你必须显示图像。我将如何执行该操作?我已更新我的答案以显示如何执行该操作
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()