Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,100 +0,0 @@
|
|
1 |
-
import pygame
|
2 |
-
import random
|
3 |
-
import numpy as np
|
4 |
-
|
5 |
-
# Initialize Pygame
|
6 |
-
pygame.init()
|
7 |
-
|
8 |
-
# Set up some constants
|
9 |
-
WIDTH, HEIGHT = 800, 600
|
10 |
-
PLAYER_SIZE = 50
|
11 |
-
ALIEN_SIZE = 50
|
12 |
-
BULLET_SIZE = 10
|
13 |
-
|
14 |
-
# Set up some colors
|
15 |
-
WHITE = (255, 255, 255)
|
16 |
-
RED = (255, 0, 0)
|
17 |
-
GREEN = (0, 255, 0)
|
18 |
-
|
19 |
-
# Set up the display
|
20 |
-
screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
21 |
-
|
22 |
-
# Set up the font
|
23 |
-
font = pygame.font.Font(None, 36)
|
24 |
-
|
25 |
-
# Set up the player
|
26 |
-
player_pos = [WIDTH / 2, HEIGHT / 2]
|
27 |
-
player_vel = [0, 0]
|
28 |
-
|
29 |
-
# Set up the aliens
|
30 |
-
aliens = []
|
31 |
-
for _ in range(10):
|
32 |
-
alien_pos = [random.randint(0, WIDTH), random.randint(0, HEIGHT)]
|
33 |
-
alien_vel = [random.uniform(-2, 2), random.uniform(-2, 2)]
|
34 |
-
aliens.append([alien_pos, alien_vel])
|
35 |
-
|
36 |
-
# Set up the bullets
|
37 |
-
bullets = []
|
38 |
-
|
39 |
-
# Game loop
|
40 |
-
while True:
|
41 |
-
# Handle events
|
42 |
-
for event in pygame.event.get():
|
43 |
-
if event.type == pygame.QUIT:
|
44 |
-
pygame.quit()
|
45 |
-
sys.exit()
|
46 |
-
elif event.type == pygame.KEYDOWN:
|
47 |
-
if event.key == pygame.K_SPACE:
|
48 |
-
# Shoot a bullet
|
49 |
-
bullet_pos = [player_pos[0], player_pos[1]]
|
50 |
-
bullet_vel = [0, -5]
|
51 |
-
bullets.append([bullet_pos, bullet_vel])
|
52 |
-
|
53 |
-
# Move the player
|
54 |
-
keys = pygame.key.get_pressed()
|
55 |
-
if keys[pygame.K_w]:
|
56 |
-
player_vel[1] -= 0.5
|
57 |
-
if keys[pygame.K_s]:
|
58 |
-
player_vel[1] += 0.5
|
59 |
-
if keys[pygame.K_a]:
|
60 |
-
player_vel[0] -= 0.5
|
61 |
-
if keys[pygame.K_d]:
|
62 |
-
player_vel[0] += 0.5
|
63 |
-
|
64 |
-
player_pos[0] += player_vel[0]
|
65 |
-
player_pos[1] += player_vel[1]
|
66 |
-
|
67 |
-
# Move the aliens
|
68 |
-
for alien in aliens:
|
69 |
-
alien[0][0] += alien[1][0]
|
70 |
-
alien[0][1] += alien[1][1]
|
71 |
-
|
72 |
-
# Check if the alien is off the screen
|
73 |
-
if alien[0][0] < 0 or alien[0][0] > WIDTH or alien[0][1] < 0 or alien[0][1] > HEIGHT:
|
74 |
-
aliens.remove(alien)
|
75 |
-
|
76 |
-
# Move the bullets
|
77 |
-
for bullet in bullets:
|
78 |
-
bullet[0][1] += bullet[1][1]
|
79 |
-
|
80 |
-
# Check if the bullet is off the screen
|
81 |
-
if bullet[0][1] < 0:
|
82 |
-
bullets.remove(bullet)
|
83 |
-
|
84 |
-
# Check if the bullet hit an alien
|
85 |
-
for alien in aliens:
|
86 |
-
if (bullet[0][0] - alien[0][0]) ** 2 + (bullet[0][1] - alien[0][1]) ** 2 < (BULLET_SIZE + ALIEN_SIZE) ** 2:
|
87 |
-
aliens.remove(alien)
|
88 |
-
bullets.remove(bullet)
|
89 |
-
|
90 |
-
# Draw everything
|
91 |
-
screen.fill((0, 0, 0))
|
92 |
-
pygame.draw.rect(screen, WHITE, (player_pos[0], player_pos[1], PLAYER_SIZE, PLAYER_SIZE))
|
93 |
-
for alien in aliens:
|
94 |
-
pygame.draw.rect(screen, RED, (alien[0][0], alien[0][1], ALIEN_SIZE, ALIEN_SIZE))
|
95 |
-
for bullet in bullets:
|
96 |
-
pygame.draw.rect(screen, GREEN, (bullet[0][0], bullet[0][1], BULLET_SIZE, BULLET_SIZE))
|
97 |
-
|
98 |
-
# Update the display
|
99 |
-
pygame.display.flip()
|
100 |
-
pygame.time.Clock().tick(60)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|