-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
134 lines (102 loc) · 3.81 KB
/
utils.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
from classes.twin import Twin
from classes.exit import Exit
from classes.boundary import Boundary
from classes.trap import Trap
from classes.key import Key
from classes.player import Player
from classes.text import Text
from classes.push_block import PushBlock
from pygame import mouse
from pygame import mixer
from random import choice
import os
"""
██╗ ██╗████████╗██╗██╗ ███████╗
██║ ██║╚══██╔══╝██║██║ ██╔════╝
██║ ██║ ██║ ██║██║ ███████╗
██║ ██║ ██║ ██║██║ ╚════██║
╚██████╔╝ ██║ ██║███████╗███████║
╚═════╝ ╚═╝ ╚═╝╚══════╝╚══════╝
"""
LevelWidth = 30
LevelHeight = 20
def IsKey( entity ):
return isinstance( entity, Key )
def IsTrap( entity ):
return isinstance( entity, Trap )
def IsPlayer( entity ):
return isinstance( entity, Player )
def IsExit( entity ):
return isinstance( entity, Exit )
def IsTwin( entity ):
return isinstance( entity, Twin )
def IsBoundary( entity ):
return isinstance( entity, Boundary )
def IsText( entity ):
return isinstance( entity, Text )
def IsAPickedUp( entity ):
return entity.IsPickupable
def HasInventory( entity ):
return entity.HasInventory
def HasKey( entity ):
if not HasInventory( entity ):
return False
elif entity.inventory["keys"] > 0:
return True
return False
def IsKeyActivated( entity ):
if IsExit( entity ):
if entity.NeedsKey:
return True
return False
def GenerateBlankLevel(level_name:str = "level") -> str:
"""
This script generates a blank level
"""
max_level_id = 0
for root, dirs, files in os.walk( os.path.join( os.getcwd(), "levels" ) ):
for file in files:
file = file.split( "." )
if file[1] == "txt":
if file[0].startswith( level_name ):
max_level_id += 1
BlankLevel = ""
for y in range(LevelHeight):
BlankLevel += f'{"0" * LevelWidth }\n'
with open(f"levels/{level_name}{max_level_id}.txt", "w") as file:
file.write( BlankLevel )
return f"{level_name}{max_level_id}"
def mouseToGrid() -> list:
mouse_x, mouse_y = mouse.get_pos()
closestPostion = [100,100]
for x in range( LevelWidth ):
for y in range( LevelHeight ): # important if we make the game window dynamic this must be changed
if 32 > mouse_x - x*32 and 32 > mouse_y - y*32 and 0 <= mouse_x - x*32 and 0 <= mouse_y - y*32:
closestPostion = [x,y]
return closestPostion
def MakeEmptyGridList() -> list:
BlankGrid = []
for y in range(LevelHeight):
line = []
for x in range(LevelWidth):
line.append( "0" )
BlankGrid.append( line )
return BlankGrid
class music_manager:
def __init__(self) -> None:
self.music = {
"menu":["assets/music/main_menu.wav"],
"game":["assets/music/main1.wav","assets/music/main2.wav"],
"level_editor":["assets/music/main1.wav","assets/music/main2.wav","assets/music/main_menu.wav"]
}
self.current_state = ""
def update( self, state ):
if mixer.music.get_busy() != True or self.current_state != state:
mixer.music.fadeout(20)
for State in self.music.keys():
if state == State:
mixer.music.load( choice(self.music[State]) )
mixer.music.play()
self.current_state = state
if __name__ == '__main__':
GenerateBlankLevel("level")