flappy_bird.py

Created by danio

Created on April 22, 2020

2.64 KB


from math import *
from kandinsky import *
from ion import *
from time import *
from random import *

def g_over():
  global y
  global t_fin
  sleep(0.1)
  y+=200
  t_fin=monotonic()
  fill_rect(0,0,400,300,blanc)
  draw_string("GAME OVER !",105,100)

def bird():
  global y
  if keydown(KEY_UP):
    y-=40
    collision()
    fill_rect(x,y+40,20,20,blanc)
  y+=10
  fill_rect(x,y,20,20,jaune)
  collision()
  fill_rect(x,y-20,20,20,blanc)

def collision():
  col=(0, 0, 248)
  if get_pixel(x+20,y)==col:
    g_over()
  if get_pixel(x+20,y+20)==col:
    g_over()
  if get_pixel(x,y)==col:
    g_over()
  if get_pixel(x,y+20)==col:
    g_over()

def decor():
  global xd
  global xd2
  if xd<-220:
    xd=320
    hb1()
  if xd2<-220:
    xd2=320
    hb2()
  fill_rect(xd+20,yd1,20,90,blanc)
  fill_rect(xd,yd1,20,90,bleu)
  fill_rect(xd+110,yd2,20,90,blanc)
  fill_rect(xd+90,yd2,20,90,bleu)
  fill_rect(xd+200,yd3,20,90,blanc)
  fill_rect(xd+180,yd3,20,90,bleu)
  xd-=10
  fill_rect(xd2+20,yd4,20,90,blanc)
  fill_rect(xd2,yd4,20,90,bleu)
  fill_rect(xd2+110,yd5,20,90,blanc)
  fill_rect(xd2+90,yd5,20,90,bleu)
  fill_rect(xd2+200,yd6,20,90,blanc)
  fill_rect(xd2+180,yd6,20,90,bleu)
  xd2-=10

def hb1():
  global yd1
  global yd2
  global yd3
  if yd6==0:
    yd1=110
  elif yd6==110:
    yd1=0
  yd2=choice(hb)
  yd3=choice(hb)
  if yd1==0 and yd2==0:
    yd3=110
  elif yd1==110 and yd2==110:
    yd3=0
  else:
    yd3=choice(hb)

def hb2():
  global yd4
  global yd5
  global yd6
  if yd3==0:
    yd4=110
  elif yd3==110:
    yd4=0
  yd5=choice(hb)
  yd6=choice(hb)
  if yd4==0 and yd5==0:
    yd6=110
  elif yd4==110 and yd5==110:
    yd6=0
  else:
    yd6=choice(hb)

def flappy():
  global x
  global y
  global xd
  global xd2
  global yd6
  global best_temps
  x,y=120,100
  xd,xd2=320,590
  yd6=0
  hb1()
  hb2()
  fill_rect(0,200,320,25,marron)
  fill_rect(x,y,20,20,jaune)
  t1=monotonic()
  t_départ=t1
  point=0
  while y>-10 and y<180:
    t=monotonic()
    dt=t-t1
    temps=ceil(t-t_départ)
    draw_string("Time : "+str(temps),10,30)
    draw_string("Best Time : "+str(best_temps),10,10)
    if dt>0.1:
      t1=monotonic()
      decor()
      bird()
      collision()
    fill_rect(0,200,320,25,marron)
  
  g_over()
  temps_final=ceil(t_fin-t_départ)
  if temps_final>best_temps:
    best_temps=temps_final
  draw_string("Time : "+str(temps_final),10,30)
  draw_string("Best Time : "+str(best_temps),10,10)
  draw_string("RETRY ? (OK)",100,130)

jaune=color(255,255,0)
blanc=color(255,255,255)
marron=color(139,69,19)
bleu=color(0,0,255)
hb=[0,110]
jeu=1
best_temps=0
flappy()
while True:
  if keydown(KEY_OK):
    fill_rect(0,0,400,300,blanc)
    flappy()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.