import random,time
WIDTH =800
HEIGHT = 600
balls = []
score = 0
stime = time.perf_counter()
ltime = 60
win = False
lost = False
def update():
if win or lost:
return
if len(balls)< 5:
addball()
updateball()
counttime()
checkgame()
def addball():
ball=Actor("typing_balloon",(random.randint(40,WIDTH-40),HEIGHT-20))
ball.char=chr(random.randint(65,90))
balls.append(ball)
def draw():
screen.fill((255,255,255))
for ball in balls:
ball.draw()
screen.draw.text(ball.char,center=ball.center,color="red")
screen.draw.text("Score:"+str(score),bottomleft=(10,HEIGHT-10),color="black")
screen.draw.text("Time:"+str(ltime),bottomleft=(WIDTH-80,HEIGHT-10),color="black")
if win:
screen.draw.text("You Win!",center=(WIDTH//2,HEIGHT//2),fontsize=50,color="red")
elif lost:
screen.draw.text("You Lost!",center=(WIDTH//2,HEIGHT//2),fontsize=50,color="red")
def updateball():
global score
for ball in balls:
ball.y -= random.randint(1,2)
if ball.bottom <0 :
balls.remove(ball)
score -= 1
def on_key_down(key):
global score
for ball in balls:
if str(key)=="keys."+ball.char:
score += 2
sounds.eat.play()
balls.remove(ball)
def counttime():
global ltime
ptime=int(time.perf_counter()-stime)
ltime=60-ptime
def checkgame():
global win,lost
if score>=100:
win = True
sounds.win.play()
if ltime<=0 or score < -10:
lost = True
sounds.fail.play()