Finalisierung Screen 1 + Kommentierung der Logik
This commit is contained in:
parent
0e27803469
commit
a2ece792f8
BIN
code/__pycache__/functions.cpython-312.pyc
Normal file
BIN
code/__pycache__/functions.cpython-312.pyc
Normal file
Binary file not shown.
BIN
code/__pycache__/setup.cpython-312.pyc
Normal file
BIN
code/__pycache__/setup.cpython-312.pyc
Normal file
Binary file not shown.
BIN
code/__pycache__/sprites.cpython-312.pyc
Normal file
BIN
code/__pycache__/sprites.cpython-312.pyc
Normal file
Binary file not shown.
61
code/functions.py
Normal file
61
code/functions.py
Normal file
|
@ -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)
|
|
@ -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)
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue