/
Main.py
327 lines (274 loc) · 17 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#COMMIT DAMN YOU
import pygame, sys, random
from pygame.locals import *
from Tile import *
from Dungeon import *
from Actor import *
from Menu import *
from Log import *
from LevelHandler import levelhandler
#Database importing for Item_Database and Rarity Database
database=Item_Database()
database.import_database()
Rarity_Database= [[int for i in range(8)] for j in range(54)]
increment=0
for line in open("LootRarity.csv"):
Rarity_Database[increment]=line.split(",")
increment=increment+1
#Set the width and height of the board. For pixel size, multiply each variable by 32.
boardWidth = 32
boardHeight = 25
dungeonLevel = 0
#Set Colors - Colors in pyGame are in RGB format. This is out of convenience.
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
#Here we initialize the pygame surface and create the Displaysurface which will hold our main screen, passing along the board size variables*32 for actual pixel size.
pygame.init()
DISPLAYSURF = pygame.display.set_mode((boardWidth*32,boardHeight*32+160), 0, 32)
pygame.display.set_caption('Dev') #Window Title
DISPLAYSURF.fill(WHITE) #Fill the screen area with white.
#Create LevelHandler to keep track of dungeons and current floor
#Create new dungeon with the width and height specified.
dungeon = Dungeon(boardWidth, boardHeight)
#All elements in the object list will be printed when drawObjects are called.
#dungeon.objectlist = []
#Code to generate a monster a random location
dungeon.room_Generation(boardWidth,boardHeight)
#Append some items to the dungeon.objectlist
healer= NPC(64,0,200,1,'healer', Rarity_Database, database)
shopkeeper= NPC(0,64,200,1,'shopkeeper', Rarity_Database, database)
player = Player(32,32,'images/player.png',"Name",5000,50)
dungeon.objectlist.append(player)
dungeon.objectlist.append(healer)
dungeon.objectlist.append(shopkeeper)
dungeon.spawn_Monsters(boardWidth,boardHeight,Rarity_Database,database,dungeon.objectlist)
#Append the map to the LevelHandler and set it as the current level
levelhandler.dungeonlist.append(dungeon)
levelhandler.move_floor(1)
newdungeon = Dungeon(boardWidth,boardHeight)
newdungeon.testing_Generation(boardWidth,boardHeight)
newdungeon.testing_Monsters(boardWidth,boardHeight,Rarity_Database,database,newdungeon.objectlist)
#Append the map to the LevelHandler
levelhandler.dungeonlist.append(newdungeon)
stats=StatsMenu(player)
#global log
#log = Log()
#Function to draw the map to the board.
def drawMap(boardWidth, boardHeight):
for y in xrange(boardHeight):
for x in xrange(boardWidth):
DISPLAYSURF.blit(levelhandler.activelevel().map[x][y].Img, (levelhandler.activelevel().map[x][y].xcoordinate,levelhandler.activelevel().map[x][y].ycoordinate)) #Surface.Blit takes an image and a coordinate and then draws to that location.
#Function to draw the objects to the board
def drawObjects():
for object in levelhandler.activelevel().objectlist:
DISPLAYSURF.blit(object.Img, (object.xcoordinate, object.ycoordinate))
def redrawScreen():
drawMap(boardWidth, boardHeight)
drawObjects()
redrawStats() #Updates the current player stats at the end of each turn
redrawLog() #Updates the log after each player turn
pygame.display.update()
def playersTurn(): #Pauses the game and allows the player to take a turn
playersturn = True
while playersturn == True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if (event.key == K_o):
levelhandler.move_floor(-1)
levelhandler.activelevel().objectlist.append(player)
redrawScreen()
if (event.key == K_p):
levelhandler.move_floor(1)
levelhandler.activelevel().objectlist.append(player)
redrawScreen()
if (event.key == K_d):
player.move(32,0) #Modify the game state when some action is entered. The move function takes target coordinates, the map[] list of a Dungeon object (see Dungeon.py), and the object list.
playersturn = False
if (event.key == K_a):
player.move(-32,0)
playersturn = False
if (event.key == K_w):
player.move(0,-32)
playersturn = False
if (event.key == K_s):
player.move(0,32)
playersturn = False
if (event.key == K_h):
print ('Some Bullshit')
if (event.key == K_i): #inventory
paused = 1
while (paused == 1):
drawMap(boardWidth, boardHeight)
drawObjects()
boolean = False
invmenu=player.inventorymenu
invmenu.Update()
invscreen=player.inventorysheet
DISPLAYSURF.blit(invscreen, (invmenu.xloc,invmenu.yloc ))
pygame.display.update()
for event in pygame.event.get():
if event.type==MOUSEBUTTONDOWN and event.button == 1 :
cursx,cursy=event.pos
if cursx>invmenu.xloc and cursx<invmenu.xloc+200 and cursy >invmenu.yloc and cursy < invmenu.yloc+10:
boolean=True
while(boolean):
for event in pygame.event.get():
if event.type==MOUSEBUTTONUP and event.button == 1 :
boolean=False
relx, rely = event.pos
invmenu.xloc=invmenu.xloc+(relx-cursx)
invmenu.yloc=invmenu.yloc+(rely-cursy)
elif event.type==MOUSEBUTTONDOWN and event.button == 3:
cursx,cursy=event.pos
if(cursx>invmenu.xloc+10 and cursx<invmenu.xloc+42 and cursy>invmenu.yloc+20 and cursy<invmenu.yloc+52):
player.equip_item(0)
if(cursx>invmenu.xloc+60 and cursx<invmenu.xloc+92 and cursy>invmenu.yloc+20 and cursy<invmenu.yloc+52):
player.equip_item(1)
if(cursx>invmenu.xloc+110 and cursx<invmenu.xloc+142 and cursy>invmenu.yloc+20 and cursy<invmenu.yloc+52):
player.equip_item(2)
if(cursx>invmenu.xloc+10 and cursx<invmenu.xloc+42 and cursy>invmenu.yloc+60 and cursy<invmenu.yloc+92):
player.equip_item(3)
if(cursx>invmenu.xloc+60 and cursx<invmenu.xloc+92 and cursy>invmenu.yloc+60 and cursy<invmenu.yloc+92):
player.equip_item(4)
if(cursx>invmenu.xloc+110 and cursx<invmenu.xloc+142 and cursy>invmenu.yloc+60 and cursy<invmenu.yloc+92):
player.equip_item(5)
if event.type == KEYDOWN:
if event.key == K_p:
redrawScreen()
paused = 0
if (event.key == K_e): #equipment
paused = 1
while (paused == 1):
drawMap(boardWidth, boardHeight)
drawObjects()
equipmenu=player.charactermenu
equipscreen=player.charactersheet
print player.charactermenu.xloc,player.charactermenu.yloc
DISPLAYSURF.blit(equipscreen, (player.charactermenu.xloc,player.charactermenu.yloc))
pygame.display.update()
for event in pygame.event.get():
if event.type==MOUSEBUTTONDOWN and event.button == 1 :
cursx,cursy=event.pos
if cursx>player.charactermenu.xloc and cursx<player.charactermenu.xloc+200 and cursy >player.charactermenu.yloc and cursy < player.charactermenu.yloc+10:
boolean=True
while(boolean):
for event in pygame.event.get():
if event.type==MOUSEBUTTONUP and event.button == 1 :
boolean=False
relx, rely = event.pos
player.charactermenu.xloc=player.charactermenu.xloc+(relx-cursx)
player.charactermenu.yloc=player.charactermenu.yloc+(rely-cursy)
elif event.type==MOUSEBUTTONDOWN and event.button == 3:
cursx,cursy=event.pos
if(cursx>player.charactermenu.xloc+10 and cursx<player.charactermenu.xloc+42 and cursy>player.charactermenu.yloc+10 and cursy<player.charactermenu.yloc+42 and player.equipment.check_slot('head') == False):
index = 0
for item in player.equipment.items:
if item.type=='head':
break
else:
index=index+1
player.unequip_item(index)
if(cursx>player.charactermenu.xloc+150 and cursx<player.charactermenu.xloc+182 and cursy>player.charactermenu.yloc+10 and cursy<player.charactermenu.yloc+42 and player.equipment.check_slot('shoulders') == False):
index = 0
for item in player.equipment.items:
if item.type=='shoulders':
break
else:
index=index+1
player.unequip_item(index)
if(cursx>player.charactermenu.xloc+10 and cursx<player.charactermenu.xloc+42 and cursy>player.charactermenu.yloc+60 and cursy<player.charactermenu.yloc+92 and player.equipment.check_slot('chest') == False):
index = 0
for item in player.equipment.items:
if item.type=='chest':
break
else:
index=index+1
player.unequip_item(index)
if(cursx>player.charactermenu.xloc+150 and cursx<player.charactermenu.xloc+182 and cursy>player.charactermenu.yloc+60 and cursy<player.charactermenu.yloc+92 and player.equipment.check_slot('hands') == False):
index = 0
for item in player.equipment.items:
if item.type=='hands':
break
else:
index=index+1
player.unequip_item(index)
if(cursx>player.charactermenu.xloc+10 and cursx<player.charactermenu.xloc+42 and cursy>player.charactermenu.yloc+110 and cursy<player.charactermenu.yloc+142 and player.equipment.check_slot('legs') == False):
index = 0
for item in player.equipment.items:
if item.type=='legs':
break
else:
index=index+1
player.unequip_item(index)
if(cursx>player.charactermenu.xloc+150 and cursx<player.charactermenu.xloc+182 and cursy>player.charactermenu.yloc+110 and cursy<player.charactermenu.yloc+142 and player.equipment.check_slot('feet') == False):
index = 0
for item in player.equipment.items:
if item.type=='feet':
break
else:
index=index+1
player.unequip_item(index)
if(cursx>player.charactermenu.xloc+10 and cursx<player.charactermenu.xloc+42 and cursy>player.charactermenu.yloc+160 and cursy<player.charactermenu.yloc+192 and (player.equipment.check_slot('1h') == False or player.equipment.check_slot('2h') == False)):
index = 0
for item in player.equipment.items:
if item.type=='1h' or item.type=='2h':
break
else:
index=index+1
player.unequip_item(index)
if(cursx>player.charactermenu.xloc+150 and cursx<player.charactermenu.xloc+182 and cursy>player.charactermenu.yloc+160 and cursy<player.charactermenu.yloc+192 and player.equipment.check_slot('shield') == False):
index = 0
for item in player.equipment.items:
if item.type=='shield':
break
else:
index=index+1
player.unequip_item(index)
if event.type == KEYDOWN:
if event.key == K_p:
redrawScreen()
paused = 0
def speedAdjust():
objectlist = sorted(levelhandler.activelevel().objectlist, key=lambda object:object.speed, reverse = True)
return objectlist
def redrawStats():
stats.Update(player)
DISPLAYSURF.blit(stats.menusurface, (stats.xcoord, stats.ycoord))
def redrawLog():
DISPLAYSURF.blit(log.menusurface, (log.xcoord, log.ycoord))
#The game structure is as follows:
#1) Draw the map and allow the player to take his first turn.
#2) After the player takes his first turn, enter the game loop.
#3a) Re order the list of actors depending on speed. Higher speed = earlier turn order
#3b) Iterate through the list of actors, allowing each to take his respective turn.
#3c) When you get to the player, pause the game and allow him to take his turn.
#3d) After everyone has taken their turns, refresh the screen with the new game state.
levelhandler.activelevel().objectlist = speedAdjust() #Adjust the objects according to speed
redrawScreen() #First draw the screen
redrawLog() #Updates the log after each player turn
playersTurn() #Player ALWAYS gets first turn. This is to avoid a monster attacking you when you enter the level before you have a chance to respond or receiving an attack before the screen is drawn.
redrawScreen() #Update the screen after the player takes the first turn, and then begin game loop.
redrawLog() #Updates the log after each player turn
while True:
levelhandler.activelevel().objectlist = speedAdjust()
for object in levelhandler.activelevel().objectlist:
if object == player:
playersTurn()
redrawStats() #Updates the current player stats at the end of each turn
redrawLog() #Updates the log after each player turn
redrawScreen
else:
if object.type == 'monster':
object.look()
direction = [32,-32]
object.move(random.choice(direction),random.choice(direction))
#print(object.name + " growls") #Here, the monster will take their turn
redrawScreen()
redrawStats() #Updates the current player stats at the end of each turn
redrawLog() #Updates the log after each player turn