I am currently working on a traffic simulator with Pygame, however i am struggling with implementing the logic of the traffic signals.
Any help is welcome.
My traffic simulation is grid based. The tiles in the grid are being drawn by looping trough a tile list.
self.map = [['/','/','/','/','s','n','/','/','/','/'],
['/','/','/','/','s','n','/','/','/','/'],
['/','/','/','/','s','n','/','/','/','/'],
['/','/','/','t1','s','n','t2','/','/','/'],
['w','w','w','w','c','c','w','w','w','w'],
['o','o','o','o','c','c','o','o','o','o'],
['/','/','/','t3','s','n','t4','/','/','/'],
['/','/','/','/','s','n','/','/','/','/'],
['/','/','/','/','s','n','/','/','/','/'],
['/','/','/','/','s','n','/','/','/','/']]
The characters t1, t2, t3 and t4 represent the traffic signals. In the code below i am looping trough the tile map.
def load_tiles(self):
self.tiles = pygame.sprite.Group()
self.trafficsignals = pygame.sprite.Group()
type = ['c', 'n', 'w', 's', 'o']
tsType = ['t1', 't2', 't3', 't4']
x, y = 0, 0
# iterate through the tile map list
for row in self.map:
x = 0
for tile in row:
if tile == '/':
self.tiles.add(Tile((200, 200, 200), x * self.tile_size, y * self.tile_size, self.tile_size))
elif tile in type: # add road tiles to tile sprite group
self.tiles.add(Tile((100, 100, 100), x * self.tile_size, y * self.tile_size, self.tile_size))
elif tile in tsType: # add trafficsignals to trafficsignal sprite group
self.tiles.add(Tile((200, 200, 200), x * self.tile_size, y * self.tile_size, self.tile_size))
self.trafficsignals.add(ts( x * self.tile_size, y * self.tile_size, tile, self.trafficsignals, self.screen))
# Move to next tile in current row
x += 1
# Move to next row
y += 1
return
This function is being called once in the init
function of my main
class since the tiles only need to be drawn once, however i am not sure if this is the right approach since the traffic signals are supposed to change color.
after drawing the sprite groups to my screen the image below is the result i get.
The black bars are where my traffic signals are supposed to go. These traffic signals are objects made using a separate class.
class Trafficsignal(pygame.sprite.Sprite):
def __init__(self, x, y, tile, signals, screen):
super().__init__()
self.screen = screen
self.red = pygame.image.load('images/signals/red.png').convert_alpha
self.yellow = pygame.image.load('images/signals/yellow.png').convert_alpha
self.green = pygame.image.load('images/signals/green.png').convert_alpha
self.noOfSignals = signals # number of traffic signals
self.currentRed = 0 # Indicates which signal is red currently
self.currentYellow = 0 # Indicates which signal is yellow currently
self.currentGreen = 0 # Indicates which signal is green currently
self.redTime = 100
self.yellowTime = 5
self.greenTime = 20
self.image = pygame.Surface([15, 42])
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# rotate trafficsignal bar to corresponding angle
if tile == 't1':
self.image = pygame.transform.rotate(self.image, 180)
if tile == 't2':
self.image = pygame.transform.rotate(self.image, 90)
if tile == 't3':
self.image = pygame.transform.rotate(self.image, -90)
def update(self):
pass
I am currently stuck on the logic of how a traffic signal changes color. I want the traffic signals to change color in a clockwise direction. Only one traffic signal is allowed to be green. I already made some variables like currentGreen
to determine which color a traffic signal currently is, but i am not really sure on how to implement this logic since i'm still pretty new to game development.