-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinvaders_game_2.py
More file actions
80 lines (58 loc) · 1.57 KB
/
invaders_game_2.py
File metadata and controls
80 lines (58 loc) · 1.57 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
"""
Rect attributes
x,y
top, left, right, bottom
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery
size, width, height
w,h
"""
# SPACE INVADERS
import pygame, sys, random
pygame.init()
clock = pygame.time.Clock()
width = 50
height = 30
x = 250
y = 250
enemy_start_pos = 500
blue = (0, 0, 255)
red = (255, 0, 0)
run = True
vel = 1
i = 0 # an index of random numbers
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Space Invaders Simplified")
enemy_y = random.randint(0, 500)
player = pygame.Rect(10, 10, width, height)
enemy = pygame.Rect(500, enemy_y, width, height)
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > 0:
player.x -= vel
if keys[pygame.K_RIGHT] and x < 500 - width:
player.x += vel
if keys[pygame.K_DOWN] and y < 500 - height:
player.y += vel
if keys[pygame.K_UP] and y > 0:
player.y -= vel
pygame.draw.rect(win, red, player)
pygame.draw.rect(win, blue, enemy)
enemy.x -= 0.5
pygame.display.update()
win.fill((0, 0, 0))
if enemy.left <= 50:
i += 1
enemy.right = 500
# print("Horiz:", player.x, enemy.left, enemy.right)
# print("Vert:", player.y, enemy.top, enemy.bottom)
if (enemy.left <= player.x <= enemy.right) and \
(enemy.top <= player.y <= enemy.bottom):
print("Collision detected!")
run = False
clock.tick()
pygame.quit()