from ion import * from kandinsky import * from kandinsky import fill_rect as FILL from random import * from random import choice, randint as RAND from time import sleep from time import monotonic # Add import for monotonic # Set up the screen WIDTH = 320 HEIGHT = 240 PLATFORM_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 = 40 PLATFORM_HEIGHT = 10 PLATFORM1_X = 100 PLATFORM1_Y = 100 PLATFORM2_X = 150 PLATFORM2_Y = 40 # Critt dimensions CRITT_WIDTH = 12 CRITT_HEIGHT = 16 # Player dimensions PLAYER_WIDTH = 16 PLAYER_HEIGHT = 12 player_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 = 4 BULLET_HEIGHT = 4 bullets = [] # 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_HEIGHT critt1_direction = 1 # 1 for right, -1 for left critt2_x = randint(PLATFORM2_X, PLATFORM2_X + PLATFORM_WIDTH - CRITT_WIDTH) critt2_y = PLATFORM2_Y - CRITT_HEIGHT critt2_direction = 1 # 1 for right, -1 for left # Jumping variables for the player jumping = False jump_count = 0 max_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 = -1 while True: # 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 if critt1_x >= PLATFORM1_X and critt1_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 if critt2_x >= PLATFORM2_X and critt2_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 if hit_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_time if elapsed_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 if critt1_x < PLATFORM1_X or critt1_x + CRITT_WIDTH > PLATFORM1_X + PLATFORM_WIDTH: critt1_direction *= -1 if critt2_x < PLATFORM2_X or critt2_x + CRITT_WIDTH > PLATFORM2_X + PLATFORM_WIDTH: critt2_direction *= -1 # Check for collision with critters if ( (player_x + PLAYER_WIDTH >= critt1_x and player_x <= critt1_x + CRITT_WIDTH and player_y + PLAYER_HEIGHT >= critt1_y) or (player_x + PLAYER_WIDTH >= critt2_x and player_x <= critt2_x + CRITT_WIDTH and player_y + PLAYER_HEIGHT >= critt2_y) ): # Player hit by a critt, trigger hit effect hit_start_time = monotonic() # Handle player's gravity if player_y < max_player_y: player_y += player_gravity # Handle jumping if jumping: if jump_count < max_jump_count: if player_y > (jump_height + PLATFORM_HEIGHT + PLATFORM1_Y) or player_y > (jump_height + PLATFORM_HEIGHT + PLATFORM2_Y): # Player is in the air player_y -= 4 jump_count += 1 else: jumping = False # Check for jump key (KEY_UP) if keydown(KEY_UP) and not jumping: jumping = True jump_count = 0 # Move player left (KEY_LEFT) if keydown(KEY_LEFT): player_x -= player_speed if player_x < 0: player_x = 0 # Move player right (KEY_RIGHT) if keydown(KEY_RIGHT): player_x += player_speed if player_x + PLAYER_WIDTH > WIDTH: player_x = WIDTH - PLAYER_WIDTH # Handle shooting bullets (KEY_OK) if keydown(KEY_OK): bullet_x = player_x + (PLAYER_WIDTH // 2) - (BULLET_WIDTH // 2) bullet_y = player_y + (PLAYER_HEIGHT // 2) - (BULLET_HEIGHT // 2) bullet_direction = 1 if player_x < WIDTH // 2 else -1 # Shoot left or right bullets.append((bullet_x, bullet_y, bullet_direction)) # Update and draw bullets new_bullets = [] for bullet in bullets: bullet_x, bullet_y, bullet_direction = bullet bullet_x += bullet_direction * 5 # Adjust bullet speed here if bullet_x >= 0 and bullet_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)