Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
93 lines (73 loc) · 3.2 KB
/
Copy pathmain.py
File metadata and controls
93 lines (73 loc) · 3.2 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
83
84
85
86
87
88
89
90
91
92
93
import sys
import pygame
from constants import *
from logger import log_state
from logger import log_event
from player import Player
from asteroid import Asteroid
from shot import Shot
from asteroidfield import AsteroidField
def main():
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# creating a group to hold objects for asteroids
asteroids = pygame.sprite.Group()
# creating a group to hold objects that could be updated
updatable = pygame.sprite.Group()
# creating a group to hold objects that could be drawn
drawable = pygame.sprite.Group()
# creating a group to hold objects that are shots
shots = pygame.sprite.Group()
# Player here is the name of the class, not an instance
# this will add future Player instances into one of the two different groups
Player.containers = (updatable, drawable)
# do the same for Asteroid class, ensure each of its instance automatically add to these groups upon creation
Asteroid.containers = (asteroids, updatable, drawable)
AsteroidField.containers = updatable
Shot.containers = (shots, updatable, drawable)
asteroid_field = AsteroidField()
player_1 = Player(SCREEN_WIDTH/2,SCREEN_HEIGHT/2)
clock = pygame.time.Clock()
dt = 0.0 #store decimal number of seconds
# the main game loop
while True:
log_state()
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
screen.fill("black")
# used to call instance with draw method. Now call draw on each drawable by looping through the group "drawable
# player_1.draw(screen)
for thing in drawable:
thing.draw(screen)
# used to call instance with update method. Now call update() on groups
# player_1.update(dt)
updatable.update(dt)
# checking if player collides with asteroids
for asteroid in asteroids:
if asteroid.collide_with(player_1):
log_event("player_hit")
print("Game over!")
sys.exit()
# checking if shot collides with asteroids
for asteroid in asteroids:
for shot in shots:
if asteroid.collide_with(shot):
log_event("asteroid_shot")
#kill() method is a build-in feature of pygame sprites
# It removes the "killed" object from all its groups so that the engine stops updating and drawing it.
shot.kill()
asteroid.split()
pygame.display.flip()
# at the end of each iteration of the game loop
# call .tick() method on the clock object, passing 60 and save the return value divided by 1000 into dt
# the .tick() method returns the amount of time that passed since the last time it was called, the delta time
dt = clock.tick(60)/1000
# print out dt to for testing purpose
# print(dt)
# Print-out for testing purposes
#print(f"Starting Asteroids with pygame version: {pygame.version.ver}")
#print(f"Screen width: {SCREEN_WIDTH}")
#print(f"Screen height: {SCREEN_HEIGHT}")
if __name__ == "__main__":
main()