Page 133 - E-Modul Coding For Kids Berbasis Raspberry Pi
P. 133
sebelumnya. Dengan membagi tugas ke dalam fungsi yang berbeda, Anda dapat membuat
perubahan tersebut dengan cepat dan mudah.
import pygame, pygame.event, pygame.key
from pygame.locals import *
import random
import time
options = {K_UP:"up", K_DOWN:"down", K_LEFT:"left",
K_RIGHT:"right"}
def prepare():
pygame.init()
screen = pygame.display.set_mode((250, 1))
pygame.display.set_caption("Test your speed!")
def nexttarget():
target = random.choice(options.keys())
print options[target]
return target
def keypressed():
pygame.event.pump()
keyspressed = pygame.key.get_pressed()
for key in options.keys():
if keyspressed[key]:
return key
else:
return None
def play(duration):
prepare()
start = time.time()
end = start + duration
score = 0
target = nexttarget()
122