0

I'm trying to create a "powerbar." In the following code I can press left & right key to increase the bar, and the prg terminates when the bar_start_width > 400 px. I would like the "powerbar" to decrease while bar_start_width > 0. So, if you stop to press the buttons, the bar will slowly decrease until bar_start_width > 0. With my code it is slowly decreasing bar_start_width, but not updating the display. The while loop also continues running.

Here is full code: import time import random import pygame

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()

I have tried also to think in terms of a for loop to solve my problems but after long time of trying out different things this is the best I can come up with. Can anyone help, or link me to a similar discussion? Thanks!

1 Answers1

1

I'm not sure why you have a nested while loop in the main while loop. If I understand you correctly, you want to decrease the width every frame if (not while) the width is greater than 0. Just remove the while loop and the time.sleep call and decrease the width if bar_start_width > 0:.

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()

I've rearranged a few things, because the game logic and the event loop were mixed. You should generally try to separate the event handling, game logic and the rendering.

skrx
  • 19,980
  • 5
  • 34
  • 48