Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added imgs/rock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions pydrivingsim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
83 changes: 83 additions & 0 deletions pydrivingsim/rock.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion scenarios/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# The basic scenarios
from scenarios.main_scenarios import AutonomousVehicle, OnlyVehicle, BasicSpeedLimit, BasicTrafficLight, GetTheCoins
from scenarios.main_scenarios import AutonomousVehicle, OnlyVehicle, BasicSpeedLimit, Scenario_BasicTL, GetTheCoins, ObstacleRocks
24 changes: 20 additions & 4 deletions scenarios/main_scenarios.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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))
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)
4 changes: 2 additions & 2 deletions simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down