为什么我的 pygame 游戏在显示矩形时丢帧?
Why is my pygame game losing frames when displaying rects?
我正在尝试编写一个简单的游戏,该游戏有一个玩家对象和一个使用循环创建的地板。我不确定为什么它丢帧这么快。我有时钟滴答 60 fps 但很快下降到 15 fps。提前致谢!
main.py
black = (0, 0, 0)
blue = (50, 60, 200)
white = (255, 255, 255)
font = pygame.font.Font(None, 30)
window = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Platformer")
gravity = -0.5
player = Player(400, 0)
level1 = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
]
for y in range(0, len(level1)):
for x in range(0, len(level1[y])):
if level1[y][x] == 1:
blockList.append(Block(x*32, y*32))
window.fill(blue)
for block in blockList:
block.render(window)
player.x += moveX
player.update(gravity, blockList)
player.render(window)
clock.tick(60)
for x in range(0, len(level1[y])):
if level1[y][x] == 1:
blockList.append(Block(x*32, y*32))``` Im pretty sure thats where the error is...
这里有一个很好的学习点。优化代码的第一步是从循环中去除不必要的数学运算。正如评论中所建议的那样,如果你是 运行 每个帧计算中的双循环,它可能会减慢你的速度。也许其他事情也是如此。
让我们假设您的 "level 1" 块没有变化 frame-to-frame。因此,我们可以在 开始循环之前 计算黑名单。
# make blocklist
level1= [[0,0, ... , 0],
...
[1,1, ... , 1]]
blocklist = []
for y ...
for x ...
blocklist.append(...)
# now start your frame loop
while True:
window.fill()
for block in blocklist:
...
clock.tick(60)
我正在尝试编写一个简单的游戏,该游戏有一个玩家对象和一个使用循环创建的地板。我不确定为什么它丢帧这么快。我有时钟滴答 60 fps 但很快下降到 15 fps。提前致谢!
main.py
black = (0, 0, 0)
blue = (50, 60, 200)
white = (255, 255, 255)
font = pygame.font.Font(None, 30)
window = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Platformer")
gravity = -0.5
player = Player(400, 0)
level1 = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
]
for y in range(0, len(level1)):
for x in range(0, len(level1[y])):
if level1[y][x] == 1:
blockList.append(Block(x*32, y*32))
window.fill(blue)
for block in blockList:
block.render(window)
player.x += moveX
player.update(gravity, blockList)
player.render(window)
clock.tick(60)
for x in range(0, len(level1[y])):
if level1[y][x] == 1:
blockList.append(Block(x*32, y*32))``` Im pretty sure thats where the error is...
这里有一个很好的学习点。优化代码的第一步是从循环中去除不必要的数学运算。正如评论中所建议的那样,如果你是 运行 每个帧计算中的双循环,它可能会减慢你的速度。也许其他事情也是如此。
让我们假设您的 "level 1" 块没有变化 frame-to-frame。因此,我们可以在 开始循环之前 计算黑名单。
# make blocklist
level1= [[0,0, ... , 0],
...
[1,1, ... , 1]]
blocklist = []
for y ...
for x ...
blocklist.append(...)
# now start your frame loop
while True:
window.fill()
for block in blocklist:
...
clock.tick(60)