from kandinsky import * from ion import * from time import sleep from random import randint # Constants WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # Player attributes player_x = 50 player_y = 150 player_width = 15 player_height = 20 player_color = GREEN player_jump = False player_jump_height = 0 player_velocity = 2 player_max_jump_height = 40 player_outline_color = BLUE # Critter monster attributes critter_x = 200 critter_y = 170 critter_width = player_width * 2 critter_height = player_height * 2 critter_color = BLACK critter_eye_color = RED critter_eye_size = 2 critter_speed = 2 # Initial speed critter_descending = False # Flag to indicate if the critter is descending # Energy attributes player_energy = randint(25, 50) # Gravity attributes gravity = 2 # Platform attributes platform_x = 0 platform_y = 180 platform_width = 320 platform_height = 10 platform_color = BLACK # Screen boundaries screen_width = 320 screen_height = 240 # Game variables GAME_OVER = False # Main game loop while not GAME_OVER: # Clear the screen with the background color fill_rect(0, 0, screen_width, screen_height, WHITE) # Draw the platform on the screen fill_rect(platform_x, platform_y, platform_width, platform_height, platform_color) # Handle user input if keydown(KEY_LEFT) and player_x > 0: player_x -= player_velocity if keydown(KEY_RIGHT) and player_x + player_width < screen_width: player_x += player_velocity # Apply gravity if not player_jump and player_y + player_height < platform_y: player_y += gravity # Jumping if keydown(KEY_OK) and not player_jump and player_y + player_height == platform_y: player_jump = True player_jump_height = 0 if player_jump: player_jump_height += gravity player_y -= gravity if player_jump_height >= player_max_jump_height: player_jump = False # Draw the player with eyes and blue outline fill_rect(player_x, player_y, player_width, player_height, player_color) fill_rect(player_x + 4, player_y + 4, 3, 3, WHITE) # Left eye fill_rect(player_x + player_width - 7, player_y + 4, 3, 3, WHITE) # Right eye fill_rect(player_x - 1, player_y - 1, player_width + 2, player_height + 2, player_outline_color) # Outline # Move the critter monster if critter_descending: critter_y += critter_speed if critter_y + critter_height > screen_height: critter_descending = False else: critter_y -= critter_speed if critter_y < 0: critter_descending = True critter_x -= critter_speed # Reset critter monster position if it goes off-screen if critter_x + critter_width < 0: critter_x = screen_width critter_y = randint(50, 150) # Randomize critter monster's vertical position critter_speed = randint(1, 5) # Randomize critter monster's speed # Draw the critter monster with red eyes fill_rect(critter_x, critter_y, critter_width, critter_height, critter_color) fill_rect(critter_x + critter_width // 4, critter_y + critter_height // 4, critter_eye_size, critter_eye_size, critter_eye_color) # Left eye fill_rect(critter_x + critter_width // 2 + critter_width // 4, critter_y + critter_height // 4, critter_eye_size, critter_eye_size, critter_eye_color) # Right eye # Check for collisions with screen boundaries if player_x < 0: player_x = 0 elif player_x + player_width > screen_width: player_x = screen_width - player_width if player_y < 0: player_y = 0 elif player_y + player_height > screen_height: player_y = screen_height - player_height # Check for collisions with the platform if player_y + player_height > platform_y and player_x + player_width > platform_x and player_x < platform_x + platform_width: player_y = platform_y - player_height player_jump = False player_jump_height = 0 # Check for collisions with the critter monster if (player_x + player_width > critter_x and player_x < critter_x + critter_width and player_y + player_height > critter_y and player_y < critter_y + critter_height): player_color = RED # Player turns red player_energy -= randint(1, 2) # Decrease energy by a random amount # Player returns to the original color when not hit by the critter monster else: player_color = GREEN # Check player's energy if player_energy <= 0: GAME_OVER = True # Draw player's energy on top of the screen fill_rect(5, 5, player_energy * 2, 10, GREEN) # Green energy bar fill_rect(5, 5, 50 * 2, 10, BLACK) # Clear the energy bar background draw_string("Energy:", 5, 20, BLACK, WHITE) draw_string(str(player_energy), 80, 20, BLACK, WHITE) # Update the screen sleep(0.02) # Game over screen fill_rect(0,0,322,222(255,)*3) draw_string("GAME OVER", 100, 80, RED, WHITE) draw_string("Energy:", 100, 110, BLACK, WHITE) draw_string(str(player_energy), 180, 110, BLACK, WHITE)