Skip to content

Commit e460aaa

Browse files
Update installing-packages.rst
1 parent cee95c2 commit e460aaa

1 file changed

Lines changed: 264 additions & 1 deletion

File tree

source/tutorials/installing-packages.rst

Lines changed: 264 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,267 @@
1-
.. _installing-packages:
1+
import pygame
2+
import random
3+
import math
4+
5+
pygame.init()
6+
7+
WIDTH, HEIGHT = 1000, 650
8+
screen = pygame.display.set_mode((WIDTH, HEIGHT))
9+
pygame.display.set_caption("Battle Royale")
10+
11+
clock = pygame.time.Clock()
12+
13+
# Colors
14+
WHITE = (255, 255, 255)
15+
BLACK = (20, 20, 20)
16+
GREEN = (50, 200, 80)
17+
RED = (220, 50, 50)
18+
BLUE = (60, 120, 255)
19+
YELLOW = (240, 210, 50)
20+
GRAY = (100, 100, 100)
21+
22+
# Player
23+
player = pygame.Rect(480, 300, 35, 35)
24+
player_speed = 5
25+
health = 100
26+
27+
# Bullets
28+
bullets = []
29+
30+
# Enemies
31+
enemies = []
32+
for _ in range(12):
33+
x = random.randint(50, WIDTH - 80)
34+
y = random.randint(50, HEIGHT - 80)
35+
enemies.append(pygame.Rect(x, y, 30, 30))
36+
37+
# Safe zone
38+
zone_x = WIDTH // 2
39+
zone_y = HEIGHT // 2
40+
zone_radius = 300
41+
42+
font = pygame.font.SysFont("Arial", 24)
43+
big_font = pygame.font.SysFont("Arial", 55)
44+
45+
running = True
46+
game_over = False
47+
victory = False
48+
49+
while running:
50+
51+
clock.tick(60)
52+
53+
for event in pygame.event.get():
54+
55+
if event.type == pygame.QUIT:
56+
running = False
57+
58+
# Shoot
59+
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
60+
mouse_x, mouse_y = pygame.mouse.get_pos()
61+
62+
dx = mouse_x - player.centerx
63+
dy = mouse_y - player.centery
64+
65+
distance = math.hypot(dx, dy)
66+
67+
if distance != 0:
68+
dx /= distance
69+
dy /= distance
70+
71+
bullets.append([
72+
player.centerx,
73+
player.centery,
74+
dx * 12,
75+
dy * 12
76+
])
77+
78+
if not game_over:
79+
80+
# ---------------- PLAYER MOVEMENT ----------------
81+
82+
keys = pygame.key.get_pressed()
83+
84+
if keys[pygame.K_w] or keys[pygame.K_UP]:
85+
player.y -= player_speed
86+
87+
if keys[pygame.K_s] or keys[pygame.K_DOWN]:
88+
player.y += player_speed
89+
90+
if keys[pygame.K_a] or keys[pygame.K_LEFT]:
91+
player.x -= player_speed
92+
93+
if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
94+
player.x += player_speed
95+
96+
player.clamp_ip(screen.get_rect())
97+
98+
# ---------------- BULLETS ----------------
99+
100+
for bullet in bullets[:]:
101+
102+
bullet[0] += bullet[2]
103+
bullet[1] += bullet[3]
104+
105+
bullet_rect = pygame.Rect(
106+
bullet[0],
107+
bullet[1],
108+
8,
109+
8
110+
)
111+
112+
if not screen.get_rect().colliderect(bullet_rect):
113+
bullets.remove(bullet)
114+
continue
115+
116+
# Hit enemies
117+
for enemy in enemies[:]:
118+
119+
if bullet_rect.colliderect(enemy):
120+
121+
enemies.remove(enemy)
122+
123+
if bullet in bullets:
124+
bullets.remove(bullet)
125+
126+
break
127+
128+
# ---------------- ENEMY AI ----------------
129+
130+
for enemy in enemies:
131+
132+
dx = player.centerx - enemy.centerx
133+
dy = player.centery - enemy.centery
134+
135+
distance = math.hypot(dx, dy)
136+
137+
if distance > 0:
138+
dx /= distance
139+
dy /= distance
140+
141+
enemy.x += int(dx * 1.2)
142+
enemy.y += int(dy * 1.2)
143+
144+
# Enemy damages player
145+
if enemy.colliderect(player):
146+
health -= 0.25
147+
148+
# ---------------- SAFE ZONE ----------------
149+
150+
distance_from_zone = math.hypot(
151+
player.centerx - zone_x,
152+
player.centery - zone_y
153+
)
154+
155+
if distance_from_zone > zone_radius:
156+
health -= 0.15
157+
158+
# Slowly shrink zone
159+
zone_radius -= 0.01
160+
161+
if health <= 0:
162+
health = 0
163+
game_over = True
164+
victory = False
165+
166+
if len(enemies) == 0:
167+
game_over = True
168+
victory = True
169+
170+
# ================= DRAW =================
171+
172+
screen.fill((45, 120, 55))
173+
174+
# Safe zone
175+
pygame.draw.circle(
176+
screen,
177+
BLUE,
178+
(zone_x, zone_y),
179+
int(zone_radius),
180+
5
181+
)
182+
183+
# Enemies
184+
for enemy in enemies:
185+
pygame.draw.rect(screen, RED, enemy)
186+
187+
# Player
188+
pygame.draw.rect(screen, BLUE, player)
189+
190+
# Bullets
191+
for bullet in bullets:
192+
pygame.draw.circle(
193+
screen,
194+
YELLOW,
195+
(int(bullet[0]), int(bullet[1])),
196+
4
197+
)
198+
199+
# Health bar
200+
pygame.draw.rect(
201+
screen,
202+
BLACK,
203+
(20, 20, 204, 24)
204+
)
205+
206+
pygame.draw.rect(
207+
screen,
208+
GREEN,
209+
(22, 22, int(health * 2), 20)
210+
)
211+
212+
health_text = font.render(
213+
f"HP: {int(health)}",
214+
True,
215+
WHITE
216+
)
217+
218+
screen.blit(health_text, (20, 50))
219+
220+
# Enemy count
221+
enemy_text = font.render(
222+
f"Enemies: {len(enemies)}",
223+
True,
224+
WHITE
225+
)
226+
227+
screen.blit(enemy_text, (20, 80))
228+
229+
# Controls
230+
controls = font.render(
231+
"WASD = Move | Mouse = Shoot",
232+
True,
233+
WHITE
234+
)
235+
236+
screen.blit(controls, (20, HEIGHT - 40))
237+
238+
# Game over
239+
if game_over:
240+
241+
if victory:
242+
text = big_font.render(
243+
"BOOYAH! YOU WIN!",
244+
True,
245+
YELLOW
246+
)
247+
else:
248+
text = big_font.render(
249+
"YOU WERE ELIMINATED",
250+
True,
251+
RED
252+
)
253+
254+
screen.blit(
255+
text,
256+
(
257+
WIDTH // 2 - text.get_width() // 2,
258+
HEIGHT // 2 - text.get_height() // 2
259+
)
260+
)
261+
262+
pygame.display.flip()
263+
264+
pygame.quit().. _installing-packages:
2265

3266
===================
4267
Installing Packages

0 commit comments

Comments
 (0)