我的python/pygame程序有什么问题?
我是python和pygame的新手,我正在尝试制作一个基本的RPG。当我运行我的代码时,它不会说有任何错误,但除了一个空白的黑屏外,什么也不会显示。我的代码有什么问题我的python/pygame程序有什么问题?,python,pygame,Python,Pygame,我是python和pygame的新手,我正在尝试制作一个基本的RPG。当我运行我的代码时,它不会说有任何错误,但除了一个空白的黑屏外,什么也不会显示。我的代码有什么问题 import pygame, sys,os pygame.init() window = pygame.display.set_mode((800,600)) img_path = os.path.join("C:\Users\Billy\Documents\programming\rpg" , "down.png") im
import pygame, sys,os
pygame.init()
window = pygame.display.set_mode((800,600))
img_path = os.path.join("C:\Users\Billy\Documents\programming\rpg" , "down.png")
img_path2 = os.path.join("C:\Users\Billy\Documents\programming\rpg" , "roof.png")
class player(object):
def __init__ (self):
self.image_s = pygame.image.load(img_path)
self.image_b = self.image_s.get_rect()
self.x = 400
self.y = 300
def handle_keys(self):
key = pygame.key.get_pressed()
dist = 50
if key[pygame.K_DOWN]:
self.y += dist
elif key [pygame.K_UP]:
self.y -= dist
if key [pygame.K_RIGHT]:
self.x += dist
if key[pygame.K_LEFT]:
self.x -= dist
def background (self, window):
bg = os.path.join("C:\Users\Billy\Documents\programming\rpg" , "bg.png")
self.image2 = pygame.image.load(bg)
window.blit(self.image2, (0,0))
def draw(self, window):
window.blit(self.image, (self.x, self.y))
def checkCollision(self, sprite1, sprite2):
col = pygame.sprite.collide_rect(sprite1, sprite2)
if col == True:
dist = 0
class house(object):
def __init__(self, x=700, y=500):
self.image_s = pygame.image.load(img_path2)
self.image_b = self.image_s.get_rect()
self.x = x
self.y = y
def rock_draw(self, window):
window.blit(self.image, (self.x , self.y))
gameLoop = True
while gameLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameLoop = False
pygame.display.flip()
sys.exit()
pygame.quit()
我很确定你需要在文件路径上做两个反斜杠,否则它会认为这是一个转义字符 例如,代替
img_path = os.path.join("C:\Users\Billy\Documents\programming\rpg" , "down.png")
放
对于像您这样的游戏,一节课是这样开始的:
class(object):
将需要在程序稍后的某个地方调用。我建议您将其更改为:
class(pygame.sprite.Sprite)
通常,对于任何类,您都需要这样称呼它:
class = Class()
如果您不这样做,它将不会被使用或激活。这就解释了为什么什么都没发生。你还没有给你的班级打电话。第二,你有一个黑屏,因为你从来没有用颜色填充它。大多数人都想要白色,因此,在您的案例中,以下是如何用颜色填充屏幕:
window.fill([255, 255, 255])
这会使您的屏幕变白。您将把它放在while循环中,但放在for循环之前。我希望这对你有帮助 我刚报名写这个答案 这是您的主要游戏循环: 您没有调用其中的任何函数,您希望这样吗?你只看到黑屏是很正常的,因为你在主游戏循环中没有被调用任何函数。也可以使用pygame.display.update而不是pygame.display.flip。不要使用sys.exit,使用quit
pygame.display.flip类似于pygame.display.update更好。请正确缩进您的代码。好吧,除了初始化窗口之外,您从来不会对窗口执行任何操作,所以我不知道您还希望发生什么……这种类型的问题应该出现在代码审阅网站上,对吗?您在哪里创建了对象?就好像你写了一堆函数却从未调用过?因此,它们不会显示任何错误,除了缩进错误,真正的错误会在您创建类实例后显示。这是真的,但它与OP目前遇到的问题并不特别相关。
window.fill([255, 255, 255])
gameLoop = True
while gameLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameLoop = False
pygame.display.flip()
sys.exit()
pygame.quit()