-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
137 lines (110 loc) · 4.48 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import pygame
import os
import threading
import time
from src.player import Player
from src.weapons import WeaponGroup
from src.obstacles import ObstacleGroup
from src.monster import MonsterGroup
from src.screens import StartScreen, LevelSelectionScreen, GameOverScreen
pygame.init()
pygame.mixer.init()
screen_width, screen_height = 1600, 1000
map_width, map_height = 1921, 1021
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Monster Battle")
game_over_sound = pygame.mixer.Sound(os.path.join('assets', 'gameover.mp3'))
win_sound = pygame.mixer.Sound(os.path.join('assets', 'win.mp3'))
bg_music = pygame.mixer.music.load(os.path.join('assets', "background_music.mp3"))
def play_background_music():
pygame.mixer.music.play(-1)
while True:
if not pygame.mixer.music.get_busy():
pygame.mixer.music.play(-1)
background_music_thread = threading.Thread(target=play_background_music)
background_music_thread.daemon = True
background_music_thread.start()
def init(level=1):
number_monsters = [10, 25, 100]
monster_speed = [1, 2, 3]
weapon_health = [5, 10, 50]
player = Player(screen, 150, 150, 'player_fists1.png')
obstacles = ObstacleGroup(screen, 'obstacles.png')
weapons = WeaponGroup(screen, map_width, map_height, 5, obstacles, weapon_health[level-1])
monsters = MonsterGroup(screen, map_width, map_height, number_monsters[level-1], obstacles, monster_speed[level-1])
bullets = pygame.sprite.Group()
return player, weapons, obstacles, monsters, bullets
def main():
running = True
clock = pygame.time.Clock()
player, weapons, obstacles, monsters, bullets = init()
level_screen = LevelSelectionScreen(screen)
lost_screen = GameOverScreen(screen, True)
won_screen = GameOverScreen(screen, False)
game_running = False
game_over = False
won = False
selected_level = False
level = 1
while running:
if game_running:
screen.fill((150, 100, 50))
keys = pygame.key.get_pressed()
player.update(keys, obstacles, weapons, monsters)
if player.health <= 0:
game_running = False
game_over = True
selected_level = False
game_over_sound.play()
weapons.update()
obstacles.update()
offset_x = screen_width // 2 - player.rect.centerx
offset_y = screen_height // 2 - player.rect.centery
obstacles.draw(offset_x, offset_y)
weapons.draw(offset_x, offset_y)
player.update_direction(obstacles, offset_x, offset_y)
player.draw(offset_x, offset_y)
monsters.update(bullets, player.rect, obstacles, offset_x, offset_y)
monsters.draw(offset_x, offset_y)
bullets.update(obstacles, monsters)
for bullet in bullets:
bullet.draw(offset_x, offset_y)
else:
if game_over and not selected_level:
if won:
won_screen.draw()
else:
lost_screen.draw()
else:
level_screen.draw()
if len(monsters.monsters) == 0:
game_running = False
game_over = True
won = True
selected_level = False
win_sound.play()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if player.weapon and game_running:
bullet = player.weapon.shoot(player, pygame.mouse.get_pos(), offset_x, offset_y, map_width, map_height)
if bullet is not None:
bullets.add(bullet)
if not game_running and not selected_level and level_screen.handle_event(event):
selected_level = True
level = level_screen.handle_event(event)
player, weapons, obstacles, monsters, bullets = init(level)
game_running = True
if game_over and (lost_screen.handle_event(event) or won_screen.handle_event(event)):
game_over = False
won = False
time.sleep(0.1)
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
running = False
pygame.display.flip()
clock.tick(100)
pygame.quit()
if __name__ == "__main__":
main()