【问题标题】:Inconsistent drawing in Pygame on Mac OSX MojaveMac OSX Mojave 上 Pygame 中的绘图不一致
【发布时间】:2019-05-20 19:03:24
【问题描述】:

我在一个月左右的时间里一直在制作一款 Pokemon 风格的游戏,最近刚刚更新到 Mojave 以查看新功能。

更新后,我注意到我的游戏存在一些小问题,其中一个具体问题是 HP 条的绘制方式与使用 High Sierra 时不同。之前它的宽度会逐渐减小,但现在程序只是在动画播放的时间内冻结,然后跳到动画的结尾。为什么会出现这种情况?

我现在知道 Pygame 在 Mojave 上存在绘图问题(可能是由于新的暗模式),但我还没有看到有人描述过这样一种情况,在这种情况下,他们只有 一小部分程序受到了影响——大多数报告都是从 18 年开始的,并抱怨 完全没有绘图

无论如何,这是我的 HP Bar 动画代码:

def changeHP(barPos, maxHP, startHP, dif): #This function displays the animation for hp loss/gain
    timer = pygame.time.Clock()
    for i in range(int(dif*100/maxHP)):
        difPer = (maxHP*i/100)
        if startHP - difPer <= maxHP/5:
            color = Color(255, 0, 0, 255)
        elif startHP - difPer <= maxHP/2:
            color = Color(255, 255, 0, 255)
        else:
            color = Color(0, 255, 0, 255)
        pygame.draw.rect(screen, Color(255, 255, 255, 255), (barPos[0], barPos[1], 144, 9))
        pygame.draw.rect(screen, color, (barPos[0], barPos[1], ((startHP - difPer)/maxHP) * 144, 9))
        pygame.display.update()
        timer.tick(60)

对不起,如果某些值看起来令人费解,这是一项正在进行的工作。

【问题讨论】:

  • 您是否从多个地方致电pygame.display.update() 和/或timer.tick()?也许有某种显示更新切分音正在进行 - 更新正在发生,但“刷新”运行得太近,无法在屏幕上看到它??

标签: python macos pygame macos-mojave


【解决方案1】:

嗯,我认为这实际上不会起作用,但它确实起作用了。

我在循环中添加了一个事件队列检查,一切都恢复正常了。

这是我更新的代码:

def changeHP(barPos, maxHP, startHP, dif): #This function displays the animation for hp loss/gain
timer = pygame.time.Clock()
for i in range(int(dif*100/maxHP)):
    difPer = (maxHP*i/100)
    if startHP - difPer <= maxHP/5:
        color = Color(255, 0, 0, 255)
    elif startHP - difPer <= maxHP/2:
        color = Color(255, 255, 0, 255)
    else:
        color = Color(0, 255, 0, 255)
    pygame.draw.rect(screen, Color(255, 255, 255, 255), (barPos[0], barPos[1], 144, 9))
    pygame.draw.rect(screen, color, (barPos[0], barPos[1], ((startHP - difPer)/maxHP) * 144, 9))
    pygame.display.update()
    timer.tick(60)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

【讨论】:

    猜你喜欢
    • 2019-04-21
    • 2019-04-20
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多