from kandinsky import * from kandinsky import fill_rect as FILL from kandinsky import draw_string as STR from ion import * from time import sleep from random import choice, randint as RAND WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) px = 50 py = 150 pw = 15 ph = 20 pc = GREEN pjump = False pjumph = 0 pvelocity = 2 pmax_jumph = 40 poutline_c = BLUE boulder_x = 200 boulder_y = 170 boulder_w = pw * 2 boulder_h = ph * 2 boulder_c = (RAND(0, 25), RAND(0, 25), RAND(0, 25)) boulder_eye_c = RED boulder_eye_size = 2 boulder_speed = 2 # Initial speed boulder_descending = False # Flag to indicate if the boulder is descending critt_x = 50 critt_y = 50 critt_size = 10 critt_c = BLACK critt_speed = 1 critt_direction = choice([(1, 0), (-1, 0), (0, 1), (0, -1)]) critt_change_direction = RAND(20, 100) critt_approach_chance = 20 # Percentage chance to approach the p penergy = RAND(25, 50) gravity = 2 S_WIDTH = 320 S_HEIGHT = 222 # Adjusted screen h # Game variables GAME_OVER = False drone_c = BLACK drones_list = [] plats = [] bullets = [] facing_left = False facing_right = True def shoot_bullet(): if facing_left: bullet = {"x": px, "y": py + ph // 2, "direction": -1} elif facing_right: bullet = {"x": px + pw, "y": py + ph // 2, "direction": 1} bullets.append(bullet) def create_drone(x, y, w, h, c): drones_list.append([x, y, w, h, c]) def create_plat(x, y, w, h, c): plats.append([x, y, w, h, c]) while not GAME_OVER: if py < - 20: STR("success", 20, 1) FILL(0, 0, S_WIDTH, S_HEIGHT, (255,) * 3) for plat in plats: FILL(plat[0], plat[1], plat[2], plat[3], plat[4]) if keydown(KEY_LEFT) and px > 0: px -= pvelocity if keydown(KEY_RIGHT) and px + pw < S_WIDTH: px += pvelocity if not pjump and py + ph < S_HEIGHT: py += gravity if keydown(KEY_UP) and not pjump: pjump = True pjumph = 0 if pjump: pjumph += gravity py -= gravity if pjumph >= pmax_jumph: pjump = False FILL(px, py, pw, ph, pc) FILL(px + 4, py + 4, 3, 3, WHITE) # Left eye FILL(px + pw - 7, py + 4, 3, 3, WHITE) # Right eye FILL( px - 1, py - 1, pw + 2, ph + 2, poutline_c, ) # Outline if boulder_descending: boulder_y += boulder_speed if boulder_y + boulder_h > S_HEIGHT: boulder_descending = False else: boulder_y -= boulder_speed if boulder_y < 0: boulder_descending = True boulder_x -= boulder_speed if boulder_x + boulder_w < 0: boulder_x = S_WIDTH boulder_y = RAND(50, 150) # Randomize boulder monster's vertical position boulder_speed = RAND(1, 5) # Randomize boulder monster's speed FILL(boulder_x, boulder_y, boulder_w, boulder_h, boulder_c) FILL( boulder_x + boulder_w // 4, boulder_y + boulder_h // 4, boulder_eye_size, boulder_eye_size, boulder_eye_c, ) # Left eye FILL( boulder_x + boulder_w // 2 + boulder_w // 4, boulder_y + boulder_h // 4, boulder_eye_size, boulder_eye_size, boulder_eye_c, ) # Right eye if RAND(1, 100) <= critt_approach_chance: if critt_x < px: critt_x += critt_speed * RAND(1, 3) # Fast approach else: critt_x -= critt_speed * RAND(1, 3) # Fast approach if critt_y < py: critt_y += critt_speed * RAND(1, 3) # Fast approach else: critt_y -= critt_speed * RAND(1, 3) # Fast approach FILL( critt_x, critt_y, critt_size, critt_size, critt_c, ) # Check for collisions with screen boundaries if px < 0: px = 0 elif px + pw > S_WIDTH: px = S_WIDTH - pw if py < 0: py = 0 elif py + ph > S_HEIGHT: py = S_HEIGHT - ph # Check for collisions with the plats for plat in plats: if ( px + pw > plat[0] and px < plat[0] + plat[2] and py + ph > plat[1] and py < plat[1] + plat[3] ): if py < plat[1] + plat[3] // 2: py = plat[1] - ph pjump = False # Allow jumping when standing on a plat else: py = plat[1] + plat[3] # Check for collisions with the boulder monster if ( px + pw > boulder_x and px < boulder_x + boulder_w and py + ph > boulder_y and py < boulder_y + boulder_h ): penergy -= RAND(1, 2) # Lose energy FILL(px, py, pw, ph, RED) if penergy < 0: GAME_OVER = True # Player energy is less than zero, game over # Check for collisions with the critt if ( px + pw > critt_x and px < critt_x + critt_size and py + ph > critt_y and py < critt_y + critt_size ): penergy -= RAND(1, 2) FILL(px, py, pw, ph, RED) if penergy < 0: GAME_OVER = True FILL(5, 5, penergy * 2, 10, GREEN) # Green energy bar FILL(5, 5, 50 * 2, 10, BLACK) # Clear the energy bar background STR("Energy:", 5, 20, BLACK, WHITE) STR(str(penergy), 80, 20, BLACK, WHITE) # Random drone if RAND(1, 100) <= 1: # 1% chance to create a drone create_drone(S_WIDTH, RAND(50, 200), 10, 2, BLACK) # Update drones for drone in drones_list: drone[0] -= 3 # Move the drone to the left FILL(drone[0], drone[1], drone[2], drone[3], drone[4]) # Draw the drone # Check for collisions with the p if ( px + pw > drone[0] and px < drone[0] + drone[2] and py + ph > drone[1] and py < drone[1] + drone[3] ): penergy -= 1 # Lose energy FILL(px, py, pw, ph, RED) if penergy < 0: GAME_OVER = True # Player energy is less than zero, game over # Remove drones that go off-screen drones_list = [drone for drone in drones_list if drone[0] > -drone[2]] # Update the screen sleep(0.02) if keydown(KEY_OK): shoot_bullet() for bullet in bullets: bullet["x"] += 5 * bullet["direction"] FILL( bullet["x"], bullet["y"], 4, 2, (RAND(100, 255), RAND(100, 255), RAND(100, 255)), ) if bullet["x"] > S_WIDTH or bullet["x"] < 0: bullets.remove(bullet) # Game over screen FILL(0, 0, S_WIDTH, S_HEIGHT, (255,) * 3) STR("GAME OVER", 100, 80, RED, WHITE) STR("Energy:", 100, 110, BLACK, WHITE) STR(str(penergy), 180, 110, BLACK, WHITE)