Pygame窗口剩余blan

2024-05-19 03:40:42 发布

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

我正在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()

Tags: 代码selfinitdef平台pygamewinplayer
1条回答
网友
1楼 · 发布于 2024-05-19 03:40:42

问题是:

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的状态不会改变。你知道吗


我建议分别阅读^{}^{}。看一个简单的例子,它可以做你想做的。你知道吗

代码的主要思想是方法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

使用精灵作为平台和播放器的完整示例。精灵按组组织:

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()

相关问题 更多 >

    热门问题