fromionimport*fromkandinskyimport*fromkandinskyimportfill_rectasFILLfromrandomimport*fromrandomimportchoice,randintasRANDfromtimeimportsleepfromtimeimportmonotonic# Add import for monotonic
# Set up the screen
WIDTH=320HEIGHT=240PLATFORM_COLOR=color(0,255,0)# Green
CRITT_COLOR=color(0,0,0)# Black
EYE_COLOR=color(255,255,255)# White
PLAYER_COLOR=color(0,0,255)# Blue
BULLET_COLOR=color(255,0,0)# Red for bullets
# Platform dimensions and positions
PLATFORM_WIDTH=40PLATFORM_HEIGHT=10PLATFORM1_X=100PLATFORM1_Y=100PLATFORM2_X=150PLATFORM2_Y=40# Critt dimensions
CRITT_WIDTH=12CRITT_HEIGHT=16# Player dimensions
PLAYER_WIDTH=16PLAYER_HEIGHT=12player_x=20# Place player in column 20
player_y=200# Place player in row 200
player_speed=2# Player movement speed
player_gravity=1# Gravity force
max_player_y=200# Maximum player height
# Bullet dimensions
BULLET_WIDTH=4BULLET_HEIGHT=4bullets=[]# Store bullets as (x, y, direction) tuples
# Initial critt positions and directions
critt1_x=randint(PLATFORM1_X,PLATFORM1_X+PLATFORM_WIDTH-CRITT_WIDTH)critt1_y=PLATFORM1_Y-CRITT_HEIGHTcritt1_direction=1# 1 for right, -1 for left
critt2_x=randint(PLATFORM2_X,PLATFORM2_X+PLATFORM_WIDTH-CRITT_WIDTH)critt2_y=PLATFORM2_Y-CRITT_HEIGHTcritt2_direction=1# 1 for right, -1 for left
# Jumping variables for the player
jumping=Falsejump_count=0max_jump_count=3# Allow three consecutive jumps
jump_height=3*PLAYER_HEIGHT# Three times the player's height
# Variables for the hit effect
hit_duration=0.8# in seconds
hit_start_time=-1whileTrue:# Clear the screen
fill_rect(0,0,WIDTH,HEIGHT,color(255,255,255))# Draw the platforms
FILL(PLATFORM1_X,PLATFORM1_Y,PLATFORM_WIDTH,PLATFORM_HEIGHT,PLATFORM_COLOR)FILL(PLATFORM2_X,PLATFORM2_Y,PLATFORM_WIDTH,PLATFORM_HEIGHT,PLATFORM_COLOR)# Draw critt 1
ifcritt1_x>=PLATFORM1_Xandcritt1_x<=PLATFORM1_X+PLATFORM_WIDTH:FILL(critt1_x,critt1_y,CRITT_WIDTH,CRITT_HEIGHT,CRITT_COLOR)FILL(critt1_x+2,critt1_y+2,4,4,EYE_COLOR)FILL(critt1_x+6,critt1_y+2,4,4,EYE_COLOR)# Draw critt 2
ifcritt2_x>=PLATFORM2_Xandcritt2_x<=PLATFORM2_X+PLATFORM_WIDTH:FILL(critt2_x,critt2_y,CRITT_WIDTH,CRITT_HEIGHT,CRITT_COLOR)FILL(critt2_x+2,critt2_y+2,4,4,EYE_COLOR)FILL(critt2_x+6,critt2_y+2,4,4,EYE_COLOR)# Draw player
ifhit_start_time==-1:FILL(player_x,player_y,PLAYER_WIDTH,PLAYER_HEIGHT,PLAYER_COLOR)FILL(player_x+2,player_y+2,4,4,color(255,255,255))FILL(player_x+10,player_y+2,4,4,color(255,255,255))else:current_time=monotonic()elapsed_time=current_time-hit_start_timeifelapsed_time<hit_duration:# Player turns red when hit
FILL(player_x,player_y,PLAYER_WIDTH,PLAYER_HEIGHT,color(255,0,0))else:hit_start_time=-1# Reset hit effect
# Update critt positions
critt1_x+=critt1_direction*randint(1,5)critt2_x+=critt2_direction*randint(1,5)# Check boundaries and change direction if needed
ifcritt1_x<PLATFORM1_Xorcritt1_x+CRITT_WIDTH>PLATFORM1_X+PLATFORM_WIDTH:critt1_direction*=-1ifcritt2_x<PLATFORM2_Xorcritt2_x+CRITT_WIDTH>PLATFORM2_X+PLATFORM_WIDTH:critt2_direction*=-1# Check for collision with critters
if ((player_x+PLAYER_WIDTH>=critt1_xandplayer_x<=critt1_x+CRITT_WIDTHandplayer_y+PLAYER_HEIGHT>=critt1_y)or(player_x+PLAYER_WIDTH>=critt2_xandplayer_x<=critt2_x+CRITT_WIDTHandplayer_y+PLAYER_HEIGHT>=critt2_y)):# Player hit by a critt, trigger hit effect
hit_start_time=monotonic()# Handle player's gravity
ifplayer_y<max_player_y:player_y+=player_gravity# Handle jumping
ifjumping:ifjump_count<max_jump_count:ifplayer_y>(jump_height+PLATFORM_HEIGHT+PLATFORM1_Y)orplayer_y>(jump_height+PLATFORM_HEIGHT+PLATFORM2_Y):# Player is in the air
player_y-=4jump_count+=1else:jumping=False# Check for jump key (KEY_UP)
ifkeydown(KEY_UP)andnotjumping:jumping=Truejump_count=0# Move player left (KEY_LEFT)
ifkeydown(KEY_LEFT):player_x-=player_speedifplayer_x<0:player_x=0# Move player right (KEY_RIGHT)
ifkeydown(KEY_RIGHT):player_x+=player_speedifplayer_x+PLAYER_WIDTH>WIDTH:player_x=WIDTH-PLAYER_WIDTH# Handle shooting bullets (KEY_OK)
ifkeydown(KEY_OK):bullet_x=player_x+(PLAYER_WIDTH//2)-(BULLET_WIDTH//2)bullet_y=player_y+(PLAYER_HEIGHT//2)-(BULLET_HEIGHT//2)bullet_direction=1ifplayer_x<WIDTH//2else-1# Shoot left or right
bullets.append((bullet_x,bullet_y,bullet_direction))# Update and draw bullets
new_bullets=[]forbulletinbullets:bullet_x,bullet_y,bullet_direction=bulletbullet_x+=bullet_direction*5# Adjust bullet speed here
ifbullet_x>=0andbullet_x<=WIDTH:FILL(bullet_x,bullet_y,BULLET_WIDTH,BULLET_HEIGHT,BULLET_COLOR)new_bullets.append((bullet_x,bullet_y,bullet_direction))bullets=new_bullets# Update the screen
sleep(0.05)
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.