frommathimport*fromrandomimport*fromkandinskyimport*fromionimport*fromtimeimport*fromkandinskyimportfill_rectasFfromrandomimportrandintasRfromionimportkeydownasKCYAN=(0,255,255)RED=(255,0,0)DARKRED=(135,0,0)DARKGREEN=(0,135,0)WHITE=(255,255,255)BLACK=(0,0,0)GREEN=(0,255,0)BLUE=(0,0,255)#screen width and height
SCRW=322SCRH=222SCRA=SCRW*SCRHworld=1game=Truebg=(randint(0,35),randint(0,35),randint(0,35))#player
psc=0pe=9plife=3pc=CYANedash=Falseedash_timer=0eattacks=Falseeruns=Falseedefeated=Falsedooropen=Falseetimer=0echase=Falseedir=0#enemys energy
ee=R(100,300)ex=R(180,280)ey=R(40,100)ew=12eh=18ec=(R(0,255),R(0,255),R(0,255))doorx=-10doory=-10doorw=9doorh=20doorc=(35,20,0)defpause():draw_string("(PAUSED)",110,100,(randint(0,255),randint(0,255),randint(0,255)),bg)whileK(KEY_OK):passwhilenotK(KEY_OK):passwhileK(KEY_OK):draw_string("",110,100,bg,bg)passbooljump,compteur_saut,L_saut=False,0,[int(-0.4*(x/4)**2+70)forxinrange(-52,52)]+[0]#L_saut=[int(-1*(x/6)**2+70) for x in range(-50,50)]
#length side +
#side is the size or width
side=15#player x
x=15#player y
y_floor=200#player height i guess..
#player y = height - side i guess?
y=y_floor-side# just a counter for the rgb effect on the cube
c=0#background color
c_sky=bg#clouds
c_cloud=(randint(0,255),randint(0,255),randint(0,255))fill_rect(0,0,320,222,c_sky)score=0#variable for the square: length of each part
c_1=side//7c_2=side//12c_3=side-c_1*2-c_2*2#player colors
c_col_1=(randint(0,255),randint(0,255),randint(0,255))c_col_3=(randint(0,255),randint(0,255),randint(0,255))defdisplay_square():c_1=side//7c_2=side//8c_3=side-c_1*2-c_2*2fill_rect(x,y,side,c_1,c_col_1)fill_rect(x,y+side-c_1,side,c_1,c_col_1)fill_rect(x,y+c_1,c_1,c_3+c_2*2,c_col_1)fill_rect(x+side-c_1,y+c_1,c_1,c_3+c_2*2,c_col_1)fill_rect(x+c_1,y+c_1,side-2*c_1,c_2,c_carre)fill_rect(x+c_1,y+c_1+c_2+c_3,side-2*c_1,c_2,c_carre)fill_rect(x+c_1,y+c_1+c_2,c_2,c_3,c_carre)fill_rect(x+c_1+c_2+c_3,y+c_1+c_2,c_2,c_3,c_carre)fill_rect(x+c_1+c_2,y+c_1+c_2,side-(c_1+c_2)*2,side-(c_1+c_2)*2,c_col_3)#carre
defhsv_to_rgb(h,s=1.0,v=1.0):#rgb effect, feel free to use it in your own project
h=(h%360)/360ifs==0.0:v*=255;return (v,v,v)i=int(h*6.)# XXX assume int() truncates!
f=(h*6.)-i;p,q,t=int(255*(v*(1.-s))),int(255*(v*(1.-s*f))),int(255*(v*(1.-s*(1.-f))));v*=255;i%=6ifi==0:return (v,t,p)ifi==1:return (q,v,p)ifi==2:return (p,v,t)ifi==3:return (p,q,v)ifi==4:return (t,p,v)ifi==5:return (v,p,q)#
defwipe_out_previous_square(b):fill_rect(x-b,y-b,2*b+side,b,c_sky)fill_rect(x-b,y,b,side,c_sky)fill_rect(x+side,y,b,side,c_sky)fill_rect(x-b,y+side,2*b+side,y_floor-y-side,c_sky)#
###laser
#length or amount i guess
L_projectiles=[]#width and height of bullets
w_projectile=8h_projectile=6#velocity of bullets
v_projectile=4##### SKY PART
offset=0#counter for clouds
L_clouds=[[0]*9+[2,1,1]+[0]*9,#here you can easily change the aspect of clouds (2 is meant to be directly placed at the left of 1) {1 = a square of a cloud and 2 is the part to erase}
[0,0,2,1,1,0,0,0,0,2,1,1,0,0,0,2,1,1,0,0,0],[0,2,1,1,1,1,0,0,2,1,1,1,1,0,2,1,1,1,1,0,0],[2,1,1,1,1,1,0,2,1,1,1,1,1,0,2,1,1,1,1,1,0]]defdisplay_clouds():globalL_clouds,offsetifoffset==16:offset=0L_clouds=[[i[-1]]+i[:-1]foriinL_clouds]else:offset+=1h,l=14,16fori,pinenumerate(L_clouds):forj,qinenumerate(p):ifq==1:x_nuage,y_nuage=-1*l+offset+j*l,20+i*hfill_rect(x_nuage,y_nuage,l,h,c_cloud)ifq==2:x_nuage,y_nuage=-1*l+offset+j*l+15,20+i*hfill_rect(x_nuage,y_nuage,2,h,c_sky)#ground
y_ground1=y_floor+(222-y_floor)//3#grass on dirt
y_ground2=215#3 different colors for 3 different things
c_herbe=(25,111,61)c_ground1=(160,64,0)c_ground2=(96,48,0)defdisplay_ground():fill_rect(0,y_floor,320,222-y_floor,c_herbe)fill_rect(0,y_ground1,320,222-y_ground1,c_ground1)foriinrange(80):#320
random=randint(0,20)h=random*(y_ground2-y_ground1)//20fill_rect(i*4,y_ground1,4,h,c_herbe)foriinrange(190):#190
xground,yground=randint(0,320),randint(y_ground2-4,222)fill_rect(xground,yground,3,3,c_ground2)L_col=[(88,140,0),(0,88,24),(0,168,0)]foriinrange(320):#320
cground,xground,yground=L_col[randint(0,2)],randint(0,320),randint(y_floor,y_ground1)fill_rect(xground,yground,2,2,cground)#ground random pattern grass
display_ground()c_carre=(148,49,38)fill_rect(x,y,side,side,c_carre)Direction=1bool_shoot=True#main loop
whileTrue:etimer+=0.1draw_string("World:"+str(world),100,2,bg,BLACK)draw_string(":["+str(plife),16,2,WHITE,BLACK)draw_string(":[",240,2,GREEN,BLACK)F(259,6,round(ee/4),10,RED)F(2,3,7,13,pc)F(4,5,5,5,CYAN)F(226,3,ew-3,eh-4,ec)F(227,6,5,4,WHITE)ifetimer>R(7,10):etimer=0edir=R(1,4)echase=choice([False,True,True])#enemy chases player
ifechase:ifex<x:edir=2ifex>x:edir=1ifey<y:edir=4ifey>y:edir=3#enemy moves
ifedir==1:ex-=1F(ex+ew,ey,1,eh,bg)ifedir==2:ex+=1F(ex-1,ey,1,eh,bg)ifedir==3:ey-=1F(ex,ey+eh,ew,1,bg)ifedir==4:ey+=1F(ex,ey-1,ew,1,bg)ifdooropen:F(doorx,doory,doorw,doorh,(randint(0,255),randint(0,255),randint(0,255)))F(doorx,doory+doorh,doorw,2,WHITE)F(ex,ey,ew,eh,ec)F(ex+2,ey+4,8,4,choice([BLACK,WHITE,DARKGREEN]))ifey+eh>200:ey=200-ehedir=R(1,3)ifx+side>=exandx<=ex+ewandy+side>=eyandy<=ey+eh:F(x,y,side,side,RED)sleep(0.001)pe-=0.1ee+=0.1edir=R(1,4)ifplife<2:fill_rect(x,y,side,side,bg)ifee<50:fill_rect(ex,ey,ew,eh,"gray")ifpe<1:pe=9plife-=1ifpe>9:pe=1plife+=1#game is over when plife < 1
ifplife<1:game=Falseforiinrange(100):forjinrange(100):i=R(x,x+side)j=R(y,y+side)F(i,j,R(2,5),R(2,5),choice([BLACK,DARKRED,RED]))ifkeydown(KEY_OK):pause()precedent_y=yifkeydown(KEY_LEFT):Direction=-1ifx>0:fill_rect(x+side-2,y,2,side,c_sky)x-=2elifkeydown(KEY_RIGHT):Direction=1ifx+side<=320:fill_rect(x,y,2,side,c_sky)x+=2ifkeydown(KEY_BACKSPACE):ify==y_floor-side:booljump=True#increase size
ifkeydown(KEY_PLUS):ifside<230:side+=1y-=1x-=side%2#decrease size
elifkeydown(KEY_MINUS):ifside>8:fill_rect(x,y-1,side,3,c_sky)fill_rect(x+side-1,y+1,1,side-1,c_sky)ifbooljump:fill_rect(x-1,y+side-1,side+3,1,c_sky)fill_rect(x,y,2,side,c_sky)x+=side%2side-=1y+=1#player shoots
ifkeydown(KEY_TOOLBOX):ifbool_shoot:bool_shoot=Falseiflen(L_projectiles)<15:ifDirection==1:L_projectiles.append([x+side+3,y+4,1,side//3,side//4])else:L_projectiles.append([x-3-w_projectile,y+4,-1,side//3,side//4])elifnot(keydown(KEY_TOOLBOX)):bool_shoot=True#i guess there is limit of
#bullets fired (L_projectiles)
iflen(L_projectiles)!=0:foriinL_projectiles:#bullets reach left or right
ifi[0]>320ori[0]<0-i[3]:L_projectiles.remove(i)else:ifi[2]==1:x_eff_projectile=i[0]else:x_eff_projectile=i[0]+i[3]-v_projectilefill_rect(x_eff_projectile,i[1],v_projectile,i[4],c_sky)i[0]+=v_projectile*i[2]fill_rect(i[0],i[1],i[3],i[4],(0,0,0))fill_rect(i[0]+(i[3]//6)+1,i[1]+(i[4]//6)+1,i[3]-(i[3]//6)*2-2,i[4]-(i[4]//6)*2-2,c_carre)ifbooljump:y=y_floor-side-L_saut[compteur_saut]compteur_saut+=1ifcompteur_saut==len(L_saut):compteur_saut=0booljump=False#y=y_sol-cote
ifprecedent_y<y:fill_rect(x-2,y-4,side+4,4,c_sky)else:fill_rect(x-2,y+side,side+4,precedent_y-y,c_sky)display_square()display_square()display_clouds()c+=1c_carre=hsv_to_rgb(c)foriinrange(100):forjinrange(100):i=R(px,px+pw)j=R(py,py+ph)F(i,j,R(2,5),R(2,5),choice([BLACK,DARKRED,RED]))draw_string("GAME OVER",100,100,WHITE,bg)draw_string("SCORE:"+str(psc),100,140,CYAN,bg)
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.