frommathimport*fromrandomimport*fromrandomimportrandintasRANDfromkandinskyimport*fromkandinskyimportfill_rectasFfromionimportkeydownasKPfromkandinskyimportdraw_stringasSTRfromionimport*fromtimeimport*# screen width and height
SW,SH=320,220GAME_OVER=Falsered=(255,0,0)green=(0,255,0)blue="blue"yellow="yellow"brown="brown"cyan="cyan"pink="pink"orange="orange"purple="purple"black="black"white="white"direc=["left","right","up","down"]DIR=direc[1]bg=(110,18,23)x,y,w,h,c=220,200,12,20,(0,0,100)# ******************
# starting point: area = 1
area=1# ******************
level_1=[#area=1
[[0,208,78,14,(176,157,217)],[0,199,14,8,(144,46,229)],[126,200,32,22,(51,18,23)],[177,209,10,12,(39,41,41)]],#area=2
[[0,64,148,170,black],[105,127,114,136,black],[162,181,28,40,black],[30,43,28,28,black],[51,25,54,64,black],[37,184,42,40,(189,25,51)],[13,100,24,28,(45,138,186)]],#area=3
[[75,178,154,44,'black'],[99,145,110,44,'black'],[114,145,86,64,'red'],[236,85,86,64,'red'],[50,16,22,32,(153,126,88)]],#area=3
[[40,176,50,46,black],[168,134,154,88,black],[202,86,120,88,black],[246,44,76,88,black],[90,206,76,16,red]]]# Define bullets and critters
bullets=[]critters=[]# Add critters to the level
defadd_critters_to_level(area):# Depending on the area, add critters with different properties
ifarea==1:critters.append([50,200,8,8,green,1])# x, y, w, h, color, speed
elifarea==2:critters.append([100,180,8,8,green,1])critters.append([200,160,8,8,green,1])elifarea==3:critters.append([50,200,8,8,green,1])critters.append([250,180,8,8,green,1])add_critters_to_level(area)# Add bullets to the player's ammunition
ammo=5defrefresh_level():F(0,0,SW,SH,bg)foriinlevel_1[area]:F(*i)F(0,0,SW,SH,bg)foriinlevel_1[area]:F(*i)whilenotGAME_OVER:F(x,y,w,h,c)F(x+3,y+3,4,3,white)F(x+9,y+3,4,3,white)# Display the number of bullets left
STR("Ammo: "+str(ammo),5,5,white,bg)# Display the number of critters in the area
STR("Critters: "+str(len(critters)),200,5,green,bg)forcritterincritters:F(critter[0],critter[1],critter[2],critter[3],critter[4])critter[0]+=critter[5]# Move the critter
# Handle bullet shooting
ifKP(KEY_OK)andammo>0:bullet_x=x+w//2bullet_y=ybullets.append([bullet_x,bullet_y,2,5,white])ammo-=1# Display the number of bullets on the screen
STR("Ammo: "+str(ammo),5,5,white,bg)forbulletinbullets:F(bullet[0],bullet[1],bullet[2],bullet[3],bullet[4])bullet[1]-=5# Move the bullet upwards
# Remove bullets that go off the screen
bullets=[bulletforbulletinbulletsifbullet[1]>0]# Check for collisions between bullets and critters
forcritterincritters:forbulletinbullets:if (bullet[0]+bullet[2]>critter[0]andbullet[0]<critter[0]+critter[2]andbullet[1]+bullet[3]>critter[1]andbullet[1]<critter[1]+critter[3]):critters.remove(critter)# Remove the critter
bullets.remove(bullet)# Remove the bullet
iflen(critters)==0:add_critters_to_level(area)# Add new critters when all are defeated
ifKP(KEY_LEFT):x-=1F(x+w+1,y,1,h,bg)ifKP(KEY_RIGHT):x+=1F(x-1,y,1,h,bg)ifx>320:sleep(0.2)x=0area+=1refresh_level()add_critters_to_level(area)# Add critters to the new area
ammo=5# Reset ammo
ifx<0andarea==1:x=0ifx<0andarea>1:x=321area-=1refresh_level()add_critters_to_level(area)# Add critters to the new area
ammo=5# Reset ammo
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.