# Import necessary modules
importkandinskyaskdfromionimportkeydown,KEY_DOWN,KEY_UP,KEY_LEFT,KEY_RIGHTimportrandomimporttime# Constants
WIDTH=320HEIGHT=220PLAYER_CHAR='@'PLAYER_COLOR=(255,255,255)# White color as (R, G, B) tuple
MONSTER_CHAR='M'MONSTER_COLOR=(255,0,0)# Red color as (R, G, B) tuple
# Initial player position
player_x=WIDTH//2player_y=HEIGHT//2# Initial monster position
monster_x=random.randint(0,WIDTH-10)monster_y=random.randint(0,HEIGHT-10)# Variables for monster movement
monster_speed=random.uniform(0.5,2.0)# Random speed between 0.5 and 2.0
monster_direction_x=random.choice([-1,0,1])# Random initial x direction (-1, 0, 1)
monster_direction_y=random.choice([-1,0,1])# Random initial y direction (-1, 0, 1)
# Function to draw the player character
defdraw_player():kd.fill_rect(player_x,player_y,10,10,PLAYER_COLOR)# Function to draw the monster character
defdraw_monster():kd.fill_rect(monster_x,monster_y,10,10,MONSTER_COLOR)# Main game loop
whileTrue:# Clear the screen
kd.fill_rect(0,0,WIDTH,HEIGHT,(0,0,0))# Clear with black color
# Handle input for player movement
ifkeydown(KEY_DOWN):player_y+=5elifkeydown(KEY_UP):player_y-=5elifkeydown(KEY_LEFT):player_x-=5elifkeydown(KEY_RIGHT):player_x+=5# Ensure the player stays within the screen boundaries
player_x=max(0,min(player_x,WIDTH-10))player_y=max(0,min(player_y,HEIGHT-10))# Move the monster with variable speed and direction
ifrandom.random()<0.02:# 2% chance of changing speed and direction
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_xmonster_y+=monster_speed*monster_direction_y# Ensure the monster stays within the screen boundaries
monster_x=max(0,min(monster_x,WIDTH-10))monster_y=max(0,min(monster_y,HEIGHT-10))# Draw the player character and monster
draw_player()draw_monster()# Pause briefly to control the game speed
time.sleep(0.03)# Adjust this value for the desired frame rate
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.