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)# Screen dimensions
WIDTH=320HEIGHT=222# Player attributes
player_x=WIDTH//2player_y=HEIGHT-30player_w=20player_h=30player_color=BLUEplayer_jump=-4.0player_velocity=0.0player_on_ground=Falseplayer_energy=100# Platform attributes
platforms=[[50,HEIGHT-20,220,10,BLACK],[150,HEIGHT-70,100,10,BLACK],[50,HEIGHT-120,100,10,BLACK],]# Critter attributes
critter_x=randint(50,WIDTH-50)critter_y=HEIGHT-30critter_w=10critter_h=10critter_color=BLACKcritter_speed=1critter_direction=choice([-1,1])# Game state
GAME_OVER=False# Main game loop
whilenotGAME_OVER:# Clear the screen
fill_rect(0,0,WIDTH,HEIGHT,WHITE)# Draw platforms
forplatforminplatforms:fill_rect(platform[0],platform[1],platform[2],platform[3],platform[4])# Draw player
fill_rect(player_x,player_y,player_w,player_h,player_color)# Draw critter
fill_rect(critter_x,critter_y,critter_w,critter_h,critter_color)# Update player position and energy
ifnotplayer_on_ground:player_velocity+=0.2player_y+=int(player_velocity)ifplayer_y>=HEIGHT-player_h:player_y=HEIGHT-player_hplayer_velocity=0.0player_on_ground=Trueplayer_energy-=0.1# Update critter position
critter_x+=critter_speed*critter_directionifcritter_x<0:critter_x=0critter_direction*=-1elifcritter_x+critter_w>WIDTH:critter_x=WIDTH-critter_wcritter_direction*=-1# Check for collisions
if (player_x+player_w>critter_xandplayer_x<critter_x+critter_wandplayer_y+player_h>critter_yandplayer_y<critter_y+critter_h):player_energy-=2.0critter_x=randint(50,WIDTH-50)# Handle user input
ifkeydown(KEY_RIGHT)andplayer_x<WIDTH-player_w:player_x+=2ifkeydown(KEY_LEFT)andplayer_x>0:player_x-=2ifkeydown(KEY_OK)andplayer_on_ground:player_velocity=player_jumpplayer_on_ground=False# Draw player's energy
fill_rect(5,5,int(player_energy),5,RED)# Check for game over
ifplayer_energy<=0:GAME_OVER=True# Update the screen
sleep(0.02)# Game over screen
fill_rect(0,0,WIDTH,HEIGHT,BLACK)draw_string("GAME OVER",120,90,RED)draw_string("Score: "+str(int(player_energy)),120,120,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.