-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
76 lines (57 loc) · 2.07 KB
/
Copy pathmain.py
File metadata and controls
76 lines (57 loc) · 2.07 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
import pygame
from asteroid import Asteroid
from constants import *
from player import Player
from asteroidfield import AsteroidField
from pygame.sprite import Group
from shot import Shot
def main():
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Asteroids Game")
clock = pygame.time.Clock()
updatable = Group()
drawable = Group()
asteroids = Group()
shots = Group()
Player.containers = (updatable, drawable)
Asteroid.containers = (asteroids, updatable, drawable)
Shot.containers = (shots, updatable, drawable)
player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
asteroid_field = AsteroidField()
updatable.add(player)
drawable.add(player)
while True:
dt = clock.tick(60) / 1000 # Seconds per frame
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
# Shooting
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
shot = player.shoot()
if shot: # Only add the shot if it's not None (i.e., cooldown is over)
shots.add(shot)
# Collision detection between player and asteroids
for asteroid in asteroids:
if player.check_collision(asteroid):
print("Game over!")
pygame.quit()
return
# Bullet and asteroid collision detection
for shot in shots:
for asteroid in asteroids:
if shot.rect.colliderect(asteroid.rect): # Check if shot and asteroid collide
shot.kill() # Remove the shot from the game
asteroid.split() # Split the asteroid if it’s large enough
asteroid_field.update(dt)
updatable.update(dt)
# Fill the screen with black
screen.fill((0, 0, 0))
# Draw all drawable objects
for sprite in drawable:
sprite.draw(screen)
pygame.display.flip()
if __name__ == "__main__":
main()