import turtle  #turtle-kirjasto
import random  #satunnaisluku-kirjasto

screenx = 10  #ruudun x-akselin suuntainen alkuleveys
screeny = 10  #ruudun y-akselin suuntainen alkukorkeus


px=0    #pelaajan alkukoordinaatti x-akselin suhteen
py=-250 #pelaajan alkukoordinaatti y-akselin suhteen

x=0     #pallon alkukoordinaatti x-akselin suhteen
y=-200  #pallon alkukoordinaatti y-akselin suhteen

dx=random.randint(3,8)
dy=random.randint(5,8)


wn = turtle.Screen()    #luodaan ikkuna-objekti
wn.title("Brick Breaker") #ikkunassa näkyvä otsikko
wn.bgcolor("black")       #asetetaan ikkunan taustaväriksi musta
wn.setup(screenx, screeny)  #asetetaan ikkunan koko
wn.tracer(2)

tiles1=[]
tiles2=[]
tiles3=[]

tilesholder=[tiles1, tiles2, tiles3]

for tiles in tilesholder:
    for i in range(23):
        tiles.append(turtle.Turtle())

posx=-28
posy=150

for tiles in tilesholder:
    posx=-28
    for tile in tiles:
        tile.speed(0)
        tile.color("red")
        tile.penup()
        tile.shape("square")
        tile.setpos(posx*10, posy)
        posx += 2.5
    posy += 50


player = turtle.Turtle()  #luodaan pelaaja-objekti
player.penup()
player.speed(0)
player.color("blue")
player.shape("square")
player.shapesize(0.5,5,5)
player.setpos(px,py)

ball = turtle.Turtle()
ball.penup()
ball.speed(0)
ball.color("yellow")
ball.shape("circle")
ball.setpos(x,y)

score = 0

scorer = turtle.Turtle()
scorer.penup()
scorer.speed(0)
scorer.color("white")
scorer.setpos(-240,260)
scorer.ht() #pillottaa osoittimen
scorer.write("SCORE:{}".format(score), move= False, align="center", font=("Fixedsys",20,"normal"))

live=5

lives = turtle.Turtle()
lives.penup()
lives.speed(0)
lives.color("white")
lives.setpos(230,260)
lives.ht()
lives.write("LIVES:{}".format(live), move=False, align="center", font=("Fixedsys",20,"normal"))


def right():
    px = player.xcor()
    px += 50 
    if px > 240:
        px = 240
    player.setpos(px,py)

def left():
    px=player.xcor()
    px -= 50
    if px < -250:
        px = -250
    player.setpos(px,py)

wn.listen()
wn.onkeypress(left, "Left")
wn.onkeypress(right,"Right")


dscreenx = 20  #muuttuja, jonka arvon mukaisesti kasvatetaan ikkunaa x-akselin suuntaan
dscreeny = 20  #muuttuja, jonka arvon mukaisesti kasvatetaan ikkunaa y-akselin suuntaan



while True:
    screenx += dscreenx                 #suoritetaan "animaatio"
    screeny += dscreeny

    if screenx > 600 and screeny > 600:
        dscreenx = 0
        dscreeny = 0

    if ball.xcor() > 280 or ball.xcor() < -280:
        dx=-dx
    if ball.ycor() > 280:
        dy=-dy
    if y < -280:
        x = 0
        y = 0
        dx=random.randint(3,8)
        dy=random.randint(5,8)
        ball.setpos(x,y)
        live -= 1
        lives.clear()
        lives.write("LIVES:{}".format(live), move=False, align="center", font=("Fixedsys",20,"normal"))
        if live <=0:break


    if ball.xcor() < player.xcor()+70 and ball.xcor() > player.xcor()-70 and ball.ycor() <= player.ycor() + 10:
        dy=-dy

    for tiles in tilesholder:
        for tile in tiles:
            if tile.isvisible():
                if ball.xcor() > tile.xcor()-20 and ball.xcor() < tile.xcor()+20 and ball.ycor() < tile.ycor()+20 and ball.ycor() > tile.ycor()-20:
                    tile.hideturtle()
                    score += 1
                    scorer.clear()
                    scorer.write("SCORE:{}".format(score), move= False, align="center", font=("Fixedsys",20,"normal"))
                    dy=-random.randint(5,8)
            
    x=ball.xcor()
    x+=dx
    y=ball.ycor()
    y += dy
    ball.setpos(int(x), int(y))
    wn.setup(screenx, screeny)
    wn.update()






















