0

Hi there and thank you for your time reading this.

This is related to the project I'm making. It's a reaction test game. Basically, Arcade buttons with LEDs light up, and players need to press them to score points. Whack-a-Mole style.

The game is timed, 30 seconds. But instead of using a numerical timer, I'm using a progress bar moving across the screen to indicate the remaining time instead. image below: game screenshot

I added this as an image blit in the code, but it doesn't refresh correctly. You can see it only at the start of the game, and when players press a button.

It needs to be constantly blitting across the screen.

Maybe I'm just missing something or my indentation is wrong? Would really appreciate any inputs on this.

Main game loop code is below:

# MAIN GAME SEQUENCE CODE ---------------------------------------------------------------------------------------------
# Setup Pygame refresh rate to 120 fps
clock.tick(60)
while game_running:
# Idle Screen
    idle_scrn()

# Instructions
    inst_seq()

# GAME PLAY SECTION --------
    score = 0
    time_Out = 0
    leds.off()
    start_Time = pygame.time.Clock()
    start = int(time.time())
    time_move = -500
    random_num = random.randint(0, 11)
    leds.on(random_num)
    print(random_num)
    while time_Out != 30:
        screen.blit(Timebar, (time_move,1000))
        time_move += 50
        pygame.display.update()
        for event in pygame.event.get():
            print("For Event started, Game screen started")
            screen.fill((255,255,255))
            screen.blit(Game_screen[6], (0,0))
            score_textback = score_fontback.render("00", 3, (199,199,199))
            score_text = score_font.render(str(score), 3, (255,37,24))
            ctr = 1110
            if score >= 10:
                ctr -= 155
            center_pos = (ctr, 580)
            score_rect = score_text.get_rect(center = center_pos)
            screen.blit(score_textback, (645, 390))
            screen.blit(score_text, score_rect)
            if event.type == pygame.JOYBUTTONDOWN:
                print("If for event JOYBUTTONDOWN")
                if event.button == random_num:
                    B = event.button
                    print(B, " button pressed, CORRECT")
                    leds.off()
                    butt_Sound.play()
                    random_num = random.randint(0, 11)
                    leds.on(random_num)
                    score += 1
                    pygame.display.update()
        current_Time = int(time.time())
        time_Out = current_Time - start
        #pygame.display.update()
        #print("TIMER: ", time_Out)
        #print(type(start_Time))
        #print(current_Time)
        #print(type(current_Time))
    #pygame.display.update()
    # FINAL SCORE ------------------------
    final_Score()
exit()

The image is Timebar. in the code, we move it 50 pixels across the screen in every refresh.

Thank you in advance.

  • Please consider creating a [mcve] so it's easier to help you with your problem. You should reorganise your code so there's only one call to `pygame.display.update()` per loop. – import random Jul 19 '21 at 13:11

1 Answers1

0

Here's a minimal example, perhaps you'll find it useful:

import pygame
pygame.init()
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
fps = 60
clock = pygame.time.Clock()
progress_width = 0  # initialse progress tracker
total_time = 0
target_time = 10_000  # ten seconds in milliseconds
finished = False
while not finished:
    # Handle Events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            finished = True
        elif event.type == pygame.KEYDOWN:
            progress_width = 0  # reset progress
            total_time = 0
    # Update Game State
    dt = clock.tick(fps)  # limit FPS and return time since last call
    total_time += dt
    progress_width += width * dt / target_time  # scale bar width
    progress_rect = pygame.Rect(0, height-20, int(progress_width), 20)
    # Draw Background
    screen.fill(pygame.Color("black")) 
    # Draw Sprites
    pygame.draw.rect(screen, pygame.Color("red"), progress_rect)
    # Update Display
    pygame.display.flip()
    # Display the elapsed time in the title bar
    pygame.display.set_caption(f"Simple Progress {total_time:,d} ms")  
pygame.quit()

It calculates the time elapsed and updates the progress bar width. It takes ten seconds to fill the screen width and pressing a key resets the progress.

Your main loop should look something like:

  • Handle Events
  • Update Game State
  • Draw sprites
  • Update Screen

If you're trying to update the screen or handle events in multiple places, things get confusing pretty quickly.

import random
  • 3,054
  • 1
  • 17
  • 22