flappy_jules.py

Created by elnix90

Created on June 03, 2023

8.2 KB

Le jeu Flappy Bird, merci de me prévenir en cas de bugs sur discord: Elnix90#0116


# Modules
from kandinsky import fill_rect as rect,draw_string as ds
from ion import keydown as kd
from time import sleep,monotonic
from random import randint

###########################################

# Colors:
SKY    = (0,0,0) # Black
PILLAR = (0,255,0) # Green
DEAD   = (255,0,0) # Red
BIRD   = (0,100,255) # Cyan
LINE   = (0,255,255) # Grey
SHIELD = (255,255,0) # yellow
TEXT   = (255,255,255) # White

###########################################

def draw_bird(x,y,col):
  if col!=SKY:
    rect(x-10,y-10,20,20,LINE)
    rect(x-8,y-8,16,16,col)
    rect(x,y-5,5,5,LINE)
  else:rect(x-10,y-10,20,20,SKY)

###########################################

def draw_arrow(x,y,type,col):
  if type==0:
    rect(x-10,y-6,12,12,col)
    rect(x-2,y+2,8,8,col)
    rect(x+6,y+10,4,4,col)
    rect(x-10,y-6,8,4,SKY)
    rect(x-10,y-2,4,4,SKY)
  elif type==1:
    rect(x-10,y+6,12,-12,col)
    rect(x-2,y-2,8,-8,col)
    rect(x+6,y-10,4,-4,col)
    rect(x-10,y+2,8,4,SKY)
    rect(x-10,y-2,4,4 ,SKY)
  elif type==2:
    rect(x-10,y-8,6,16,col)
    rect(x-5,y-4,6,8,col)
    rect(x+1,y-2,8,4,col)

###########################################

def draw_jet(x,y,boost,col):
  if col!=SKY:
    draw_bird(x,y,col)
    rect(x-13,y-5,10,17,LINE)
    rect(x-11,y-3,6,boost,DEAD)
  else:
    rect(x-10,y-10,20,20,SKY)
    rect(x-13,y-5,10,boost+2,SKY)

###########################################


