import pygame as pg
from random import randint
pg.init()
mojEkran =pg.display.set_mode([1000,625])
pg.display.set_caption("ROBOT")

def novoMesto():
    randomX = randint(100,990)
    randomY = randint(100,615)
    return(randomX,randomY)

mojaPozadina = pg.image.load('pozadina.png').convert()
mojIgrac = pg.image.load('robot.png').convert()
mojIgrac.set_colorkey((0,0,0))
mojZvuk = pg.mixer.Sound('GameOver.ogg')
pg.time.set_timer(pg.USEREVENT,1000)
time = 60
mojaBuba=novoMesto()
ZLATNA=(255,200,0)
rezultat=0
font = pg.font.SysFont("monospace", 30)
igracX = 500
igracY = 300

def crtaj():
    mojEkran.blit(mojaPozadina,[0,0])
    porukaR = "Rezultat:"+str(rezultat)
    tekstRezultata = font.render(porukaR, True, ZLATNA)
    porukaV = "Vreme:"+str(time)
    tekstVreme = font.render(porukaV, True, ZLATNA)
    mojEkran.blit(tekstRezultata, (5, 5))
    mojEkran.blit(tekstVreme, (450, 5))
    mojEkran.blit(mojIgrac,[igracX-30,igracY-50])
    pg.draw.ellipse(mojEkran,ZLATNA,(mojaBuba[0],mojaBuba[1],10,20))

def obradi_dogadjaj(dogadjaj):
    global time, igracX, igracY
    if dogadjaj.type==pg.KEYDOWN and time>0:
        if dogadjaj.key == pg.K_LEFT:
            igracX-=20
        elif dogadjaj.key == pg.K_RIGHT:
            igracX+=20
        elif dogadjaj.key == pg.K_UP:
            igracY-=20
        elif dogadjaj.key == pg.K_DOWN:
                igracY+=20
    elif dogadjaj.type == pg.USEREVENT:
        if time>0:
            time-=1

def novi_frejm():
    global rezultat, mojaBuba
    if igracX<=(mojaBuba[0]+30) and igracX>=(mojaBuba[0]-30):
        if igracY<=(mojaBuba[1]+50) and igracY>=(mojaBuba[1]-50):
            mojZvuk.play()
            rezultat+=1;
            pg.display.update()
            mojaBuba=novoMesto()    

kraj = False
while not kraj:
    crtaj()
    pg.display.update()
    
    for dogadjaj in pg.event.get():
        if dogadjaj.type ==pg.QUIT:
            kraj=True
        else:
            obradi_dogadjaj(dogadjaj)
    
    novi_frejm()
    pg.display.update()
    
pg.quit()
