-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
71 lines (56 loc) · 1.9 KB
/
main.py
File metadata and controls
71 lines (56 loc) · 1.9 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
import pygame
from constants import *
from player import Player
from asteroid import Asteroid
from asteroidfield import AsteroidField
from bullet import Bullet
def main():
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Asteroids")
clock = pygame.time.Clock()
dt = 0
# Create sprite groups
update_group = pygame.sprite.Group()
draw_group = pygame.sprite.Group()
asteroid_group = pygame.sprite.Group()
bullet_group = pygame.sprite.Group()
# Create player and add it to the groups
Player.containers = (update_group, draw_group)
player = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
# Create asteroids and add them to the groups
Asteroid.containers = (asteroid_group, update_group, draw_group)
# Create asteroid field and add it to the groups
AsteroidField.containers = update_group
AsteroidField()
# Bullet group
Bullet.containers = (update_group, draw_group, bullet_group)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
screen.fill((0, 0, 0))
# Update all sprites
for group in update_group:
group.update(dt)
# Check for collisions
for asteroid in asteroid_group:
if player.check_collision(asteroid):
print("Game over!")
player.kill()
pygame.quit()
return
for bullet in bullet_group:
if bullet.check_collision(asteroid):
bullet.kill()
asteroid.split()
break
# Draw all sprites
for group in draw_group:
group.draw(screen)
pygame.display.flip()
clock.tick(60)
dt = clock.get_time() / 1000
if __name__ == "__main__":
main()