frommathimport*fromrandomimport*fromkandinskyimport*fromionimport*fromtimeimportsleep# Constants
RED=(255,0,0)WHITE=(255,255,255)BLACK=(0,0,0)GREEN=(0,255,0)CYAN=(0,255,255)BLUE=(0,0,255)# Game variables
GAME_OVER=Falsefloat_energy=30times_almost_fell=0traveled=0score=0life=3# Player attributes
player_x=115player_y=30player_width=14player_height=22player_color=(randint(100,255),randint(100,255),randint(100,255))player_jump=Falseplayer_jump_height=0# Gravity settings
gravity=1.0# Map attributes
MAPX=0MAPY=0MAPW=5000MAPH=222# Map boundary boxes
MAPBOX_X1=[MAPX,MAPY,5,MAPH,BLACK]MAPBOX_X2=[MAPX+MAPW-5,MAPY,5,MAPH,BLACK]MAPBOX_Y1=[MAPX,MAPY,MAPW,5,BLACK]MAPBOX_Y2=[MAPX,MAPY+MAPH-5,MAPW,5,BLACK]# Ropes
rope_x=MAPX+randint(200,500)rope_h=randint(50,120)rope_y=MAPY+MAPH-rope_h-5rope_w=randint(500,1200)rope_c=(randint(0,255),randint(0,255),randint(0,255))rope=[rope_x,rope_y,rope_w,rope_h,rope_c]# Walls
wall_list_x=[]for_inrange(3):r_x=MAPX+randint(1000,3000)r_h=randint(50,160)r_y=MAPY+MAPH-r_h-5r_w=randint(180,1200)r_c=(randint(0,255),randint(0,255),randint(0,255))wall_list_x.append([r_x,r_y,r_w,r_h,r_c])# Background color
bg=WHITE# Main game loop
whilenotGAME_OVER:# Clear the screen
fill_rect(0,0,322,222,bg)# Draw map boundaries
fill_rect(*MAPBOX_X1)fill_rect(*MAPBOX_X2)fill_rect(*MAPBOX_Y1)fill_rect(*MAPBOX_Y2)# Apply gravity
ifnotplayer_jumpandplayer_y+player_height<MAPH:player_y+=gravity# Draw the player
fill_rect(int(player_x),int(player_y),player_width,player_height,player_color)fill_rect(int(player_x+3),int(player_y+3),4,5,BLACK)fill_rect(int(player_x+9),int(player_y+3),4,5,BLACK)# Handle user input
ifkeydown(KEY_LEFT):player_x-=2ifkeydown(KEY_RIGHT):player_x+=2# Jumping
ifkeydown(KEY_OK)andnotplayer_jump:player_jump=Trueifplayer_jump:ifplayer_jump_height<40:player_y-=2player_jump_height+=2else:player_jump=Falseplayer_jump_height=0# Check for collisions with obstacles
ifget_pixel(int(player_x),int(player_y+player_height+2))==REDand \
get_pixel(int(player_x+player_width),int(player_y+player_height-1))==RED:sleep(0.25)fill_rect(int(player_x),int(player_y),player_width,player_height,WHITE)player_x=15player_y=30life-=1# Handle game over
iflife<1:sleep(0.4)fill_rect(0,0,322,222,BLACK)sleep(1)draw_string("GAME OVER",100,80,RED,BLACK)sleep(1)draw_string("Score:"+str(score),100,130,WHITE,BLACK)sleep(1.5)draw_string("PRESS [OK]",120,180,BLACK,GREEN)GAME_OVER=True# Update the screen
sleep(0.03)# Adjust this value to control the game speed
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.