Advertisement
EdmundC

airplane_shooter

Jul 15th, 2024 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.33 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.         r"   _____ _          _____        __               _               ",
  26.         r"  / ____| |        |  __ \     / _|             | |              ",
  27.         r" | (___ | | ___   _| |  | | ___| |_ ___ _ __   __| | ___ _ __ ___ ",
  28.         r"  \___ \| |/ / | | | |  | |/ _ \ _/ _ \ '_ \ / _` |/ _ \ '__/ __|",
  29.         r"  ____) |   <| |_| | |__| |  __/ ||  __/ | | | (_| |  __/ |  \__ \\",
  30.         r" |_____/|_|\_\\__, |_____/ \___|_| \___|_| |_|\__,_|\___|_|  |___/",
  31.         r"               __/ |                                              ",
  32.         r"              |___/                                               "
  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 main(stdscr):
  54.     # Initialize the screen
  55.     curses.curs_set(0)
  56.     stdscr.nodelay(1)
  57.     stdscr.timeout(0)
  58.     stdscr.keypad(True)
  59.  
  60.     # Define game dimensions
  61.     sh, sw = stdscr.getmaxyx()
  62.     w = curses.newwin(sh, sw, 0, 0)
  63.  
  64.     # Start screen loop
  65.     while True:
  66.         # Clear the screen
  67.         w.clear()
  68.  
  69.         # Draw start screen
  70.         draw_start_screen(w, sh, sw)
  71.  
  72.         # Refresh the screen
  73.         w.refresh()
  74.  
  75.         # Wait for user input to start the game
  76.         key = stdscr.getch()
  77.         if key != -1:
  78.             break
  79.  
  80.     # Plane dimensions
  81.     plane_width = 14
  82.     plane_height = 3
  83.  
  84.     # Enemy dimensions
  85.     enemy_width = 4
  86.     enemy_height = 3
  87.  
  88.     # Initial position of the plane (bottom center)
  89.     plane_x = sw // 2 - plane_width // 2
  90.     plane_y = sh - plane_height - 1
  91.  
  92.     # List to keep track of bullets and enemies
  93.     bullets = []
  94.     enemies = []
  95.  
  96.     # Initial score and level
  97.     score = 0
  98.     level = 1
  99.  
  100.     # Health variables
  101.     max_health = 7
  102.     health = max_health
  103.  
  104.     # Timers for enemy and bullet movement
  105.     bullet_move_timer = 0
  106.     enemy_move_timer = 0
  107.  
  108.     # Movement delays
  109.     bullet_delay = 0.1    # Bullet movement interval
  110.     enemy_delay = 0.4     # Enemy movement interval
  111.  
  112.     # Pause flag
  113.     paused = False
  114.  
  115.     # Game loop variables
  116.     frame_delay = 1 / 30  # Targeting 30 FPS
  117.     last_frame_time = time.time()
  118.     last_bullet_time = time.time()
  119.     last_enemy_time = time.time()
  120.  
  121.     # Main game loop
  122.     while True:
  123.         # Calculate elapsed time since last frame
  124.         current_time = time.time()
  125.         elapsed_time = current_time - last_frame_time
  126.         bullet_elapsed_time = current_time - last_bullet_time
  127.         enemy_elapsed_time = current_time - last_enemy_time
  128.         last_frame_time = current_time
  129.  
  130.         # Clear the screen
  131.         w.clear()
  132.  
  133.         # Draw the background
  134.         for y in range(sh):
  135.             for x in range(sw):
  136.                 if (x == 0 or x == sw - 1 or y == 0 or y == sh - 1):
  137.                     w.addch(y, x, ' ')
  138.                 else:
  139.                     w.addch(y, x, '█')
  140.  
  141.         # Draw the plane
  142.         draw_plane(w, plane_x, plane_y)
  143.  
  144.         # Draw bullets
  145.         for bx, by in bullets:
  146.             w.addch(by, bx, '|')
  147.  
  148.         # Draw enemies
  149.         for ex, ey in enemies:
  150.             draw_enemy(w, ex, ey)
  151.  
  152.         # Generate enemies
  153.         if random.randint(0, 20) == 0:
  154.             if len(enemies) < 5:  # Limit enemies on screen to 5
  155.                 enemies.append((random.randint(0, sw - enemy_width), 0))
  156.  
  157.             # Increase level every 20 enemies killed
  158.             if score > 0 and score % 20 == 0:
  159.                 level += 1
  160.                 if level > 255:
  161.                     level = 255
  162.  
  163.         # Move bullets every 0.1 seconds
  164.         if bullet_elapsed_time >= bullet_delay:
  165.             bullets = [(bx, by - 1) for bx, by in bullets if by > 0]
  166.             last_bullet_time = current_time
  167.  
  168.         # Move enemies every 0.4 seconds
  169.         if enemy_elapsed_time >= enemy_delay:
  170.             enemies = [(ex, ey + 1) for ex, ey in enemies if ey + enemy_height < sh]
  171.             last_enemy_time = current_time
  172.  
  173.             # Remove the oldest enemy if more than 5 enemies are on screen
  174.             while len(enemies) > 5:
  175.                 enemies.pop(0)
  176.  
  177.         # Detect collisions
  178.         for bx, by in bullets:
  179.             for ex, ey in enemies:
  180.                 if ex <= bx < ex + enemy_width and ey <= by < ey + enemy_height:
  181.                     score += 1
  182.                     bullets.remove((bx, by))
  183.                     enemies.remove((ex, ey))
  184.                     break
  185.  
  186.         # Update health if enemies reach the bottom of the screen
  187.         for ex, ey in enemies:
  188.             if ey + enemy_height >= sh:
  189.                 health -= 1
  190.                 enemies.remove((ex, ey))
  191.                 if health <= 0:
  192.                     # Game over
  193.                     w.addstr(sh // 2, sw // 2 - 5, "GAME OVER")
  194.                     w.refresh()
  195.                     time.sleep(3)
  196.                     return
  197.  
  198.         # Display the score, level, and health
  199.         w.addstr(0, 0, f'Score: {score}')
  200.         w.addstr(0, sw - 15, f'Level: {level}')
  201.         draw_health_bar(w, health, max_health)
  202.  
  203.         # Refresh the screen
  204.         w.refresh()
  205.  
  206.         # Get user input
  207.         key = stdscr.getch()
  208.  
  209.         # Check for user input to pause or quit the game
  210.         if key == ord('p'):
  211.             paused = not paused
  212.         elif key == ord('q'):
  213.             break
  214.  
  215.         # Fire bullets
  216.         if key == ord(' ') and not paused:
  217.             # Calculate bullet position from top center of plane
  218.             bullet_x = plane_x + plane_width // 2
  219.             bullet_y = plane_y
  220.             bullets.append((bullet_x, bullet_y))
  221.  
  222.         # Move plane
  223.         if key == ord('w') and plane_y > 0:
  224.             plane_y -= 1
  225.         elif key == ord('s') and plane_y < sh - plane_height - 1:
  226.             plane_y += 1
  227.         elif key == ord('a'):
  228.             plane_x -= 1
  229.             if plane_x < 0:
  230.                 plane_x = sw - plane_width
  231.         elif key == ord('d'):
  232.             plane_x += 1
  233.             if plane_x > sw - plane_width:
  234.                 plane_x = 0
  235.  
  236.         # Pause game if paused
  237.         if paused:
  238.             continue
  239.  
  240.         # Cap the elapsed time to ensure a maximum frame rate
  241.         if elapsed_time < frame_delay:
  242.             time.sleep(frame_delay - elapsed_time)
  243.  
  244. if __name__ == "__main__":
  245.     curses.wrapper(main)
  246.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement