importkandinskyaskdfromionimportkeydown,KEY_DOWN,KEY_UP,KEY_LEFT,KEY_RIGHT,KEY_BACKSPACE,KEY_EXEimportrandomimporttimeimportmathWIDTH=320HEIGHT=220PLAYER_CHAR='@'PLAYER_COLOR=(255,255,255)PLAYER_EYE_COLOR=(0,0,0)PLAYER_OUTLINE_COLOR=(0,0,255)# Blue outline color
MONSTER_CHAR='M'MONSTER_COLOR=(255,0,0)EYE_COLOR=(0,0,0)KEY_CHAR='K'KEY_FOREGROUND_COLOR=(random.randint(0,255),random.randint(0,255),random.randint(0,255))# Random foreground color
KEY_BACKGROUND_COLOR=(255,255,0)# Yellow background color
DOOR_CHAR='D'DOOR_COLOR=(128,128,128)# Gray door color
player_x=WIDTH//2player_y=HEIGHT//2player_energy=100# Initial energy value
player_has_eyes=Trueplayer_speed=5# Initial player speed
has_key=False# Player doesn't have the key initially
door_locked=True# The door is locked initially
key_x=random.randint(30,WIDTH-30)key_y=random.randint(30,HEIGHT-30)monsters=[{'x':random.randint(0,WIDTH-10),'y':random.randint(0,HEIGHT-10),'speed':random.uniform(0.5,2.0),'direction_x':random.choice([-1,0,1]),'direction_y':random.choice([-1,0,1]),'chase_probability':0.1},{'x':random.randint(0,WIDTH-10),'y':random.randint(0,HEIGHT-10),'speed':random.uniform(0.5,2.0),'direction_x':random.choice([-1,0,1]),'direction_y':random.choice([-1,0,1]),'chase_probability':0.05},{'x':random.randint(0,WIDTH-10),'y':random.randint(0,HEIGHT-10),'speed':random.uniform(0.5,2.0),'direction_x':random.choice([-1,0,1]),'direction_y':random.choice([-1,0,1]),'chase_probability':0.2}]defcalculate_angle(monster):dx=player_x-monster['x']dy=player_y-monster['y']returnmath.atan2(dy,dx)defmove_toward_player(monster):angle=calculate_angle(monster)monster_speed_x=monster['speed']*math.cos(angle)monster_speed_y=monster['speed']*math.sin(angle)returnmonster_speed_x,monster_speed_ydefdraw_player():# Draw a blue outline for the player
player_width=10player_height=10player_outline_width=player_width+4# Increase the width for the outline
player_outline_height=player_height+4# Increase the height for the outline
player_outline_x=player_x-2# Adjust the position for the outline
player_outline_y=player_y-2# Adjust the position for the outline
kd.fill_rect(player_outline_x,player_outline_y,player_outline_width,player_outline_height,PLAYER_OUTLINE_COLOR)kd.fill_rect(player_x,player_y,player_width,player_height,PLAYER_COLOR)ifplayer_has_eyes:eye_x1,eye_y1=player_x+2,player_y+2eye_x2,eye_y2=player_x+7,player_y+2kd.fill_rect(eye_x1,eye_y1,2,2,PLAYER_EYE_COLOR)kd.fill_rect(eye_x2,eye_y2,2,2,PLAYER_EYE_COLOR)defdraw_monster(monster):x=int(monster['x'])y=int(monster['y'])kd.fill_rect(x,y,10,10,MONSTER_COLOR)eye_x1,eye_y1=x+2,y+2eye_x2,eye_y2=x+7,y+2kd.fill_rect(eye_x1,eye_y1,2,2,EYE_COLOR)kd.fill_rect(eye_x2,eye_y2,2,2,EYE_COLOR)defdraw_key():ifnothas_key:# Draw the key with a key-like appearance
key_width=10key_height=10key_teeth_width=2key_teeth_height=5key_foreground_x=key_xkey_foreground_y=key_ykey_background_x=key_x-2key_background_y=key_y-2# Draw the key background (yellow)
kd.fill_rect(key_background_x,key_background_y,key_width+4,key_height+4,KEY_BACKGROUND_COLOR)# Draw the key teeth (random foreground color)
foriinrange(3):kd.fill_rect(key_foreground_x,key_foreground_y,key_teeth_width,key_teeth_height,KEY_FOREGROUND_COLOR)key_foreground_x+=key_teeth_width+1defdraw_door():ifdoor_locked:kd.fill_rect(250,150,20,40,DOOR_COLOR)else:kd.fill_rect(250,150,20,40,(0,255,0))# Green color when door is unlocked
whileTrue:kd.fill_rect(0,0,WIDTH,HEIGHT,(0,0,0))ifkeydown(KEY_DOWN):player_y+=player_speedelifkeydown(KEY_UP):player_y-=player_speedelifkeydown(KEY_LEFT):player_x-=player_speedelifkeydown(KEY_RIGHT):player_x+=player_speedelifkeydown(KEY_BACKSPACE):# Increase speed when Backspace is pressed
player_speed=10# Increase player speed
else:player_speed=5# Reset player speed
player_x=max(0,min(player_x,WIDTH-10))player_y=max(0,min(player_y,HEIGHT-10))player_energy-=0.01# Slowly lose energy
formonsterinmonsters:ifrandom.random()<monster['chase_probability']:monster_speed_x,monster_speed_y=move_toward_player(monster)else:ifrandom.random()<0.02:monster['speed']=random.uniform(0.5,2.0)monster['direction_x']=random.choice([-1,0,1])monster['direction_y']=random.choice([-1,0,1])monster['x']+=monster['speed']*monster['direction_x']monster['y']+=monster['speed']*monster['direction_y']monster['x']=max(0,min(monster['x'],WIDTH-10))monster['y']=max(0,min(monster['y'],HEIGHT-10))if (player_x<monster['x']+10andplayer_x+10>monster['x']andplayer_y<monster['y']+10andplayer_y+10>monster['y']):player_energy-=10# Lose energy if hit by a monster
draw_monster(monster)draw_player()draw_key()draw_door()kd.draw_string(str(int(player_energy)),10,10,(0,0,0))# Change font color to black
if (player_x<key_x+10andplayer_x+10>key_xandplayer_y<key_y+10andplayer_y+10>key_yandnothas_keyandkeydown(KEY_EXE)# Use EXE key to pick up the key
):has_key=True# Pick up the key when player touches it
if (player_x<270andplayer_x+10>250andplayer_y<190andplayer_y+10>150andhas_keyanddoor_locked):door_locked=False# Unlock the door when player touches it with the key
if (player_x<270andplayer_x+10>250andplayer_y<190andplayer_y+10>150andnotdoor_locked):break# Exit the game when the door is unlocked
time.sleep(0.03)
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.