Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import curses
- import random
- import time
- def main(stdscr):
- # Initialize the screen
- curses.curs_set(0)
- stdscr.nodelay(1)
- stdscr.timeout(100)
- # Define game dimensions
- sh, sw = stdscr.getmaxyx()
- w = curses.newwin(sh, sw, 0, 0)
- # Define game elements
- plane = '^'
- bullet = '|'
- enemy = 'W'
- # Initial position of the plane
- plane_x = sw // 2
- plane_y = sh - 2
- # List to keep track of bullets and enemies
- bullets = []
- enemies = []
- # Initial score
- score = 0
- # Main game loop
- while True:
- # Clear the screen
- w.clear()
- # Draw the plane
- w.addch(plane_y, plane_x, plane)
- # Draw bullets
- for bx, by in bullets:
- w.addch(by, bx, bullet)
- # Move bullets
- bullets = [(bx, by - 1) for bx, by in bullets if by > 0]
- # Draw enemies
- for ex, ey in enemies:
- w.addch(ey, ex, enemy)
- # Move enemies
- enemies = [(ex, ey + 1) for ex, ey in enemies if ey < sh]
- # Detect collisions
- for bx, by in bullets:
- for ex, ey in enemies:
- if bx == ex and by == ey:
- score += 1
- bullets.remove((bx, by))
- enemies.remove((ex, ey))
- break
- # Get user input
- key = w.getch()
- # Quit the game
- if key == ord('q'):
- break
- # Move plane
- if key == curses.KEY_LEFT and plane_x > 0:
- plane_x -= 1
- if key == curses.KEY_RIGHT and plane_x < sw - 1:
- plane_x += 1
- # Shoot bullet
- if key == ord(' '):
- bullets.append((plane_x, plane_y - 1))
- # Generate enemies
- if random.randint(0, 20) == 0:
- enemies.append((random.randint(0, sw - 1), 0))
- # Display the score
- w.addstr(0, 0, f'Score: {score}')
- # Refresh the screen
- w.refresh()
- # Delay for a short period
- time.sleep(0.05)
- if __name__ == "__main__":
- curses.wrapper(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement