gpt4 book ai didi

python - Pygame 窗口保持空白

转载 作者:行者123 更新时间:2023-11-28 16:56:30 29 4
gpt4 key购买 nike

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

最佳答案

一个问题是:

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 的完整示例。 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()

关于python - Pygame 窗口保持空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57842885/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com