Advertisement
EdmundC

airplane_shooter

Jul 15th, 2024 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.54 KB | None | 0 0
  1. import curses
  2. import random
  3. import time
  4.  
  5. def draw_plane(w, plane_x, plane_y):
  6.     plane = [
  7.         "       /\\       ",
  8.         " =====|==|===== ",
  9.         "      /  \\      "
  10.     ]
  11.     for i, line in enumerate(plane):
  12.         w.addstr(plane_y + i, plane_x, line)
  13.  
  14. def draw_enemy(w, enemy_x, enemy_y):
  15.     enemy = [
  16.         " __/",
  17.         "|SS|",
  18.         " ‾‾ "
  19.     ]
  20.     for i, line in enumerate(enemy):
  21.         w.addstr(enemy_y + i, enemy_x, line)
  22.  
  23. def draw_start_screen(w, sh, sw):
  24.     title = [
  25.         "   _____ _          _____        __               _               ",
  26.         "  / ____| |        |  __ \\      / _|             | |              ",
  27.         " | (___ | | ___   _| |  | | ___| |_ ___ _ __   __| | ___ _ __ ___ ",
  28.         "  \\___ \\| |/ / | | | |  | |/ _ \\  _/ _ \\ '_ \\ / _` |/ _ \\ '__/ __|",
  29.         "  _____) |   <| |_| | |__| |  __/ ||  __/ | | | (_| |  __/ |  \\__ \\",
  30.         " |_____/|_|\\_\\\\__, |_____/ \\___|_| \\___|_| |_|\\__,_|\\___|_|  |___/",
  31.         "               __/ |                                              ",
  32.         "              |___/                                               "
  33.     ]
  34.  
  35.     # Calculate the starting position of the title
  36.     title_x = sw // 2 - len(title[0]) // 2
  37.     title_y = sh // 2 - len(title) // 2
  38.  
  39.     # Draw the title on the screen
  40.     for i, line in enumerate(title):
  41.         w.addstr(title_y + i, title_x, line)
  42.  
  43.     # Add instructions below the title
  44.     instructions = "Press any key to start"
  45.     instr_x = sw // 2 - len(instructions) // 2
  46.     instr_y = title_y + len(title) + 2
  47.     w.addstr(instr_y, instr_x, instructions)
  48.  
  49. def draw_health_bar(w, health, max_health):
  50.     health_bar = "[" + "█" * health + " " * (max_health - health) + "]"
  51.     w.addstr(1, 0, f"Health: {health_bar}")
  52.  
  53. def draw_level_up(w, level, sh, sw):
  54.     if level % 5 == 0:
  55.         # Define the ASCII art to display
  56.         ascii_art = [
  57.             "┌────┬────┐",
  58.             "│    │    │",
  59.             "├─   │   ─┤",
  60.             "│    │    │",
  61.             "│    │    │",
  62.             "│    │    │",
  63.             "│    │    │",
  64.             "│    │    │",
  65.             "│    │    │",
  66.             "│    │    │",
  67.             "│    │    │",
  68.             "│    │    │",
  69.             "├─   │   ─┤",
  70.             "│    │    │",
  71.             "│    │    │",
  72.             "│    │    │",
  73.             "│    │    │",
  74.             "│    │    │",
  75.             "│    │    │",
  76.             "│    │    │",
  77.             "│    │    │",
  78.             "│    │    │",
  79.             "│    │    │",
  80.             "├─   │   ─┤",
  81.             "│    │    │",
  82.             "│ ┌┐ │ ┌┐ │",
  83.             "│ ││ │ ││ │",
  84.             "│ └┘ │ └┘ │",
  85.             "│    │    │",
  86.             "│|||||||||│",
  87.             "│    │    │",
  88.             "└────┴────┘"
  89.         ]
  90.  
  91.         # Calculate position to display ASCII art on the right side
  92.         art_x = sw - len(ascii_art[0]) - 2
  93.         art_y = sh // 2 - len(ascii_art) // 2
  94.  
  95.         # Draw the ASCII art
  96.         for i, line in enumerate(ascii_art):
  97.             w.addstr(art_y + i, art_x, line)
  98.  
  99. def main(stdscr):
  100.     # Initialize the screen
  101.     curses.curs_set(0)
  102.     stdscr.nodelay(1)
  103.     stdscr.timeout(0)
  104.     stdscr.keypad(True)
  105.  
  106.     # Define game dimensions
  107.     sh, sw = stdscr.getmaxyx()
  108.     w = curses.newwin(sh, sw, 0, 0)
  109.  
  110.     # Start screen loop
  111.     while True:
  112.         # Clear the screen
  113.         w.clear()
  114.  
  115.         # Draw start screen
  116.         draw_start_screen(w, sh, sw)
  117.  
  118.         # Refresh the screen
  119.         w.refresh()
  120.  
  121.         # Wait for user input to start the game
  122.         key = stdscr.getch()
  123.         if key != -1:
  124.             break
  125.  
  126.     # Plane dimensions
  127.     plane_width = 14
  128.     plane_height = 3
  129.  
  130.     # Enemy dimensions
  131.     enemy_width = 4
  132.     enemy_height = 3
  133.  
  134.     # Initial position of the plane (bottom center)
  135.     plane_x = sw // 2 - plane_width // 2
  136.     plane_y = sh - plane_height - 1
  137.  
  138.     # List to keep track of bullets and enemies
  139.     bullets = []
  140.     enemies = []
  141.  
  142.     # Initial score and level
  143.     score = 0
  144.     level = 1
  145.  
  146.     # Health variables
  147.     max_health = 7
  148.     health = max_health
  149.  
  150.     # Timers for enemy and bullet movement
  151.     bullet_move_timer = 0
  152.     enemy_move_timer = 0
  153.  
  154.     # Movement delays
  155.     bullet_delay = 0.1    # Bullet movement interval
  156.     enemy_delay = 0.4     # Enemy movement interval
  157.     plane_move_delay = 0.2  # Plane movement interval
  158.  
  159.     # Pause flag
  160.     paused = False
  161.  
  162.     # Game loop variables
  163.     frame_delay = 1 / 30  # Targeting 30 FPS
  164.     last_frame_time = time.time()
  165.     last_bullet_time = time.time()
  166.     last_enemy_time = time.time()
  167.     last_plane_move_time = time.time()
  168.  
  169.     # Main game loop
  170.     while True:
  171.         # Calculate elapsed time since last frame
  172.         current_time = time.time()
  173.         elapsed_time = current_time - last_frame_time
  174.         bullet_elapsed_time = current_time - last_bullet_time
  175.         enemy_elapsed_time = current_time - last_enemy_time
  176.         plane_move_elapsed_time = current_time - last_plane_move_time
  177.         last_frame_time = current_time
  178.  
  179.         # Clear the screen
  180.         w.clear()
  181.  
  182.         # Draw the plane
  183.         draw_plane(w, plane_x, plane_y)
  184.  
  185.         # Draw bullets
  186.         for bx, by in bullets:
  187.             w.addch(by, bx, '|')
  188.  
  189.         # Draw enemies
  190.         for ex, ey in enemies:
  191.             draw_enemy(w, ex, ey)
  192.  
  193.         # Generate enemies
  194.         if random.randint(0, 20) == 0:
  195.             if len(enemies) < 5:  # Limit enemies on screen to 5
  196.                 enemies.append((random.randint(0, sw - enemy_width), 0))
  197.  
  198.             # Increase level every 20 enemies killed
  199.             if score > 0 and score % 20 == 0:
  200.                 level += 1
  201.                 if level > 255:
  202.                     level = 255
  203.  
  204.         # Move bullets every 0.1 seconds
  205.         if bullet_elapsed_time >= bullet_delay:
  206.             bullets = [(bx, by - 1) for bx, by in bullets if by > 0]
  207.             last_bullet_time = current_time
  208.  
  209.         # Move enemies every 0.4 seconds
  210.         if enemy_elapsed_time >= enemy_delay:
  211.             enemies = [(ex, ey + 1) for ex, ey in enemies if ey + enemy_height < sh]
  212.             last_enemy_time = current_time
  213.  
  214.             # Remove the oldest enemy if more than 5 enemies are on screen
  215.             while len(enemies) > 5:
  216.                 enemies.pop(0)
  217.  
  218.         # Move plane every 0.2 seconds
  219.         if plane_move_elapsed_time >= plane_move_delay:
  220.             # Get user input
  221.             key = stdscr.getch()
  222.  
  223.             # Move plane
  224.             if key == ord('w') and plane_y > 0:
  225.                 plane_y -= 1
  226.             elif key == ord('s') and plane_y < sh - plane_height - 1:
  227.                 plane_y += 1
  228.             elif key == ord('a') and plane_x > 0:
  229.                 plane_x -= 1
  230.             elif key == ord('d') and plane_x < sw - plane_width:
  231.                 plane_x += 1
  232.  
  233.             last_plane_move_time = current_time
  234.  
  235.         # Detect collisions
  236.         for bx, by in bullets:
  237.             for ex, ey in enemies:
  238.                 if ex <= bx < ex + enemy_width and ey <= by < ey + enemy_height:
  239.                     score += 1
  240.                     bullets.remove((bx, by))
  241.                     enemies.remove((ex, ey))
  242.                     break
  243.  
  244.         # Update health if enemies reach the
  245.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement