-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
73 lines (67 loc) · 2.66 KB
/
main.py
File metadata and controls
73 lines (67 loc) · 2.66 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
# this allows us to use code from
# the open-source pygame library
# throughout this file
# import the function_hello function
# and the variable_player variable
# into the current file
# from module import function_hello, variable_player
# import everything from a module
# into the current file
# from module import *
# 1 Event Handling: Process all input events at the start of the loop.
# 2 Game Logic Updates: Process your game state changes next.
# 3 Screen Clearing: Use screen.fill((0, 0, 0)) early in the drawing phase to ensure a clean background for your new frame.
# 4 Drawing Operations: Render all necessary game objects, text, and other visuals after clearing.
# 5 Display Update: Finally, call pygame.display.update() to swap the newly prepared frame with the on-screen buffer, showing all updates to the player.
import pygame
import sys
from constants import *
from player import Player
from asteroid import *
from asteroidfield import *
from shot import *
def main():
pygame.init()
#create groups
updatable = pygame.sprite.Group()
drawable = pygame.sprite.Group()
asteroids = pygame.sprite.Group()
shots = pygame.sprite.Group()
clock = pygame.time.Clock()
dt = 0
print("Starting asteroids!")
print(f"Screen width: {SCREEN_WIDTH}")
print(f"Screen height: {SCREEN_HEIGHT}")
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Create player in middle of screen
Player.containers = (updatable, drawable)
AsteroidField.containers = (updatable,)
Asteroid.containers = (asteroids, updatable, drawable)
Shot.containers = (shots, updatable, drawable)
player = Player(SCREEN_WIDTH/2, SCREEN_HEIGHT/2)
asteroid_field = AsteroidField()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
# 1. Update all objects in the 'updatable' group.
for sprite in updatable:
sprite.update(dt) # Assuming 'dt' (delta time) is being passed if necessary
for asteroid in asteroids: # asteroids is your sprite group
if player.collision(asteroid):
print("Game over!")
sys.exit()
for shot in shots:
if shot.collision(asteroid):
asteroid.split()
shot.kill()
# 2. Clear the screen (fill with a background color, e.g., black)
screen.fill((0, 0, 0))
# 3. Draw all objects in the 'drawable' group.
for sprite in drawable:
sprite.draw(screen)
# 4. Refresh the display.
pygame.display.flip()
dt = clock.tick(60)/1000
if __name__ == "__main__":
main()