fromkandinskyimport*fromkandinskyimportfill_rectasFILLfromkandinskyimportdraw_stringasSTRfromionimport*fromtimeimportsleepfromrandomimportchoice,randintasRANDWHITE=(255,255,255)BLACK=(0,0,0)RED=(255,0,0)GREEN=(0,255,0)BLUE=(0,0,255)px=50py=150pw=15ph=20pc=GREENpjump=Falsepjumph=0pvelocity=2pmax_jumph=40poutline_c=BLUEboulder_x=200boulder_y=170boulder_w=pw*2boulder_h=ph*2boulder_c=(RAND(0,25),RAND(0,25),RAND(0,25))boulder_eye_c=REDboulder_eye_size=2boulder_speed=2# Initial speed
boulder_descending=False# Flag to indicate if the boulder is descending
critt_x=50critt_y=50critt_size=10critt_c=BLACKcritt_speed=1critt_direction=choice([(1,0),(-1,0),(0,1),(0,-1)])critt_change_direction=RAND(20,100)critt_approach_chance=20# Percentage chance to approach the p
penergy=RAND(25,50)gravity=2S_WIDTH=320S_HEIGHT=222# Adjusted screen h
# Game variables
GAME_OVER=Falsedrone_c=BLACKdrones_list=[]plats=[]bullets=[]facing_left=Falsefacing_right=Truedefshoot_bullet():iffacing_left:bullet={"x":px,"y":py+ph//2,"direction":-1}eliffacing_right:bullet={"x":px+pw,"y":py+ph//2,"direction":1}bullets.append(bullet)defcreate_drone(x,y,w,h,c):drones_list.append([x,y,w,h,c])defcreate_plat(x,y,w,h,c):plats.append([x,y,w,h,c])whilenotGAME_OVER:ifpy<-20:STR("success",20,1)FILL(0,0,S_WIDTH,S_HEIGHT,(255,)*3)forplatinplats:FILL(plat[0],plat[1],plat[2],plat[3],plat[4])ifkeydown(KEY_LEFT)andpx>0:px-=pvelocityifkeydown(KEY_RIGHT)andpx+pw<S_WIDTH:px+=pvelocityifnotpjumpandpy+ph<S_HEIGHT:py+=gravityifkeydown(KEY_UP)andnotpjump:pjump=Truepjumph=0ifpjump:pjumph+=gravitypy-=gravityifpjumph>=pmax_jumph:pjump=FalseFILL(px,py,pw,ph,pc)FILL(px+4,py+4,3,3,WHITE)# Left eye
FILL(px+pw-7,py+4,3,3,WHITE)# Right eye
FILL(px-1,py-1,pw+2,ph+2,poutline_c,)# Outline
ifboulder_descending:boulder_y+=boulder_speedifboulder_y+boulder_h>S_HEIGHT:boulder_descending=Falseelse:boulder_y-=boulder_speedifboulder_y<0:boulder_descending=Trueboulder_x-=boulder_speedifboulder_x+boulder_w<0:boulder_x=S_WIDTHboulder_y=RAND(50,150)# Randomize boulder monster's vertical position
boulder_speed=RAND(1,5)# Randomize boulder monster's speed
FILL(boulder_x,boulder_y,boulder_w,boulder_h,boulder_c)FILL(boulder_x+boulder_w//4,boulder_y+boulder_h//4,boulder_eye_size,boulder_eye_size,boulder_eye_c,)# Left eye
FILL(boulder_x+boulder_w//2+boulder_w//4,boulder_y+boulder_h//4,boulder_eye_size,boulder_eye_size,boulder_eye_c,)# Right eye
ifRAND(1,100)<=critt_approach_chance:ifcritt_x<px:critt_x+=critt_speed*RAND(1,3)# Fast approach
else:critt_x-=critt_speed*RAND(1,3)# Fast approach
ifcritt_y<py:critt_y+=critt_speed*RAND(1,3)# Fast approach
else:critt_y-=critt_speed*RAND(1,3)# Fast approach
FILL(critt_x,critt_y,critt_size,critt_size,critt_c,)# Check for collisions with screen boundaries
ifpx<0:px=0elifpx+pw>S_WIDTH:px=S_WIDTH-pwifpy<0:py=0elifpy+ph>S_HEIGHT:py=S_HEIGHT-ph# Check for collisions with the plats
forplatinplats:if (px+pw>plat[0]andpx<plat[0]+plat[2]andpy+ph>plat[1]andpy<plat[1]+plat[3]):ifpy<plat[1]+plat[3]//2:py=plat[1]-phpjump=False# Allow jumping when standing on a plat
else:py=plat[1]+plat[3]# Check for collisions with the boulder monster
if (px+pw>boulder_xandpx<boulder_x+boulder_wandpy+ph>boulder_yandpy<boulder_y+boulder_h):penergy-=RAND(1,2)# Lose energy
FILL(px,py,pw,ph,RED)ifpenergy<0:GAME_OVER=True# Player energy is less than zero, game over
# Check for collisions with the critt
if (px+pw>critt_xandpx<critt_x+critt_sizeandpy+ph>critt_yandpy<critt_y+critt_size):penergy-=RAND(1,2)FILL(px,py,pw,ph,RED)ifpenergy<0:GAME_OVER=TrueFILL(5,5,penergy*2,10,GREEN)# Green energy bar
FILL(5,5,50*2,10,BLACK)# Clear the energy bar background
STR("Energy:",5,20,BLACK,WHITE)STR(str(penergy),80,20,BLACK,WHITE)# Random drone
ifRAND(1,100)<=1:# 1% chance to create a drone
create_drone(S_WIDTH,RAND(50,200),10,2,BLACK)# Update drones
fordroneindrones_list:drone[0]-=3# Move the drone to the left
FILL(drone[0],drone[1],drone[2],drone[3],drone[4])# Draw the drone
# Check for collisions with the p
if (px+pw>drone[0]andpx<drone[0]+drone[2]andpy+ph>drone[1]andpy<drone[1]+drone[3]):penergy-=1# Lose energy
FILL(px,py,pw,ph,RED)ifpenergy<0:GAME_OVER=True# Player energy is less than zero, game over
# Remove drones that go off-screen
drones_list=[dronefordroneindrones_listifdrone[0]>-drone[2]]# Update the screen
sleep(0.02)ifkeydown(KEY_OK):shoot_bullet()forbulletinbullets:bullet["x"]+=5*bullet["direction"]FILL(bullet["x"],bullet["y"],4,2,(RAND(100,255),RAND(100,255),RAND(100,255)),)ifbullet["x"]>S_WIDTHorbullet["x"]<0:bullets.remove(bullet)# Game over screen
FILL(0,0,S_WIDTH,S_HEIGHT,(255,)*3)STR("GAME OVER",100,80,RED,WHITE)STR("Energy:",100,110,BLACK,WHITE)STR(str(penergy),180,110,BLACK,WHITE)
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.