Advertisement
EdmundC

airplane_shooter

Oct 5th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. import curses
  2. import random
  3. import time
  4.  
  5. # Initialize the game window
  6. stdscr = curses.initscr()
  7. curses.curs_set(0)  # Hide the cursor
  8. stdscr.nodelay(1)  # Make getch non-blocking
  9. stdscr.timeout(100)  # Screen refresh delay (ms)
  10.  
  11. # Constants
  12. PLANE_CHAR = '^'
  13. BULLET_CHAR = '|'
  14. DRONE_CHAR = 'X'
  15. PLANE_Y = curses.LINES - 2
  16.  
  17. def main(stdscr):
  18.     # Initialize positions
  19.     plane_x = curses.COLS // 2
  20.     bullets = []
  21.     drones = []
  22.     score = 0
  23.     running = True
  24.    
  25.     while running:
  26.         stdscr.clear()
  27.        
  28.         # Control plane movement
  29.         key = stdscr.getch()
  30.         if key == curses.KEY_RIGHT and plane_x < curses.COLS - 2:
  31.             plane_x += 1
  32.         elif key == curses.KEY_LEFT and plane_x > 1:
  33.             plane_x -= 1
  34.         elif key == ord(' '):
  35.             bullets.append((PLANE_Y - 1, plane_x))
  36.         elif key == ord('q'):
  37.             running = False
  38.        
  39.         # Update bullet positions
  40.         bullets = [(y - 1, x) for y, x in bullets if y > 0]
  41.        
  42.         # Spawn drones randomly
  43.         if random.randint(1, 10) == 1:
  44.             drones.append((0, random.randint(1, curses.COLS - 2)))
  45.        
  46.         # Update drone positions
  47.         drones = [(y + 1, x) for y, x in drones if y < curses.LINES - 1]
  48.        
  49.         # Check for collisions
  50.         for bullet in bullets:
  51.             for drone in drones:
  52.                 if bullet[0] == drone[0] and bullet[1] == drone[1]:
  53.                     bullets.remove(bullet)
  54.                     drones.remove(drone)
  55.                     score += 1
  56.                     break
  57.        
  58.         # Draw the plane
  59.         stdscr.addch(PLANE_Y, plane_x, PLANE_CHAR)
  60.        
  61.         # Draw bullets
  62.         for y, x in bullets:
  63.             stdscr.addch(y, x, BULLET_CHAR)
  64.        
  65.         # Draw drones
  66.         for y, x in drones:
  67.             stdscr.addch(y, x, DRONE_CHAR)
  68.        
  69.         # Display score
  70.         stdscr.addstr(0, 2, f'Score: {score}')
  71.        
  72.         stdscr.refresh()
  73.         time.sleep(0.05)
  74.    
  75.     # Clean up
  76.     curses.endwin()
  77.  
  78. curses.wrapper(main)
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement