-
Notifications
You must be signed in to change notification settings - Fork 0
/
wuseltest002.py
943 lines (802 loc) · 37.4 KB
/
wuseltest002.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
"""
Author: Horst JENS
Email: [email protected]
Github: github.com/spheppner
Version: 002
# DONE:Leute sollen von start-tile ausgehend herumwuseln, immer wieder zurück kommen, eventuell pärchen bilden und häuser bauen
TODO: schlafenszeit 8 h / tag, leute sollen zu hause sein.
"""
import pygame
import random
import os
class VectorSprite(pygame.sprite.Sprite):
"""base class for sprites. this class inherits from pygames sprite class"""
number = 0
numbers = {} # { number, Sprite }
def __init__(self, **kwargs):
self._default_parameters(**kwargs)
self._overwrite_parameters()
pygame.sprite.Sprite.__init__(self, self.groups) # call parent class. NEVER FORGET !
self.number = VectorSprite.number # unique number for each sprite
VectorSprite.number += 1
VectorSprite.numbers[self.number] = self
self.create_image()
self.distance_traveled = 0 # in pixel
self.rect.center = (-300, -300) # avoid blinking image in topleft corner
if self.angle != 0:
self.set_angle(self.angle)
# self.visible = False # will be changed to True when age becomes positive
def _overwrite_parameters(self):
"""change parameters before create_image is called"""
pass
def _default_parameters(self, **kwargs):
"""get unlimited named arguments and turn them into attributes
default values for missing keywords"""
for key, arg in kwargs.items():
setattr(self, key, arg)
if "layer" not in kwargs:
self._layer = 4
else:
self._layer = self.layer
# if "static" not in kwargs:
# self.static = False
if "pos" not in kwargs:
self.pos = pygame.math.Vector2(x=150, y=150)
if "move" not in kwargs:
self.move = pygame.math.Vector2(x=0, y=0)
# if "acceleration" not in kwargs:
# self.acc = 0.0 # pixel/second speed is constant
# TODO: acc, gravity-vector
if "radius" not in kwargs:
self.radius = 5
if "width" not in kwargs:
self.width = self.radius * 2
if "height" not in kwargs:
self.height = self.radius * 2
if "color" not in kwargs:
# self.color = None
self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
# if "hitpoints" not in kwargs:
# self.hitpoints = 100
# self.hitpointsfull = self.hitpoints # makes a copy
# if "mass" not in kwargs:
# self.mass = 10
# if "damage" not in kwargs:
# self.damage = 10
if "bounce_on_edge" not in kwargs:
self.bounce_on_edge = False
if "kill_on_edge" not in kwargs:
self.kill_on_edge = False
if "warp_on_edge" not in kwargs:
self.warp_on_edge = False
if "angle" not in kwargs:
self.angle = 0 # facing right?
if "max_age" not in kwargs:
self.max_age = None
if "max_distance" not in kwargs:
self.max_distance = None
if "picture" not in kwargs:
self.picture = None
if "bossnumber" not in kwargs:
self.bossnumber = None
if "kill_with_boss" not in kwargs:
self.kill_with_boss = False
if "sticky_with_boss" not in kwargs:
self.sticky_with_boss = False
if "speed" not in kwargs:
self.speed = None
if "age" not in kwargs:
self.age = 0 # age in seconds
if "visible" not in kwargs:
self.visible = False # becomes True when self.age becomes >= 0
def kill(self):
if self.number in self.numbers:
del VectorSprite.numbers[self.number] # remove Sprite from numbers dict
pygame.sprite.Sprite.kill(self)
def create_image(self):
if self.picture is not None:
self.image = self.picture.copy()
else:
self.image = pygame.Surface((self.width, self.height))
self.image.fill((self.color))
# self.image = self.image.convert_alpha()
self.image0 = self.image.copy()
self.rect = self.image.get_rect()
self.width = self.rect.width
self.height = self.rect.height
def rotate(self, by_degree):
"""rotates a sprite and changes it's angle by by_degree"""
self.angle += by_degree
oldcenter = self.rect.center
self.image = pygame.transform.rotate(self.image0, self.angle)
self.image.convert_alpha()
self.rect = self.image.get_rect()
self.rect.center = oldcenter
def set_angle(self, degree):
"""rotates a sprite and changes it's angle to degree"""
self.angle = degree
oldcenter = self.rect.center
self.image = pygame.transform.rotate(self.image0, self.angle)
self.image.convert_alpha()
self.rect = self.image.get_rect()
self.rect.center = oldcenter
def update(self, seconds):
"""calculate movement, position and bouncing on edge"""
# position and move are pygame.math.Vector2 objects
# ----- kill because... ------
# if self.hitpoints <= 0:
# self.kill()
# TODO: pygame.DirtySprite verwenden, mit dirty und visible flag
if self.age < 0:
self.visible = False
else:
self.visible = True
if self.max_age is not None and self.age > self.max_age:
self.kill()
if self.max_distance is not None and self.distance_traveled > self.max_distance:
self.kill()
# ---- movement with/without boss ----
if self.bossnumber is not None:
if self.kill_with_boss:
if self.bossnumber not in VectorSprite.numbers:
self.kill()
if self.sticky_with_boss:
boss = VectorSprite.numbers[self.bossnumber]
# self.pos = v.Vec2d(boss.pos.x, boss.pos.y)
self.pos = pygame.math.Vector2(x=boss.pos.x, y=boss.pos.y)
if self.age > 0:
self.pos += self.move * seconds
self.distance_traveled += self.move.length() * seconds
self.age += seconds
if not self.visible:
self.rect.center = (-200, -200)
else:
self.wallbounce()
self.rect.center = (round(self.pos.x, 0), round(self.pos.y, 0))
def wallbounce(self):
# ---- bounce / kill on screen edge ----
# ------- left edge ----
if self.pos.x < 0:
if self.kill_on_edge:
self.kill()
elif self.bounce_on_edge:
self.pos.x = 0
self.move.x *= -1
elif self.warp_on_edge:
self.pos.x = Viewer.width
# -------- upper edge -----
if self.pos.y < 0:
if self.kill_on_edge:
self.kill()
elif self.bounce_on_edge:
self.pos.y = 0
self.move.y *= -1
elif self.warp_on_edge:
self.pos.y = Viewer.height
# -------- right edge -----
if self.pos.x > Viewer.width:
if self.kill_on_edge:
self.kill()
elif self.bounce_on_edge:
self.pos.x = Viewer.width
self.move.x *= -1
elif self.warp_on_edge:
self.pos.x = 0
# --------- lower edge ------------
if self.pos.y > Viewer.height:
if self.kill_on_edge:
self.hitpoints = 0
self.kill()
elif self.bounce_on_edge:
self.pos.y = Viewer.height
self.move.y *= -1
elif self.warp_on_edge:
self.pos.y = 0
class FragmentSprite(VectorSprite):
def _overwrite_parameters(self):
super()._overwrite_parameters()
self.alpha = 255
self.delta_alpha = 255 / self.max_age if self.max_age > 0 else 1
def update(self, seconds):
super().update(seconds)
# 0 = full transparency, 255 = no transparency at all
self.alpha -= self.delta_alpha * seconds * 0.4 # slowly become more transparent
self.image.set_alpha(self.alpha)
self.image.convert_alpha()
class Flytext(VectorSprite):
def __init__(self, text, fontsize=22, acceleration_factor=1.02, max_speed=300, **kwargs):
"""a text flying upward and for a short time and disappearing"""
VectorSprite.__init__(self, **kwargs)
##self._layer = 7 # order of sprite layers (before / behind other sprites)
##pygame.sprite.Sprite.__init__(self, self.groups) # THIS LINE IS IMPORTANT !!
self.text = text
self.acceleartion_factor = acceleration_factor
self.max_speed = max_speed
self.kill_on_edge = True
self.image = make_text(self.text, self.color, fontsize)[0] # font 22
self.rect = self.image.get_rect()
def update(self, seconds):
self.move *= self.acceleartion_factor
if self.move.length() > self.max_speed:
self.move.normalize_ip()
self.move *= self.max_speed
VectorSprite.update(self, seconds)
class FlyingObject(VectorSprite):
image = None # will be set from Viewer.create_tiles
def _overwrite_parameters(self):
self.move = pygame.math.Vector2(x=self.endpos[0] - self.startpos[0], y=self.endpos[1] - self.startpos[1])
self.picture = self.image
self.create_image()
distance = self.max_distance = self.move.length()
if distance > 0:
self.move.normalize_ip() # reduce to lenght 1
else:
self.max_age = 0 # kill this arrow as soon as possible
self.move *= self.speed #
self.duration = distance / self.speed # in seconds
# arrow shall start in the middle of tile, not in the topleft corner
self.pos = pygame.math.Vector2(self.startpos[0], self.startpos[1])
self.set_angle(self.move.angle_to(pygame.math.Vector2(1, 0)))
def tile_to_pixel(pos, center=False):
"""get a tile coordinate (x,y) and returns pixel (x,y) of tile
if center is True, returns the center of the tile,
if center is False, returns the upper left corner of the tile"""
x, y = pos
# x2 = Viewer.pcx + (x - Game.player.x) * Viewer.grid_size[0]
# y2 = Viewer.pcy + (y - Game.player.y) * Viewer.grid_size[1]
x2 = Viewer.pcx + (x + 1000) * Viewer.grid_size[0]
y2 = Viewer.pcy + (y + 1000) * Viewer.grid_size[1]
if center:
x2 += Viewer.grid_size[0] // 2
y2 += Viewer.grid_size[1] // 2
# print(x2, y2)
return (x2, y2)
def pixel_to_tile(pixelcoordinate):
"""transform pixelcoordinate (x,y, from pygame mouse) into grid tile coordinate."""
# returns distance to player tile in tiles (relative coordinates)"""
x, y = pixelcoordinate
# return (x - self.pcx) // Viewer.grid_size[0], (y - self.pcy) // Viewer.grid_size[1]
return x // Viewer.grid_size[0], y // Viewer.grid_size[1]
def megaroll(dicestring="1d6 1d20", bonus=0):
"""roll all the dice in the dicestring and adds a bonus to the sum
1d6 means one 6-sided die without re-roll
1D6 means one 6-sided die with re-roll.
re-roll: 1D6 means that when hightest side (6) is rolled, 5 (=6-1) is added and he rolls again"""
dlist = dicestring.split(" ")
total = 0
# print("calculating: ", dicestring, "+", bonus)
for code in dlist:
# print("---processing", code)
if "d" in code:
# reroll = False
rolls = int(code.split("d")[0])
sides = int(code.split("d")[1])
total += roll((rolls, sides), bonus=0, reroll=False)
elif "D" in code:
# reroll = True
rolls = int(code.split("D")[0])
sides = int(code.split("D")[1])
total += roll((rolls, sides), bonus=0, reroll=True)
else:
raise SystemError("unknow dice type: {} use 1d6, 1D20 etc".format(code))
# print("---result of", code, "is :", str(total))
# print("adding " + str(bonus) + "=", str(total + bonus))
return total + bonus
def roll(dice, bonus=0, reroll=True):
"""simulate a dice throw, and adding a bonus
reroll means that if the highest number is rolled,
one is substracted from the score and
another roll is added, until a not-hightest number is rolled.
e.g. 1D6 throws a 6, and re-rolls a 2 -> (6-1)+2= 7"""
# TODO format-micro-language for aligning the numbers better
# TODO: accepting string of several dice, like '2D6 3d4' where 'd' means no re-roll, 'D' means re-roll
rolls = dice[0]
sides = dice[1]
total = 0
# print("------------------------")
# print("rolling {}{}{} + bonus {}".format(rolls, "D" if reroll else "d", sides, bonus))
# print("------------------------")
i = 0
verb = "rolls "
# for d in range(rolls):
while True:
i += 1
if i > rolls:
break
value = random.randint(1, sides)
if reroll and value == sides:
total += value - 1
# print("die #{} {} {} ∑: {} (count as {} and rolls again)".format(i, verb, value, total, value - 1))
verb = "re-rolls"
i -= 1
continue
else:
total += value
# print("die #{} {} {} ∑: {}".format(i, verb, value, total))
verb = "rolls "
# print("=========================")
# print("=result: {}".format(total))
# print("+bonus: {}".format(bonus))
# print("=========================")
# print("=total: {}".format(total + bonus))
return total + bonus
def minmax(value, lower_limit=-1, upper_limit=1):
"""constrains a value inside two limits"""
value = max(lower_limit, value)
value = min(upper_limit, value)
return value
def randomizer(list_of_chances=(1.0,)):
"""gives back an integer depending on chance.
e.g. randomizer((.75, 0.15, 0.05, 0.05)) gives in 75% 0, in 15% 1, and in 5% 2 or 3"""
total = sum(list_of_chances)
v = random.random() * total # a value between 0 and total
edge = 0
for i, c in enumerate(list_of_chances):
edge += c
if v <= edge:
return i
else:
raise SystemError("problem with list of chances:", list_of_chances)
def make_text(text="@", font_color=(255, 0, 255), font_size=48, font_name="mono", bold=True, grid_size=None):
"""returns pygame surface with text and x, y dimensions in pixel
grid_size must be None or a tuple with positive integers.
Use grid_size to scale the text to your desired dimension or None to just render it
You still need to blit the surface.
Example: text with one char for font_size 48 returns the dimensions 29,49
"""
myfont = pygame.font.SysFont(font_name, font_size, bold)
size_x, size_y = myfont.size(text)
mytext = myfont.render(text, True, font_color)
mytext = mytext.convert_alpha() # pygame surface, use for blitting
if grid_size is not None:
# TODO error handler if grid_size is not a tuple of positive integers
mytext = pygame.transform.scale(mytext, grid_size)
mytext = mytext.convert_alpha() # pygame surface, use for blitting
return mytext, (grid_size[0], grid_size[1])
return mytext, (size_x, size_y)
def write(background, text, x=50, y=150, color=(0, 0, 0),
font_size=None, font_name="mono", bold=True, origin="topleft"):
"""blit text on a given pygame surface (given as 'background')
the origin is the alignement of the text surface
"""
if font_size is None:
font_size = 24
font = pygame.font.SysFont(font_name, font_size, bold)
width, height = font.size(text)
surface = font.render(text, True, color)
if origin == "center" or origin == "centercenter":
background.blit(surface, (x - width // 2, y - height // 2))
elif origin == "topleft":
background.blit(surface, (x, y))
elif origin == "topcenter":
background.blit(surface, (x - width // 2, y))
elif origin == "topright":
background.blit(surface, (x - width, y))
elif origin == "centerleft":
background.blit(surface, (x, y - height // 2))
elif origin == "centerright":
background.blit(surface, (x - width, y - height // 2))
elif origin == "bottomleft":
background.blit(surface, (x, y - height))
elif origin == "bottomcenter":
background.blit(surface, (x - width // 2, y))
elif origin == "bottomright":
background.blit(surface, (x - width, y - height))
def get_line(start, end):
"""Bresenham's Line Algorithm
Produces a list of tuples from start and end
source: http://www.roguebasin.com/index.php?title=Bresenham%27s_Line_Algorithm#Python
see also: https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
#>>> points1 = get_line((0, 0), (3, 4))
# >>> points2 = get_line((3, 4), (0, 0))
#>>> assert(set(points1) == set(points2))
#>>> print points1
#[(0, 0), (1, 1), (1, 2), (2, 3), (3, 4)]
#>>> print points2
#[(3, 4), (2, 3), (1, 2), (1, 1), (0, 0)]
"""
# Setup initial conditions
x1, y1 = start
x2, y2 = end
dx = x2 - x1
dy = y2 - y1
# Determine how steep the line is
is_steep = abs(dy) > abs(dx)
# Rotate line
if is_steep:
x1, y1 = y1, x1
x2, y2 = y2, x2
# Swap start and end points if necessary and store swap state
swapped = False
if x1 > x2:
x1, x2 = x2, x1
y1, y2 = y2, y1
swapped = True
# Recalculate differentials
dx = x2 - x1
dy = y2 - y1
# Calculate error
error = int(dx / 2.0)
ystep = 1 if y1 < y2 else -1
# Iterate over bounding box generating points between start and end
y = y1
points = []
for x in range(x1, x2 + 1):
coord = (y, x) if is_steep else (x, y)
points.append(coord)
error -= abs(dy)
if error < 0:
y += ystep
error += dx
# Reverse the list if the coordinates were swapped
if swapped:
points.reverse()
return points
class CursorSprite(VectorSprite):
def create_image(self):
self.image = pygame.surface.Surface((Viewer.grid_size[0],
Viewer.grid_size[1]))
c = random.randint(100, 250)
pygame.draw.rect(self.image, (c, c, c), (0, 0, Viewer.grid_size[0],
Viewer.grid_size[1]), 3)
self.image.set_colorkey((0, 0, 0))
self.image.convert_alpha()
self.rect = self.image.get_rect()
def update(self, seconds):
self.create_image() # always make new image every frame with different color
super().update(seconds)
class HumanSprite(VectorSprite):
def _overwrite_parameters(self):
self.location = (8,5)
self.old_location = (8,5)
self.hour = 0
self.dx, self.dy = 0, 0
self.location_history = [(8,5)]
self.home_location = (8,5)
self.homeVector = pygame.math.Vector2(8* Viewer.grid_size[0] + Viewer.grid_size[0]//2,5 * Viewer.grid_size[1]+ Viewer.grid_size[1]//2)
self.sleep = False
def wander_around(self):
"""go to a nearby location or go home"""
self.dx = random.randint(-1,1)
self.dy = random.randint(-1,1)
if self.sleep:
self.dx, self.dy = 0, 0
#self.location[0] += self.dx
#self.location[1] += self.dy
self.location = ( self.location[0] + self.dx, self.location[1] + self.dy)
self.location_history.append((self.location[0], self.location[1]))
def create_image(self):
print("ich bin human", self.number)
self.image = pygame.surface.Surface((Viewer.grid_size[0], Viewer.grid_size[1]))
self.image.set_colorkey((0,0,0))
self.image.blit(Viewer.images["human001"], (0,0)) # TODO: wenn image NICHT 64x64, in die Mitte von self.image blitten, aber blit hat als 2. argument topleft corner
pygame.draw.rect(self.image, (random.randint(0,255), random.randint(0,255), random.randint(0,255)),
(0,55,64,14))
self.image.convert_alpha()
self.rect = self.image.get_rect()
def update(self, seconds):
super().update( seconds)
print(self.homeVector, self.pos)
if self.sleep and (self.homeVector[0] != int(self.pos[0]) or (self.homeVector[1] != int(self.pos[1]))):
print("i go home to sleep")
self.move = self.homeVector - self.pos
if self.move.length() != 0:
self.move.normalize_ip() # 1
self.move *= 40 # pixel / sec
self.location = pixel_to_tile((self.pos[0], self.pos[1]))
#print("i am now at location", self.location, "my home is at", self.home_location)
else:
self.move = pygame.math.Vector2(0,0)
def hour_update(self, hour):
print(hour, "davor loc", self.location, "and loc_history:", self.location_history)
if self.hour % 24 > 22 or self.hour % 24 < 6:
self.sleep = True
else:
self.sleep = False
print(self.hour, self.sleep)
try:
self.location = self.location_history[hour]
print("hour found")
except IndexError:
print("hour not found")
# this hour is not yet in location history
self.wander_around() # updating loc
# move sprite to loc
self.hour = hour
# sleeping time ?
x, y = self.location[0], self.location[1]
print("hour", hour, "teleporting to", x,y )
self.pos = pygame.math.Vector2( x* Viewer.grid_size[0] + Viewer.grid_size[0]//2,
y * Viewer.grid_size[1]+ Viewer.grid_size[1]//2)
#print(hour ,"danach loc ", self.location, "and loc_history:", self.location_history)
# testing if want to be home but not yet home
print("sleep", self.sleep)
#if self.sleep and self.pos[0] != self.home_location[0] and self.pos[1] != self.home_location[1]:
# print("i want to go home!")
#self.move = pygame.math.Vector2(self.pos[0]-self.home_location[0], self.pos[1]-self.home_location[1])
#self.move.normalize_ip() # 1
#self.move *= 20 # pixel / sec
class Viewer():
hour = 0
width = 0 # screen x resolution in pixel
height = 0 # screen y resolution in pixel
panel_width = 208
log_height = 0
grid_size = (32, 32)
images = {}
viewport = [0,0] # offset of the topleft corner of the viewport in grid coordinates
pcx = 0 # player x coordinate in pixel
pcy = 0 # player y coordinate in pixel
def __init__(self, width=640, height=400, grid_size=(32, 32), fps=60, ):
"""Initialize pygame, window, background, font,...
default arguments """
# self.game = game
self.fps = fps
# position in pixel where all the gold sprites are flying to:
Viewer.grid_size = grid_size # make global readable
Viewer.width = width
Viewer.height = height
self.cursor_x = 0
self.cursor_y = 0
pygame.init()
# player center in pixel
Viewer.pcx = (width - Viewer.panel_width) // 2 # set player in the middle of the screen
Viewer.pcy = (height - Viewer.log_height) // 2
self.screen = pygame.display.set_mode((self.width, self.height), pygame.DOUBLEBUF)
self.clock = pygame.time.Clock()
self.playtime = 0.0
# ------ surfaces for radar, panel and log ----
# all surfaces are black by default
self.panelscreen = pygame.surface.Surface((Viewer.panel_width, Viewer.height))# - Viewer.panel_width))
self.panelscreen.fill((64, 128, 64))
self.panelscreen0 = pygame.surface.Surface((Viewer.panel_width, Viewer.height))# - Viewer.panel_width))
self.panelscreen0.fill((64, 128, 64))
self.logscreen = pygame.surface.Surface((Viewer.width - Viewer.panel_width, Viewer.log_height))
# ------ background images ------
self.backgroundfilenames = [] # every .jpg or .jpeg file in the folder 'data'
self.make_background()
# ------ joysticks ----
pygame.joystick.init()
self.joysticks = [pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count())]
for j in self.joysticks:
j.init()
# ------ create bitmaps for player and dungeon tiles ----
self.prepare_spritegroups()
self.cursor = CursorSprite(pos=pygame.math.Vector2(Viewer.grid_size[0] // 2, Viewer.grid_size[
1] // 2)) # pos=pygame.math.Vector2(x=Viewer.pcx, y=Viewer.pcy))
self.load_map_tiles()
self.load_sprites()
self.create_map()
for person in range(4):
#print("pos:", self.tile_to_pixel((8* Viewer.grid_size[0],5 * Viewer.grid_size[1])))
HumanSprite(pos=pygame.math.Vector2(8* Viewer.grid_size[0] + Viewer.grid_size[0]//2,5 * Viewer.grid_size[1]+ Viewer.grid_size[1]//2))
self.run()
def load_map_tiles(self):
"""load maptiles from files"""
self.house001 = pygame.image.load(os.path.join("data", "haus64_001.png"))
self.house001.convert_alpha() # to be sure of transparency
def load_sprites(self):
image = pygame.image.load(os.path.join("data", "mandal2.png"))
image.convert_alpha()
Viewer.images["human001"] = image
def create_map(self, xtiles= 20, ytiles= 20):
"""creates a 2d array to be later represented by graphical tiles.
the 'map' 2d array is just a nested list of chars
each char represent a graphical tile"""
self.map = [["." for x in range(xtiles)] for y in range(ytiles)] # create a nested list
# set some random tiles:
for y, line in enumerate(self.map):
for x, char in enumerate(line): # c = Haus, . = nix, a,b = Farbe
if x == 8 and y == 5:
self.map[y][x] = "a"
else:
self.map[y][x] = "." # #random.choice((".","a","b"))
def prepare_spritegroups(self):
self.allgroup = pygame.sprite.LayeredUpdates() # for drawing
self.whole_screen_group = pygame.sprite.Group()
self.flytextgroup = pygame.sprite.Group()
self.cursorgroup = pygame.sprite.Group()
self.humangroup = pygame.sprite.Group()
VectorSprite.groups = self.allgroup
Flytext.groups = self.allgroup, self.flytextgroup
CursorSprite.groups = self.allgroup
HumanSprite.groups = self.allgroup, self.humangroup
def draw_panel(self):
self.panelscreen.blit(self.panelscreen0, (0, 0))
write(self.panelscreen, text="cursor x:{} y:{} (pixel)".format(self.cursor.pos.x, self.cursor.pos.y), x=5, y=5,
color=(255, 255, 255), font_size=12)
write(self.panelscreen, text="cursor x:{} y:{} (tiles)".format(
self.cursor.pos.x // Viewer.grid_size[0], self.cursor.pos.y // Viewer.grid_size[1]), x=5, y=25,
color=(255, 255, 255), font_size=12)
write(self.panelscreen, text="hour: {}".format(Viewer.hour), x=5, y=45, color=(0,200,200), font_size=12)
# - hitpoint bar in red, starting left
# pygame.draw.rect(self.panelscreen, (200, 0, 0),
# (0, 35, self.game.player.hitpoints * Viewer.panel_width / self.game.player.hitpoints_max, 28))
# -y35--------------------
# blit panelscreen
self.screen.blit(self.panelscreen, (Viewer.width - self.panel_width, 0))
#@staticmethod
# def load_images(self):
# """single images. char looks to the right by default?"""
# self.images["arch-mage-attack"] = pygame.image.load(
# os.path.join("data", "arch-mage-attack.png")).convert_alpha()
def make_background(self):
"""scans the subfolder 'data' for .jpg files, randomly selects
one of those as background image. If no files are found, makes a
white screen"""
try:
for root, dirs, files in os.walk("data"):
for file in files:
if file[-4:].lower() == ".jpg" or file[-5:].lower() == ".jpeg":
self.backgroundfilenames.append(os.path.join(root, file))
random.shuffle(self.backgroundfilenames) # remix sort order
self.background = pygame.image.load(self.backgroundfilenames[0])
except:
print("no folder 'data' or no jpg files in it")
self.background = pygame.Surface(self.screen.get_size()).convert()
self.background.fill((255, 255, 255)) # fill background #
# TODO: background füllen mit logscreen/panelscreen farbe
self.background = pygame.transform.scale(self.background,
(Viewer.width, Viewer.height))
pygame.draw.rect(self.background, (64, 128, 64), (
Viewer.width - Viewer.panel_width, Viewer.panel_width, Viewer.height - Viewer.panel_width,
Viewer.height - Viewer.panel_width))
self.background.convert()
def move_cursor_to(self, x, y):
"""moves the cursor to tiles xy, """
x = self.pcx + x * self.grid_size[0]
y = self.pcy + y * self.grid_size[1]
if x < 0 or y < 0 or x > (self.width - self.panel_width) or y > (self.height - self.log_height):
# print("mouse outside game panel", x, y)
return # cursor can not move outside of the game window
#
# ---- finally, move the cursor ---
self.cursor_x = x
self.cursor_y = y
# self.screen.blit(self.background, (0, 0))
def tile_blit(self, surface, x_pos, y_pos, corr_x=0, corr_y=0):
"""correctly blits a surface at tile-position x,y, so that the player is always centered at pcx, pcy"""
x = (x_pos - self.game.player.x) * self.grid_size[0] + self.pcx + corr_x
y = (y_pos - self.game.player.y) * self.grid_size[1] + self.pcy + corr_y
# check if the tile is inside the game screen, otherwise ignore
if (x > (Viewer.width - Viewer.panel_width)) or (y > (Viewer.height - Viewer.log_height)):
return
if (x + self.grid_size[0]) < 0 or (y + self.grid_size[1]) < 0:
return
self.screen.blit(surface, (x, y))
def draw_grid(self, color=(200, 200, 200)):
"""draws lines according to Viewer.grid_size"""
for y in range(0, Viewer.height, Viewer.grid_size[1]):
pygame.draw.line(self.screen, color, (0, y), (Viewer.width - Viewer.panel_width, y), 1)
for x in range(0, Viewer.width - Viewer.panel_width, Viewer.grid_size[0]):
pygame.draw.line(self.screen, color, (x, 0), (x, Viewer.height), 1)
def draw_map_tiles(self):
# blit (the visible tile of) self.map on the screen
legend = {"." : (32,32,32),
"a" : (128,255,0),
"b" : (128,0,255),
"c" : (0,255,255)
}
for y, line in enumerate(self.map):
for x, char in enumerate(line):
#print("legend:", self.map[y][x])
px = (x-Viewer.viewport[0]) * Viewer.grid_size[0]
py = (y-Viewer.viewport[1]) * Viewer.grid_size[1]
if char == "c":
# make a green grass ground below the house, because house has transpacrencey
pygame.draw.rect(self.screen, (0,200,0),
(px, py, Viewer.grid_size[0], Viewer.grid_size[1]))
self.screen.blit(self.house001, ( px, py))
else:
pygame.draw.rect(self.screen, legend[self.map[y][x]],(px, py, Viewer.grid_size[0], Viewer.grid_size[1]))
def run(self):
"""The mainloop"""
running = True
pygame.mouse.set_visible(True)
oldleft, oldmiddle, oldright = False, False, False
self.redraw = True
# exittime = 0
self.spriteless_background = pygame.Surface((Viewer.width - Viewer.panel_width, Viewer.height))
self.screen.blit(self.spriteless_background, (0, 0))
### pygame.display.flip()
# log_lines = len(Game.log)
while running:
milliseconds = self.clock.tick(self.fps) #
seconds = milliseconds / 1000
self.playtime += seconds
# ------ mouse handler ------
# left, middle, right = pygame.mouse.get_pressed()
# oldleft, oldmiddle, oldright = left, middle, right
# ------ joystick handler -------
# for number, j in enumerate(self.joysticks):
# if number == 0:
# x = j.get_axis(0)
# y = j.get_axis(1)
# buttons = j.get_numbuttons()
# for b in range(buttons):
# pushed = j.get_button(b)
# ------------ pressed keys (in this moment pressed down)------
pressed_keys = pygame.key.get_pressed()
# -------- events ------
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# ------- pressed and released key ------
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
# ---- change time (hour) -----
if event.key == pygame.K_PLUS:
Viewer.hour += 1
for human in self.humangroup:
human.hour_update(Viewer.hour)
if event.key == pygame.K_MINUS:
if Viewer.hour > 0:
Viewer.hour -= 1
for human in self.humangroup:
human.hour_update(Viewer.hour)
# ---- move the game cursor with wasd ----
if event.key == pygame.K_a:
self.cursor.pos.x -= self.grid_size[0]
if event.key == pygame.K_d:
self.cursor.pos.x += self.grid_size[0]
if event.key == pygame.K_w:
self.cursor.pos.y -= self.grid_size[1]
if event.key == pygame.K_s:
self.cursor.pos.y += self.grid_size[1]
# ---- move the viewport with cursor keys ----
if event.key == pygame.K_LEFT:
self.viewport[0] -= 1
if event.key == pygame.K_RIGHT:
self.viewport[0] += 1
if event.key == pygame.K_UP:
self.viewport[1] -= 1
if event.key == pygame.K_DOWN:
self.viewport[1] += 1
# self.redraw = True
# reset_cursor = False
# Game.cursor_x -= 1
# ----------- magic with ctrl key and dynamic key -----
# if pressed_keys[pygame.K_RCTRL] or pressed_keys[pygame.K_LCTRL]:
# if event.mod & pygame.KMOD_CTRL: # any or both ctrl keys are pressed
# key = pygame.key.name(event.key) # name of event key: a, b, c etc.
# --- set cursor to mouse if inside play area -----
mousepos = pygame.mouse.get_pos()
if mousepos[0] < Viewer.width - Viewer.panel_width:
x, y = pixel_to_tile(pygame.mouse.get_pos())
#print(x, y)
self.cursor.pos = pygame.math.Vector2(x * Viewer.grid_size[0] + Viewer.grid_size[0] // 2,
y * Viewer.grid_size[1] + Viewer.grid_size[1] // 2)
# self.move_cursor_to(x, y) # only moves if on valid tile
# ============== draw screen =================
# screen_without_sprites = self.screen.copy()
# self.allgroup.clear(bgd=self.screen)
# self.allgroup.clear(self.screen, self.spriteless_background)
# self.screen.blit(self.spriteless_background,
# (0, 0)) # NOTICE: out-comment this line for very cool effect at goldexplosion
self.screen.blit(self.background, (0, 0))
self.draw_map_tiles()
self.draw_grid()
self.allgroup.update(seconds)
# ------ Cursor -----
# self.cursor.pos = pygame.math.Vector2(self.tile_to_pixel((self.cursor_x, self.cursor_y), center=True))
self.allgroup.draw(self.screen)
# dirtyrects = []
self.draw_panel() # always draw panel #unter allgropu draw: münzen unsichtbar, flackert
# dirtyrects.append(pygame.Rect(Viewer.width - Viewer.panel_width, 0, Viewer.panel_width, Viewer.height))
# write text below sprites
fps_text = "FPS: {:5.3}".format(self.clock.get_fps())
pygame.draw.rect(self.screen, (64, 255, 64), (Viewer.width - 110, Viewer.height - 20, 110, 20))
write(self.screen, text=fps_text, origin="bottomright", x=Viewer.width - 2, y=Viewer.height - 2,
font_size=16, bold=True, color=(0, 0, 0))
# self.cursor.pos += pygame.math.Vector2(Viewer.grid_size[0]//2, Viewer.grid_size[1]//2) # center on tile
# -------- next frame -------------
# print(dirtyrects)
pygame.display.flip()
# -----------------------------------------------------
pygame.mouse.set_visible(True)
pygame.quit()
if __name__ == '__main__':
# g = Game(tiles_x=80, tiles_y=40)
Viewer(width=1168, height=768, grid_size=(64, 64)) # , (35,35))