-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
63 lines (53 loc) · 1.49 KB
/
main.py
File metadata and controls
63 lines (53 loc) · 1.49 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
import pygame
from settings import *
from player import Player
from enemy import Enemy
from bullet import Bullet
# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption(TITLE)
clock = pygame.time.Clock()
# Sprite Groups (Manages all objects efficiently)
all_sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()
bullets = pygame.sprite.Group()
# Create Player
player = Player()
all_sprites.add(player)
# Create 8 Enemies
for i in range(8):
m = Enemy()
all_sprites.add(m)
mobs.add(m)
# --- Game Loop ---
running = True
while running:
# 1. Process Input
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bullet = Bullet(player.rect.centerx, player.rect.top)
all_sprites.add(bullet)
bullets.add(bullet)
# 2. Update
all_sprites.update()
# Check if a bullet hit a mob
hits = pygame.sprite.groupcollide(mobs, bullets, True, True)
for hit in hits:
# Respawn the enemy
m = Enemy()
all_sprites.add(m)
mobs.add(m)
# Check if a mob hit the player
hits = pygame.sprite.spritecollide(player, mobs, False)
if hits:
running = False # Game Over
# 3. Draw / Render
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()