diff --git a/imgs/rock.png b/imgs/rock.png new file mode 100644 index 0000000..24346b7 Binary files /dev/null and b/imgs/rock.png differ diff --git a/pydrivingsim/__init__.py b/pydrivingsim/__init__.py index cc5509a..58726ad 100644 --- a/pydrivingsim/__init__.py +++ b/pydrivingsim/__init__.py @@ -13,5 +13,7 @@ from pydrivingsim.suggestedspeedsignal import SuggestedSpeedSignal from pydrivingsim.graphicobject import GraphicObject +from pydrivingsim.rock import Rock + # The Agent from pydrivingsim.agent import Agent \ No newline at end of file diff --git a/pydrivingsim/rock.py b/pydrivingsim/rock.py new file mode 100644 index 0000000..d47eeac --- /dev/null +++ b/pydrivingsim/rock.py @@ -0,0 +1,83 @@ +import pygame +import random + +from pydrivingsim import VirtualObject, World + +class RockSprite(pygame.sprite.Sprite): + def __init__(self, rock_obstacle): + super().__init__() + img = "imgs/rock.png" + self.image_fix = [] + self.sprite = pygame.image.load(img).convert_alpha() + w, h = self.sprite.get_size() + scale_x = (World().scaling_factor * rock_obstacle.len) / w + scale_y = (World().scaling_factor * rock_obstacle.width) / h + + new_w = max(1, int(w * scale_x)) + new_h = max(1, int(h * scale_y)) + + self.image_fix.append( + pygame.transform.smoothscale(self.sprite, (new_w, new_h)) + ) + self.image = self.image_fix[0] + self.rect = self.image_fix[0].get_rect() + self.size = self.image_fix[0].get_size() + self.rock_obstacle = rock_obstacle + + def resize(self, len, width): + w, h = self.sprite.get_size() + s = World().scaling_factor + + scale_x = (s * len) / w + scale_y = (s * width) / h + + new_w = max(1, int(w * scale_x)) + new_h = max(1, int(h * scale_y)) + + scaled = pygame.transform.smoothscale(self.sprite, (new_w, new_h)) + + if self.image_fix: + self.image_fix[0] = scaled + else: + self.image_fix.append(scaled) + + self.image = self.image_fix[0] + self.rect = self.image.get_rect() + self.size = self.image.get_size() + + def update(self) -> None: + self.rect.center = [ + (self.rock_obstacle.pos[0] - World().get_world_pos()[0]) * World().scaling_factor + World().screen_world_center[0], + (World().get_world_pos()[1] - self.rock_obstacle.pos[1]) * World().scaling_factor + World().screen_world_center[1] + ] + self.image = self.image_fix[self.rock_obstacle.state] + +class Rock(VirtualObject): + __metadata = { + "dt": 0.1 + } + def __init__( self ): + super().__init__(self.__metadata["dt"]) + # Sprite + self.size = 1 # 0.5 + self.state = 0 + self.pos = (0,0) + self.len = 1.0 + self.width = 1.0 + self.rock = RockSprite(self) + self.group = pygame.sprite.Group() + self.group.add(self.rock) + self.reset() + + def set_pos_size(self, point, len: float, width: float): + self.pos = point + self.len = len + self.width = width + self.rock.resize(len, width) + + def reset(self): + self.state = 0 + + def render( self ): + self.rock.update() + self.group.draw(World().screen) \ No newline at end of file diff --git a/scenarios/__init__.py b/scenarios/__init__.py index 2520440..9257b39 100644 --- a/scenarios/__init__.py +++ b/scenarios/__init__.py @@ -1,2 +1,2 @@ # The basic scenarios -from scenarios.main_scenarios import AutonomousVehicle, OnlyVehicle, BasicSpeedLimit, BasicTrafficLight, GetTheCoins \ No newline at end of file +from scenarios.main_scenarios import AutonomousVehicle, OnlyVehicle, BasicSpeedLimit, Scenario_BasicTL, GetTheCoins, ObstacleRocks \ No newline at end of file diff --git a/scenarios/main_scenarios.py b/scenarios/main_scenarios.py index 0c615ae..f429360 100644 --- a/scenarios/main_scenarios.py +++ b/scenarios/main_scenarios.py @@ -1,5 +1,5 @@ -from pydrivingsim import TrafficLight, Target, TrafficCone, SuggestedSpeedSignal, GraphicObject, Vehicle, Agent, Coin +from pydrivingsim import TrafficLight, Target, TrafficCone, SuggestedSpeedSignal, GraphicObject, Vehicle, Agent, Coin, Rock class OnlyVehicle(): def __init__(self): @@ -46,15 +46,24 @@ def terminate(self): self.agent.terminate() -class BasicTrafficLight(): +class Scenario_BasicTL(): def __init__(self): + + # draw the cones cone = TrafficCone() cone.set_pos((1.0,0)) cone = TrafficCone() cone.set_pos((1.0,2)) cone = TrafficCone() cone.set_pos((1.0,-2)) - + + # draw the rocks + rock = Rock() + rock.set_pos_size((1, -5), 2.0, 2.0) + rock = Rock() + rock.set_pos_size((1, 5), 1.0, 1.0) + + # set pos of the TL trafficlight = TrafficLight() trafficlight.set_pos((160,-3)) trafficlight.reset() @@ -93,4 +102,11 @@ def __init__(self): signal = SuggestedSpeedSignal(90) signal.set_pos((96, 4)) super = GraphicObject("imgs/pictures/superstrada.png", 5) - super.set_pos((100,6)) \ No newline at end of file + super.set_pos((100,6)) + +class ObstacleRocks(): + def __init__(self): + rock = Rock() + rock.set_pos_size((20, -2), 2.0, 2.0) + rock = Rock() + rock.set_pos_size((45, 2), 3.0, 4.0) \ No newline at end of file diff --git a/simulator.py b/simulator.py index c696cf7..03d2a65 100644 --- a/simulator.py +++ b/simulator.py @@ -5,7 +5,7 @@ import signal from pydrivingsim import World -from scenarios import BasicSpeedLimit, BasicTrafficLight, OnlyVehicle, AutonomousVehicle, GetTheCoins +from scenarios import BasicSpeedLimit, Scenario_BasicTL, OnlyVehicle, AutonomousVehicle, GetTheCoins class GracefulKiller: kill_now = False @@ -20,7 +20,7 @@ def main(): # Enable this to test only single vehicle #av = OnlyVehicle() av = AutonomousVehicle() - BasicTrafficLight() + Scenario_BasicTL() # Enable this to test the coins #GetTheCoins() # Enable this to test the speed limit