fromkandinskyimport*fromionimport*fromtimeimportsleepfromrandomimportrandint,choice# Constants
WHITE=(255,255,255)BLACK=(0,0,0)RED=(255,0,0)GREEN=(0,255,0)BLUE=(0,0,255)# Player attributes
player_x=50player_y=150player_width=15player_height=20player_color=GREENplayer_jump=Falseplayer_jump_height=0player_velocity=2player_max_jump_height=40player_outline_color=BLUE# Critter monster attributes
critter_x=200critter_y=170critter_width=player_width*2critter_height=player_height*2critter_color=BLACKcritter_eye_color=REDcritter_eye_size=2critter_speed=2# Initial speed
critter_descending=False# Flag to indicate if the critter is descending
# Small critter attributes
small_critter_x=50small_critter_y=50small_critter_size=10small_critter_color=BLACKsmall_critter_speed=1small_critter_direction=choice([(1,0),(-1,0),(0,1),(0,-1)])small_critter_change_direction=randint(20,100)small_critter_approach_chance=20# Percentage chance to approach the player
# Energy attributes
player_energy=randint(25,50)# Gravity attributes
gravity=2# Platform attributes
platform_x=0platform_y=180platform_width=320platform_height=10platform_color=BLACK# Screen boundaries
screen_width=320screen_height=240# Game variables
GAME_OVER=False# Bullets attributes
bullet_color=BLACKbullets=[]defcreate_bullet(x,y,width,height,color):bullets.append([x,y,width,height,color])# Main game loop
whilenotGAME_OVER:# Clear the screen with the background color
fill_rect(0,0,screen_width,screen_height,(255,)*3)# Draw the platform on the screen
fill_rect(platform_x,platform_y,platform_width,platform_height,platform_color)# Handle user input
ifkeydown(KEY_LEFT)andplayer_x>0:player_x-=player_velocityifkeydown(KEY_RIGHT)andplayer_x+player_width<screen_width:player_x+=player_velocity# Apply gravity
ifnotplayer_jumpandplayer_y+player_height<platform_y:player_y+=gravity# Jumping
ifkeydown(KEY_OK)andnotplayer_jumpandplayer_y+player_height==platform_y:player_jump=Trueplayer_jump_height=0ifplayer_jump:player_jump_height+=gravityplayer_y-=gravityifplayer_jump_height>=player_max_jump_height:player_jump=False# Draw the player with eyes and blue outline
fill_rect(player_x,player_y,player_width,player_height,player_color)fill_rect(player_x+4,player_y+4,3,3,WHITE)# Left eye
fill_rect(player_x+player_width-7,player_y+4,3,3,WHITE)# Right eye
fill_rect(player_x-1,player_y-1,player_width+2,player_height+2,player_outline_color)# Outline
# Move the critter monster
ifcritter_descending:critter_y+=critter_speedifcritter_y+critter_height>screen_height:critter_descending=Falseelse:critter_y-=critter_speedifcritter_y<0:critter_descending=Truecritter_x-=critter_speed# Reset critter monster position if it goes off-screen
ifcritter_x+critter_width<0:critter_x=screen_widthcritter_y=randint(50,150)# Randomize critter monster's vertical position
critter_speed=randint(1,5)# Randomize critter monster's speed
# Draw the critter monster with red eyes
fill_rect(critter_x,critter_y,critter_width,critter_height,critter_color)fill_rect(critter_x+critter_width//4,critter_y+critter_height//4,critter_eye_size,critter_eye_size,critter_eye_color)# Left eye
fill_rect(critter_x+critter_width//2+critter_width//4,critter_y+critter_height//4,critter_eye_size,critter_eye_size,critter_eye_color)# Right eye
# Randomly approach the player
ifrandint(1,100)<=small_critter_approach_chance:ifsmall_critter_x<player_x:small_critter_x+=small_critter_speed*randint(1,3)# Fast approach
else:small_critter_x-=small_critter_speed*randint(1,3)# Fast approach
ifsmall_critter_y<player_y:small_critter_y+=small_critter_speed*randint(1,3)# Fast approach
else:small_critter_y-=small_critter_speed*randint(1,3)# Fast approach
# Draw the small critter
fill_rect(small_critter_x,small_critter_y,small_critter_size,small_critter_size,small_critter_color)# Check for collisions with screen boundaries
ifplayer_x<0:player_x=0elifplayer_x+player_width>screen_width:player_x=screen_width-player_widthifplayer_y<0:player_y=0elifplayer_y+player_height>screen_height:player_y=screen_height-player_height# Check for collisions with the platform
ifplayer_y+player_height>platform_yandplayer_x+player_width>platform_xandplayer_x<platform_x+platform_width:player_y=platform_y-player_heightplayer_jump=Falseplayer_jump_height=0# Check for collisions with the critter monster
if (player_x+player_width>critter_xandplayer_x<critter_x+critter_widthandplayer_y+player_height>critter_yandplayer_y<critter_y+critter_height):player_color=RED# Player turns red
player_energy-=randint(1,2)# Lose energy
ifplayer_energy<0:GAME_OVER=True# Player energy is less than zero, game over
# Check for collisions with the small critter
if (player_x+player_width>small_critter_xandplayer_x<small_critter_x+small_critter_sizeandplayer_y+player_height>small_critter_yandplayer_y<small_critter_y+small_critter_size):player_energy-=randint(1,2)# Lose energy
ifplayer_energy<0:GAME_OVER=True# Player energy is less than zero, game over
# Update the energy bar at the top of the screen
fill_rect(5,5,player_energy*2,10,GREEN)# Green energy bar
fill_rect(5,5,50*2,10,BLACK)# Clear the energy bar background
draw_string("Energy:",5,20,BLACK,WHITE)draw_string(str(player_energy),80,20,BLACK,WHITE)# Random bullet
ifrandint(1,100)<=1:# 1% chance to create a bullet
create_bullet(screen_width,randint(50,200),10,2,BLACK)# Update bullets
forbulletinbullets:bullet[0]-=3# Move the bullet to the left
fill_rect(bullet[0],bullet[1],bullet[2],bullet[3],bullet[4])# Draw the bullet
# Check for collisions with the player
if (player_x+player_width>bullet[0]andplayer_x<bullet[0]+bullet[2]andplayer_y+player_height>bullet[1]andplayer_y<bullet[1]+bullet[3]):player_energy-=1# Lose energy
ifplayer_energy<0:GAME_OVER=True# Player energy is less than zero, game over
# Remove bullets that go off-screen
bullets=[bulletforbulletinbulletsifbullet[0]>-bullet[2]]# Update the screen
sleep(0.02)# Game over screen
fill_rect(0,0,screen_width,screen_height,(255,)*3)draw_string("GAME OVER",100,80,RED,WHITE)draw_string("Energy:",100,110,BLACK,WHITE)draw_string(str(player_energy),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.