首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Powerbar pygame脚本,pygame

Powerbar pygame脚本,pygame
EN

Stack Overflow用户
提问于 2018-04-06 15:34:33
回答 1查看 117关注 0票数 0

我在试着创造一个“powerbar”在下面的代码中,我可以按left & right键来增加条,当bar_start_width >400px时,prg终止。当bar_start_width >0时,我希望"powerbar“减小。因此,如果您停下来按下按钮,条形图将慢慢减小,直到bar_start_width > 0。在我的代码中,它正在缓慢地减少bar_start_width,但不会更新显示。while循环也会继续运行。

这是完整的代码:导入时间导入随机导入pygame

代码语言:javascript
运行
复制
import time
import pygame
pygame.init()
display_width = 800
display_height = 600
black = (0, 0, 0)
white = (255, 255, 255)
gameDisplay = pygame.display.set_mode((display_width, display_height))
clock = pygame.time.Clock()

def powerbar(thingx, thingy, thingw, thingh, color):
    pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])

def game_quit():
    pygame.quit()
    quit()

def game_loop():

    decrease_speed = -1
    bar_start_width = 0
    bar_x = 100
    bar_y = 100
    bar_height = 50
    bar_color = black
    x_change = 0

    while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

        gameDisplay.fill(white)  # the background color before img

        # Should change so you can ONLY press right after left (or opposite)
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_change = 5
            if event.key == pygame.K_RIGHT:
                x_change = 5


        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                x_change = 0

        bar_start_width += x_change

        # Draw powerbar
        powerbar(bar_x, bar_y, bar_start_width, bar_height, bar_color)

        if bar_start_width > 10:
            while True:
                bar_start_width += decrease_speed
                time.sleep(0.5)
                print(bar_start_width) #See output in terminal
                if bar_start_width == 0:
                    break


        if bar_start_width > 400:
            game_quit()


    pygame.display.update()
    clock.tick(60)

game_loop()
pygame.quit()
quit()

我也尝试过用for循环来解决我的问题,但在长时间尝试不同的东西之后,这是我能想到的最好的方法。是否有人可以帮助我,或将我链接到类似的讨论中?谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-04-11 21:46:04

我不明白为什么要在主while循环中嵌套while循环。如果我理解正确的话,如果宽度大于0 (而不是当宽度大于0时),您希望减少每一帧的宽度。只需删除while循环和time.sleep调用并减小宽度if bar_start_width > 0:即可。

代码语言:javascript
运行
复制
import pygame


pygame.init()
black = (0, 0, 0)
white = (255, 255, 255)
gameDisplay = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()


def game_loop():
    decrease_speed = -1
    bar_start_width = 0
    bar_x = 100
    bar_y = 100
    bar_height = 50
    x_change = 0

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = 5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

        bar_start_width += x_change

        if bar_start_width > 0:
            bar_start_width += decrease_speed
        if bar_start_width <= 0:  # Set it to the minimum width of 1.
            bar_start_width = 1

        if bar_start_width > 400:
            print('> 400')

        gameDisplay.fill(white)
        # Draw powerbar
        pygame.draw.rect(gameDisplay, black, (bar_x, bar_y, bar_start_width, bar_height))

        pygame.display.update()
        clock.tick(60)

game_loop()
pygame.quit()

我重新安排了一些东西,因为游戏逻辑和事件循环混合在一起。你通常应该尝试分离事件处理,游戏逻辑和渲染。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49687690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档