forked from asnramos/pygame-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11_intro_sprites.py
64 lines (46 loc) · 1.29 KB
/
11_intro_sprites.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
import pygame, random
WHITE = (255, 255, 255)
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()
pygame.init()
screen = pygame.display.set_mode([900, 600])
clock = pygame.time.Clock()
done = False
score = 0
meteor_list = pygame.sprite.Group()
all_sprite_list = pygame.sprite.Group()
for i in range(50):
meteor = Meteor()
meteor.rect.x = random.randrange(900)
meteor.rect.y = random.randrange(600)
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
mouse_pos = pygame.mouse.get_pos()
player.rect.x = mouse_pos[0]
player.rect.y = mouse_pos[1]
meteor_hit_list = pygame.sprite.spritecollide(player, meteor_list, True)
for meteor in meteor_hit_list:
score += 1
print(score)
screen.fill(WHITE)
all_sprite_list.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()