Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import curses
- import random
- import time
- # Initialize the game window
- stdscr = curses.initscr()
- curses.curs_set(0) # Hide the cursor
- stdscr.nodelay(1) # Make getch non-blocking
- stdscr.timeout(100) # Screen refresh delay (ms)
- # Constants
- PLANE_CHAR = '^'
- BULLET_CHAR = '|'
- DRONE_CHAR = 'X'
- PLANE_Y = curses.LINES - 2
- def main(stdscr):
- # Initialize positions
- plane_x = curses.COLS // 2
- bullets = []
- drones = []
- score = 0
- running = True
- while running:
- stdscr.clear()
- # Control plane movement
- key = stdscr.getch()
- if key == curses.KEY_RIGHT and plane_x < curses.COLS - 2:
- plane_x += 1
- elif key == curses.KEY_LEFT and plane_x > 1:
- plane_x -= 1
- elif key == ord(' '):
- bullets.append((PLANE_Y - 1, plane_x))
- elif key == ord('q'):
- running = False
- # Update bullet positions
- bullets = [(y - 1, x) for y, x in bullets if y > 0]
- # Spawn drones randomly
- if random.randint(1, 10) == 1:
- drones.append((0, random.randint(1, curses.COLS - 2)))
- # Update drone positions
- drones = [(y + 1, x) for y, x in drones if y < curses.LINES - 1]
- # Check for collisions
- for bullet in bullets:
- for drone in drones:
- if bullet[0] == drone[0] and bullet[1] == drone[1]:
- bullets.remove(bullet)
- drones.remove(drone)
- score += 1
- break
- # Draw the plane
- stdscr.addch(PLANE_Y, plane_x, PLANE_CHAR)
- # Draw bullets
- for y, x in bullets:
- stdscr.addch(y, x, BULLET_CHAR)
- # Draw drones
- for y, x in drones:
- stdscr.addch(y, x, DRONE_CHAR)
- # Display score
- stdscr.addstr(0, 2, f'Score: {score}')
- stdscr.refresh()
- time.sleep(0.05)
- # Clean up
- curses.endwin()
- curses.wrapper(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement