Hackathon/code/sprites.py
2024-07-11 11:13:32 +02:00

161 lines
5.8 KiB
Python

import pygame
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
player_right = pygame.image.load("images/stickman_right.png")
player_right = pygame.transform.scale(player_right, (30, 67))
player_left = pygame.image.load("images/stickman_left.png")
player_left = pygame.transform.scale(player_left, (30, 67))
self.player_direction = [player_right, player_left]
self.image = self.player_direction[0]
self.rect = self.image.get_rect(center = (900, 500))
self.walking_speed = 10
def player_inputs(self):
key = pygame.key.get_pressed()
if key[pygame.K_a]:
self.rect.x -= self.walking_speed
self.image = self.player_direction[1]
if key[pygame.K_d]:
self.rect.x += self.walking_speed
self.image = self.player_direction[0]
if key[pygame.K_s]:
self.rect.y += self.walking_speed
if key[pygame.K_w]:
self.rect.y -= self.walking_speed
def get_last_position(self):
self.last_position = self.rect.topleft
def set_back(self):
self.rect.topleft = self.last_position
def left_screen(self):
if self.rect.x > 1860:
self.rect.x = -60
if self.rect.x < -60:
self.rect.x = 1860
def left_scene(self, current_scene):
if current_scene == "scene_1" and self.rect.y >= 1080:
self.rect.y = -80
return "scene_2"
if current_scene == "scene_1" and self.rect.x >= 1880:
self.rect.x = -80
return "scene_8"
if current_scene == "scene_1" and self.rect.x <= -80:
self.rect.x = 1880
return "scene_3"
if current_scene == "scene_1" and self.rect.y <= -80:
self.rect.y = 1080
return "scene_6"
if current_scene == "scene_2" and self.rect.y <= -80:
self.rect.x = 900
self.rect.y = 1050
return "scene_1"
if current_scene == "scene_2" and self.rect.x <= -80:
self.rect.x = 1800
return "scene_4"
if current_scene == "scene_2" and self.rect.x >= 1880:
self.rect.x = -80
self.rect.y = 830
return "scene_9"
if current_scene == "scene_3" and self.rect.x >= 1880:
self.rect.x = -80
return "scene_1"
if current_scene == "scene_3" and self.rect.y >= 1080:
self.rect.y = -80
return "scene_4"
if current_scene == "scene_3" and self.rect.y <= -80:
self.rect.y = 1080
return "scene_5"
if current_scene == "scene_4" and self.rect.x >= 1880:
self.rect.x = -80
return "scene_2"
if current_scene == "scene_4" and self.rect.y <= -80:
self.rect.y = 1080
return "scene_3"
if current_scene == "scene_5" and self.rect.y >= 1080:
self.rect.y = -80
return "scene_3"
if current_scene == "scene_5" and self.rect.x >= 1880:
self.rect.x = -80
return "scene_6"
if current_scene == "scene_6" and self.rect.x <= -80:
self.rect.x = 1880
return "scene_5"
if current_scene == "scene_6" and self.rect.y >= 1080:
self.rect.y = -80
return "scene_1"
if current_scene == "scene_6" and self.rect.x >= 1880:
self.rect.x = -80
return "scene_7"
if current_scene == "scene_7" and self.rect.x <= -80:
self.rect.x = 1880
return "scene_6"
if current_scene == "scene_7" and self.rect.y >= 1080:
self.rect.y = -80
return "scene_8"
if current_scene == "scene_8" and self.rect.y <= -80:
self.rect.y = 1080
return "scene_7"
if current_scene == "scene_8" and self.rect.x <= -80:
self.rect.x = 1880
return "scene_1"
if current_scene == "scene_8" and self.rect.y >= 1080:
self.rect.y = -80
return "scene_9"
if current_scene == "scene_9" and self.rect.x <= -80:
self.rect.x = 1880
self.rect.y = 780
return "scene_2"
if current_scene == "scene_9" and self.rect.y <= -80:
self.rect.y = 1080
return "scene_8"
return current_scene
def update(self):
self.get_last_position()
self.player_inputs()
# self.left_screen()
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=None, street=None, crossing_id=None):
super().__init__()
self.image = pygame.Surface([width, height], pygame.SRCALPHA)
self.image.fill((0, 0, 0, 255))
self.rect = self.image.get_rect(center=(left, top))
self.id = id
self.street = street
self.crossing_id = crossing_id
def light_up(self, r, g, b, oppacity):
self.image.fill((r, g, b, oppacity))
def reset_light(self):
self.image.fill((0, 0, 0))
class Detectors(pygame.sprite.Sprite):
def __init__(self, left, top, width, height, id=None, street=None, crossing_id=None):
super().__init__()
self.image = pygame.Surface([width, height], pygame.SRCALPHA)
self.image.fill((0, 0, 0, 0))
#pygame.draw.rect(self.image, "White", self.image.get_rect(), 1) # Zum Debuggen eine Umrandung
self.rect = self.image.get_rect(center=(left, top))
self.id = id
self.street = street
self.crossing_id = crossing_id