Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import curses
- import random
- import time
- def draw_plane(w, plane_x, plane_y):
- plane = [
- " /\\ ",
- " =====|==|===== ",
- " / \\ "
- ]
- for i, line in enumerate(plane):
- w.addstr(plane_y + i, plane_x, line)
- def draw_enemy(w, enemy_x, enemy_y):
- enemy = [
- " __/",
- "|SS|",
- " ‾‾ "
- ]
- for i, line in enumerate(enemy):
- w.addstr(enemy_y + i, enemy_x, line)
- def draw_start_screen(w, sh, sw):
- title = [
- " _____ _ _____ __ _ ",
- " / ____| | | __ \\ / _| | | ",
- " | (___ | | ___ _| | | | ___| |_ ___ _ __ __| | ___ _ __ ___ ",
- " \\___ \\| |/ / | | | | | |/ _ \\ _/ _ \\ '_ \\ / _` |/ _ \\ '__/ __|",
- " _____) | <| |_| | |__| | __/ || __/ | | | (_| | __/ | \\__ \\",
- " |_____/|_|\\_\\\\__, |_____/ \\___|_| \\___|_| |_|\\__,_|\\___|_| |___/",
- " __/ | ",
- " |___/ "
- ]
- # Calculate the starting position of the title
- title_x = sw // 2 - len(title[0]) // 2
- title_y = sh // 2 - len(title) // 2
- # Draw the title on the screen
- for i, line in enumerate(title):
- w.addstr(title_y + i, title_x, line)
- # Add instructions below the title
- instructions = "Press any key to start"
- instr_x = sw // 2 - len(instructions) // 2
- instr_y = title_y + len(title) + 2
- w.addstr(instr_y, instr_x, instructions)
- def draw_health_bar(w, health, max_health):
- health_bar = "[" + "█" * health + " " * (max_health - health) + "]"
- w.addstr(1, 0, f"Health: {health_bar}")
- def draw_level_up(w, level, sh, sw):
- if level % 5 == 0:
- # Define the ASCII art to display
- ascii_art = [
- "┌────┬────┐",
- "│ │ │",
- "├─ │ ─┤",
- "│ │ │",
- "│ │ │",
- "│ │ │",
- "│ │ │",
- "│ │ │",
- "│ │ │",
- "│ │ │",
- "│ │ │",
- "│ │ │",
- "├─ │ ─┤",
- "│ │ │",
- "│ │ │",
- "│ │ │",
- "│ │ │",
- "│ │ │",
- "│ │ │",
- "│ │ │",
- "│ │ │",
- "│ │ │",
- "│ │ │",
- "├─ │ ─┤",
- "│ │ │",
- "│ ┌┐ │ ┌┐ │",
- "│ ││ │ ││ │",
- "│ └┘ │ └┘ │",
- "│ │ │",
- "│|||||||||│",
- "│ │ │",
- "└────┴────┘"
- ]
- # Calculate position to display ASCII art on the right side
- art_x = sw - len(ascii_art[0]) - 2
- art_y = sh // 2 - len(ascii_art) // 2
- # Draw the ASCII art
- for i, line in enumerate(ascii_art):
- w.addstr(art_y + i, art_x, line)
- def main(stdscr):
- # Initialize the screen
- curses.curs_set(0)
- stdscr.nodelay(1)
- stdscr.timeout(0)
- stdscr.keypad(True)
- # Define game dimensions
- sh, sw = stdscr.getmaxyx()
- w = curses.newwin(sh, sw, 0, 0)
- # Start screen loop
- while True:
- # Clear the screen
- w.clear()
- # Draw start screen
- draw_start_screen(w, sh, sw)
- # Refresh the screen
- w.refresh()
- # Wait for user input to start the game
- key = stdscr.getch()
- if key != -1:
- break
- # Plane dimensions
- plane_width = 14
- plane_height = 3
- # Enemy dimensions
- enemy_width = 4
- enemy_height = 3
- # Initial position of the plane (bottom center)
- plane_x = sw // 2 - plane_width // 2
- plane_y = sh - plane_height - 1
- # List to keep track of bullets and enemies
- bullets = []
- enemies = []
- # Initial score and level
- score = 0
- level = 1
- # Health variables
- max_health = 7
- health = max_health
- # Timers for enemy and bullet movement
- bullet_move_timer = 0
- enemy_move_timer = 0
- # Movement delays
- bullet_delay = 0.1 # Bullet movement interval
- enemy_delay = 0.4 # Enemy movement interval
- plane_move_delay = 0.2 # Plane movement interval
- # Pause flag
- paused = False
- # Game loop variables
- frame_delay = 1 / 30 # Targeting 30 FPS
- last_frame_time = time.time()
- last_bullet_time = time.time()
- last_enemy_time = time.time()
- last_plane_move_time = time.time()
- # Main game loop
- while True:
- # Calculate elapsed time since last frame
- current_time = time.time()
- elapsed_time = current_time - last_frame_time
- bullet_elapsed_time = current_time - last_bullet_time
- enemy_elapsed_time = current_time - last_enemy_time
- plane_move_elapsed_time = current_time - last_plane_move_time
- last_frame_time = current_time
- # Clear the screen
- w.clear()
- # Draw the plane
- draw_plane(w, plane_x, plane_y)
- # Draw bullets
- for bx, by in bullets:
- w.addch(by, bx, '|')
- # Draw enemies
- for ex, ey in enemies:
- draw_enemy(w, ex, ey)
- # Generate enemies
- if random.randint(0, 20) == 0:
- if len(enemies) < 5: # Limit enemies on screen to 5
- enemies.append((random.randint(0, sw - enemy_width), 0))
- # Increase level every 20 enemies killed
- if score > 0 and score % 20 == 0:
- level += 1
- if level > 255:
- level = 255
- # Move bullets every 0.1 seconds
- if bullet_elapsed_time >= bullet_delay:
- bullets = [(bx, by - 1) for bx, by in bullets if by > 0]
- last_bullet_time = current_time
- # Move enemies every 0.4 seconds
- if enemy_elapsed_time >= enemy_delay:
- enemies = [(ex, ey + 1) for ex, ey in enemies if ey + enemy_height < sh]
- last_enemy_time = current_time
- # Remove the oldest enemy if more than 5 enemies are on screen
- while len(enemies) > 5:
- enemies.pop(0)
- # Move plane every 0.2 seconds
- if plane_move_elapsed_time >= plane_move_delay:
- # Get user input
- key = stdscr.getch()
- # Move plane
- if key == ord('w') and plane_y > 0:
- plane_y -= 1
- elif key == ord('s') and plane_y < sh - plane_height - 1:
- plane_y += 1
- elif key == ord('a') and plane_x > 0:
- plane_x -= 1
- elif key == ord('d') and plane_x < sw - plane_width:
- plane_x += 1
- last_plane_move_time = current_time
- # Detect collisions
- for bx, by in bullets:
- for ex, ey in enemies:
- if ex <= bx < ex + enemy_width and ey <= by < ey + enemy_height:
- score += 1
- bullets.remove((bx, by))
- enemies.remove((ex, ey))
- break
- # Update health if enemies reach the
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement