/
monster-game.py
210 lines (174 loc) · 6.65 KB
/
monster-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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import pygame
import time
import random
from monster import Monster
from hero import Hero
from characters import Characters
from goblin import Goblin
# Key Stroke Variables
KEY_UP = 273
KEY_DOWN = 274
KEY_LEFT = 276
KEY_RIGHT = 275
KEY_ENTER = 13
# Other Variables
level_count = 1
bg_sound_path = "./sounds/music.wav"
lose_sound_path = "./sounds/lose.wav"
win_sound_path = "./sounds/win.wav"
bg_image_path = "./images/background.png"
window_width = 512
window_height = 480
def restart_level(monster, hero, goblin1, goblin2, goblin3):
monster.monster_restart()
hero.hero_restart()
goblin1.goblin_restart()
goblin2.goblin_restart()
goblin3.goblin_restart()
def main():
# Initialize pygame
pygame.init()
# Importing image
background_image = pygame.image.load(bg_image_path)
# Importing and Intialize Sound
pygame.mixer.init()
bg_sound = pygame.mixer.Sound(bg_sound_path)
lose_sound = pygame.mixer.Sound(lose_sound_path)
win_sound = pygame.mixer.Sound(win_sound_path)
# Set up the screen (drawing surface for the game)
screen = pygame.display.set_mode((window_width, window_height))
# Game Window Title
pygame.display.set_caption('Monster Game')
# Calls the Clock object and sets it to variable clock (for shorthand purposes)
clock = pygame.time.Clock()
#################### Game initialization ####################
# Created a monster instance
game_monster = Monster(120, 120, 3, 3)
game_hero = Hero(256, 240)
game_goblin1 = Goblin(360, 360)
game_goblin2 = Goblin(360, 360)
game_goblin3 = Goblin(360, 360)
# Sprite Groups
monster_group = pygame.sprite.Group()
monster_group.add(game_monster)
hero_group = pygame.sprite.Group()
hero_group.add(game_hero)
goblin_group = pygame.sprite.Group()
goblin_group.add(game_goblin1)
goblin_group.add(game_goblin2)
goblin_group.add(game_goblin3)
# FPS settings
FPS = 60
# Music Start
bg_sound.play()
#################### While loop used to have the game continuously run ####################
stop_game = False
while not stop_game:
# Event is for user input such as keypress to clicks
for event in pygame.event.get():
#Key Strokes Movement (Hero Movement and Enter Key)
if event.type == pygame.KEYDOWN:
if event.key == KEY_DOWN:
if game_hero.dead == False:
game_hero.dir_y = 3
elif event.key == KEY_UP:
if game_hero.dead == False:
game_hero.dir_y = -3
elif event.key == KEY_LEFT:
if game_hero.dead == False:
game_hero.dir_x = -3
elif event.key == KEY_RIGHT:
if game_hero.dead == False:
game_hero.dir_x = 3
elif event.key == KEY_ENTER:
print("Enter Works")
restart_level(game_monster, game_hero, game_goblin1, game_goblin2, game_goblin3)
monster_group.add(game_monster)
goblin_group.add(game_goblin1)
goblin_group.add(game_goblin2)
goblin_group.add(game_goblin3)
bg_sound.play()
if event.type == pygame.KEYUP:
if event.key == KEY_DOWN:
game_hero.dir_y = 0
elif event.key == KEY_UP:
game_hero.dir_y = 0
elif event.key == KEY_LEFT:
game_hero.dir_x = 0
elif event.key == KEY_RIGHT:
game_hero.dir_x = 0
# Event that if user clicks exit
if event.type == pygame.QUIT:
stop_game = True
# Game logic
# Renders background, monster, and hero image
screen.blit(background_image, [0, 0])
screen.blit(game_hero.image, [game_hero.x, game_hero.y])
screen.blit(game_monster.image, [game_monster.x, game_monster.y])
screen.blit(game_goblin1.image, [game_goblin1.x, game_goblin1.y])
screen.blit(game_goblin2.image, [game_goblin2.x, game_goblin2.y])
screen.blit(game_goblin3.image, [game_goblin3.x, game_goblin3.y])
# Hero Movement
game_hero.character_movement()
game_hero.character_fence(1)
game_hero.rect_update()
# Monster Movement
game_monster.monster_random_movement()
game_monster.character_movement()
game_monster.character_fence(2)
game_monster.rect_update()
# Goblin Movement
game_goblin1.monster_random_movement()
game_goblin1.character_movement()
game_goblin1.character_fence(2)
game_goblin1.rect_update()
game_goblin2.monster_random_movement()
game_goblin2.character_movement()
game_goblin2.character_fence(2)
game_goblin2.rect_update()
game_goblin3.monster_random_movement()
game_goblin3.character_movement()
game_goblin3.character_fence(2)
game_goblin3.rect_update()
# Sprite Collide
collide_monster = pygame.sprite.spritecollide(game_hero, monster_group, True)
collide_goblin = pygame.sprite.spritecollide(game_hero, goblin_group, True)
if collide_monster:
bg_sound.stop()
game_hero.x = 230
game_hero.y = 200
game_monster.is_dead()
win_sound.play()
if collide_goblin:
bg_sound.stop()
game_monster.x = 500
game_monster.y = 500
game_hero.hero_dead()
lose_sound.play()
game_monster.dir_x = 0
game_monster.dir_y = 0
if game_monster.dead == True:
font = pygame.font.Font(None, 20)
play_again_text = font.render("Press `Enter` to Play Again!", True, (0, 0, 0))
screen.blit(play_again_text, [172, 220])
game_hero.x = 236
game_hero.y = 185
if game_hero.dead == True:
font = pygame.font.Font(None, 20)
play_again_text = font.render("Press `Enter` to Play Again!", True, (0, 0, 0))
screen.blit(play_again_text, [172, 220])
game_goblin1.x = 236
game_goblin1.y = 185
game_goblin2.x = 236
game_goblin2.y = 185
game_goblin3.x = 236
game_goblin3.y = 185
game_hero.dir_x = 0
game_hero.dir_y = 0
# Game display
pygame.display.update()
# Set the tick rate to 60 ms, which means the game runs at 60 FPS
clock.tick(FPS)
pygame.quit()
if __name__ == '__main__':
main()