Advertisement
EdmundC

airplane_shooter

Jul 15th, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. import curses
  2. import random
  3. import time
  4.  
  5. def main(stdscr):
  6.     # Initialize the screen
  7.     curses.curs_set(0)
  8.     stdscr.nodelay(1)
  9.     stdscr.timeout(100)
  10.  
  11.     # Define game dimensions
  12.     sh, sw = stdscr.getmaxyx()
  13.     w = curses.newwin(sh, sw, 0, 0)
  14.  
  15.     # Define game elements
  16.     plane = '^'
  17.     bullet = '|'
  18.     enemy = 'W'
  19.  
  20.     # Initial position of the plane
  21.     plane_x = sw // 2
  22.     plane_y = sh - 2
  23.  
  24.     # List to keep track of bullets and enemies
  25.     bullets = []
  26.     enemies = []
  27.  
  28.     # Initial score
  29.     score = 0
  30.  
  31.     # Main game loop
  32.     while True:
  33.         # Clear the screen
  34.         w.clear()
  35.  
  36.         # Draw the plane
  37.         w.addch(plane_y, plane_x, plane)
  38.  
  39.         # Draw bullets
  40.         for bx, by in bullets:
  41.             w.addch(by, bx, bullet)
  42.  
  43.         # Move bullets
  44.         bullets = [(bx, by - 1) for bx, by in bullets if by > 0]
  45.  
  46.         # Draw enemies
  47.         for ex, ey in enemies:
  48.             w.addch(ey, ex, enemy)
  49.  
  50.         # Move enemies
  51.         enemies = [(ex, ey + 1) for ex, ey in enemies if ey < sh]
  52.  
  53.         # Detect collisions
  54.         for bx, by in bullets:
  55.             for ex, ey in enemies:
  56.                 if bx == ex and by == ey:
  57.                     score += 1
  58.                     bullets.remove((bx, by))
  59.                     enemies.remove((ex, ey))
  60.                     break
  61.  
  62.         # Get user input
  63.         key = w.getch()
  64.  
  65.         # Quit the game
  66.         if key == ord('q'):
  67.             break
  68.  
  69.         # Move plane
  70.         if key == curses.KEY_LEFT and plane_x > 0:
  71.             plane_x -= 1
  72.         if key == curses.KEY_RIGHT and plane_x < sw - 1:
  73.             plane_x += 1
  74.  
  75.         # Shoot bullet
  76.         if key == ord(' '):
  77.             bullets.append((plane_x, plane_y - 1))
  78.  
  79.         # Generate enemies
  80.         if random.randint(0, 20) == 0:
  81.             enemies.append((random.randint(0, sw - 1), 0))
  82.  
  83.         # Display the score
  84.         w.addstr(0, 0, f'Score: {score}')
  85.  
  86.         # Refresh the screen
  87.         w.refresh()
  88.  
  89.         # Delay for a short period
  90.         time.sleep(0.05)
  91.  
  92. if __name__ == "__main__":
  93.     curses.wrapper(main)
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement