From 753cb75b1d918403932bca1ccc8bd05e824c41c9 Mon Sep 17 00:00:00 2001 From: moritz Date: Sat, 6 Jul 2024 22:34:48 +0200 Subject: [PATCH] =?UTF-8?q?Laternen=20erstellt=20und=20Lichtsensoren=20und?= =?UTF-8?q?=20die=20lichter=20anschaltet,=20sobald=20der=20Spieler=20in=20?= =?UTF-8?q?der=20N=C3=A4he=20ist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/__pycache__/sprites.cpython-310.pyc | Bin 1307 -> 2955 bytes code/simulation.py | 47 ++++++++++++++++++++--- code/sprites.py | 43 ++++++++++++++++++++- 3 files changed, 82 insertions(+), 8 deletions(-) diff --git a/code/__pycache__/sprites.cpython-310.pyc b/code/__pycache__/sprites.cpython-310.pyc index 4d785eb09d97ff81dec4c39d1b8cb210c05a57e9..13689d0f045971d63491caaadb6be5c560c4725a 100644 GIT binary patch literal 2955 zcmbVOOOG2x5bmBC&y2n6%_bPMD2?{iy1$$5&M|y>hw4=(#odt@)SF z*xy*JJ`OEz(_c4;2qt*K>TI70OW4ntuqA8rw)NDm+qCD10__zRdk*cn!lONJu~(oy zUleJtxY%ElebTUzxet@@NUDghD8T*j5~DsOlCr04%#Rsosnxc}j^M}q38#5s z?2bt1v1L}^*u=4B9LsR55g%DCYs3O?#M+*4b{WlWn%iw37XHp@T@;M$@oK}}xD&Rd zzN*tWdejNKfr?v)>0ZCvs#?=G$e8I}BEy}tVu@@TDFsiemKmmh(tN@*Hc~#x&5P7*pXSm+?P2?FBDlZRbf{*d#XbRqA-!!cHe|45V4X`8uz-H z-;!y7MdpB3?aa|KY1TJqlE&=%#%u-a$dF9BFH z7Y#aOsct+}y_O0)omeW_ZG0a_kHYk@*KI^SAsf0+(o*mBk5rNT15$dMNQrOqE%T?D zRpG-6IktOqXHS7_(Fp*IXzD+Rq?EX0>zF;^BTjRBm#G~B$#Gb_Aoz7=v7kU}S6Bu> z&q(E0cnowrb2OM z_9JVK6oOAU4a+nCb6&NGa_>@+Q@NLdpwknB1oO=x_+=0#ImeVzuE%J6A8M;avhu8E zBbg*Yu;TkG5JBRoc`fNpTKQ~~`(^ss+qU6Ew0Y`5PUAUwoyfZWZThoA{m{<5gH%bG z>Zo9j(`b)3T0{W)5m=O!a%r5#F}briMMTn$((2+x1MH*|mvn z>T5}=+~8gH6}@nrKt2S)LG=;HG8vbETqOp;J25%I zDqLabTp%>ATnEJ27pVcp#fvKQe+cnT>FUU(!}AC8IWi~rXz~A%8Bw z_DS&!BI*;c(CyTxAPX=uD;5(s?;oFm^Et>DAYX#u>tP)pxbFm5=Xg}8@eMD|y(p$}D+KKn%D#pWyckv%9Z!vqvr+|p!Ta1+; zx?Gc~NDw3|0U|`;geXWcZ$VCCWol7;W?n&QNiqLqE|!209-u@G!vcnd42%pZ3_$lV z`e`y2DF8)*6xbv*L)c0SQW8s2Wq>M*nScZjV-;@z*jzn`j3)c!%dD#P96&j+IYo*< z)-B%n_~e|#;^O4goSgW0B$o++)PbB=1Tu{oh>Jm5Ie-Rpa0~E)MKxK9IDulf*oq5^ nGD}i#u?2%^upE+6V3&e@%3&jto1apelWNBZGLQvqpac^DAv9M| diff --git a/code/simulation.py b/code/simulation.py index e7d98b0..bfa1805 100644 --- a/code/simulation.py +++ b/code/simulation.py @@ -14,8 +14,32 @@ clock = pygame.time.Clock() player = pygame.sprite.GroupSingle() player.add(sprites.Player()) -top_street_boarder = pygame.Rect(0, 50, SCREEN_WIDTH, 4) +streets = pygame.sprite.Group() +street_1 = sprites.Streets(0, 100, SCREEN_WIDTH, 4) +street_2 = sprites.Streets(0, 200, SCREEN_WIDTH, 4) +streets.add(street_1) +streets.add(street_2) +lanterns = pygame.sprite.Group() +lantern_list = [] +detectors = pygame.sprite.Group() +detectors_list = [] +id = 1 + +for y_pos in range(50, 251, 200): + x_pos = 100 + while x_pos < SCREEN_WIDTH: + lantern = sprites.Lanterns(x_pos, y_pos, 25, 25, id) + lantern_list.append(lantern) + detector = sprites.Detectors(x_pos, y_pos, 133, 300, id) + detectors_list.append(detector) + x_pos += 150 + id += 1 + +for item in lantern_list: + lanterns.add(item) +for item in detectors_list: + detectors.add(item) if __name__ == "__main__": @@ -26,16 +50,27 @@ if __name__ == "__main__": run = False - screen.fill("Grey") + screen.fill("Grey") + detectors.draw(screen) + streets.draw(screen) + lanterns.draw(screen) player.draw(screen) player.update() - - #pygame.draw.rect(screen, (0, 0, 0), line) + for lantern in lanterns: + lantern.reset_light() - #if stickman_rect.colliderect(line): - # stickman_rect.y = 55 + # 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 lantern.id == detector.id: + lantern.light_up() + if pygame.sprite.spritecollide(player.sprite, streets, False): + player.sprite.set_back() + + pygame.display.update() clock.tick(60) diff --git a/code/sprites.py b/code/sprites.py index 418e2e5..700af23 100644 --- a/code/sprites.py +++ b/code/sprites.py @@ -10,7 +10,9 @@ class Player(pygame.sprite.Sprite): self.player_direction = [player_right, player_left] self.image = self.player_direction[0] - self.rect = self.image.get_rect(topleft = (0, 55)) + self.rect = self.image.get_rect(center = (0, 150)) + + def player_inputs(self): key = pygame.key.get_pressed() @@ -25,5 +27,42 @@ class Player(pygame.sprite.Sprite): if key[pygame.K_w]: self.rect.y -= 3 + def get_last_position(self): + self.last_position = self.rect.topleft + + def set_back(self): + self.rect.topleft = self.last_position + def update(self): - self.player_inputs() \ No newline at end of file + self.get_last_position() + self.player_inputs() + + +class Streets(pygame.sprite.Sprite): + def __init__(self, left, top, width, height): + super().__init__() + self.image = pygame.Surface([width, height]) + self.image.fill((0, 0, 0)) + self.rect = self.image.get_rect(topleft=(left, top)) + +class Lanterns(pygame.sprite.Sprite): + def __init__(self, left, top, width, height, id): + super().__init__() + self.image = pygame.Surface([width, height]) + self.image.fill((0, 0, 0)) + self.rect = self.image.get_rect(center=(left, top)) + self.id = id + + def light_up(self): + self.image.fill((255, 255, 0)) + + def reset_light(self): + self.image.fill((0, 0, 0)) + +class Detectors(pygame.sprite.Sprite): + def __init__(self, left, top, width, height, id): + super().__init__() + self.image = pygame.Surface([width, height]) + self.image.fill("Grey") + self.rect = self.image.get_rect(center=(left, top)) + self.id = id