fromkandinskyimportfill_rectaskrect,draw_stringastextfromionimportkeydownfromrandomimportrandintfromtimeimport*# Code by Fime, nsi.xyz/jetpackbird
SCREEN_W=320SCREEN_H=222GAME_W=200SCORE_OFFSET=5SCORE_W=100GAME_H=200OFFSET_X=(SCREEN_W-SCORE_W-GAME_W-SCORE_OFFSET)//2# game offset
OFFSET_Y=(SCREEN_H-GAME_H)//2KEY={"ok":4}COLOR={"bg":(50,200,255),"bird":(255,200,0),"hurt":(255,50,0),"jp":(150,150,150),"mouth":(255,100,0),"pipe":(100,255,20),"line":(0,0,0),"font1":(60,60,60),"font2":(255,255,255)}LINE_W=2#line width
TARG_FPS=20TARG_SPF=1/TARG_FPS# targ second per frame
defsec(time):returnmonotonic()-timedefrect(x,y,h_s,v_s,c):krect(int(x),int(y),int(h_s),int(v_s),COLOR["line"])ifh_s>2*LINE_W:krect(int(x)+LINE_W,int(y)+LINE_W,int(h_s)-2*LINE_W,int(v_s)-2*LINE_W,c)defdrawBird(x,y,s):ifsec(hurt_time)>1or(sec(hurt_time)*5%2>0.5):c=COLOR["bird"]else:c=COLOR["hurt"]rect(x,y,s,s,c)rect(x-s/2,y+s/8,s/2,s/2,COLOR["jp"])rect(x+s/2,y+s/2,s/1.5,s/3,COLOR["mouth"])rect(x+s/4,y+s/5,s/3,s/3,COLOR["font2"])o=s/2ifflapandint(sec(flap_time)*16)%2==0elses/4rect(x-s/4,y+o,s/2,s/2,c)ifflap:s2=min(s/2+o*2,OFFSET_Y+GAME_H-y-s/8*6)rect(x-s/2,y+s/8*5,s/2,s2,COLOR["mouth"])krect(int(x-s/2+LINE_W*2),int(y+s/8*5.5),int(s/2-LINE_W*4),int(s2/2),COLOR["font2"])defdrawPipe(i):pipe=pipes[i]x,y=min(max(pipe[0],OFFSET_X),OFFSET_X+GAME_W),pipe[1]ifx<=OFFSET_X:h_s=pipe[2]-(x-pipe[0])elifx>=OFFSET_X+GAME_W-pipe[2]:h_s=(GAME_W+OFFSET_X)-xelse:h_s=pipe[2]v_s=pipe[3]rect(x,y,h_s,v_s,COLOR["pipe"])defdrawHeart(x,y,s,c):heart=("01100","11110","01111","11110","01100")forx2inrange(len(heart)):fory2inrange(len(heart[x2])):ifint(heart[x2][y2]):krect(x+x2*s,y+y2*s,s,s,c)definitScreen():rect(OFFSET_X,OFFSET_Y,GAME_W,GAME_H,COLOR["bg"])drawScorePannel()actualizeScore()actualizeLife()defclearScreen():krect(OFFSET_X+LINE_W,OFFSET_Y+LINE_W,GAME_W-2*LINE_W,GAME_H-2*LINE_W,COLOR["bg"])defcreatePipe(x,y,h_s,v_s):#h_s : horizontal size
#v_s : vertical size
pipe=[x,y,h_s,v_s]pipes.append(pipe)defaddPipes(x,s):space_size=GAME_H//2-difficulty*2space_y=randint(OFFSET_Y,OFFSET_Y+GAME_H-space_size-20)createPipe(x,OFFSET_Y,s,space_y)createPipe(x,OFFSET_Y+space_size+space_y,s,GAME_H-(space_size+space_y))defhitBox(p1,p2):x1=p1[0]y1=p1[1]hs1=p1[2]vs1=p1[3]x2=p2[0]y2=p2[1]hs2=p2[2]vs2=p2[3]ifx1<=x2+hs2andx1+hs1>=x2andy1<=y2+vs2andy1+vs1>=y2:returnTrueelse:returnFalsedefgameEngine():globalbird,pipes,score,best_score,difficulty,flap,total_time,flap_time,fps,hurt_time,lifeprint(">initialisation: dead...")difficulty=1life=3score=0best_score=readBestScore()pipes=[]pipe_size=50flap=0jump_speed=4max_speed=15fps=TARG_FPSbird={"x":20,"y":OFFSET_Y+GAME_H/2,"spd_x":5,"spd_y":-5,"size":20,"color":COLOR["bird"]}addPipes(GAME_W+OFFSET_X,pipe_size)addPipes(GAME_W/2-pipe_size/2+OFFSET_X,pipe_size)initScreen()spf=TARG_SPFtotal_time=monotonic()flap_time=monotonic()hurt_time=monotonic()-1whilenot(life<1andsec(hurt_time)>0.5):ifsec(spf)>=TARG_SPF:#GAME DATA
fps=int(1/sec(spf))#print(fps)
spf=monotonic()#PHYSICS
limits_y=[OFFSET_Y,OFFSET_Y+GAME_H]bird["spd_y"]+=jump_speed/3bird["spd_y"]=max(-max_speed,min(max_speed,bird["spd_y"]))bird["y"]+=bird["spd_y"]ifbird["y"]<=limits_y[0]:bird["y"]=limits_y[0]bird["spd_y"]=0ifbird["y"]>=limits_y[1]-bird["size"]:bird["y"]=limits_y[1]-bird["size"]bird["spd_y"]=0ifkeydown(KEY["ok"]):bird["spd_y"]-=abs(-max_speed-bird["spd_y"])/max_speed*jump_speedifflap==0:flap_time=monotonic()flap=1else:flap=0remove_pipe=[]last_speed=int(bird["spd_x"])foriinpipes:i[0]-=last_speedifhitBox((bird["x"],bird["y"],bird["size"],bird["size"]),i):ifsec(hurt_time)>1:iflife>0:hurt_time=monotonic()life-=1difficulty+=randint(1,3)actualizeLife()print(">{}s:hurt! life:{}".format(int(sec(total_time)),life))ifi[0]+i[2]<=OFFSET_X:ifi[1]!=OFFSET_Y:bird["spd_x"]+=abs(max_speed-bird["spd_x"])/(4*max_speed)score+=1best_score=max(score,best_score)actualizeScore()addPipes(GAME_W+OFFSET_X,pipe_size)remove_pipe.append(i)foriinremove_pipe:pipes.remove(i)#DISPLAY
clearScreen()foriinrange(len(pipes)):drawPipe(i)drawBird(bird["x"],bird["y"],bird["size"])print(">game end: dead...")print(">fps : {}".format(fps))transition()saveScore(best_score)gameEngine()defactualizeScore():x=OFFSET_X+GAME_W+SCORE_OFFSET+SCORE_W//2y=OFFSET_Yc=COLOR["font2"]text(str(score),x-len(str(score))*5,y+40,c,COLOR["bird"])text(str(best_score),x-len(str(best_score))*5,y+100,c,COLOR["bird"])defactualizeLife():x=OFFSET_X+GAME_W+SCORE_OFFSET+5y=OFFSET_Y+150foriinrange(3):ifi>=life:c=COLOR["line"]else:c=COLOR["hurt"]drawHeart(x+i*30,y,5,c)defdrawScorePannel():x=OFFSET_X+GAME_W+SCORE_OFFSETy=OFFSET_Yrect(x,y,SCORE_W,GAME_H,COLOR["bird"])x=OFFSET_X+GAME_W+SCORE_W//2y=OFFSET_Ytext("SCORE",x-len("SCORE")*5,y+10,COLOR["font1"],COLOR["bird"])text("BEST",x-len("BEST")*5,y+70,COLOR["font1"],COLOR["bird"])defreadBestScore():try:file=open("jp_bird.sav","r")best=file.readline()file.close()print(">score loaded !")returnint(best)except:print(">failed to read the score...")return0defsaveScore(score):try:file=open("jp_bird.sav","w")file.truncate(0)file.write(str(score))file.close()print(">score saved !")except:print(">failed to save the score...")deftransition():forcin[COLOR["font1"],COLOR["bg"]]:foryinrange(OFFSET_Y,OFFSET_Y+GAME_H,10):sleep(0.016)krect(OFFSET_X,y,GAME_W,10,c)gameEngine()
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
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.