我的python / pygame程序有什么问题?

时间:2015-01-08 19:15:50

标签: 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")
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()

3 个答案:

答案 0 :(得分:1)

我只是注册写这个答案。

  

这是你的主要游戏循环:

gameLoop = True
while gameLoop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameLoop = False

    pygame.display.flip()
sys.exit()
pygame.quit()

你没有在那里调用任何功能,你期待那个?你只看到一个黑屏是正常的。因为你的主游戏环中没有任何功能。也可以使用pygame.display.update()代替pygame.display.flip()。并且请勿使用sys.exit,请使用quit()

pygame.display.flip()this类似,pygame.display.update()更好。

答案 1 :(得分:0)

对于像你这样的游戏,这类游戏就是这样开始的:

class(object):

需要在程序后面的某个地方调用。我建议你把它改成:

class(pygame.sprite.Sprite)

通常使用 任何 类,您需要将其称为:

class = Class()

如果不这样做,它将不会被使用或激活。这解释了为什么没有发生。你还没有打电话给你的课程。其次,你有黑屏,因为你永远不会用颜色填充它。大多数人都想要白色,所以这里是你用彩色填充屏幕的方式(在你的情况下):

window.fill([255, 255, 255])

这会使您的屏幕变白。您将在while循环中但在for循环之前将其放入。我希望这可以帮助你!

答案 2 :(得分:-1)

我很确定你需要在你的文件路径上做两个反斜杠,否则它会认为它是一个转义字符。

例如,而不是

    img_path = os.path.join("C:\Users\Billy\Documents\programming\rpg" , "down.png")

    img_path = os.path.join("C:\\Users\\Billy\\Documents\\programming\\rpg" , "down.png")