Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions plane_battle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import random
random.seed(0)

WIDTH = 20
HEIGHT = 10

player_x = WIDTH // 2
enemy_x = random.randint(0, WIDTH - 1)
bullet = None # (x, y) or None
enemy_dir = 1

print("Welcome to Plane Battle!")
print("Move with 'a' (left) and 'd' (right), shoot with 'w'. Quit with 'q'.")

while True:
board = [[' ' for _ in range(WIDTH)] for _ in range(HEIGHT)]
# Draw enemy
board[0][enemy_x] = 'V'
# Draw bullet
if bullet is not None:
bx, by = bullet
if 0 <= by < HEIGHT:
board[by][bx] = '|'
else:
bullet = None
# Draw player
board[HEIGHT - 1][player_x] = '^'

print('\n'.join(''.join(row) for row in board))

# Check win
if bullet is not None and bullet[1] == 0 and bullet[0] == enemy_x:
print("You win!")
break

cmd = input("Command: ").strip().lower()
if cmd == 'a' and player_x > 0:
player_x -= 1
elif cmd == 'd' and player_x < WIDTH - 1:
player_x += 1
elif cmd == 'w' and bullet is None:
bullet = (player_x, HEIGHT - 2)
elif cmd == 'q':
print("Goodbye!")
break

# Update bullet position
if bullet is not None:
bx, by = bullet
bullet = (bx, by - 1)

# Move enemy
enemy_x += enemy_dir
if enemy_x <= 0 or enemy_x >= WIDTH - 1:
enemy_dir *= -1