Computer Science With Python and Pygame

"""
 Show how to put a timer on the screen.
 Sample Python/Pygame Programs
 Simpson College Computer Science
 http://programarcadegames.com/
 http://simpson.edu/computer-science/
"""
import pygame
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
pygame.init()
# Set the height and width of the screen
size = [700, 500]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
font = pygame.font.Font(None, 25)
frame_count = 0
frame_rate = 60
start_time = 90
# -------- Main Program Loop -----------
while not done:
 for event in pygame.event.get(): # User did something
 if event.type == pygame.QUIT: # If user clicked close
 done = True # Flag that we are done so we exit this loop
 # Set the screen background
 screen.fill(WHITE)
 # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
 # --- Timer going up ---
 # Calculate total seconds
 total_seconds = frame_count // frame_rate
 # Divide by 60 to get total minutes
 minutes = total_seconds // 60
 # Use modulus (remainder) to get seconds
 seconds = total_seconds % 60
 # Use python string formatting to format in leading zeros
 output_string = "Time: {0:02}:{1:02}".format(minutes, seconds)
 # Blit to the screen
 text = font.render(output_string, True, BLACK)
 screen.blit(text, [250, 250])
 # --- Timer going down ---
 # --- Timer going up ---
 # Calculate total seconds
 total_seconds = start_time - (frame_count // frame_rate)
 if total_seconds < 0:
 total_seconds = 0
 # Divide by 60 to get total minutes
 minutes = total_seconds // 60
 # Use modulus (remainder) to get seconds
 seconds = total_seconds % 60
 # Use python string formatting to format in leading zeros
 output_string = "Time left: {0:02}:{1:02}".format(minutes, seconds)
 # Blit to the screen
 text = font.render(output_string, True, BLACK)
 screen.blit(text, [250, 280])
 # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
 frame_count += 1
 # Limit frames per second
 clock.tick(frame_rate)
 # Go ahead and update the screen with what we've drawn.
 pygame.display.flip()
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()

Copyright © 2017
English version by Paul Vincent Craven
Spanish version by Antonio Rodríguez Verdugo
Russian version by Vladimir Slav
Turkish version by Güray Yildirim
Portuguese version by Armando Marques Sobrinho and Tati Carvalho
Dutch version by Frank Waegeman
Hungarian version by Nagy Attila
Finnish version by Jouko Järvenpää
French version by Franco Rossi
Korean version by Kim Zeung-Il
Chinese version by Kai Lin

AltStyle によって変換されたページ (->オリジナル) /