-
Notifications
You must be signed in to change notification settings - Fork 9
/
map.py
144 lines (126 loc) · 5.39 KB
/
map.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
import random
import pygame
from assets import GAME_ASSETS
from enemy import Enemy
class Map:
def __init__(self, window):
"""
Initialize the Map class.
Args:
window (pygame.Surface): The game window surface.
"""
self.window = window
self.map_image = pygame.image.load(GAME_ASSETS["dungeon_map"]).convert_alpha()
self.map_image = pygame.transform.scale(self.map_image, (self.window.get_width(), self.window.get_height()))
self.player_images = {
'Warrior': pygame.image.load(GAME_ASSETS['warrior']).convert_alpha(),
'Mage': pygame.image.load(GAME_ASSETS['mage']).convert_alpha(),
'Rogue': pygame.image.load(GAME_ASSETS["rogue"]).convert_alpha()
}
self.player_type = None
self.player_position = [self.window.get_width() / 2, self.window.get_height() / 2]
self.enemies = [
Enemy(GAME_ASSETS["goblin"], [50, 50], self.window),
Enemy(GAME_ASSETS["orc"], [self.window.get_width() - 120, 50], self.window),
Enemy(GAME_ASSETS["skeleton"], [50, self.window.get_height() - 120], self.window),
Enemy(GAME_ASSETS["skeleton"], [self.window.get_width() - 120, self.window.get_height() - 120], self.window)
]
self.in_combat = False # Ensure this attribute is defined in the constructor
self.current_enemy = None
self.blue_orb = None
self.game_over = False
def load_player(self, character_type):
"""
Load the player character.
Args:
character_type (str): The type of character to load.
"""
self.player_type = character_type
self.player_image = self.player_images[character_type]
self.player_image = pygame.transform.scale(self.player_image, (int(self.player_image.get_width() * 0.15), int(self.player_image.get_height() * 0.15)))
def check_for_combat(self):
"""
Check if the player is in combat with any enemy.
Returns:
bool: True if the player is in combat, False otherwise.
"""
for enemy in self.enemies:
if pygame.math.Vector2(enemy.position).distance_to(self.player_position) < 50:
self.in_combat = True
self.current_enemy = enemy
return True
return False
def handle_combat(self):
"""
Handle combat between the player and the current enemy.
"""
if self.in_combat and self.current_enemy:
player_damage = random.randint(5, 10)
enemy_defeated = self.current_enemy.take_damage(player_damage)
print(f"Player attacks! Deals {player_damage} damage to the enemy.")
if enemy_defeated:
print("Enemy defeated!")
self.enemies.remove(self.current_enemy)
self.in_combat = False
self.current_enemy = None
if not self.enemies:
self.spawn_blue_orb()
else:
enemy_damage = random.randint(5, 10)
print(f"Enemy attacks back! Deals {enemy_damage} damage to the player.")
# Assume player has a method to take damage
# self.player.take_damage(enemy_damage)
def spawn_blue_orb(self):
"""
Spawn the blue orb in the center of the map.
"""
self.blue_orb = pygame.image.load(GAME_ASSETS["blue_orb"]).convert_alpha()
self.blue_orb = pygame.transform.scale(self.blue_orb, (50, 50))
self.orb_position = [self.window.get_width() / 2 - 25, self.window.get_height() / 2 - 25]
def check_orb_collision(self):
"""
Check if the player has collided with the blue orb.
Returns:
bool: True if the player has collided with the blue orb, False otherwise.
"""
if self.blue_orb and pygame.math.Vector2(self.orb_position).distance_to(self.player_position) < 25:
self.game_over = True
print("YOU WIN") # This can be modified to a more visual display if needed.
return True
return False
def handle_events(self):
"""
Handle user input events.
Returns:
str: 'quit' if the game is over and should be exited, None otherwise.
"""
if self.game_over:
return 'quit' # Stop processing events if game is over
keys = pygame.key.get_pressed()
move_speed = 2
if keys[pygame.K_LEFT]:
self.player_position[0] -= move_speed
if keys[pygame.K_RIGHT]:
self.player_position[0] += move_speed
if keys[pygame.K_UP]:
self.player_position[1] -= move_speed
if keys[pygame.K_DOWN]:
self.player_position[1] += move_speed
if not self.in_combat:
if self.check_for_combat():
return
self.handle_combat()
if self.blue_orb and self.check_orb_collision():
return 'quit'
def draw(self):
"""
Draw the game objects on the window.
"""
self.window.fill((0, 0, 0))
self.window.blit(self.map_image, (0, 0))
self.window.blit(self.player_image, (self.player_position[0], self.player_position[1]))
for enemy in self.enemies:
enemy.draw()
if self.blue_orb:
self.window.blit(self.blue_orb, self.orb_position)
pygame.display.flip()