From a2ece792f8af2a940b5a6badd9e47fad358c714f Mon Sep 17 00:00:00 2001 From: moritz Date: Mon, 8 Jul 2024 11:35:13 +0200 Subject: [PATCH] Finalisierung Screen 1 + Kommentierung der Logik --- code/__pycache__/functions.cpython-312.pyc | Bin 0 -> 2595 bytes code/__pycache__/setup.cpython-312.pyc | Bin 0 -> 5095 bytes code/__pycache__/sprites.cpython-312.pyc | Bin 0 -> 6252 bytes code/functions.py | 61 +++++++++++++++++++++ code/setup.py | 8 +-- code/simulation.py | 16 ++---- code/sprites.py | 2 +- 7 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 code/__pycache__/functions.cpython-312.pyc create mode 100644 code/__pycache__/setup.cpython-312.pyc create mode 100644 code/__pycache__/sprites.cpython-312.pyc create mode 100644 code/functions.py diff --git a/code/__pycache__/functions.cpython-312.pyc b/code/__pycache__/functions.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..385f1094ed0bb908c77bc9775e24fb86cd88c017 GIT binary patch literal 2595 zcmcImO>7%Q6rSDnAN~QFb!9g;ZXkumL=BB-lFCJFbam`kEzg!!vzn8O0}JT^ctOmlwY z22~D8LfwWw%L^F;(`W!)`Uau*g|u*)%J;N1pXfkH<>tgRhdDYX=Rva(MUysW zn#2iGW7Bedhy`rGMr^uas4Z}4A1~(mey<$p$Fl2Z?j3zkG~OA&k;VLwQGqYM;R~?1 ze&4ZuYe3V;|43*9z(yMswT(ROzK4#m=|6Nz6S6T2onNS!RE@~8R^j8gVoAowVp?Qs z!f$|aG^NQTrBH!6Y$Kbo@0z)6YxykrQE=(abz6J+u_H^bfBZpNY+Vd21Tu#{ zf3fWJl$^&3&SROuTzhUZ-(Ga~?ufs*SA5Iad%JGknu}(AYvOYUxnMf$Iy&HI%LVaO zAzGwgBEul5m&h{*ln5&eRwBaX=2D}Mp<%c@SV;3tSGI#)3qy#QKu%~$vdiG-D>jKJ zsv1v?(d0Tn;5U#*sr)c>6=O6x5sQ+?VXQWCO%74Xky?J56B5D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=QjGwC-2otzNr9+xRhh2u`5rt)2)kH(9(C|(G&r5YIq0ut}l3x79n7~b|NQn|EoBW zM|H8(zbt+0QN!;%FYiR?Pm$bX2$}=xd7MOm9615iD@OiJ0GRrS>!9;2>2RArjv4N= z>_;*)Lu6r@={Ig&BqkdAfUl)PW@h>py#SwR@mdVPV;9G9|0IDmVHek5JXijp^g$)C zMGtIQBQUWx!HL}A;$(TYG+Sxks<&@lBQUYn-p*|+epJ3%x?1Vjrgv;xBQUWRZsQ`q Va?b~D1u7f1>l?OzL12QB@L$FmM~MIc literal 0 HcmV?d00001 diff --git a/code/functions.py b/code/functions.py new file mode 100644 index 0000000..28ff6e3 --- /dev/null +++ b/code/functions.py @@ -0,0 +1,61 @@ +import pygame + + + +# Laternen durchgehen, abgleichen mit der Laterne die wir haben, sobald die in der gleichen Straße sind, +# findet ein Vergleich statt, die id der Laterne in der Kreuzung, die erste oder die letzte ist in der Straße +# Jenachdem werden dann die zwei folgenden Laternen angeschalten (id: 2 und 3) oder die zwei zuvorstehenden +# Laternen (id: alte_id -1, alte_id - 2). Und jenachdem gedimmed geschaltet. + +def light_street_when_in_intersection(lanterns, lantern_id, street): + for lantern in lanterns: + if lantern.street == street: + if lantern_id == 1: + if lantern.id == 2: + lantern.light_up(176, 179, 0, 255) + if lantern.id == 3: + lantern.light_up(176, 179, 0, 128) + else: + if lantern.id == lantern_id - 1: + lantern.light_up(176, 179, 0, 255) + if lantern.id == lantern_id - 2: + lantern.light_up(176, 179, 0, 128) + + +# Funktion um alle Laternen einer Kreuzung auf die gleiche Farbe/Helligkeit zu dimmen +def light_intersection_dimmed(lanterns, crossing_id, alpha): + for lantern in lanterns: + if lantern.crossing_id == crossing_id: + lantern.light_up(176, 179, 0, alpha) + + +def check_sensor(collisions, lanterns): + # Detektoren, welche aktiviert wurden durchgehen und mit allen Laternen abgleichen + for detector in collisions: + for lantern in lanterns: + + # Wenn Kreuzungsdetektor betreten wird, komplette Kreuzung aufleuchten lassen + if detector.crossing_id == lantern.crossing_id and detector.crossing_id is not None: + lantern.light_up(255, 255, 0, 255) + + # Ankreuzende Straßen der Kreuzung, beleuchten + light_street_when_in_intersection(lanterns, lantern.id, lantern.street) + + # Checken ob Laterne in der selben Straße steht, dann zum leuchten bringen falls straße und id gleich ist + # Angrenzende Laternen anhand der id rausfinden und gedimmed anschalten + if lantern.street == detector.street: + + # Checken ob direkte Nachbar Laterne oder Laterne an zweiter Stelle + #Falls sich die Lampe in der Kreuzung befindet, soll die ganze Kreuzung gedimmed an sein + if lantern.id == detector.id - 1 or lantern.id == detector.id + 1: + lantern.light_up(176, 179, 0, 255) + if lantern.crossing_id is not None: + light_intersection_dimmed(lanterns, lantern.crossing_id, 255) + if lantern.id == detector.id - 2 or lantern.id == detector.id + 2: + lantern.light_up(176, 179, 0, 128) + if lantern.crossing_id is not None: + light_intersection_dimmed(lanterns, lantern.crossing_id, 128) + + # Direkt zugehörige Laterne anmachen + if lantern.id == detector.id: + lantern.light_up(255, 255, 0, 255) \ No newline at end of file diff --git a/code/setup.py b/code/setup.py index 52b0fe5..5b4d933 100644 --- a/code/setup.py +++ b/code/setup.py @@ -50,7 +50,7 @@ def setup_lanterns_detectors_new(SCREEN_WIDTH): # Street 4 for x_pos in range(825, 976, 150): y_pos = 325 - id = 1 + id = 2 while y_pos <= 625: lantern = sprites.Lanterns(x_pos, y_pos, 25, 25, id, street="street_4") lantern_list.append(lantern) @@ -75,7 +75,7 @@ def setup_lanterns_detectors_new(SCREEN_WIDTH): # Street 6 for y_pos in range(725, 976, 150): x_pos = 1075 - id = 1 + id = 2 while x_pos <= 1775: lantern = sprites.Lanterns(x_pos, y_pos, 25, 25, id, street="street_6") lantern_list.append(lantern) @@ -85,14 +85,14 @@ def setup_lanterns_detectors_new(SCREEN_WIDTH): id += 1 # Crossing 1 - cross_lantern_1 = sprites.Lanterns(825, 225, 25, 25, id=9, street="street_1", crossing_id=1) + cross_lantern_1 = sprites.Lanterns(825, 225, 25, 25, id=1, street="street_4", crossing_id=1) cross_lantern_2 = sprites.Lanterns(825, 75, 25, 25, id=9, street="street_1", crossing_id=1) cross_lantern_3 = sprites.Lanterns(975, 225, 25, 25, id=1, street="street_2", crossing_id=1) cross_lantern_4 = sprites.Lanterns(975, 75, 25, 25, id=1, street="street_2", crossing_id=1) cross_detector_1 = sprites.Detectors(900, 150, 260, 260, crossing_id=1) # Crossing 1 - cross_lantern_5 = sprites.Lanterns(825, 725, 25, 25, id=9, street="street_5", crossing_id=2) + cross_lantern_5 = sprites.Lanterns(825, 725, 25, 25, id=6, street="street_4", crossing_id=2) cross_lantern_6 = sprites.Lanterns(825, 875, 25, 25, id=9, street="street_5", crossing_id=2) cross_lantern_7 = sprites.Lanterns(975, 725, 25, 25, id=1, street="street_6", crossing_id=2) cross_lantern_8 = sprites.Lanterns(975, 875, 25, 25, id=1, street="street_6", crossing_id=2) diff --git a/code/simulation.py b/code/simulation.py index 2f38dfc..7c3bd8f 100644 --- a/code/simulation.py +++ b/code/simulation.py @@ -1,6 +1,7 @@ import pygame import sprites import setup +import functions pygame.init() @@ -27,6 +28,8 @@ for item in detectors_list: for item in streets_list: streets.add(item) + + if __name__ == "__main__": run = True while run: @@ -46,17 +49,8 @@ if __name__ == "__main__": # Check for collision and light up corresponding lantern collisions = pygame.sprite.spritecollide(player.sprite, detectors, False) - for detector in collisions: - for lantern in lanterns: - if detector.crossing_id == lantern.crossing_id and detector.crossing_id is not None: - lantern.light_up(255, 255, 0, 255) - if lantern.street == detector.street: - if lantern.id == detector.id - 1 or lantern.id == detector.id + 1: - lantern.light_up(176, 179, 0, 255) - if lantern.id == detector.id - 2 or lantern.id == detector.id + 2: - lantern.light_up(176, 179, 0, 128) - if lantern.id == detector.id: - lantern.light_up(255, 255, 0, 255) + functions.check_sensor(collisions, lanterns) + if pygame.sprite.spritecollide(player.sprite, streets, False): diff --git a/code/sprites.py b/code/sprites.py index 0d1a499..2bebe9a 100644 --- a/code/sprites.py +++ b/code/sprites.py @@ -12,7 +12,7 @@ class Player(pygame.sprite.Sprite): self.image = self.player_direction[0] self.rect = self.image.get_rect(center = (0, 150)) - self.walking_speed = 5 + self.walking_speed = 10 def player_inputs(self): key = pygame.key.get_pressed()