import time as t
import random as r
from gint import *
from gintpp import *

play = False
stop = False
speed = 3

def credits():
  pass
  
def shop():
  pass
  
def settings():
  pass

class Bird:
  def __init__(self):
    self.x = 5
    self.y = 32
    self.score = 0
    self.jump_key = KEY_SHIFT
    self.high_score = 0
    self.state = "wait"
    self.flap_c = 0
    self.fall_c = 0
    self.fly_time = 6
    self.img = ["   ####   ",
                " ## #  #  ",
                "# # #   # ",
                "#  # #### ",
                " ### #   #",
                "  #   ### ",
                "   ###    "]
  
  def flap(self,pressed):
    self.state = "flap"
    self.fall_c = 0
    if pressed:
      self.flap_c = self.fly_time
    self.flap_c -= 1
    self.y -= self.flap_c
  
  def fall(self):
    self.state = "fall"
    self.fall_c += 1
    self.y += self.fall_c
  
  def draw(self):
    dlimage(self.x, self.y, self.img, "T")
    
player = Bird()
    
class Pipe:
  def __init__(self,start):
    self.size = 5
    self.score = True
    if start:
      self.x=81
    self.x2h=self.x
    self.x1h=self.x2h-self.size
    self.y2h=r.randint(10,25)
    self.y1h=self.y2h-self.size
    
    self.x2l=self.x2h
    self.x1l=self.x1h
    self.y1l=self.y2h+r.randint(25,30)
    self.y2l=self.y1l+self.size
    
  def draw(self):
    self.x2h=self.x
    self.x1h=self.x2h-self.size
    self.x2l=self.x2h
    self.x1l=self.x1h
    
    if(self.x <= int(0+self.size/2)):
      self.__init__(False)
      self.x = 128
      player.score += 1
      
    drect_border(self.x1h,self.y1h,self.x2h,self.y2h,C_WHITE,1,C_BLACK)
    drect_border(self.x1l,self.y1l,self.x2l,self.y2l,C_WHITE,1,C_BLACK)
    drect_border(self.x1h+1,01,self.x2h-1,self.y1h,C_WHITE,1,C_BLACK)
    drect_border(self.x1l+1,63,self.x2l-1,self.y2l,C_WHITE,1,C_BLACK)
    
p1=Pipe(True)
p2=Pipe(True)
p3=Pipe(True)

while not(stop):
  if not(play):
    choice = dmenu("Flappy Bird", ["play","credits","shop","settings","quit"], "2.0")
    if choice==1: play,stop = True,False
    elif choice==2: credits()
    elif choice==3: shop()
    elif choice==4: settings()
    elif choice==5: stop,play = True,False
  
  player.score = 0
  player.flap_c=player.fly_time
  player.fall_c=0
  player.x = 5
  player.y = 32
  p1.__init__(True)
  p2.__init__(True)
  p2.x = 121
  p3.__init__(True)
  p3.x = 121
  p3_move = False
  while play:
    dclear(C_WHITE)
    event = pollevent()
    if (event.type == KEYEV_NONE) and (player.flap_c == 0):
      player.fall()
    elif (event.type == KEYEV_DOWN) and (event.key == player.jump_key):
      player.flap(True)
    else:
      player.flap(False)
    
    if(player.y > 58) or (player.y < 0):
      play = False
    elif (((player.x+len(player.img[0]) >= p1.x1h)and(player.x+len(player.img[0]) <= p1.x2h)) and ((player.y+len(player.img) >= p1.y1l) or (player.y <= p1.y2h))):
      play = False
    elif (((player.x+len(player.img[0]) >= p2.x1h)and(player.x+len(player.img[0]) <= p2.x2h)) and ((player.y+len(player.img) >= p2.y1l) or (player.y <= p2.y2h))):
      play = False
    elif (((player.x+len(player.img[0]) >= p3.x1h)and(player.x+len(player.img[0]) <= p3.x2h)) and ((player.y+len(player.img) >= p3.y1l) or (player.y <= p3.y2h))):
      play = False
    
    p1.x -= speed
    p2.x -= speed
    p1.draw()
    p2.draw()
    if p3_move:  
      p3.x -= speed
      p3.draw()
    elif (p2.x <= 80):
      p3_move = True
    player.draw()
    dupdate()
    t.sleep(0.055)
  choice = dmenu("score: "+str(player.score), ["play again","menu","quit"], "tuper")
  if choice==1: play = True
  elif choice==3: stop = True

print("")
print("bye o/")
print("")