-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
96 lines (76 loc) · 2.39 KB
/
Copy pathgame.py
File metadata and controls
96 lines (76 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import pygame
import random
import sys
# Initialize Pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Termux Dodge")
# Colors
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
BLACK = (0, 0, 0)
# Player settings
player_radius = 25
player_x = WIDTH // 2
player_y = HEIGHT - 100
# Enemy settings
enemy_size = 50
enemy_speed = 7
enemies = []
# Clock to control frame rate
clock = pygame.time.Clock()
score = 0
font = pygame.font.SysFont("monospace", 35)
def spawn_enemy():
x_pos = random.randint(0, WIDTH - enemy_size)
enemies.append([x_pos, 0])
running = True
while running:
# 1. Event Handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Move player to touch/mouse position
elif event.type == pygame.MOUSEMOTION:
# event.pos returns a tuple (x, y)
mouse_x, mouse_y = event.pos
player_x = mouse_x
# We keep player_y fixed at the bottom for this game
# 2. Update Game Logic
# Spawn enemies roughly every 30 frames
if random.randint(1, 30) == 1:
spawn_enemy()
# Move enemies
for enemy in enemies:
enemy[1] += enemy_speed
# Check Collision
# Simple box vs circle collision check
e_rect = pygame.Rect(enemy[0], enemy[1], enemy_size, enemy_size)
p_rect = pygame.Rect(player_x - player_radius, player_y - player_radius, player_radius*2, player_radius*2)
if e_rect.colliderect(p_rect):
print(f"Game Over! Final Score: {score}")
score = 0
enemies = [] # Clear enemies to restart
# Remove enemies that go off screen
# (We iterate backwards to safely remove items from a list)
for i in range(len(enemies)-1, -1, -1):
if enemies[i][1] > HEIGHT:
enemies.pop(i)
score += 1
# 3. Drawing
screen.fill(BLACK)
# Draw Player
pygame.draw.circle(screen, BLUE, (player_x, player_y), player_radius)
# Draw Enemies
for enemy in enemies:
pygame.draw.rect(screen, RED, (enemy[0], enemy[1], enemy_size, enemy_size))
# Draw Score
score_text = font.render(f"Score: {score}", True, WHITE)
screen.blit(score_text, (10, 10))
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()