forked from OpenBMB/ChatDev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame.py
76 lines (76 loc) · 2.71 KB
/
game.py
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
'''
This file contains the Game class which manages the game logic.
'''
import pygame
from player import Player
from enemy import Enemy
from bullet import Bullet
class Game:
def __init__(self, screen):
self.screen = screen
self.clock = pygame.time.Clock()
self.destroyed_count = 0
self.paused = False
# Create the player
player_x = 400
player_y = 500
self.player = Player(player_x, player_y)
# Create the enemies
self.enemies = []
self.spawn_enemies()
# Create the bullets
self.bullets = []
def spawn_enemies(self):
enemy_x = 100
enemy_y = 50
enemy_spacing = 100
num_enemies = 6
for i in range(num_enemies):
enemy = Enemy(enemy_x + i * enemy_spacing, enemy_y)
self.enemies.append(enemy)
def handle_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bullet_x = self.player.x + self.player.width / 2
bullet_y = self.player.y
bullet = Bullet(bullet_x, bullet_y)
self.bullets.append(bullet)
def update(self):
if not self.paused:
for bullet in self.bullets:
bullet.update()
for enemy in self.enemies:
enemy.update()
self.player.update()
# Check for collisions between bullets and enemies
destroyed_enemies = []
for bullet in self.bullets:
for enemy in self.enemies:
if bullet.collides_with(enemy):
destroyed_enemies.append(enemy)
self.destroyed_count += 1
# Remove destroyed enemies and bullets
for enemy in destroyed_enemies:
self.enemies.remove(enemy)
self.bullets = [bullet for bullet in self.bullets if not bullet.collides_with_any(self.enemies)]
def draw(self):
self.screen.fill((0, 0, 0))
for bullet in self.bullets:
bullet.draw(self.screen)
for enemy in self.enemies:
enemy.draw(self.screen)
self.player.draw(self.screen)
self.clock.tick(60)
def get_destroyed_count(self):
return self.destroyed_count
def enemy_reached_bottom(self):
for enemy in self.enemies:
if enemy.y + enemy.height >= 600:
return True
return False
def pause(self):
self.paused = True