首页 > 解决方案 > Pygame 窗口保持空白

问题描述

我正在 PyGame 中编写平台游戏,当我添加平台重力代码(我什至不确定它是否有效)时,在所有剩余代码正常运行后,窗口变为空白。

这是我最小的、可重复的示例:

import pygame
pygame.init()

win = pygame.display.set_mode((500,480))
platform = pygame.image.load('platform.png')
sprite = pygame.image.load('player.png')
platforms = []

class Platform(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def draw(self, win):
        win.blit(platform, (self.x, self.y))

class Player(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.hitbox = (self.x, self.y, 25, 25)
        self.speedY = 0

    def draw(self, win):
        win.blit(sprite, (self.x, self.y))
        pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)

    #Checks whether the player is on a platform or below it
    #If they are, then they stay
    for platform in platforms:
        while self.hitbox[1] + self.hitbox[3] >= platform.y + 36:
            check = 0
        while self.hitbox[1] + self.hitbox[3] < platform.y:
            check = 1

    #Keeps player on top
    def touch_ground(self):
        while self.hitbox[1] + self.hitbox[3] > platform.y:
            self.speedY -= 1
            self.y += self.speedY

    #Makes player fall down until touching platform
    def fall(self):
        while check == 1:
            self.speedY += 1
            self.y += self.speedY
        touch_ground()

我预计玩家在不接触平台时会跌倒,并在他们接触平台时停留,但我得到了一个空白窗口,5 秒后停止响应。没有错误消息。

这是主要代码:

def redrawGameWindow():
    win.blit(bg, (0,0))
    bla.draw(win)
    goblin.draw(win)
    for platform in platforms:
        platform.draw(win)

    pygame.display.update()


run = True
while run:
    clock.tick(27)

    if level == 1:
        p1 = platforms.append(Platform(150, 375, 'medium'))
        p2 = platforms.append(Platform(300, 325, 'short'))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    redrawGameWindow()

pygame.quit()

标签: pythonpygame

解决方案


一个问题是:

for platform in platforms:
   while self.hitbox[1] + self.hitbox[3] >= platform.y + 36:
       check = 0
   while self.hitbox[1] + self.hitbox[3] < platform.y:
       check = 1 

当循环运行时,while 循环中的条件永远不会改变。如果条件是 False,那么循环中的代码永远不会执行,或者如果它的计算结果为True,那么循环将永远不会终止。

目前尚不清楚此代码的用途。代码片段改变了一个变量的状态check,但是这个变量在另一个循环中被评估:

def fall(self):
   while check == 1:
       self.speedY += 1
       self.y += self.speedY
   touch_ground()

最后,这也是一个无限循环。原因和以前一样。循环运行时,状态check不会改变。


pygame.sprite.Sprite我建议分别阅读pygame.sprite.Group。看一个简单的例子,它可以做你想做的事。

代码的主要思想是方法fall。该方法将下降限制在窗口的底部。此外,该方法找到玩家“下方”的平台,并将跌落限制在平台顶部。由于首先识别玩家下方的平台,然后执行跌倒,最后限制跌倒,因此算法避免了玩家的“跌倒”,即使玩家跌倒的距离远大于平台高度和玩家高度之和。

class Player(pygame.sprite.Sprite):

    # [...]

    def fall(self, win, platforms):

        # find platforms under the player
        pl = [p for p in platforms if p.rect.left < self.rect.right and
                                      p.rect.right > self.rect.left and
                                      p.rect.top >= self.rect.bottom]

        # fall down
        self.speedY += 1
        self.rect.y += self.speedY

        if self.rect.bottom >= win.get_height():
            # limit to the ground
            self.rect.bottom = win.get_height()
            self.speedY = 0
        else:
            # limit to the top of the platforms
            for p in pl:
                if self.rect.bottom >= p.rect.top:
                    self.rect.bottom = min(p.rect.top, self.rect.bottom)
                    self.speedY = 0

将 Sprite 用于平台和播放器的完整示例。精灵按组组织:

import pygame
pygame.init()

win = pygame.display.set_mode((500,480))
clock = pygame.time.Clock()

class Platform(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        self.image = pygame.image.load('player.png')
        self.rect = self.image.get_rect(topleft = (x, y)) 

class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        self.image = pygame.image.load('platform.png')
        self.rect = self.image.get_rect(topleft = (x, y))
        self.speedY = 0 

    def fall(self, win, platforms):

        # find platforms under the player
        pl = [p for p in platforms if p.rect.left < self.rect.right and 
                                      p.rect.right > self.rect.left and
                                      p.rect.top >= self.rect.bottom]

        # fall down
        self.speedY += 1
        self.rect.y += self.speedY

        if self.rect.bottom >= win.get_height():
            # limit to the ground
            self.rect.bottom = win.get_height()
            self.speedY = 0
        else:
            # limit to the top of the platforms
            for p in pl:
                if self.rect.bottom >= p.rect.top:
                    self.rect.bottom = min(p.rect.top, self.rect.bottom)
                    self.speedY = 0  


p1 = Platform(150, 375)
p2 = Platform(300, 325)
platforms = pygame.sprite.Group([p1, p2])

player = Player(180, 0)
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
all_sprites.add(platforms)

run = True
while run:

    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    player.fall(win, platforms)

    win.fill(0)
    all_sprites.draw(win)
    pygame.display.update()

推荐阅读