-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguide_drmario.py
4378 lines (4076 loc) · 199 KB
/
guide_drmario.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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pygame, sys, math, time, os, random, perlin, pickle, datetime, webbrowser
from pygame.locals import *
def moveParallax(val):
global parallaxPos
parallaxPos=(parallaxPos[0]+val[0],parallaxPos[1]+val[1])
if parallaxPos[0]>0:parallaxPos=(-40+parallaxPos[0],parallaxPos[1])
elif parallaxPos[0]<-39:parallaxPos=(parallaxPos[0]+40,parallaxPos[1])
if parallaxPos[1]>0:parallaxPos=(parallaxPos[0],-40+parallaxPos[1])
elif parallaxPos[1]<-39:parallaxPos=(parallaxPos[0],parallaxPos[1]+40)
def loadTileMasks():
global tilemasks
tilemaskimg=pygame.image.load("Textures/tilemasks.png").convert_alpha()
tilemasks=[]
for j in range(5):
for i in range(13):
surf=pygame.Surface((8,8),pygame.SRCALPHA)
surf.blit(tilemaskimg,(-i*9,-j*9))
surf=pygame.transform.scale(surf,(BLOCKSIZE,BLOCKSIZE))
tilemasks.append(surf)
def loadMiscGui():
global miscGuiImages
miscGuiImages=[]
miscTilesetimg=pygame.image.load("Textures/miscTileset.png").convert()
for j in range(1):
for i in range(11):
surf=pygame.Surface((48,48))
surf.set_colorkey((255,0,255))
surf.blit(miscTilesetimg,(-i*48,-j*48))
miscGuiImages.append(surf)
def loadBackgroundImages():
global backgroundImages
backgroundimg=pygame.image.load("Textures/backgroundTilesheet.png").convert()
backgroundImages=[]
for i in range(5):
surf=pygame.Surface((20,20))
surf.blit(backgroundimg,(-i*20,0))
surf=pygame.transform.scale(surf,(40,40))
backgroundImages.append(surf)
def loadTiles():
global tileImages
tilesetimg=pygame.image.load("Textures/tileset.png").convert_alpha()
tileImages=[]
for j in range(10):
for i in range(10):
surf=pygame.Surface((8,8),pygame.SRCALPHA)
surf.set_colorkey((255,0,255))
surf.blit(tilesetimg,(-i*8,-j*8))
surf=pygame.transform.scale(surf,(BLOCKSIZE,BLOCKSIZE))
tileImages.append(surf)
def loadWallTiles():
global wallTileImages
walltilesetimg=pygame.image.load("Textures/wallTileset.png").convert_alpha()
wallTileImages=[]
for j in range(10):
for i in range(10):
surf=pygame.Surface((8,8),pygame.SRCALPHA)
surf.blit(walltilesetimg,(-i*8,-j*8))
surf=pygame.transform.scale(surf,(BLOCKSIZE,BLOCKSIZE))
wallTileImages.append(surf)
def loadProjectileImages():
global projectileImages
projectileimg=pygame.image.load("Textures/projectileTileset.png").convert()
projectileImages=[]
for j in range(10):
for i in range(10):
surf=pygame.Surface((16,16))
surf.blit(projectileimg,(-i*16,-j*16))
surf.set_colorkey((255,0,255))
projectileImages.append(surf)
def loadItemTiles():
global itemImages
itemimg=pygame.image.load("Textures/itemTileset.png").convert()
itemImages=[]
for j in range(10):
for i in range(10):
surf=pygame.Surface((16,16))
surf.blit(itemimg,(-16*i,-16*j))
surf2=pygame.Surface((24,24))
surf2.fill((255,0,255))
surf2.set_colorkey((255,0,255))
surf2.blit(surf,(4,4))
surf2=pygame.transform.scale(surf2,(48,48))
itemImages.append(surf2)
def loadHairStyles():
global hairStyles
hairStyles=[]
scale=2
img=pygame.transform.scale(pygame.image.load("Textures/hairsTileset.png"),(int(22*10*scale),int(24*scale)))
for i in range(10):
surf=pygame.Surface((int(22*scale),int(24*scale)))
surf.set_colorkey((255,0,255))
surf.blit(img,(-i*22*scale,0))
surf=pygame.transform.scale(surf,(int(20*scale),int(24*scale)))
hairStyles.append(surf)
def loadTorsoFrames():
global torsoFrames
torsoFrames=[]
scale=2
img=pygame.transform.scale(pygame.image.load("Textures/torsoTileset.png"),(int(20*19*scale),int(30*4*scale)))
for j in range(4):
for i in range(19):
surf=pygame.Surface((int(20*scale),int(30*scale)))
surf.set_colorkey((255,0,255))
surf.blit(img,(-i*20*scale,-j*30*scale))
torsoFrames.append(surf)
def loadSlimeFrames():
global slimeFrames
slimeFrames=[]
scale=2
img=pygame.transform.scale(pygame.image.load("Textures/slimeTileset.png"),(int(16*3*scale),int(12*5*scale)))
for j in range(5):
for i in range(3):
surf=pygame.Surface((int(16*scale),int(12*scale)))
surf.set_colorkey((255,0,255))
surf.blit(img,(-i*16*scale,-j*12*scale))
surf.set_alpha(200)
slimeFrames.append(surf)
def compileBackgroundImages():#creates a larger surf compiled with background surfs
global backsurfs
backsurfs=[]
for k in range(5):
backsurf=pygame.Surface((screenW+40,screenH+40))
for i in range(int(screenW/40+1)):
for j in range(int(screenH/40+1)):
backsurf.blit(backgroundImages[k],(i*40,j*40))
backsurfs.append(backsurf)
class Model():#used to store info about the player's looks (fairly useless atm)
def __init__(self,sex,hairID,skinCol,hairCol,eyeCol,shirtCol,underShirtCol,trouserCol,shoeCol):
self.sex=sex
self.skinCol=skinCol
self.hairID=hairID
self.hairCol=hairCol
self.eyeCol=eyeCol
self.shirtCol=shirtCol
self.underShirtCol=underShirtCol
self.trouserCol=trouserCol
self.shoeCol=shoeCol
class Enemy():
def __init__(self,pos,enemyID):
self.pos=pos
self.vel=(0,0)
self.ID=enemyID
self.name=str(enemyData[self.ID][0])
self.type=str(enemyData[self.ID][1])
self.HP=int(enemyData[self.ID][2])
self.maxHP=int(self.HP)
self.defense=int(enemyData[self.ID][3])
self.knockBackResist=int(enemyData[self.ID][4])
self.attackDamage=int(enemyData[self.ID][5])
self.bloodColour=tuple(enemyData[self.ID][6])
self.rect=Rect(self.pos[0]-BLOCKSIZE,self.pos[1]-BLOCKSIZE/1.5,BLOCKSIZE*2,BLOCKSIZE*1.5)
self.grounded=False
self.stopLeft=False
self.stopRight=False
self.movingLeft=False
self.movingRight=False
self.damageTick=0
self.jumpTick=100
self.despawnTick=0
self.animationFrame=0
self.gameID=random.randint(1000,9999)
enemies.append(self)
def update(self):
self.stopLeft=False
self.stopRight=False
if self.despawnTick<=0:
self.despawnTick=100
self.checkDespawn()
else:self.despawnTick-=1
if self.movingLeft:#moves player left
if not self.stopLeft:
self.vel=(-3,self.vel[1])
if self.movingRight:#moves player right
if not self.stopRight:
self.vel=(3,self.vel[1])
if not self.grounded:
self.vel=(self.vel[0],self.vel[1]+GRAVITY)
self.runAI()
self.vel=(self.vel[0]*0.95,self.vel[1]*0.95)
self.pos=(self.pos[0]+self.vel[0],self.pos[1]+self.vel[1])
self.rect.left=self.pos[0]-self.rect.width/2#updating rect
self.rect.top=self.pos[1]-self.rect.height/2
self.blockpos=(math.floor(self.pos[1]//BLOCKSIZE),math.floor(self.pos[0]//BLOCKSIZE))
self.grounded=False
if self.vel[0]<0:
if self.pos[0]<WORLDBOARDER_WEST:self.pos=(int(WORLDBOARDER_WEST),self.pos[1])
elif self.vel[0]>0:
if self.pos[0]>WORLDBOARDER_EAST:self.pos=(int(WORLDBOARDER_EAST),self.pos[1])
if self.vel[1]<0:
if self.pos[1]<WORLDBOARDER_NORTH:
self.pos=(self.pos[0],int(WORLDBOARDER_NORTH))
self.vel=(self.vel[0],0)
elif self.vel[1]>0:
if self.pos[1]>WORLDBOARDER_SOUTH:
self.pos=(self.pos[0],int(WORLDBOARDER_SOUTH))
self.vel=(self.vel[0],0)
self.grounded=True
if self.damageTick<=0:
if clientPlayer.rect.colliderect(self.rect):
if clientPlayer.pos[0]<self.pos[0]:
direction=-1
else:direction=1
clientPlayer.damage(self.attackDamage,["enemy",self.name],knockBack=10,direction=direction)
self.damageTick=100
else:self.damageTick-=1
for j in range(-2,3):
for i in range(-2,3):
try:
val=mapData[self.blockpos[1]+j][self.blockpos[0]+i][0]
if val not in uncollidableBlocks:
blockrect=Rect(BLOCKSIZE*(self.blockpos[1]+j),BLOCKSIZE*(self.blockpos[0]+i),BLOCKSIZE,BLOCKSIZE)
if val in platformBlocks:platform=True
else:platform=False
if blockrect.colliderect(int(self.rect.left-1),int(self.rect.top+2),1,int(self.rect.height-4)):self.stopLeft=True#is there a solid block left
if blockrect.colliderect(int(self.rect.right+1),int(self.rect.top+2),1,int(self.rect.height-4)):self.stopRight=True#is there a solid block right
if blockrect.colliderect(self.rect):
deltaX = self.pos[0]-blockrect.centerx
deltaY = self.pos[1]-blockrect.centery
if abs(deltaX) > abs(deltaY):
if deltaX > 0:
if not platform:
self.pos=(blockrect.right+self.rect.width/2,self.pos[1])#move enemy right
self.vel=(0,self.vel[1])#stop enemy horizontally
else:
if not platform:
self.pos=(blockrect.left-self.rect.width/2,self.pos[1])#move enemy left
self.vel=(0,self.vel[1])#stop enemy horizontally
else:
if deltaY > 0:
if self.vel[1]<0:
if not platform:
if Rect(self.rect.left+3,self.rect.top,self.rect.width-6,self.rect.height).colliderect(blockrect):
self.pos=(self.pos[0],blockrect.bottom+self.rect.height/2)#move enemy down
self.vel=(self.vel[0],0)#stop enemy vertically
else:
if self.vel[1]>0:
if Rect(self.rect.left+3,self.rect.top,self.rect.width-6,self.rect.height).colliderect(blockrect):
self.pos=(self.pos[0],blockrect.top-self.rect.height/2+1)#move enemy up
self.vel=(self.vel[0]*0.5,0)#slow down enemy horizontally and stop player vertically
self.grounded=True
self.movingRight=False
self.movingLeft=False
except:None
self.animate()
def damage(self,val,knockBack=0,direction=0,crit=False):
self.movingRight=False
self.movingLeft=False
val-=self.defense
val+=random.randint(-1,1)
if val<1:val=1
if crit:val*=2
self.HP-=val
if self.HP<0:self.HP=0
if knockBack!=0:
valx=knockBack*(1-self.knockBackResist)
valy=-3*(1-self.knockBackResist)
self.vel=(direction*valx,valy)
damageNumber(self.pos,val,crit=crit)
if self.HP>0:#check if the enemy has died from damage
if SFX:
sounds[13].play()
if PARTICLES:
for i in range(int(5*PARTICLEDENSITY)):#blood
Particle((self.pos[0]-clientPlayer.pos[0]+screenW/2,self.pos[1]-clientPlayer.pos[1]+screenH/2),self.bloodColour,life=100,GRAV=0.02,angle=-math.pi/2,spread=math.pi,magnitude=random.random()*2)
else:
self.kill()
def kill(self):
coinRange=enemyData[self.ID][8]
coinDrops=coinsFromVal(random.randint(coinRange[0],coinRange[1]))
for i in range(len(coinDrops)):
if coinDrops[i]>0:
PhysicsItem(self.pos,24-i,amnt=coinDrops[i],pickupDelay=0)
if PARTICLES:
for i in range(int(25*PARTICLEDENSITY)):#more blood
Particle((self.pos[0]-clientPlayer.pos[0]+screenW/2,self.pos[1]-clientPlayer.pos[1]+screenH/2),self.bloodColour,life=100,GRAV=0.02,angle=-math.pi/2,spread=math.pi,magnitude=random.random()*4)
if SFX:
sounds[14].play()#death sound
enemies.remove(self)
def animate(self):
if not self.grounded:
if self.vel[1]>2:
self.animationFrame=2
elif self.vel[1]<-2:
self.animationFrame=1
else:
self.animationFrame=0
else:
self.animationFrame=0
def checkDespawn(self):
if self.pos[0]<clientPlayer.pos[0]-xMaxBlocks*1.5*BLOCKSIZE:enemies.remove(self)
elif self.pos[0]>clientPlayer.pos[0]+xMaxBlocks*1.5*BLOCKSIZE:enemies.remove(self)
elif self.pos[1]<clientPlayer.pos[1]-yMaxBlocks*1.5*BLOCKSIZE:enemies.remove(self)
elif self.pos[1]>clientPlayer.pos[1]+yMaxBlocks*1.5*BLOCKSIZE:enemies.remove(self)
def draw(self):
left=self.rect.left-clientPlayer.pos[0]+screenW/2
top=self.rect.top-clientPlayer.pos[1]+screenH/2
screen.blit(slimeFrames[self.ID*3+self.animationFrame],(left,top))
if self.HP<self.maxHP:
hpFloat=self.HP/self.maxHP
col=(255*(1-hpFloat),255*hpFloat,0)
pygame.draw.rect(screen,darkenCol(col),Rect(left,top+30,self.rect.width,10),0)
pygame.draw.rect(screen,col,Rect(left+2,top+32,(self.rect.width-4)*hpFloat,6),0)
if HITBOXES:
pygame.draw.rect(screen,(255,0,0),Rect(self.rect.left-clientPlayer.pos[0]+screenW/2,self.rect.top-clientPlayer.pos[1]+screenH/2,self.rect.width,self.rect.height),1)
def runAI(self):
if self.type=="Slime":
if self.grounded:
if self.jumpTick<=0:
self.jumpTick=random.randint(40,50)
if clientPlayer.pos[0]<self.pos[0]:
if clientPlayer.alive:
self.vel=(-3,-8.5+random.random())
self.movingLeft=True
else:
self.vel=(3,-8.5+random.random())
self.movingRight=True
else:
if clientPlayer.alive:
self.vel=(3,-8.5+random.random())
self.movingRight=True
else:
self.vel=(-3,-8.5+random.random())
self.movingLeft=True
else:
self.jumpTick-=1
def updateEnemies():
for enemy in enemies:
enemy.update()
def drawEnemies():
for enemy in enemies:
enemy.draw()
def coinsFromVal(val):
coins=[0,0,0,0]
coins[0] = val//1000000
val-=coins[0]*1000000
coins[1] = val//10000
val-=coins[1]*10000
coins[2] = val//100
val-=coins[2]*100
coins[3] = val
return coins
class Player():#stores all info about a player
def __init__(self,pos,model,name="unassigned",HP=100,maxHP=100,hotbar=None,inventory=None,playTime=0,creationDate=None):
self.pos=pos
self.oldpos=pos
self.rect=Rect(self.pos[0]-playerWidth/2,self.pos[1]-playerHeight/2,playerWidth,playerHeight)#hitbox
self.vel=(0,0)
self.model=model#model class
self.name=name
if creationDate==None:
date=datetime.datetime.now()
self.creationDate=str(str(date)[:19])
else:self.creationDate=creationDate
if hotbar==None:self.hotbar=[Item(25),Item(15),Item(19),Item(17),Item(0,amnt=100),Item(1,amnt=100),Item(2,amnt=100),Item(3,amnt=100),Item(4,amnt=100),Item(13,amnt=100)]
else:self.hotbar=hotbar
if inventory==None:self.inventory=[[None for i in range(4)]for j in range(10)]
else:self.inventory=inventory
self.miningTick=0
self.playTime=playTime
sprites=Player.renderSprites(self.model)
self.sprites=sprites[0]
self.armSprites=sprites[1]
self.animationTick=0
self.animationTick2=0
self.armAnimationFrame=0
self.armAnimationTick=0
self.animationFrame=5#which frame to blit
self.alive=True
self.respawnTick=0#how long till respawn
if HP==0:self.HP=maxHP
else:self.HP=HP
self.maxHP=maxHP
hpcol=(255*(1-self.HP/self.maxHP),255*(self.HP/self.maxHP),0)
self.hpText=outlineText(str(self.HP),hpcol,DEFAULTFONT,outlineColour=(hpcol[0]*0.5,hpcol[1]*0.5,hpcol[2]*0.5))
self.hpXpos=screenW-10-self.HP-self.hpText.get_width()/2
self.grounded=True
self.movingLeft=False
self.movingRight=False
self.movingDown=False#is player walking
self.direction=1
self.armSwing=False
self.stopLeft=False#stop movement left
self.stopRight=False#stop movement right
self.animationSpeed=1
self.movingDownTick=0
self.stopMovingDown=False
self.lastBlockOn=0
self.knockBackResist=0
self.defense=0
self.hotbarIndex=0
self.useTick=0
self.canUse=False
self.armHold=False
self.holdAngle=0
self.currentItemImage=None
self.swingAngle=0
self.itemSwing=False
self.enemiesHit=[]
self.unPickupableItems=[]
self.hotbarImage=pygame.Surface((480,48));self.hotbarImage.set_colorkey((255,0,255))
self.inventoryImage=pygame.Surface((480,192));self.inventoryImage.set_colorkey((255,0,255))
self.inventoryOpen=False
self.oldInventoryPositions=[]
self.posDiff=(0,0)
def update(self):
if self.alive:
if self.movingLeft:#moves player left
if not self.stopLeft:
if self.movingDown:self.vel=(-1,self.vel[1])
else:self.vel=(-3,self.vel[1])
if self.movingRight:#moves player right
if not self.stopRight:
if self.movingDown:self.vel=(1,self.vel[1])
else:self.vel=(3,self.vel[1])
oldpos=tuple(self.pos)#used to work out how much parallax and particles need to move
self.vel=(self.vel[0]*0.9,self.vel[1]*0.99+GRAVITY)
self.pos=(self.pos[0]+self.vel[0],self.pos[1]+self.vel[1])
self.rect.left=self.pos[0]-playerWidth/2#updating rect
self.rect.top=self.pos[1]-playerHeight/2
self.blockpos=(math.floor(self.pos[1]//BLOCKSIZE),math.floor(self.pos[0]//BLOCKSIZE))
self.grounded=False
self.stopLeft=False
self.stopRight=False
fallDamaged=False#so fall damage is only applied once
if not self.canUse:
if self.useTick<=0:
self.armHold=False
self.canUse=True
else:self.useTick-=1
if self.vel[0]<0:
if self.pos[0]<WORLDBOARDER_WEST:self.pos=(int(WORLDBOARDER_WEST),self.pos[1])
elif self.vel[0]>0:
if self.pos[0]>WORLDBOARDER_EAST:self.pos=(int(WORLDBOARDER_EAST),self.pos[1])
if self.vel[1]<0:
if self.pos[1]<WORLDBOARDER_NORTH:
self.pos=(self.pos[0],int(WORLDBOARDER_NORTH))
self.vel=(self.vel[0],0)
elif self.vel[1]>0:
if self.pos[1]>WORLDBOARDER_SOUTH:
self.pos=(self.pos[0],int(WORLDBOARDER_SOUTH))
self.vel=(self.vel[0],0)
self.grounded=True
if self.miningTick<=0:
if pygame.mouse.get_pressed()[0] or pygame.mouse.get_pressed()[2]:
use=False
if clientPlayer.inventoryOpen:
if not Rect(5,5,480,244).collidepoint(m) and not Rect(screenW-50,screenH-20,50,20).collidepoint(m):
use=True
else:use=True
if use and clientPrompt==None and waitToUse==False:
if pygame.mouse.get_pressed()[0]:
self.useItem()
elif pygame.mouse.get_pressed()[2]:
self.useItem(alt=True)
else:
self.miningTick-=1
for j in range(-2,3):
for i in range(-2,3):
try:
val=mapData[self.blockpos[1]+j][self.blockpos[0]+i][0]
if val not in uncollidableBlocks:
blockrect=Rect(BLOCKSIZE*(self.blockpos[1]+j),BLOCKSIZE*(self.blockpos[0]+i),BLOCKSIZE,BLOCKSIZE)
if val in platformBlocks:platform=True
else:
platform=False
if blockrect.colliderect(int(self.rect.left-1),int(self.rect.top+2),1,int(self.rect.height-4)):self.stopLeft=True#is there a solid block left
if blockrect.colliderect(int(self.rect.right+1),int(self.rect.top+2),1,int(self.rect.height-4)):self.stopRight=True#is there a solid block right
if blockrect.colliderect(self.rect):
deltaX = self.pos[0]-blockrect.centerx
deltaY = self.pos[1]-blockrect.centery
if abs(deltaX) > abs(deltaY):
if deltaX > 0:
if not platform:
self.pos=(blockrect.right+playerWidth/2,self.pos[1])#move player right
self.vel=(0,self.vel[1])#stop player horizontally
else:
if not platform:
self.pos=(blockrect.left-playerWidth/2,self.pos[1])#move player left
self.vel=(0,self.vel[1])#stop player horizontally
else:
if deltaY > 0:
if self.vel[1]<0:
if not platform:
if Rect(self.rect.left+3,self.rect.top,self.rect.width-6,self.rect.height).colliderect(blockrect):
self.pos=(self.pos[0],blockrect.bottom+playerHeight/2)#move player down
self.vel=(self.vel[0],0)#stop player vertically
else:
if self.vel[1]>0:
if Rect(self.rect.left+3,self.rect.top,self.rect.width-6,self.rect.height).colliderect(blockrect):
if platform:
if self.movingDown:
collide=False
else:
if self.vel[1]<5:
if self.pos[1]+BLOCKSIZE<blockrect.top:
collide=True
else:
collide=True
else:collide=True
if collide:
if not fallDamaged:
if self.vel[1]>8:
damage=int(((self.vel[1]-7.5)*5)**1.5)#work out fall damage
self.damage(damage,["falling","World"])#apply fall damage once
fallDamaged=True
self.lastBlockOn=int(val)
self.movingDownTick=-1
self.pos=(self.pos[0],blockrect.top-playerHeight/2+1)#move player up
self.vel=(self.vel[0]*0.5,0)#slow down player horizontally and stop player vertically
self.grounded=True
except:None
if self.stopMovingDown:#wait before setting movingdown to false based on player y vel
if self.movingDownTick<0:
self.movingDown=False
self.stopMovingDown=False
else:
self.movingDownTick-=self.vel[1]
if self.alive:
self.posDiff=(self.pos[0]-oldpos[0],self.pos[1]-oldpos[1])
moveParallax((-self.posDiff[0]*PARALLAXAMNT,-self.posDiff[1]*PARALLAXAMNT))#move parallax based on vel
else:# if player is not alive, wait to respawn
if self.respawnTick>0:
self.respawnTick-=1
else:
self.respawn()
self.updateInventoryOldSlots()
def damage(self,val,source,knockBack=0,direction=None):
if not CREATIVE and self.alive:
val-=self.defense
val+=random.randint(-1,1)
if val<1:
val=1
self.HP-=val
damageNumber(self.pos,val,colour=(240,20,20))
if self.HP<0:self.HP=0
if knockBack!=0:
valx=knockBack*(1-self.knockBackResist)
valy=-3*(1-self.knockBackResist)
self.vel=(direction*valx,valy)
if self.HP>0:#check if the player has died from damage
if SFX:
sounds[random.randint(7,9)].play()#random hurt sound
if PARTICLES:
for i in range(int(5*PARTICLEDENSITY)):#blood
Particle((screenW/2,screenH/2),(230,0,0),life=100,GRAV=0.02,angle=-math.pi/2,spread=math.pi,magnitude=random.random()*2)
else:
self.kill(source)
hpFloat=self.HP/self.maxHP
self.hpText=outlineText(str(self.HP),((1-hpFloat)*255,hpFloat*255,0),DEFAULTFONT,outlineColour=((1-hpFloat)*180,hpFloat*180,0))
self.hpXpos=screenW-10-hpFloat*100-self.hpText.get_width()/2
def kill(self,source):
if self.alive:
message(getDeathMessage(clientPlayer.name,source),(255,255,255))
self.respawnTick=500#respawn delay
self.alive=False
self.vel=(0,0)
self.posDiff=(0,0)
if PARTICLES:
for i in range(int(25*PARTICLEDENSITY)):#more blood
Particle((screenW/2,screenH/2),(230,0,0),life=100,GRAV=0.02,angle=-math.pi/2,spread=math.pi,magnitude=random.random()*4)
if SFX:
sounds[11].play()#death sound
def respawn(self):
self.pos=tuple(clientWorld.spawnPoint)#set pos to clientWorld.spawnPoint
self.vel=(0,0)
self.alive=True
self.HP=int(self.maxHP)#reset hp
self.hpText=outlineText(str(self.HP),(0,255,0),DEFAULTFONT,outlineColour=(0,180,0))
self.hpXpos=screenW-10-100-self.hpText.get_width()/2
def renderCurrentItemImage(self):
item=self.hotbar[self.hotbarIndex]
if item!=None:
self.currentItemImage=itemImages[item.ID].copy()
scale=item.size/itemData[item.ID][4][3]
self.currentItemImage=pygame.transform.scale(self.currentItemImage,(int(self.currentItemImage.get_width()*scale),int(self.currentItemImage.get_height()*scale)))
def animate(self):
if self.animationTick<=0:#happens every 'animationSpeed' frames
self.animationTick=self.animationSpeed
if self.grounded:
if self.movingLeft:#if moving left, cycle through left frames
if self.animationFrame<29:
self.animationFrame+=1
else:
self.animationFrame=17
return
elif self.movingRight:#if moving right, cycle through right frames
if self.animationFrame<14:
self.animationFrame+=1
else:
self.animationFrame=2
return
else:#if idle put arms down
if self.direction==0:
self.animationFrame=15
elif self.direction==1:
self.animationFrame=0
else:#puts arms in the air if not grounded
if self.direction==0:
self.animationFrame=16
elif self.direction==1:
self.animationFrame=1
else:
self.animationTick-=1
if self.armHold:
if self.direction==1:
self.armAnimationFrame=19
else:
self.armAnimationFrame=39
elif self.armSwing:
if self.armAnimationTick<=0:
self.armAnimationTick=2
if self.direction==1:
if self.armAnimationFrame<3:
self.armAnimationFrame+=1
else:
if self.movingRight:self.armAnimationFrame=6
else:self.armAnimationFrame=26
self.armSwing=False
else:
if self.armAnimationFrame<23 and self.armAnimationFrame>=20:
self.armAnimationFrame+=1
else:
if self.movingRight:self.armAnimationFrame=6
else:self.armAnimationFrame=26
self.armSwing=False
else:
self.armAnimationTick-=0.75
else:
if self.grounded:
if self.movingLeft:#if moving left, cycle through left frames
if self.armAnimationFrame<38:
self.armAnimationFrame+=1
else:
self.armAnimationFrame=26
return
elif self.movingRight:#if moving right, cycle through right frames
if self.armAnimationFrame<18:
self.armAnimationFrame+=1
else:
self.armAnimationFrame=6
return
else:#if idle put arms down
if self.direction==0:
self.armAnimationFrame=20
elif self.direction==1:
self.armAnimationFrame=0
else:#puts arms in the air if not grounded
if self.direction==0:
self.armAnimationFrame=25
elif self.direction==1:
self.armAnimationFrame=5
def renderSprites(model,directions=2,armFrameCount=20,torsoFrameCount=15):#create an array of surfs for the current character used for animation/blitting
sprites=[]
armSprites=[]
for j in range(directions):#for both directions
hair=colourSurface(hairStyles[model.hairID],model.hairCol)
if j==1:#flip if necessary
hair=pygame.transform.flip(hair,True,False)
torso=colourSurface(torsoFrames[0],model.shirtCol)
if j==0:#flip if necessary
torso=pygame.transform.flip(torso,True,False)
head=colourSurface(hairStyles[9],model.skinCol)
pygame.draw.rect(head,(255,254,255),Rect(20,22,4,4),0)
pygame.draw.rect(head,model.eyeCol,Rect(22,22,2,4),0)
if j==1:#flip if necessary
head=pygame.transform.flip(head,True,False)
for i in range(torsoFrameCount):#all animation frames for one direction
bodySurf=pygame.Surface((44,75))
bodySurf.fill((255,0,255))
bodySurf.set_colorkey((255,0,255))#create the surf for the whole player with a colourkey of (255,0,255)
trousers=colourSurface(torsoFrames[i+1],model.trouserCol)
if j==0:#flip if necessary
trousers=pygame.transform.flip(trousers,True,False)
shoes=colourSurface(torsoFrames[i+16],model.shoeCol)
if j==0:#flip if necessary
shoes=pygame.transform.flip(shoes,True,False)
bodySurf.blit(torso,(0,4))
bodySurf.blit(trousers,(0,4))
bodySurf.blit(shoes,(0,4))
bodySurf.blit(head,(0,0))
bodySurf.blit(hair,(0,0))
sprites.append(bodySurf)
for i in range(armFrameCount):#all animation frames for one direction
armSurf=pygame.Surface((44,75))
armSurf.fill((255,0,255))
armSurf.set_colorkey((255,0,255))
arms=colourSurface(torsoFrames[i+31],model.underShirtCol)
if j==0:#flip if necessary
arms=pygame.transform.flip(arms,True,False)
hands=colourSurface(torsoFrames[i+51],model.skinCol)
if j==0:#flip if necessary
hands=pygame.transform.flip(hands,True,False)
armSurf.blit(arms,(0,4))
armSurf.blit(hands,(0,4))
armSprites.append(armSurf)
return [sprites,armSprites]
def useItem(self,alt=False):
global mapData
swing=False
item=self.hotbar[self.hotbarIndex]
if item!=None:
if "block" in item.tags:
if math.sqrt((screenW/2-m[0])**2+(screenH/2-m[1])**2)<BLOCKSIZE*6 or CREATIVE:
blockpos=(int((m[0]+clientPlayer.pos[0]-screenW/2)//BLOCKSIZE),int((m[1]+clientPlayer.pos[1]-screenH/2)//BLOCKSIZE))
if onScreen(blockpos[0],blockpos[1]):
blockrect=Rect(BLOCKSIZE*blockpos[0],BLOCKSIZE*blockpos[1]+1,BLOCKSIZE,BLOCKSIZE)
if not blockrect.colliderect(clientPlayer.rect):
if shift:
if mapData[blockpos[0]][blockpos[1]][1]==-1:
if getNeighborCount(blockpos[0],blockpos[1],tile=1)>0:
if not CREATIVE:
self.hotbar[self.hotbarIndex].amnt-=1
dat=["H",self.hotbarIndex]
if dat not in self.oldInventoryPositions:
self.oldInventoryPositions.append(dat)
if self.hotbar[self.hotbarIndex].amnt<=0:
self.hotbar[self.hotbarIndex]=None
mapData[blockpos[0]][blockpos[1]][1]=item.ID
updateSurface(blockpos[0],blockpos[1])
if SFX:
playHitSfx(item.ID)
swing=True
else:
if mapData[blockpos[0]][blockpos[1]][0]==-1:
if getNeighborCount(blockpos[0],blockpos[1])>0:
if not CREATIVE:
self.hotbar[self.hotbarIndex].amnt-=1
dat=["H",self.hotbarIndex]
if dat not in self.oldInventoryPositions:
self.oldInventoryPositions.append(dat)
if self.hotbar[self.hotbarIndex].amnt<=0:
self.hotbar[self.hotbarIndex]=None
mapData[blockpos[0]][blockpos[1]][0]=item.ID
updateSurface(blockpos[0],blockpos[1])
if SFX:
playHitSfx(item.ID)
swing=True
elif "pickaxe" in item.tags:
if self.canUse or CREATIVE:
self.enemiesHit=[]
self.canUse=False
self.useTick=int(item.attackSpeed)
swing=True
self.itemSwing=True
if self.direction==1:self.swingAngle=10
else:self.swingAngle=65
if SFX:
sounds[15].play()
if math.sqrt((screenW/2-m[0])**2+(screenH/2-m[1])**2)<BLOCKSIZE*6 or CREATIVE:
if shift:datIndex=1#wall or block being clicked
else:datIndex=0
blockpos=(int((m[0]+clientPlayer.pos[0]-screenW/2)//BLOCKSIZE),int((m[1]+clientPlayer.pos[1]-screenH/2)//BLOCKSIZE))
if onScreen(blockpos[0],blockpos[1]):
tile=mapData[blockpos[0]][blockpos[1]][datIndex]
if tile!=-1:
if tile==5:
mapData[blockpos[0]][blockpos[1]][datIndex]=0
else:
mapData[blockpos[0]][blockpos[1]][datIndex]=-1
PhysicsItem(((blockpos[0]+0.5)*BLOCKSIZE,(blockpos[1]+0.5)*BLOCKSIZE),tile,pickupDelay=0)
updateSurface(blockpos[0],blockpos[1])
if tile in platformBlocks:
colour=pygame.transform.average_color(tileImages[tile],Rect(BLOCKSIZE/8,BLOCKSIZE/8,BLOCKSIZE*3/4,BLOCKSIZE/4))
else:colour=pygame.transform.average_color(tileImages[tile])
if SFX:
playHitSfx(tile)
if PARTICLES:
for i in range(int(random.randint(2,3)*PARTICLEDENSITY)):
Particle(m,colour,size=10,life=100,angle=-math.pi/2,spread=math.pi,GRAV=0.05)
elif "melee" in item.tags:
if self.canUse:
self.enemiesHit=[]
self.canUse=False
self.useTick=int(item.attackSpeed)
if SFX:
sounds[15].play()
swing=True
self.itemSwing=True
if self.direction==1:self.swingAngle=10
else:self.swingAngle=65
if swing:
if not self.armSwing:
self.armSwing=True
if self.direction==1:
self.armAnimationFrame=1
else:
self.armAnimationFrame=20
elif "ranged" in item.tags:
if self.canUse:
if m[0]<screenW/2:
self.direction=0
else:self.direction=1
self.canUse=False
self.armHold=True
self.holdAngle=math.atan2(-m[1]+screenH/2,abs(m[0]-screenW/2))
self.useTick=int(item.attackSpeed)
angle=math.atan2(m[1]-screenH/2,m[0]-screenW/2)
pos=(clientPlayer.pos[0],clientPlayer.pos[1])
weaponDamage=item.attackDamage
weaponKnockback=item.knockback
weaponVel=item.velocity
if "gun" in item.tags:
ammoID=1
elif "bow" in item.tags:
ammoID=0
source=self.name
crit=False
if random.random()<item.critStrikeChance:
crit=True
fireProjectile(pos,angle,weaponDamage,weaponKnockback,weaponVel,ammoID,source,crit)
def giveItem(self,ID,amnt=1,pos=None,unique=False,item=None):
if pos==None:
coin=False
if ID>=21 and ID<=23:
coin=True
searchData=self.findExistingItemStacks(ID)#find all suitable slots
while len(searchData[0])>0 and amnt>0:#fill all stacks of the same item first
fillCount=searchData[0][0][2]#work out how many to add to the stack
amnt-=fillCount
if amnt<0:
fillCount+=amnt
if searchData[0][0][0]=="H":#if item in hotbar increase the amnt by the calculated fillcount
self.hotbar[searchData[0][0][1]].amnt+=fillCount
if coin:
if self.hotbar[searchData[0][0][1]].amnt==itemData[ID][4][10]:
if amnt>0:
self.hotbar[searchData[0][0][1]].amnt=amnt
else:
self.hotbar[searchData[0][0][1]]=None
self.giveItem(ID+1)
amnt=0
elif searchData[0][0][0]=="I":#if item in inventory increase the amnt by the calculated fillcount
self.inventory[searchData[0][0][1][0]][searchData[0][0][1][1]].amnt+=fillCount
if coin:
if self.inventory[searchData[0][0][1][0]][searchData[0][0][1][1]].amnt==itemData[ID][4][10]:
if amnt>0:
self.inventory[searchData[0][0][1][0]][searchData[0][0][1][1]].amnt=amnt
else:
self.inventory[searchData[0][0][1][0]][searchData[0][0][1][1]]=None
self.giveItem(ID+1)
amnt=0
dat=[searchData[0][0][0],searchData[0][0][1]]
if dat not in self.oldInventoryPositions:
self.oldInventoryPositions.append(dat)#flag the position for a surface update
searchData[0].remove(searchData[0][0])#remove the used data
while len(searchData[1])>0 and amnt>0:#no stacks left to fill so fill empty slots
fillCount=searchData[1][0][2]#work out how many to add to the stack
amnt-=fillCount
if amnt<0:
fillCount+=amnt
if searchData[1][0][0]=="H":#if item in hotbar increase the amnt by the calculated fillcount
self.hotbar[searchData[1][0][1]]=Item(ID,amnt=fillCount)
elif searchData[1][0][0]=="I":#if item in inventory increase the amnt by the calculated fillcount
self.inventory[searchData[1][0][1][0]][searchData[1][0][1][1]]=Item(ID,amnt=fillCount,prefixData=prefixData)
dat=[searchData[1][0][0],searchData[1][0][1]]
if dat not in self.oldInventoryPositions:
self.oldInventoryPositions.append(dat)#flag the position for a surface update
searchData[1].remove(searchData[1][0])#remove the used data
if amnt<=0:
return [True]
else:
if ID not in self.unPickupableItems:
self.unPickupableItems.append(ID)
return [False,amnt]
else:
if pos[0]=="H":
if self.hotbar[pos[1]]==None:
if unique:
self.hotbar[pos[1]]=item
else:
self.hotbar[pos[1]]=Item(ID,amnt=amnt)
return [0]
elif self.hotbar[pos[1]].ID==ID:
if not unique:
self.hotbar[pos[1]].amnt+=amnt
if self.hotbar[pos[1]].amnt>itemData[ID][4][10]:
amnt=self.hotbar[pos[1]].amnt-itemData[ID][4][10]
self.hotbar[pos[1]].amnt=itemData[ID][4][10]
return [1,amnt]
else:
item=self.hotbar[pos[1]]
return [2,item,"H"]
elif self.hotbar[pos[1]].ID!=ID:
item=self.hotbar[pos[1]]
return [2,item,"H"]
elif pos[0]=="I":
if self.inventory[pos[1][0]][pos[1][1]]==None:
if unique:
self.inventory[pos[1][0]][pos[1][1]]=item
else:
self.inventory[pos[1][0]][pos[1][1]]=Item(ID,amnt=amnt,prefixData=prefixData)
return [0]
elif self.inventory[pos[1][0]][pos[1][1]].ID==ID:
if not unique:
self.inventory[pos[1][0]][pos[1][1]].amnt+=amnt
if self.inventory[pos[1][0]][pos[1][1]].amnt>itemData[ID][4][10]:
amnt=self.inventory[pos[1][0]][pos[1][1]].amnt-itemData[ID][4][10]
self.inventory[pos[1][0]][pos[1][1]].amnt=itemData[ID][4][10]
return [1,amnt]
else:
item=self.inventory[pos[1][0]][pos[1][1]]
return [2,item,"I"]
elif self.inventory[pos[1][0]][pos[1][1]].ID!=ID:
item=self.inventory[pos[1][0]][pos[1][1]]
return [2,item,"I"]
def removeItem(self,pos):
if pos[0]=="H":
item=self.hotbar[pos[1]]
self.hotbar[pos[1]]=None
elif pos[0]=="I":
item=self.inventory[pos[1][0]][pos[1][1]]
self.inventory[pos[1][0]][pos[1][1]]=None
if pos not in self.oldInventoryPositions:
self.oldInventoryPositions.append(pos)
return item
def findExistingItemStacks(self,ID,searchHotbar=True,searchInventory=True):#finds existing item stacks and free spaces
#[which array,position in array,amount]
existingSpaces=[]
freeSpaces=[]
if searchHotbar:#search hotbar
for i in range(10):
if self.hotbar[i]==None:
freeSpaces.append(["H",i,int(itemData[ID][4][10])])
elif self.hotbar[i].ID==ID:
available=int(itemData[ID][4][10])-self.hotbar[i].amnt
if available>0:
existingSpaces.append(["H",i,available])
if searchInventory:#search inventory
for j in range(4):
for i in range(10):
if self.inventory[i][j]==None:
freeSpaces.append(["I",(i,j),int(itemData[ID][4][10])])
elif self.inventory[i][j].ID==ID:
available=int(itemData[ID][4][10])-self.inventory[i][j].amnt
if available>0:
existingSpaces.append(["I",(i,j),available])
return [existingSpaces,freeSpaces]
def renderHotbar(self,full=False):
self.hotbarImage.fill((255,0,255))
for i in range(10):
self.hotbarImage.blit(miscGuiImages[0],(48*i,0))
if clientPlayer.hotbar[i]!=None:
self.hotbarImage.blit(itemImages[self.hotbar[i].ID],(48*i,0))
if self.hotbar[i].amnt>1:
self.hotbarImage.blit(outlineText(str(self.hotbar[i].amnt),(255,255,255),SMALLFONT),(24+48*i,30))
def updateInventoryOldSlots(self):
for data in self.oldInventoryPositions:
if data[0]=="H":
item=self.hotbar[data[1]]
self.hotbarImage.blit(miscGuiImages[0],(data[1]*48,0))
if item!=None:
self.hotbarImage.blit(itemImages[item.ID],(48*data[1],0))
if item.amnt>1:
self.hotbarImage.blit(outlineText(str(item.amnt),(255,255,255),SMALLFONT),(24+48*data[1],30))
if data[0]=="I":
item=self.inventory[data[1][0]][data[1][1]]