# Import necessary modules import kandinsky as kd from ion import keydown, KEY_DOWN, KEY_UP, KEY_LEFT, KEY_RIGHT import random import time # Constants WIDTH = 320 HEIGHT = 220 PLAYER_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 // 2 player_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 def draw_player(): kd.fill_rect(player_x, player_y, 10, 10, PLAYER_COLOR) # Function to draw the monster character def draw_monster(): kd.fill_rect(monster_x, monster_y, 10, 10, MONSTER_COLOR) # Main game loop while True: # Clear the screen kd.fill_rect(0, 0, WIDTH, HEIGHT, (0, 0, 0)) # Clear with black color # Handle input for player movement if keydown(KEY_DOWN): player_y += 5 elif keydown(KEY_UP): player_y -= 5 elif keydown(KEY_LEFT): player_x -= 5 elif keydown(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 if random.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_x monster_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