Advertisement
EdmundC

airplane_shooter

Jul 15th, 2024 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.14 KB | None | 0 0
  1. import curses
  2. import random
  3. import time
  4. import os
  5. import pygame
  6.  
  7. def draw_plane(w, plane_x, plane_y, sh, sw):
  8.     plane = [
  9.         "       /\\       ",
  10.         " =====|==|===== ",
  11.         "      /  \\      "
  12.     ]
  13.     # Adjust plane_x and plane_y to wrap around the screen
  14.     plane_x = plane_x % sw
  15.     plane_y = plane_y % sh
  16.  
  17.     for i, line in enumerate(plane):
  18.         # Ensure plane wraps around vertically if it goes off screen
  19.         if plane_y + i < sh:
  20.             w.addstr(plane_y + i, plane_x, line)
  21.  
  22. def draw_start_screen(w, sh, sw, is_dev):
  23.     title = [
  24.         "   _____ _          _____        __               _               ",
  25.         "  / ____| |        |  __ \     / _|             | |              ",
  26.         " | (___ | | ___   _| |  | | ___| |_ ___ _ __   __| | ___ _ __ ___ ",
  27.         "  \___ \| |/ / | | | |  | |/ _ \ _/ _ \ '_ \ / _` |/ _ \ '__/ __|",
  28.         "  ____) |   <| |_| | |__| |  __/ ||  __/ | | | (_| |  __/ |  \__ \\",
  29.         " |_____/|_|\_\\__, |_____/ \___|_| \___|_| |_|\__,_|\___|_|  |___/",
  30.         "               __/ |                                              ",
  31.         "              |___/                                               "
  32.     ]
  33.  
  34.     # Calculate the starting position of the title
  35.     title_x = sw // 2 - len(title[0]) // 2
  36.     title_y = sh // 2 - len(title) // 2
  37.  
  38.     # Draw the title on the screen
  39.     for i, line in enumerate(title):
  40.         w.addstr(title_y + i, title_x, line)
  41.  
  42.     # Add instructions below the title
  43.     instructions = "Press any key to start"
  44.     instr_x = sw // 2 - len(instructions) // 2
  45.     instr_y = title_y + len(title) + 2
  46.     w.addstr(instr_y, instr_x, instructions)
  47.    
  48.     if is_dev:
  49.         w.addstr(instr_y + 1, instr_x, "Dev")
  50.  
  51.     # Add hotkeys info
  52.     hotkeys_info = "HotKeys: \"e\" - Automatic Firing."
  53.     w.addstr(sh - 3, 0, hotkeys_info)
  54.     w.addstr(sh - 2, 0, "Edmund C. 2024")
  55.  
  56. def draw_health_bar(w, health, max_health):
  57.     health_bar = "[" + "█" * health + " " * (max_health - health) + "]"
  58.     w.addstr(1, 0, f"Health: {health_bar}")
  59.  
  60. def main(stdscr):
  61.     # Initialize the screen
  62.     curses.curs_set(0)
  63.     stdscr.nodelay(1)
  64.     stdscr.timeout(0)
  65.     stdscr.keypad(True)
  66.  
  67.     # Define game dimensions
  68.     sh, sw = stdscr.getmaxyx()
  69.     w = curses.newwin(sh, sw, 0, 0)
  70.  
  71.     # Check for developer mode
  72.     is_dev = os.path.exists("i.am.dev.txt")
  73.  
  74.     # Start screen loop
  75.     while True:
  76.         # Clear the screen
  77.         w.clear()
  78.  
  79.         # Draw start screen
  80.         draw_start_screen(w, sh, sw, is_dev)
  81.  
  82.         # Refresh the screen
  83.         w.refresh()
  84.  
  85.         # Wait for user input to start the game
  86.         key = stdscr.getch()
  87.         if key != -1:
  88.             break
  89.  
  90.     # Initialize pygame for sound
  91.     pygame.mixer.init()
  92.     pygame.mixer.music.load('your_audio_file.mp3')
  93.     pygame.mixer.music.play(-1)
  94.  
  95.     # Plane dimensions
  96.     plane_width = 14
  97.     plane_height = 3
  98.  
  99.     # Enemy dimensions
  100.     enemy_width = 4
  101.     enemy_height = 3
  102.  
  103.     # Initial position of the plane (bottom center)
  104.     plane_x = sw // 2 - plane_width // 2
  105.     plane_y = sh - plane_height - 1
  106.  
  107.     # List to keep track of bullets and enemies
  108.     bullets = []
  109.     enemies = []
  110.  
  111.     # Initial score and level
  112.     score = 0
  113.     level = 1
  114.  
  115.     # Health variables
  116.     max_health = 7
  117.     health = max_health
  118.  
  119.     # Timers for enemy and bullet movement
  120.     bullet_move_timer = 0
  121.     enemy_move_timer = 0
  122.  
  123.     # Movement delays
  124.     bullet_delay = 0.1    # Bullet movement interval
  125.     enemy_delay = 0.98    # Enemy movement interval
  126.     plane_move_delay = 0.1 # Plane movement interval
  127.  
  128.     # Flags
  129.     paused = False
  130.     auto_firing = False
  131.     nuked = False
  132.  
  133.     # Game loop variables
  134.     frame_delay = 1 / 30  # Targeting 30 FPS
  135.     last_frame_time = time.time()
  136.     last_bullet_time = time.time()
  137.     last_enemy_time = time.time()
  138.     last_plane_move_time = time.time()
  139.  
  140.     # Main game loop
  141.     while True:
  142.         # Calculate elapsed time since last frame
  143.         current_time = time.time()
  144.         elapsed_time = current_time - last_frame_time
  145.         bullet_elapsed_time = current_time - last_bullet_time
  146.         enemy_elapsed_time = current_time - last_enemy_time
  147.         plane_elapsed_time = current_time - last_plane_move_time
  148.         last_frame_time = current_time
  149.  
  150.         # Clear the screen
  151.         w.clear()
  152.  
  153.         # Draw the plane
  154.         draw_plane(w, plane_x, plane_y, sh, sw)
  155.  
  156.         # Draw bullets
  157.         for bx, by in bullets:
  158.             w.addch(by, bx, '|')
  159.  
  160.         # Draw enemies
  161.         for ex, ey in enemies:
  162.             w.addstr(ey, ex, "####")
  163.             w.addstr(ey + 1, ex, "#  #")
  164.             w.addstr(ey + 2, ex, "####")
  165.  
  166.         # Generate enemies
  167.         if random.randint(0, 20) == 0:
  168.             if len(enemies) < 5:  # Limit enemies on screen to 5
  169.                 enemies.append((random.randint(0, sw - enemy_width), 0))
  170.  
  171.             # Increase level every 20 enemies killed
  172.             if score > 0 and score % 20 == 0:
  173.                 level += 1
  174.                 if level > 255:
  175.                     level = 255
  176.  
  177.                 # Print nuke info
  178.                 w.addstr(3, sw - 40, "Press 'B' to nuke the screen once!")
  179.  
  180.         # Move bullets every 0.1 seconds
  181.         if bullet_elapsed_time >= bullet_delay:
  182.             bullets = [(bx, by - 1) for bx, by in bullets if by > 0]
  183.             last_bullet_time = current_time
  184.  
  185.         # Move enemies every 0.98 seconds
  186.         if enemy_elapsed_time >= enemy_delay:
  187.             enemies = [(ex, ey + 1) for ex, ey in enemies if ey + enemy_height < sh - 1]
  188.             last_enemy_time = current_time
  189.  
  190.             # Remove the oldest enemy if more than 5 enemies are on screen
  191.             while len(enemies) > 5:
  192.                 enemies.pop(0)
  193.  
  194.         # Detect collisions
  195.         for bx, by in bullets:
  196.             for ex, ey in enemies:
  197.                 if ex <= bx < ex + enemy_width and ey <= by < ey + enemy_height:
  198.                     score += 1
  199.                     bullets.remove((bx, by))
  200.                     enemies.remove((ex, ey))
  201.                     break
  202.  
  203.         # Update health if enemies reach the bottom of the screen
  204.         for ex, ey in enemies:
  205.             if ey + enemy_height >= sh - 1:
  206.                 health -= 1
  207.                 enemies.remove((ex, ey))
  208.                 if health <= 0:
  209.                     # Game over
  210.                     w.addstr(sh // 2, sw // 2 - 5, "GAME OVER")
  211.                     w.refresh()
  212.                     time.sleep(3)
  213.                     return
  214.  
  215.         # Display the score, level, and health
  216.         w.addstr(0, 0, f'Score: {score}')
  217.         w.addstr(0, sw - 15, f'Level: {level}')
  218.         draw_health_bar(w, health, max_health)
  219.  
  220.         # Refresh the screen
  221.         w.refresh()
  222.  
  223.         # Get user input
  224.         key = stdscr.getch()
  225.  
  226.         # Check for user input to pause or quit the game
  227.         if key == ord('p'):
  228.             paused = not paused
  229.         elif key == ord('q'):
  230.             break
  231.  
  232.         # Check for auto firing toggle
  233.         if key == ord('e'):
  234.             auto_firing = not auto_firing
  235.  
  236.         # Fire bullets
  237.         if (key == ord(' ') or auto_firing) and not paused:
  238.             # Calculate bullet position from top center of plane
  239.             bullet_x = plane_x + plane_width // 2
  240.             bullet_y = plane_y - 1
  241.             bullets.append((bullet_x, bullet_y))
  242.  
  243.         # Plane movement
  244.         if plane_elapsed_time >= plane_move_delay and not paused:
  245.             if key == curses.KEY_UP or key == ord('w'):
  246.                 plane_y -= 1
  247.             elif key == curses.KEY_DOWN or key == ord('s'):
  248.                 plane_y += 1
  249.             elif key == curses.KEY_LEFT or key == ord('a'):
  250.                 plane_x -= 1
  251.             elif key == curses.KEY_RIGHT or key == ord('d'):
  252.                 plane_x += 1
  253.  
  254.             # Wrap around screen horizontally and vertically
  255.             plane_x = plane_x % sw
  256.             plane_y = plane_y % sh
  257.  
  258.             last_plane_move_time = current_time
  259.  
  260.         # Adjust FPS
  261.         time.sleep(max(0, frame_delay - elapsed_time))
  262.  
  263. # Start the game
  264. curses.wrapper(main)
  265.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement