'Pygame flashing scoreboard
I am trying to add a flashing scoreboard, though I am not coming any further than creating colors and then put it in a list. I am assuming I could have problems with doing this while using font. How do I proceed?
import pygame
import sys
WIDTH = (660)
HEIGHT= (500)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
COLOR_LIST = [RED,GREEN,BLUE]
pygame.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Pong Game")
score1 = 0
score2 = 0
WHITE = (255,255,255)
font = pygame.font.Font(None, 100)
text = font.render(str(score1), 1, WHITE)
screen.blit(text, (175, 20))
text = font.render(str(score2), 1, WHITE)
screen.blit(text, (525, 20))
pygame.display.flip()
Solution 1:[1]
You don't have an event-processing loop, so your application will simply exit.
Instead of pygame.display.flip(), you need something to handle the events, then paint the screen. For example:
# ... rest of code
while True:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
# paint the screen
screen.fill( (0,0,0 ) ) # paint it black
screen.blit(text, (175, 20))
screen.blit(text, (525, 20)) # Note: BUG, uses 'text' twice
pygame.display.flip()
One way to implement flashing text, is to use a clock/timer mechanism that periodically changes the colour (or whether to paint) the text in question.
import pygame
import sys
WIDTH = 660
HEIGHT= 500
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
COLOR_LIST = [RED,GREEN,BLUE]
pygame.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Pong Game")
score1 = 0
score2 = 0
WHITE = (255,255,255)
font = pygame.font.Font(None, 100)
text1= font.render(str(score1), 1, WHITE)
text2= font.render(str(score2), 1, WHITE)
flashing_words = "*Flash*"
flashing_text = font.render( flashing_words, 1, WHITE )
next_flash_time = 0 # when is the next colour update
flash_colour = 0 # what is the current flashing colour
while True:
clock = pygame.time.get_ticks() # time now
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
# paint the screen
screen.fill( (0,0,0 ) ) # paint it black
screen.blit(text1, (175, 20))
screen.blit(text2, (525, 20))
screen.blit(flashing_text, ( WIDTH//2, HEIGHT//2 ))
# flashing text
# Is it time to update the flashing text?
if ( clock > next_flash_time ):
# Set the time for the *next* flash
next_flash_time = clock + 1000 # 1 second in the future
# Change the colour
flash_colour += 1
if ( flash_colour >= len( COLOR_LIST ) ):
flash_colour = 0 # loop around colour list
# Re-make the text-bitmap in the new colour
flashing_text = font.render( flashing_words, 1, COLOR_LIST[flash_colour] )
pygame.display.flip()
The way the above code works is to set a time in the future from the current clock. So to do something every second, we make a variable:
time_now = pygame.time.get_ticks()
time_in_future = time_now + MILLISECONDS_DELAY
The get_ticks() returns the time as a count of milliseconds (since program start). So by adding, say 1000, that's a time one second in the future. Now by comparing the variable we made with the current clock, we form a trigger:
time_now = pygame.time.get_ticks()
if ( time_now > time_in_future ):
# Do some stuff
# Reset my time_to_do_in_future to be in the future again
# so this will trigger again in a second.
time_in_future = time_now + MILLISECONDS_DELAY
This is what the code example above is implementing. It makes a time-stamp for 1000 milliseconds in the future. When this time is past, it makes a new time-stamp, and also does some stuff.
In this case the some stuff, is to advance through the colour array, and use a new colour to re-render the text into a new bitmap. This makes it flash.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kingsley |
