我正在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()
答案 0 :(得分:2)
问题是:
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()