forked from mundo-python/pygame-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
14_shooter_teclado.py
111 lines (82 loc) · 2.35 KB
/
14_shooter_teclado.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import pygame, random
BLACK = (0, 0, 0)
class Meteor(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("meteor.png").convert()
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("player.png").convert()
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.speed_x = 0
self.speed_y = 0
def changespeed(self, x):
self.speed_x += x
def update(self):
self.rect.x += self.speed_x
player.rect.y = 510
class Laser(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("laser.png").convert()
self.rect = self.image.get_rect()
def update(self):
self.rect.y -= 4
pygame.init()
SCREEN_WIDTH = 900
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
clock = pygame.time.Clock()
done = False
score = 0
meteor_list = pygame.sprite.Group()
all_sprite_list = pygame.sprite.Group()
laser_list = pygame.sprite.Group()
for i in range(50):
meteor = Meteor()
meteor.rect.x = random.randrange(SCREEN_WIDTH - 20)
meteor.rect.y = random.randrange(450)
meteor_list.add(meteor)
all_sprite_list.add(meteor)
player = Player()
all_sprite_list.add(player)
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changespeed(-3)
if event.key == pygame.K_RIGHT:
player.changespeed(3)
if event.key == pygame.K_SPACE:
laser = Laser()
laser.rect.x = player.rect.x + 45
laser.rect.y = player.rect.y - 20
laser_list.add(laser)
all_sprite_list.add(laser)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.changespeed(3)
if event.key == pygame.K_RIGHT:
player.changespeed(-3)
all_sprite_list.update()
for laser in laser_list:
meteor_hit_list = pygame.sprite.spritecollide(laser, meteor_list, True)
for meteor in meteor_hit_list:
all_sprite_list.remove(laser)
laser_list.remove(laser)
score += 1
print(score)
if laser.rect.y < -10:
all_sprite_list.remove(laser)
laser_list.remove(laser)
screen.fill([255, 255, 255])
all_sprite_list.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()