-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
65 lines (59 loc) · 2.24 KB
/
Copy pathmain.py
File metadata and controls
65 lines (59 loc) · 2.24 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
import pygame
from constants import *
from asteroid import Asteroid
from asteroidfield import AsteroidField
from player import Player
from shot import Shot
def main():
print(f"Starting Asteroids with pygame version: {pygame.version.ver}")
print(f"Screen width: {SCREEN_WIDTH}")
print(f"Screen height: {SCREEN_HEIGHT}")
pygame.init()
# Initialize a new GUI window or screen for display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
x = SCREEN_WIDTH / 2
y = SCREEN_HEIGHT / 2
# group for all objects that can be updated
updatable = pygame.sprite.Group()
# group for all the objects that can be drawn
drawable = pygame.sprite.Group()
# group for all asteroid objects
asteroids = pygame.sprite.Group()
# group for all shot objects
shots = pygame.sprite.Group()
Asteroid.containers = (asteroids, updatable, drawable)
AsteroidField.containers = (updatable)
Player.containers = (updatable, drawable)
Shot.containers = (shots, updatable, drawable)
asteroid_field = AsteroidField()
player = Player(x, y)
# dt = "delta time"
dt = 0
# Infinite while loop for the game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
pygame.Surface.fill(screen,"black")
updatable.update(dt)
for obj in drawable:
obj.draw(screen)
for asteroid in asteroids:
for shot in shots:
# Check if an asteroid has collided with a shot
if asteroid.collide(shot):
asteroid.split()
shot.kill()
# Check if an asteroid has collided with the player
if asteroid.collide(player):
print("Game over!")
exit()
# Refresh the screen
pygame.display.flip()
# Set FPS (frames per second) to a maximum of 60 times per second.
# .tick() method also returns the amount of time that has
# passed since the last time it was called in milliseconds.
dt = clock.tick(60) / 1000
if __name__ == "__main__":
main()