Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import curses
- import random
- import time
- def draw_plane(w, plane_x, plane_y):
- plane = [
- " /\\ ",
- " =====|==|===== ",
- " / \\ "
- ]
- for i, line in enumerate(plane):
- w.addstr(plane_y + i, plane_x, line)
- def draw_enemy(w, enemy_x, enemy_y):
- enemy = [
- " __/",
- "|SS|",
- " ‾‾ "
- ]
- for i, line in enumerate(enemy):
- w.addstr(enemy_y + i, enemy_x, line)
- def draw_start_screen(w, sh, sw):
- title = [
- r" _____ _ _____ __ _ ",
- r" / ____| | | __ \ / _| | | ",
- r" | (___ | | ___ _| | | | ___| |_ ___ _ __ __| | ___ _ __ ___ ",
- r" \___ \| |/ / | | | | | |/ _ \ _/ _ \ '_ \ / _` |/ _ \ '__/ __|",
- r" ____) | <| |_| | |__| | __/ || __/ | | | (_| | __/ | \__ \\",
- r" |_____/|_|\_\\__, |_____/ \___|_| \___|_| |_|\__,_|\___|_| |___/",
- r" __/ | ",
- r" |___/ "
- ]
- # Calculate the starting position of the title
- title_x = sw // 2 - len(title[0]) // 2
- title_y = sh // 2 - len(title) // 2
- # Draw the title on the screen
- for i, line in enumerate(title):
- w.addstr(title_y + i, title_x, line)
- # Add instructions below the title
- instructions = "Press any key to start"
- instr_x = sw // 2 - len(instructions) // 2
- instr_y = title_y + len(title) + 2
- w.addstr(instr_y, instr_x, instructions)
- def draw_health_bar(w, health, max_health):
- health_bar = "[" + "█" * health + " " * (max_health - health) + "]"
- w.addstr(1, 0, f"Health: {health_bar}")
- def main(stdscr):
- # Initialize the screen
- curses.curs_set(0)
- stdscr.nodelay(1)
- stdscr.timeout(0)
- stdscr.keypad(True)
- # Define game dimensions
- sh, sw = stdscr.getmaxyx()
- w = curses.newwin(sh, sw, 0, 0)
- # Start screen loop
- while True:
- # Clear the screen
- w.clear()
- # Draw start screen
- draw_start_screen(w, sh, sw)
- # Refresh the screen
- w.refresh()
- # Wait for user input to start the game
- key = stdscr.getch()
- if key != -1:
- break
- # Plane dimensions
- plane_width = 14
- plane_height = 3
- # Enemy dimensions
- enemy_width = 4
- enemy_height = 3
- # Initial position of the plane (bottom center)
- plane_x = sw // 2 - plane_width // 2
- plane_y = sh - plane_height - 1
- # List to keep track of bullets and enemies
- bullets = []
- enemies = []
- # Initial score and level
- score = 0
- level = 1
- # Health variables
- max_health = 7
- health = max_health
- # Timers for enemy and bullet movement
- bullet_move_timer = 0
- enemy_move_timer = 0
- # Movement delays
- bullet_delay = 0.1 # Bullet movement interval
- enemy_delay = 0.4 # Enemy movement interval
- # Pause flag
- paused = False
- # Game loop variables
- frame_delay = 1 / 30 # Targeting 30 FPS
- last_frame_time = time.time()
- last_bullet_time = time.time()
- last_enemy_time = time.time()
- # Main game loop
- while True:
- # Calculate elapsed time since last frame
- current_time = time.time()
- elapsed_time = current_time - last_frame_time
- bullet_elapsed_time = current_time - last_bullet_time
- enemy_elapsed_time = current_time - last_enemy_time
- last_frame_time = current_time
- # Clear the screen
- w.clear()
- # Draw the background
- for y in range(sh):
- for x in range(sw):
- if (x == 0 or x == sw - 1 or y == 0 or y == sh - 1):
- w.addch(y, x, ' ')
- else:
- w.addch(y, x, '█')
- # Draw the plane
- draw_plane(w, plane_x, plane_y)
- # Draw bullets
- for bx, by in bullets:
- w.addch(by, bx, '|')
- # Draw enemies
- for ex, ey in enemies:
- draw_enemy(w, ex, ey)
- # Generate enemies
- if random.randint(0, 20) == 0:
- if len(enemies) < 5: # Limit enemies on screen to 5
- enemies.append((random.randint(0, sw - enemy_width), 0))
- # Increase level every 20 enemies killed
- if score > 0 and score % 20 == 0:
- level += 1
- if level > 255:
- level = 255
- # Move bullets every 0.1 seconds
- if bullet_elapsed_time >= bullet_delay:
- bullets = [(bx, by - 1) for bx, by in bullets if by > 0]
- last_bullet_time = current_time
- # Move enemies every 0.4 seconds
- if enemy_elapsed_time >= enemy_delay:
- enemies = [(ex, ey + 1) for ex, ey in enemies if ey + enemy_height < sh]
- last_enemy_time = current_time
- # Remove the oldest enemy if more than 5 enemies are on screen
- while len(enemies) > 5:
- enemies.pop(0)
- # Detect collisions
- for bx, by in bullets:
- for ex, ey in enemies:
- if ex <= bx < ex + enemy_width and ey <= by < ey + enemy_height:
- score += 1
- bullets.remove((bx, by))
- enemies.remove((ex, ey))
- break
- # Update health if enemies reach the bottom of the screen
- for ex, ey in enemies:
- if ey + enemy_height >= sh:
- health -= 1
- enemies.remove((ex, ey))
- if health <= 0:
- # Game over
- w.addstr(sh // 2, sw // 2 - 5, "GAME OVER")
- w.refresh()
- time.sleep(3)
- return
- # Display the score, level, and health
- w.addstr(0, 0, f'Score: {score}')
- w.addstr(0, sw - 15, f'Level: {level}')
- draw_health_bar(w, health, max_health)
- # Refresh the screen
- w.refresh()
- # Get user input
- key = stdscr.getch()
- # Check for user input to pause or quit the game
- if key == ord('p'):
- paused = not paused
- elif key == ord('q'):
- break
- # Fire bullets
- if key == ord(' ') and not paused:
- # Calculate bullet position from top center of plane
- bullet_x = plane_x + plane_width // 2
- bullet_y = plane_y
- bullets.append((bullet_x, bullet_y))
- # Move plane
- if key == ord('w') and plane_y > 0:
- plane_y -= 1
- elif key == ord('s') and plane_y < sh - plane_height - 1:
- plane_y += 1
- elif key == ord('a'):
- plane_x -= 1
- if plane_x < 0:
- plane_x = sw - plane_width
- elif key == ord('d'):
- plane_x += 1
- if plane_x > sw - plane_width:
- plane_x = 0
- # Pause game if paused
- if paused:
- continue
- # Cap the elapsed time to ensure a maximum frame rate
- if elapsed_time < frame_delay:
- time.sleep(frame_delay - elapsed_time)
- if __name__ == "__main__":
- curses.wrapper(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement