-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
723 lines (607 loc) · 35.4 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
import pygame
pygame.init()
import startscreen
import Player
import db
import dataTypes
import item
import world
import json
import enemy
import methods
import random
import spritesheet
# TODO LIST - make someone say mada mada
#for miller
# TODO LIST - hand_Okay
#for daniel
#init pygame
p = pygame
#create a database interface
dbInt = db.DBInterface()
#another shortcut
pos = dataTypes.pos
#define color constants for easy access
WHITE = dataTypes.WHITE
BLACK = dataTypes.BLACK
RED = dataTypes.RED
GREEN = dataTypes.GREEN
BLUE = dataTypes.BLUE
#easy acces to display and clock
display = p.display
clock = p.time.Clock()
#reference to the save method
save = dbInt.save
#DATA BASE METHODS
#generate new save (compiles all information then writes to db)
def GenerateNewSave(playerName, playerClass):
player = Player.generateNewPlayerData(playerClass)
worldData = dataTypes.worldData()
player.playerClass.setupClass(player.inventory)
saveData = dataTypes.saveData(player, worldData).return_save()
dbInt.newSave(playerName, saveData)
#loads a save then return the parsed save data
def loadSave(saveName):
save = dbInt.checkSave(saveName)
return ParseSaveData(json.loads(save.userdata))
#parses the save data and returns it as a saveData object for easy use
def ParseSaveData(saveData):
player = dataTypes.playerData(
pos(saveData[0]['pos']['x'], saveData[0]['pos']['y']),
dataTypes.playerInventory(weapon=item.ItemStack(1, item.allItems[saveData[0]['inv']['weapon']]), special=item.ItemStack(1, item.allItems[saveData[0]['inv']['special']]), armour=item.ItemStack(1, item.allItems[saveData[0]['inv']['armour']]), ring=item.ItemStack(1, item.allItems[saveData[0]['inv']['ring']]), container=dataTypes.container(30, saveData[0]['inv']['container'])),
dataTypes.entityStats(hp=saveData[0]['stats']['hp'], mp=saveData[0]['stats']['mp'], defen=saveData[0]['stats']['def'] ,spd=saveData[0]['stats']['spd'], atk=saveData[0]['stats']['atk'], dex=saveData[0]['stats']['dex'], vit=saveData[0]['stats']['vit']),
Player.className[saveData[0]['class']],
dataTypes.Level(saveData[0]['level']['lvl'], saveData[0]['level']['exp'])
)#create playerdata object
worldData = dataTypes.worldData(saveData[1]['seed'])
return dataTypes.saveData(player, worldData)
#inventory elemnt
class invElement(pygame.sprite.Sprite):
def __init__(self, x, y, image, slot, Itemstack):
super().__init__()
self.position = pos(x, y)
self.image = image
self.Itemstack = Itemstack
self.rect = self.image.get_rect()
self.rect.center = (self.position.x, self.position.y)
self.invSlot = slot
#inventory slot data type
class InvSlot(pygame.sprite.Sprite):
def __init__(self, x, y, slot, itemStack=None, replacementImage = None):
super().__init__()#init sprite
#store variables
self.position = pos(x, y)
self.invSlot = slot
if itemStack:
icon = pygame.transform.scale(itemStack.material.image, (60, 60))
methods.text_to_screen(str(itemStack.amount), 50, 50, icon,font=dataTypes.GUI_FONT_SMALLER)
self.image = icon
elif replacementImage:
self.image = replacementImage
else:
self.image =pygame.Surface((64,64))
methods.text_to_screen(self.invSlot, 32, 32, self.image, font=dataTypes.GUI_FONT_SMALL, center=True)
self.rect = self.image.get_rect()
self.rect.center = (self.position.x, self.position.y)
if itemStack:
self.elem = invElement(self.position.x, self.position.y, self.image, self.invSlot, itemStack)
else:
self.elem = None
def ret_InvElemt(self):
#return inventory elemnt soted
if self.elem:
a= self.elem
self.elem= item.ItemStack(1, item.allItems["0xfff"])
return a
else:
return None
#main client object
class Client:
#define constants
running = True
def __init__(self):
#initialize pygame and the screen
self.screen = p.display.set_mode((dataTypes.w, dataTypes.h), pygame.FULLSCREEN)#make fullscreen
pygame.display.set_caption('Dungeon Explorer') # Title on the title bar of the screen
self.state = 1
self.states = {1: self.main_menu, 2:self.game, 3:self.pause}
#initialize items
item.init()
#initialize enemies
enemy.init()
self.name = None
self.enemies = p.sprite.Group()
#genned Chunks dict to easily store all genned chunks for easy reuse
self.gennedChunks = {}
warrSS = spritesheet.spritesheet("resources/Sprites/items/warrior.png")#inv slots images
warriorSlots = {
"weapon": warrSS.image_at((0, 0, 8, 8), colorkey=dataTypes.WHITE),
"special": warrSS.image_at((8, 0, 8, 8), colorkey=dataTypes.WHITE),
"armour": warrSS.image_at((0, 8, 8, 8), colorkey=dataTypes.WHITE),
"ring": warrSS.image_at((8, 8, 8, 8), colorkey=dataTypes.WHITE)
}
rangerSS = spritesheet.spritesheet("resources/Sprites/items/ranger.png")
rangerSlots = {
"weapon": rangerSS.image_at((0, 0, 8, 8), colorkey=dataTypes.WHITE),
"special": rangerSS.image_at((8, 0, 8, 8), colorkey=dataTypes.WHITE),
"armour": rangerSS.image_at((0, 8, 8, 8), colorkey=dataTypes.WHITE),
"ring": rangerSS.image_at((8, 8, 8, 8), colorkey=dataTypes.WHITE)
}
mageSS = spritesheet.spritesheet("resources/Sprites/items/mage.png")
mageSlots = {
"weapon": mageSS.image_at((0, 0, 8, 8), colorkey=dataTypes.WHITE),
"special": mageSS.image_at((8, 0, 8, 8), colorkey=dataTypes.WHITE),
"armour": mageSS.image_at((0, 8, 8, 8), colorkey=dataTypes.WHITE),
"ring": mageSS.image_at((8, 8, 8, 8), colorkey=dataTypes.WHITE)
}
self.slots = {"Warrior": warriorSlots, "Ranger": rangerSlots, "Mage": mageSlots}
def run(self):
#main game loop
while self.running:
#tick clock
clock.tick(dataTypes.FPS)
#run the states
self.states[self.state]()
#once while loop broken
p.quit()
return
def main_menu(self):
#define constants for background
load = True
menuState = 1 #1 is main menu, 2 is play menu
gennedChunks = {}#store genned chunks
loadedChunks = []
World = world.world(random.randint(1, 10000))
toGen = dataTypes.pos(-1, -1)
genTemp = dataTypes.pos(toGen.x, toGen.y)
#play menu music
pygame.mixer.music.load("resources/audio/MainMenu.wav")
pygame.mixer.music.set_volume(0.1)
pygame.mixer.music.play(-1)
#create map
for y in range(3):
genTemp.y = toGen.y + y
genTemp.x = toGen.x
for x in range(3):
genTemp.x = toGen.x + x
if str(genTemp) not in gennedChunks.keys():
gennedChunks[str(genTemp)] = world.Chunk(dataTypes.chunkData(genTemp))
gennedChunks[str(genTemp)].genChunk(World.genNoiseMap(gennedChunks[str(genTemp)].tilePos))
loadedChunks.append(gennedChunks[str(genTemp)])
#store buttons to be used in each screen
buttons1 = p.sprite.Group()
buttons1.add(methods.playButton(dataTypes.w//2, 400), methods.instructionsButton(dataTypes.w//2, 500), methods.backButton(dataTypes.w//2, 600, text="Exit"))
buttons2Load = p.sprite.Group()
buttons2NewSave = p.sprite.Group()
buttons3 = p.sprite.Group()
buttons3.add(methods.createSaveButton(dataTypes.w//2, dataTypes.h//4+dataTypes.h//2))
buttons3Next = p.sprite.Group()
buttons3Next.add(methods.nextButton(dataTypes.w//2-130, dataTypes.h//4+227, "L", fonts=[dataTypes.GUI_FONT, dataTypes.GUI_FONT_BUTTON]))
buttons3Next.add(methods.nextButton(dataTypes.w // 2 + 75, dataTypes.h // 4 + 226, "R", fonts=[dataTypes.GUI_FONT, dataTypes.GUI_FONT_BUTTON]))
buttons3Back = p.sprite.Group()
buttons3Back.add(methods.backButton(dataTypes.w//2, dataTypes.h//4+dataTypes.h//2+100))
buttons4 = p.sprite.Group(methods.backButton(dataTypes.w // 2, 100))
#contants/misc vars
saves = 5
temp = 0
menuSwapped = False
accs = []
#get save files
for x in dbInt.returnAllSaves():
buttons2Load.add(methods.loadButton(dataTypes.w // 2, 300 + temp * 100, x.name, fonts=[dataTypes.GUI_FONT, dataTypes.GUI_FONT_BUTTON]))
accs.append(x.name)
temp += 1
saves -= 1
#left over slots to be filled
for y in range(saves):
buttons2NewSave.add(methods.newSaveButton(dataTypes.w//2, 300+temp*100+y*100, fonts=[dataTypes.GUI_FONT, dataTypes.GUI_FONT_BUTTON]))
#more misc vars for menus
TextField = []
classes = [Player.warriorClass, Player.mageClass, Player.rangerClass]
classesIndex = 0
#menu loop
while load:
for e in pygame.event.get():#event queue
if e.type == p.QUIT:
quit()
if e.type == p.KEYDOWN:
if e.key == p.K_ESCAPE:
quit()
if e.type == p.MOUSEBUTTONDOWN and e.button == 1: # if it is a click
mouse = p.mouse.get_pos() # get mouse position
if not menuSwapped and menuState == 1:
for x in buttons1: # for each button on screen 1
if (x.x + x.w > mouse[0] > x.x) and (x.y + x.h > mouse[1] > x.y): # if it is on a button and it it is o the r
if x.text == 'Play':#button logic
menuState = 2
elif x.text == 'Instructions':
menuState = 4
elif x.text == 'Exit':
load=False
self.running = False
exit()
menuSwapped = True
if menuState == 2 and not menuSwapped:#if it is menu state 2
for x in buttons2Load:#for each bytton
if (x.x + x.w > mouse[0] > x.x) and (x.y + x.h > mouse[1] > x.y):#check if inside
self.Load(x.text)#start playing game audio and swap to game state
load = False
menuSwapped = True
pygame.mixer.music.stop()
pygame.mixer.music.load("resources/audio/inGame.wav")
pygame.mixer.music.set_volume(0.1)
pygame.mixer.music.play(-1)
for x in buttons2NewSave:#for each button
if (x.x + x.w > mouse[0] > x.x) and (x.y + x.h > mouse[1] > x.y):#check if click was inside
menuState = 3#button logic
menuSwapped = True
TextField = []
if menuState == 3 and not menuSwapped:#3rd menu
for x in buttons3:#for each bytton
if (x.x + x.w > mouse[0] > x.x) and (x.y + x.h > mouse[1] > x.y):#check if click was on button
if len(TextField) > 0:#generate new save
if "".join(TextField) not in accs:
GenerateNewSave("".join(TextField), classes[classesIndex])
self.Load("".join(TextField))
load=False
pygame.mixer.music.stop()
for x in buttons3Next:#for each button
if (x.x + x.w > mouse[0] > x.x) and (x.y + x.h > mouse[1] > x.y):#if click was on button
classesIndex = x.press(classesIndex)#press the button
for x in buttons3Back:#for each button
if (x.x + x.w > mouse[0] > x.x) and (x.y + x.h > mouse[1] > x.y):#if click was on button
TextField = []
classesIndex = 0
menuState = 2
if menuState == 4 and not menuSwapped:
for x in buttons4:
if p.Rect(x.x, x.y, x.w, x.h).collidepoint(mouse[0], mouse[1]):#if click was on button
menuState = 1
menuSwapped = True
if menuState == 3:#typeing logic for text field
if e.type == p.KEYDOWN:
if e.key == p.K_BACKSPACE:
if len(TextField) > 0:
TextField.pop()
elif e.key == p.K_SPACE:
TextField.append(" ")
else:
TextField.append(pygame.key.name(e.key))
if e.type == p.MOUSEBUTTONUP:#allow for another button to be pressed
menuSwapped = False
#tick the clock
clock.tick(dataTypes.FPS)
#menu state 1 main menu
if menuState == 1:
for chunk in loadedChunks:
chunk.tileGroup.draw(self.screen)
methods.text_to_screen("DUNGEON EXPLORER", dataTypes.w//2, 200, self.screen, font=dataTypes.GAME_FONT_BIG)
buttons1.update(self.screen)
#menustate 2, load screen
elif menuState == 2:
for chunk in loadedChunks:
chunk.tileGroup.draw(self.screen)
methods.text_to_screen("DUNGEON EXPLORER", dataTypes.w // 2, 200, self.screen, font=dataTypes.GAME_FONT_BIG)
buttons2NewSave.update(self.screen)
buttons2Load.update(self.screen)
#create new character
elif menuState == 3:
for chunk in loadedChunks:
chunk.tileGroup.draw(self.screen)
methods.text_to_screen("Create New Character", dataTypes.w // 2, 200, self.screen, font=dataTypes.GAME_FONT_BIG)
methods.text_to_screen("- Name -", dataTypes.w//2, dataTypes.h//4+100, self.screen, font=dataTypes.GUI_FONT)
methods.text_to_screen("".join(TextField), dataTypes.w//2, dataTypes.h//4+150, self.screen, center=True, font=dataTypes.GUI_FONT)
methods.text_to_screen(classes[classesIndex].name, dataTypes.w//2, dataTypes.h//4 +250, self.screen, center=True, font=dataTypes.GUI_FONT)
buttons3.update(self.screen)
buttons3Back.update(self.screen)
buttons3Next.update(self.screen)
#information screen
elif menuState == 4:
for chunk in loadedChunks:
chunk.tileGroup.draw(self.screen)
methods.text_to_screen("You are placed in an infinite world.", dataTypes.w // 2, dataTypes.h // 4 + 50, self.screen, font=dataTypes.GUI_FONT)
methods.text_to_screen("Enemies attack you, and holding", dataTypes.w // 2, dataTypes.h // 4 + 100, self.screen, font=dataTypes.GUI_FONT)
methods.text_to_screen("left click you can fight back.", dataTypes.w // 2, dataTypes.h // 4 + 150,self.screen, font=dataTypes.GUI_FONT)
methods.text_to_screen("The arrow keys or WASD allow you", dataTypes.w // 2, dataTypes.h // 4 + 200, self.screen, font=dataTypes.GUI_FONT)
methods.text_to_screen("to move and avoid projectiles.",dataTypes.w // 2, dataTypes.h // 4 + 250, self.screen, font=dataTypes.GUI_FONT)
methods.text_to_screen("The Spacebar is a special ability", dataTypes.w//2, dataTypes.h//4+300, self.screen, font=dataTypes.GUI_FONT)
methods.text_to_screen("Equip items enemies drop", dataTypes.w // 2, dataTypes.h // 4 + 350, self.screen, font=dataTypes.GUI_FONT)
methods.text_to_screen("to become more powerful.", dataTypes.w // 2, dataTypes.h // 4 + 400, self.screen, font=dataTypes.GUI_FONT)
methods.text_to_screen("Try to survive as long as possible.", dataTypes.w // 2, dataTypes.h // 4 + 450, self.screen, font=dataTypes.GUI_FONT)
buttons4.update(self.screen)
display.update()
#once broken from loop go to game state
self.state = 2
def game(self):
#allChunks = [_.split(":") for _ in self.gennedChunks.keys()]
#allChunks = [[int(allChunks[x][0]), int(allChunks[x][1])] for x in range(len(allChunks))]
for e in p.event.get(): # event queue
if e.type == p.QUIT:
self.running = False#stop game
if self.name:#if ingame
dbInt.save(self.name, dataTypes.saveData(self.Player.return_playerData(), self.World.returnWorldData()).return_save())#save data
if e.type == p.KEYDOWN:#if keysroke
if e.key == p.K_ESCAPE:#open pause menu
self.state = 3
if e.key == p.K_f:#use hp pot
contains, index = self.Player.inventory.container.containsGroup("Health")#see if inventory has one
if contains:#if it does
self.Player.currentHp += int(self.Player.inventory.container.contents[index].material.useMeta[1])#consume and deduct it fromhow many the player has
self.Player.inventory.container.contents[index].amount -= 1
if self.Player.currentHp > self.Player.stats.health:
self.Player.currentHp = self.Player.stats.health
if e.key == p.K_SPACE:#if it is space (use ability)
if self.Player.inventory.special and self.Player.canUseSpecial:
if self.Player.inventory.special.material.useMeta[0] == "Teleport":#use teleport ability
mouse = pygame.mouse.get_pos()#move player
self.Player.position.x += mouse[0]-dataTypes.w//2
self.Player.position.y += mouse[1]-dataTypes.h//2
self.Player.boostStats.vitality+=int(self.Player.inventory.special.material.useMeta[1])#add stat boosts
self.Player.boostStats.attack+=int(self.Player.inventory.special.material.useMeta[1])//2
elif self.Player.inventory.special.material.useMeta[0] == "Speed":#use speed ability
self.Player.boostStats.speed+=int(self.Player.inventory.special.material.useMeta[1])#boost player speed
elif self.Player.inventory.special.material.useMeta[0] == "Dexterity":#use dexterity ability
self.Player.boostStats.dexterity+=int(self.Player.inventory.special.material.useMeta[1])#boot firerate
self.Player.canUseSpecial = False#cant use special
pygame.time.set_timer(pygame.USEREVENT + 2, 3000)#add event to cancel it
else:
print("Cant Use That Yet")#error message if they cant use ability
if e.type == pygame.USEREVENT+2:#if ability event
self.Player.canUseSpecial = True#allow to use epcial
pygame.time.set_timer(pygame.USEREVENT+2, 0)#reset event timer
self.Player.boostStats = dataTypes.entityStats()#stop boosts
if e.type == pygame.USEREVENT+1 and int(self.Player.inventory.weapon.material.SlotType) == self.Player.playerClass.slotTypes[0]:#if it is attack event, attack
self.Player.Fire(pygame.mouse.get_pos())
loadedChunks = []
self.screen.fill(dataTypes.BLACK)
toGen = dataTypes.pos(self.Player.chunkPos.x-1, self.Player.chunkPos.y-1)
genTemp = dataTypes.pos(toGen.x, toGen.y)
for y in range(3):
genTemp.y = toGen.y+y
genTemp.x = toGen.x
for x in range(3):
genTemp.x = toGen.x+x
if str(genTemp) not in self.gennedChunks.keys():
self.gennedChunks[str(genTemp)] = world.Chunk(dataTypes.chunkData(genTemp))
self.gennedChunks[str(genTemp)].genChunk(self.World.genNoiseMap(self.gennedChunks[str(genTemp)].tilePos), enemiesGroup=self.enemies)
loadedChunks.append(self.gennedChunks[str(genTemp)])
for chunk in loadedChunks:
chunk.tileGroup.update(self.Player.position)
chunk.tileGroup.draw(self.screen)
for coords in list(self.gennedChunks):
temp = [int(_) for _ in coords.split(":")]
if (((temp[0] - self.Player.chunkPos.x)**2 + (temp[1] - self.Player.chunkPos.y)**2)**0.5) > 4:
del self.gennedChunks[coords]
self.enemies.update(self.Player.position, self.screen)
self.enemies.draw(self.screen)
for x in self.enemies:
x.bullets.update(self.Player.position)
x.bullets.draw(self.screen)
hits = pygame.sprite.spritecollide(self.Player, x.bullets, True)
for hit in hits:
dead = self.Player.hit(hit.damage)
if dead:
dbInt.deleteUser(self.name)
self.state = 1
self.Player.bullets.update(self.Player.position)
self.Player.bullets.draw(self.screen)
#check for colosion between bullet gorups
#deal damage to object collided with
hits = pygame.sprite.groupcollide(self.Player.bullets, self.enemies, True, False)
for hit in hits:#
outcome = hits[hit][0].hit(hit.damage)
if outcome[0]:
self.Player.level.exp+=outcome[2]
if self.Player.level.exp > 100:
self.Player.levelUp()
for x in outcome[1]:
self.Player.inventory.container.AddTo(x)
print("Item Get " + x.material.name)
self.Player.update(self.gennedChunks)
self.screen.blit(self.Player.playerAnim, (dataTypes.w/2-16 + self.Player.drawOffset, dataTypes.h/2-16))
#gui
self.drawPlayerUI()
p.display.update()
def pause(self):
load = True
exitGame = False
buttons = p.sprite.Group()
buttons.add(methods.backButton(500, 550, text="Back To Menu", fonts=[dataTypes.GUI_FONT_SMALL, dataTypes.GUI_FONT_SMALLER], boxOffset=200))
slots = p.sprite.Group()
holding = None
while load:
for e in pygame.event.get():
mouse = p.mouse.get_pos()
if e.type == p.QUIT:
#dbInt.save(self.name, dataTypes.saveData(self.Player.return_playerData(), self.World.returnWorldData()).return_save())
p.quit()
exit(0)
if e.type == p.KEYDOWN:
if e.key == p.K_ESCAPE:
if holding:
self.Player.inventory.container.contents[holding.invSlot] = holding.Itemstack
load = False
self.state = 2
if e.type == p.MOUSEBUTTONDOWN and e.button == 1: # if it is a click
for x in buttons:
if ((x.x + x.w+200) > mouse[0] > (x.x+200)) and ((x.y + x.h+200) > mouse[1] > (x.y+200)):
if x.text == "Back To Menu":
dbInt.save(self.name, dataTypes.saveData(self.Player.return_playerData(), self.World.returnWorldData()).return_save())
load=False
self.state = 1
for x in slots:
if x.rect.collidepoint(e.pos):
if x.invSlot == "Garbage":
holding = None
if x.invSlot in ["weapon", "special", "ring", "armour"]:
if x.invSlot == "weapon":
if holding:
if self.Player.playerClass.slotTypes[0] == int(holding.Itemstack.material.SlotType):
tohold = x.ret_InvElemt()
slots.remove(x)
self.Player.inventory.weapon = holding.Itemstack
holding = tohold
else:
continue
else:
holding = x.ret_InvElemt()
self.Player.inventory.weapon = item.ItemStack(1, item.Nothing)
slots.remove(x)
if x.invSlot == "special":
if holding:
if self.Player.playerClass.slotTypes[1] == int(holding.Itemstack.material.SlotType):
tohold = x.ret_InvElemt()
slots.remove(x)
self.Player.inventory.special = holding.Itemstack
holding = tohold
else:
continue
holding = tohold
else:
holding = x.ret_InvElemt()
self.Player.inventory.special = item.ItemStack(1, item.Nothing)
slots.remove(x)
if x.invSlot == "armour":
if holding:
if self.Player.playerClass.slotTypes[2] == int(holding.Itemstack.material.SlotType):
tohold = x.ret_InvElemt()
slots.remove(x)
self.Player.inventory.armour = holding.Itemstack
holding = tohold
else:
continue
holding = tohold
else:
holding = x.ret_InvElemt()
self.Player.inventory.armour = item.ItemStack(1, item.Nothing)
slots.remove(x)
if x.invSlot == "ring":
if holding:
if self.Player.playerClass.slotTypes[3] == int(holding.Itemstack.material.SlotType):
tohold = x.ret_InvElemt()
slots.remove(x)
self.Player.inventory.ring = holding.Itemstack
holding = tohold
else:
continue
holding = tohold
else:
holding = x.ret_InvElemt()
self.Player.inventory.ring = item.ItemStack(1, item.Nothing)
slots.remove(x)
continue
tohold = x.ret_InvElemt()
slots.remove(x)
traded = False
if holding:
if self.Player.inventory.container.contents[x.invSlot].material.type == holding.Itemstack.material.type:
self.Player.inventory.container.contents[x.invSlot].amount+= holding.Itemstack.amount
traded=True
else:
self.Player.inventory.container.contents[x.invSlot] = holding.Itemstack
else:
self.Player.inventory.container.contents[x.invSlot] = item.ItemStack(1, item.allItems["0xfff"])
if traded:
holding=None
else:
holding = tohold
methods.text_to_screen("PAUSED", dataTypes.w//2, dataTypes.h//8, self.screen, font=dataTypes.GUI_FONT_BIG)
Inventory = pygame.Surface((600, 600))
w, h = Inventory.get_size()
Inventory.fill(dataTypes.DARK_GRAY)
buttons.update(Inventory)
slots.empty()
slots.add(InvSlot(250, 550, "Garbage", replacementImage=pygame.transform.scale(pygame.image.load("resources/Sprites/garbage.png"), (8*4, 8*4))))
for y in range(5):
for x in range(6):
if self.Player.inventory.container.contents[str(x+6*y)].material.image:
slots.add(InvSlot(64*x+125+200+16, 64*y+100+200+16, str(x+6*y), itemStack=self.Player.inventory.container.contents[str(x+6*y)]))
else:
slots.add(InvSlot(64*x+125+200+16, 64*y+100+200+16, str(x+6*y)))
for x in slots:
if x.rect.collidepoint(pygame.mouse.get_pos()):
if x.elem:
if x.elem.Itemstack.material.type =="0xfff":
continue
methods.text_to_screen(x.elem.Itemstack.material.name+": "+x.elem.Itemstack.material.description, 300, 500, Inventory, font=dataTypes.GUI_FONT_DESC, center=True)
if not x.elem.Itemstack.material.damage == None:
methods.text_to_screen(str(x.elem.Itemstack.material.damage[0]) + "-"+str(x.elem.Itemstack.material.damage[1]) + " Damage, " + str(x.elem.Itemstack.material.range) + " Range",
300, 550, Inventory, font=dataTypes.GUI_FONT_DESC, center=True)
elif not x.elem.Itemstack.material.useMeta==[]:
methods.text_to_screen("Use: " + x.elem.Itemstack.material.useMeta[0], 300, 550, Inventory, font=dataTypes.GUI_FONT_DESC, center=True)
self.screen.blit(Inventory, (200, 200))
self.drawPlayerUISlots(slots)
slots.draw(self.screen)
if holding:
holding.rect.center = p.mouse.get_pos()
self.screen.blit(holding.image, holding.rect)
display.flip()
def drawPlayerUI(self):
pygame.draw.rect(self.screen, dataTypes.GRAY, (0, 850, dataTypes.w, 150))
if self.Player.inventory.weapon.material.image:
self.screen.blit(self.Player.inventory.weapon.material.image, (dataTypes.w // 4 + 100, 950))
else:
self.screen.blit(self.slots[self.Player.playerClass.name]["weapon"], (dataTypes.w // 4 + 100, 950))
if self.Player.inventory.special.material.image:
self.screen.blit(self.Player.inventory.special.material.image, (dataTypes.w // 4 + 200, 950))
else:
self.screen.blit(self.slots[self.Player.playerClass.name]["special"], (dataTypes.w // 4 + 200, 950))
if self.Player.inventory.armour.material.image:
self.screen.blit(self.Player.inventory.armour.material.image, (dataTypes.w // 4 + 300, 950))
else:
self.screen.blit(self.slots[self.Player.playerClass.name]["armour"], (dataTypes.w // 4 + 300, 950))
if self.Player.inventory.ring.material.image:
self.screen.blit(self.Player.inventory.ring.material.image, (dataTypes.w // 4 + 400, 950))
else:
self.screen.blit(self.slots[self.Player.playerClass.name]["ring"], (dataTypes.w // 4 + 400, 950))
hpbar = pygame.Surface((550, 20))
pygame.draw.rect(hpbar, dataTypes.RED, (0, 0, (self.Player.currentHp/self.Player.stats.health)*550, 20))
mpbar = pygame.Surface((550, 10))
pygame.draw.rect(mpbar, dataTypes.BLUE, (0,0, (self.Player.currentMp/self.Player.stats.magic)*550, 10))
expBar = pygame.Surface((550, 5))
pygame.draw.rect(expBar, (173,216,230), (0, 0, (self.Player.level.exp/100)*550, 5))
self.screen.blit(hpbar, (dataTypes.w//4, dataTypes.h-125))
self.screen.blit(mpbar, (dataTypes.w//4, dataTypes.h-100))
self.screen.blit(expBar, (dataTypes.w//4, dataTypes.h-85))
methods.text_to_screen("Lvl: "+str(self.Player.level.lvl), dataTypes.w//8, 950, self.screen, font=dataTypes.GUI_FONT)
def drawPlayerUISlots(self, group):
pygame.draw.rect(self.screen, dataTypes.GRAY, (0, 850, dataTypes.w, 150))
if self.Player.inventory.weapon.material.image:
group.add(InvSlot(dataTypes.w // 4 + 100, 950, "weapon", itemStack=self.Player.inventory.weapon))
else:
group.add(InvSlot(dataTypes.w//4+100, 950, "weapon", replacementImage=self.slots[self.Player.playerClass.name]["weapon"]))
if self.Player.inventory.special.material.image:
group.add(InvSlot(dataTypes.w // 4 + 200, 950, "special", itemStack=self.Player.inventory.special))
else:
group.add(InvSlot(dataTypes.w // 4 + 200, 950, "special",
replacementImage=self.slots[self.Player.playerClass.name]["special"]))
if self.Player.inventory.armour.material.image:
group.add(InvSlot(dataTypes.w // 4 + 300, 950, "armour", itemStack=self.Player.inventory.special))
else:
group.add(InvSlot(dataTypes.w // 4 + 300, 950, "armour",
replacementImage=self.slots[self.Player.playerClass.name]["armour"]))
if self.Player.inventory.ring.material.image:
group.add(InvSlot(dataTypes.w // 4 + 400, 950, "ring", itemStack=self.Player.inventory.ring))
else:
group.add(InvSlot(dataTypes.w // 4 + 400, 950, "ring",
replacementImage=self.slots[self.Player.playerClass.name]["ring"]))
hpbar = pygame.Surface((550, 20))
pygame.draw.rect(hpbar, dataTypes.RED, (0, 0, (self.Player.currentHp / self.Player.stats.health) * 550, 20))
mpbar = pygame.Surface((550, 10))
pygame.draw.rect(mpbar, dataTypes.BLUE, (0, 0, (self.Player.currentMp / self.Player.stats.magic) * 550, 10))
expBar = pygame.Surface((550, 5))
pygame.draw.rect(expBar, (173,216,230), (0, 0, (self.Player.level.exp/100)*550, 5))
self.screen.blit(hpbar, (dataTypes.w // 4, dataTypes.h - 125))
self.screen.blit(mpbar, (dataTypes.w // 4, dataTypes.h - 100))
self.screen.blit(expBar, (dataTypes.w//4, dataTypes.h-85))
def Load(self, name):
# TEMP - load player save
self.saveData = loadSave(name)
self.name = name
#print(self.saveData.return_save())
# create Player and world objects from passed data by the loadsave
self.Player = Player.player(self.saveData.player)
self.World = world.world(seed=self.saveData.world.seed)
#declaring game client
gameClient = Client()
gameClient.run()