-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
82 lines (64 loc) · 1.89 KB
/
main.py
File metadata and controls
82 lines (64 loc) · 1.89 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
81
82
# this allows us to use code from
# the open-source pygame library
# throughout this file
import sys
import pygame
# import constants
from constants import *
from player import Player
from asteroid import Asteroid
from asteroidfield import AsteroidField
from shot import Shot
def main():
# initialize pygame
pygame.init()
# clock and timer
clock = pygame.time.Clock()
dt = 0
# setup screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# groups
updatable = pygame.sprite.Group()
drawable = pygame.sprite.Group()
asteroids = pygame.sprite.Group()
shots = pygame.sprite.Group()
# containers
Player.containers = (updatable, drawable)
Asteroid.containers = (asteroids, updatable, drawable)
AsteroidField.containers = (updatable)
Shot.containers = (shots, updatable, drawable)
# objects
player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
asteroidfield = AsteroidField()
# game loop
while True:
# catch and handle exit
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
# sprite logic
for sprite in updatable:
sprite.update(dt)
# asteroid logic
for asteroid in asteroids:
if (asteroid.colliding_with(player)):
print("Game over!")
sys.exit(0)
for shot in shots:
if (asteroid.colliding_with(shot)):
shot.kill()
asteroid.split()
# draw
screen.fill("black")
# sprite drawing
for sprite in drawable:
sprite.draw(screen)
# screen refresh
pygame.display.flip()
# tick
dt = clock.tick(60) / 1000
print("Starting Asteroids!")
print(f"Screen width: {SCREEN_WIDTH}")
print(f"Screen height: {SCREEN_HEIGHT}")
if __name__ == "__main__":
main()