Crazy version of jetpack bird ( https://my.numworks.com/python/fime/jetpack_bird ). (not crazy good, simply random)
from cmath import * from kandinsky import * from kandinsky import fill_rect as krect, draw_string as text from ion import keydown from random import randint from time import * mode="" print("mode (day or night)") print("write '1' or '2'") while not (mode=="1" or mode=="2"): mode=input() if mode=="1": mode="day" submode="" print("submode (full or basic)") print("write '1' or '2'") while not (submode=="1" or submode=="2"): submode=input() if submode=="1": submode="full" halo=None if mode!="day": print("halo ?") print("write 'y' or 'n'") while not (halo=="y" or halo=="n"):halo=input() if halo=="n": halo=False if mode=="day": if submode=="full": SCREEN_W=340 #320 SCREEN_H=222 #222 GAME_W=230 #200 SCORE_OFFSET=0 #5 SCORE_W=100 #100 GAME_H=222 #200 else: SCREEN_W=320 SCREEN_H=222 GAME_W=200 SCORE_OFFSET=5 SCORE_W=100 GAME_H=200 else: if submode=="full": SCREEN_W=340 SCREEN_H=222 GAME_W=230 SCORE_OFFSET=0 SCORE_W=120 GAME_H=222 else: SCREEN_W=330 SCREEN_H=222 GAME_W=200 SCORE_OFFSET=0 SCORE_W=90 GAME_H=200 OFFSET_X=(SCREEN_W-SCORE_W-GAME_W-SCORE_OFFSET)//2 OFFSET_Y=(SCREEN_H-GAME_H)//2 RADIUS=GAME_H if GAME_H<GAME_W else GAME_W KEY={"ok":4} starOffsetY=0 starOffsetX=0 best_score=0 def setColor(): global COLOR if mode=="day": 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)} else: COLOR={ "bg":(50,50,50), "bird":(0,0,0), "hurt":(255,50,0), "mouth":(255,100,0), "pipe":(50,127,10), "line":(0,0,0), "font1":(155,155,155), "font2":(255,255,255)} setColor() def setLineW(): global LINE_W if mode=="day": LINE_W=2 else: LINE_W=10 setLineW() TARG_FPS=20 TARG_SPF=1/TARG_FPS def sec(time): return monotonic()-time def rect(x,y,h_s,v_s,c): if mode=="day": krect(int(x),int(y),int(h_s),int(v_s),COLOR["line"]) if h_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) else: krect(int(x),int(y),int(h_s),int(v_s),COLOR["line"]) krect(int(x)+LINE_W,int(y)+LINE_W,int(h_s)-2*LINE_W,int(v_s)-2*LINE_W,c) def drawHalo(x,y,s): col1=COLOR["bg"][0]+100 col2=COLOR["bg"][1]+100 col3=COLOR["bg"][2]+100 for i in (9,8,7,6,5,4): e=100/9*i d=i*3 f=s+d*2 col=[col1-e,col2-e,col3-e] krect(int(x)-d-s//2,int(y)-d+s//2,f,f,col) def drawBird(x,y,s): if sec(hurt_time)>1 or (sec(hurt_time)*5%2>0.5): c=COLOR["bird"] else: c=COLOR["hurt"] rect(x,y,s,s,c) if mode=="day": 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"]) else: rect(x-s/2,y+s/8,s/2,s/2,c) rect(x+s/2,y+s/2,s/1.5,s/3,c) rect(x+s/4,y+s/5,s/3,s/3,c) o=s/2 if flap and int(sec(flap_time)*16)%2==0 else s/4 rect(x-s/4,y+o,s/2,s/2,c) if flap: if mode=="day": 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"]) else: s2=min(s/2+o*2,OFFSET_Y+GAME_H-y-s/8*6) krect(int(x-s/2),int(y+s/8*5),int(s/2),int(s2-5),COLOR["line"]) if s/2>4:krect(int(x-s/2)+2,int(y+s/8*5)+2,int(s/2)-4,int(s2)-9,COLOR["mouth"]) krect(int(x-s/2+4),int(y+s/8*5.5),int(s/2-8),int(s2/2),COLOR["font2"]) def drawPipe(i): pipe=pipes[i] x,y = min(max(pipe[0],OFFSET_X),OFFSET_X+GAME_W),pipe[1] if x<=OFFSET_X: h_s = pipe[2]-(x-pipe[0]) elif x>=OFFSET_X+GAME_W-pipe[2]: h_s = (GAME_W+OFFSET_X)-x else: h_s=pipe[2] v_s = pipe[3] rect(x,y,h_s,v_s,COLOR["pipe"]) def drawHeart(x,y,s,c): heart=("01100","11110","01111","11110","01100") for x2 in range(len(heart)): for y2 in range(len(heart[x2])): if int(heart[x2][y2]): krect(x+x2*s,y+y2*s,s,s,c) def initScreen(): rect(OFFSET_X,OFFSET_Y,GAME_W,GAME_H,COLOR["bg"]) drawScorePannel(GAME_H) actualizeScore() actualizeLife() frame=0 def calculateStarOffset(): global starOffsetY,starOffsetX,mode score=true_score%GAME_W starOffsetX=int(GAME_W//130*(score%130)) if starOffsetX%GAME_W==0: if score>0: if mode=="day": mode="night" else: mode="day" setColor() setLineW() starOffsetX=1 newY=int(((RADIUS//4*pi)//4)//starOffsetX) if starOffsetX<=GAME_W//2: starOffsetY=newY else: starOffsetY=((RADIUS//4*pi)//4)//starOffsetX**(-1) def clearScreen(): global mode,frame if mode!="day": frame+=1 if frame%6==0: krect(OFFSET_X,OFFSET_Y,GAME_W,GAME_H,(0,0,0)) if mode!="day" and submode=="2": krect(0,0,20,220,color(255,255,255)) krect(OFFSET_X+LINE_W,OFFSET_Y+LINE_W,GAME_W-2*LINE_W,GAME_H-2*LINE_W,COLOR["bg"]) if mode=="day": drawSun() else: drawMoon() def drawSun(): x=int(OFFSET_X+LINE_W+starOffsetX) y=int(OFFSET_Y+LINE_W+starOffsetY) krect(x,y,40,40,(255,220,0)) def drawMoon(): x=OFFSET_X+LINE_W+starOffsetX y=OFFSET_Y+LINE_W+starOffsetY krect(x,y,40,40,color(230,230,230)) krect(x+4,y+4,9,9,color(190,190,190)) krect(x+12,y+18,20,20,color(190,190,190)) def createPipe(x,y,h_s,v_s): pipe=[x,y,h_s,v_s] pipes.append(pipe) def addPipes(x,s): space_size=GAME_H//2-difficulty*2 space_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)) def hitBox(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] if x1 <= x2+hs2 and x1+hs1 >= x2 and y1 <= y2+vs2 and y1+vs1 >= y2: return True else: return False def gameEngine(): global bird,pipes,true_score,best_score,difficulty,flap,total_time,flap_time,fps,hurt_time,life print(">initialisation: dead...") difficulty=1 life=3 old_score=-1 true_score=0 pipes=[] pipe_size=50 flap=0 jump_speed=4 max_speed=15 fps=TARG_FPS bird={ "x":45 if (mode!="day" and submode=="2") else 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_SPF total_time=monotonic() flap_time=monotonic() hurt_time=monotonic()-1 while not (life<1 and sec(hurt_time)>0.5): if sec(spf)>=TARG_SPF: if old_score<true_score: calculateStarOffset() old_score=true_score fps=int(1/sec(spf)) spf=monotonic() limits_y=[OFFSET_Y,OFFSET_Y+GAME_H] bird["spd_y"]+=jump_speed/3 bird["spd_y"]=max(-max_speed,min(max_speed,bird["spd_y"])) bird["y"]+=bird["spd_y"] if bird["y"]<=limits_y[0]: bird["y"]=limits_y[0] bird["spd_y"]=0 if bird["y"]>=limits_y[1]-bird["size"]: bird["y"]=limits_y[1]-bird["size"] bird["spd_y"]=0 if keydown(KEY["ok"]): bird["spd_y"]-=abs(-max_speed-bird["spd_y"])/max_speed*jump_speed if flap==0: flap_time=monotonic() flap=1 else: flap=0 remove_pipe=[] last_speed=int(bird["spd_x"]) for i in pipes: i[0]-=last_speed if hitBox((bird["x"],bird["y"],bird["size"],bird["size"]),i): if sec(hurt_time)>1: if life>0:hurt_time=monotonic() life-=1 difficulty+=randint(1,3) actualizeLife() print(">{}s:hurt! life:{}".format(int(sec(total_time)),life)) if i[0]+i[2]<=OFFSET_X: if i[1]!=OFFSET_Y: bird["spd_x"]+=abs(max_speed-bird["spd_x"])/(4*max_speed) true_score+=1 actualizeScore() addPipes(GAME_W+OFFSET_X,pipe_size) remove_pipe.append(i) for i in remove_pipe: pipes.remove(i) clearScreen() if halo and flap: drawHalo(bird["x"],bird["y"],bird["size"]) drawMoon() for i in range(len(pipes)): drawPipe(i) drawBird(bird["x"],bird["y"],bird["size"]) if keydown(17): draw_string("Paused",70,112,(200,80,40),(0,0,0)) while keydown(17):pass while not keydown(17):pass draw_string(" ",70,112,COLOR["bg"],COLOR["bg"]) draw_string("3",90,112,(200,80,40),(0,0,0)) sleep(0.5) draw_string("2",90,112,(200,80,40),(0,0,0)) sleep(0.5) draw_string("1",90,112,(200,80,40),(0,0,0)) sleep(0.5) print(">game end: dead...") print(">fps : {}".format(fps)) text("Press [OK]",60,114,color(0,0,0),color(255,255,255)) while not keydown(4):pass while keydown(4):pass transition() gameEngine() def actualizeScore(): global best_score drawScorePannel(140) if true_score>best_score:best_score=true_score x=OFFSET_X+GAME_W+SCORE_OFFSET+SCORE_W//2 y=OFFSET_Y c=COLOR["font2"] text(str(true_score),x-len(str(true_score))*5,y+40,c,COLOR["bird"]) text(str(best_score),x-len(str(best_score))*5,y+110,c,COLOR["bird"]) def actualizeLife(): x=OFFSET_X+GAME_W+SCORE_OFFSET+5 y=OFFSET_Y+150 for i in range(3): if i>=life: if mode=="day": c=COLOR["line"] else: c=(160,160,160) else: c=COLOR["hurt"] drawHeart(x+i*30,y,5,c) def drawScorePannel(h): x=OFFSET_X+GAME_W+SCORE_OFFSET y=OFFSET_Y rect(x,y,SCORE_W,h,COLOR["bird"]) x=OFFSET_X+GAME_W+SCORE_W//2 y=OFFSET_Y text("SCORE",x-len("SCORE")*5,y+10,COLOR["font1"],COLOR["bird"]) text("BEST",x-len("BEST")*5,y+80,COLOR["font1"],COLOR["bird"]) def transition(): for c in [COLOR["font1"],COLOR["bg"]]: for y in range(OFFSET_Y,OFFSET_Y+GAME_H,10): sleep(0.016) krect(OFFSET_X,y,GAME_W,10,c) sleep(0.5) gameEngine()