def draw_pillar(x,y,espace,type):
  if type:
    rect(x,0,50,y-espace//2,LINE)
    rect(x+2,0,46,y-espace//2-2,PILLAR)
    rect(x,y+espace//2,50,202-(y+espace//2),LINE)
    rect(x+2,y+espace//2+2,46,198-(y+espace//2-2),PILLAR)
  else:
    rect(x,0,50,y-espace//2,SKY)
    rect(x,y+espace//2,50,202-(y+espace//2),SKY)

###########################################

def draw_trail(list_Y,col):
  if len(list_Y)>1:
    for i in range(len(list_Y)-1):rect(X-10-4*i,list_Y[len(list_Y)-i-2]-2,4,4,col)

###########################################

def menu():
  rect(0,0,320,202,SKY)
  while (kd(4) or kd(52)):1
  X,Y        = 10,50
  col_titre  = [0,0,0]
  choice_col = 1
  first_iter = 1
  option     = 0
  options    = [
[["Choice mode:"],["   ","   ","   "],[0]],
[["Invincibility:"],["NO","YES"],[0]],
[["Change mode:"],["NO","YES"],[0]],
[["Auto mode:"],["NO","YES"],[0]]
]
  ds("PRESS [OK] OR [EXE] TO PLAY",25,35,BIRD,SKY)
  for i in range(len(options)):
    for j in range(len(options[i][1])):
      if j>0:z=len(options[i][1][j-1])*10+10
      else:z=0
      ds(options[i][1][j],X+len(options[i][0][0])*10+10+z,Y+30*(i+1),PILLAR,SKY)
  draw_bird(155,90,BIRD)
  draw_arrow(195,90,1,BIRD)
  draw_jet(245,90,20,BIRD)
  while not (kd(4) or kd(52)):
    if col_titre[choice_col]>=255:choice_col=randint(0,2)
    col_titre[choice_col]+=10
    rect(95,0,130,2,col_titre)
    rect(95,0,2,27,col_titre)
    rect(225,27,-130,-2,col_titre)
    rect(225,27,-2,-27,col_titre)
    ds("FLAPPY JULES",100,5,col_titre,SKY)
    if kd(0) or kd(1) or kd(2) or kd(3) or first_iter:
      if first_iter:first_iter=0
      if kd(0) or kd(3):
        ds("[",X+10*len(options[option][0][0])+10*len(options[option][1][:options[option][2][0]])+10*(len(" ".join(options[option][1][:options[option][2][0]]))),Y+30*(option+1),SKY,SKY)
        ds("]",X+10*len(options[option][0][0])+10*len(options[option][1][:options[option][2][0]])+10*(len(" ".join(options[option][1][:options[option][2][0]])))+10*(1+len(options[option][1][options[option][2][0]])),Y+30*(option+1),SKY,SKY)
      else:
        for i in range(len(options)):ds(options[i][0][0],X,Y+30*(i+1),PILLAR,SKY)
      if kd(1):option=(option-1)%len(options)
      elif kd(2):option=(option+1)%len(options)
      elif kd(0):options[option][2][0]=(options[option][2][0]-1)%len(options[option][1])
      elif kd(3):options[option][2][0]=(options[option][2][0]+1)%len(options[option][1])
      while kd(0) or kd(1) or kd(2) or kd(3):1
      ds(options[option][0][0],X,Y+30*(option+1),TEXT,SKY)
      for i in range(len(options)):
        ds("[",X+10*len(options[i][0][0])+10*len(options[i][1][:options[i][2][0]])+10*(len(" ".join(options[i][1][:options[i][2][0]]))),Y+30*(i+1),TEXT,SKY)
        ds("]",X+10*len(options[i][0][0])+10*len(options[i][1][:options[i][2][0]])+10*(len(" ".join(options[i][1][:options[i][2][0]])))+10*(1+len(options[i][1][options[i][2][0]])),Y+30*(i+1),TEXT,SKY)
  return options[0][2][0],options[1][2][0],options[2][2][0],options[3][2][0]

###########################################

score,best_s=0,0

rect(0,0,320,222,SKY)

# NEW GAME LOOP ###########################
while 1:
  mode,inv,changemode,auto=menu()

# Init screen
  rect(0,0,320,222,SKY)
  rect(0,202,320,2,LINE)
  ds("SCORE: "+str(score),0,205,TEXT,SKY)
  ds("BEST SCORE: "+str(best_s),150,205,TEXT,SKY)
  ds(str(score),70,205,SKY,SKY)

# Variables
  score     = 0
  jump      = 0
  sens      = 0
  press     = 0
  paused    = 0
  press_sh  = 0
  press_ch  = 0
  press_au  = 0
  shield_on = 0
  auto_on   = 0
  fire      = 1
  boost     = 0
  T         = 0.04
  Ec        = 1
  n         = 1
  speed     = 4
  pixels    = 5
  ecart_max = 30
  X         = 50
  Y         = 100
  espace    = 120
  pillars   = [[320,101,120]]
  list_Y    = []

# GAME LOOP ###############################
  while 1:
    frame=monotonic()

# Erase sprites
    if mode==0:draw_bird(X+4,Y,SKY)
    elif mode==1:draw_arrow(X,Y,sens,SKY)
    elif mode==2:draw_jet(X,Y,boost,SKY)
    draw_trail(list_Y,SKY)
    for i in range(len(pillars)):draw_pillar(pillars[i][0]+50-pixels,pillars[i][1],pillars[i][2],0)

# Shield system
    if not kd(24):press_sh=1
    if inv and kd(24) and press_sh:press_sh,shield_on=0,shield_on+1
    shield_on%=2

# Change mode system
    if not kd(25):press_ch=1
    if changemode and kd(25) and press_ch:press_ch,mode=0,mode+1
    mode%=3

# Auto system
    if not kd(26):press_au=1
    if auto and kd(26) and press_au:press_au,auto_on=0,auto_on+1
    auto_on%=2
    if pillars[0][0]>X-45:pillar=0
    else:pillar=1

# Bird calculating system
    if mode==0:
      if (auto_on and Y>pillars[pillar][1]+15) or ((kd(4) or kd(52)) and press==0):jump,press,Ec=16,1,1
      if not (kd(4) or kd(52)):press=0
      if jump>0:Y-=jump
      jump,Y=jump-jump//9,Y+int(speed+Ec)
      Ec+=1
# Arrow calculatig system
    elif mode==1:
      if (auto_on and Y>pillars[pillar][1]) or (kd(4) or kd(52)):Y,sens=Y-speed,1
      else:Y,sens=Y+speed,0
# Jet calculating system
    elif mode==2:
      if (auto_on and Y>pillars[pillar][1])or (kd(4) or kd(52)) and fire>-10:fire-=1
      elif fire<10:fire+=1
#      if fire>=pillars[i][1]+1:n=-1
#      else:n=1
#      if n==-1:fire-=1
#      else:fire+1
      Y+=fire
      boost=randint(17,25)
    if Y<=10:Y,jump,sens,fire=10,0,2,0
    elif Y>=192:Y,jump,sens,fire=192,0,2,0

# Trail calculating system
    if len(list_Y)>(X-10)//4+4:list_Y.pop(0)
    list_Y.append(Y)

# Pillars calculating system
    if pillars[-1][0]<200:
      new_pillar=[320]
      y=randint(pillars[-1][1]-ecart_max,pillars[-1][1]+ecart_max)
      if y+espace//2>182:y=182-espace//2
      elif y-espace//2<20:y=20+espace//2
      new_pillar.append(y)
      new_pillar.append(espace)
      pillars.append(new_pillar)
    if pillars[0][0]<-50:pillars.pop(0)
    for i in range(len(pillars)):pillars[i][0]-=pixels
    if ecart_max<80:ecart_max=10+score//10
    if espace>0:espace=120-score//10

# Drawing objects and score
    if shield_on:COLOR=SHIELD
    elif (not shield_on) and (pillars[0][1]-espace//2>Y-5 or pillars[0][1]+espace//2<Y+5) and (pillars[0][0]>=X-45 and pillars[0][0]<=X):COLOR=DEAD
    else:COLOR=BIRD
    if mode==0:draw_bird(X+4,Y,COLOR)
    elif mode==1:draw_arrow(X,Y,sens,COLOR)
    elif mode==2:draw_jet(X,Y,boost,COLOR)
    draw_trail(list_Y,COLOR)
    if COLOR==DEAD:
      sleep(1)
      break
    for i in range(len(pillars)):draw_pillar(pillars[i][0],pillars[i][1],pillars[i][2],1)
    ds(str(score),70,205,TEXT,SKY)
    ds(str(best_s),270,205,TEXT,SKY)

# Game pause system
    if kd(17):
      ds("[ GAME PAUSED ]",80,100,TEXT,SKY)
      ds("GO TO MENU: [OK] OR [EXE]",35,130,BIRD,SKY)
      while kd(17):1
      while not kd(17):
        if kd(4):break
      if kd(4):break
      while kd(17):1
      rect(35,100,250,50,SKY)

# Scoreboard calculating system
    if pillars[0][0]==-10:score+=1
    if best_s<=score:best_s=score

# Frame and movement system
    while monotonic()-frame<T:1
    if T>=0.01:T=0.04-0.0005*(score//10)
#    sleep(1)

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.