2
import pygame
from sys import exit
import random

WIDTH, HEIGHT = 1400,750
FPS = 60
HEADWAY_GAP = 40
vehicleColor = {0:"red",1:"blue",2:"yellow"}
directions = ["Right", "Left"]
classofVehicle = ["Car","Bike","Bus"]
coord = {"Right":(0,300), "Left":(1400,350)}
objects = {"Right":[],"Left":[]}

pygame.init()
myWindow = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
road = pygame.image.load("Required_images/Road/Road_final.png")

class Vehicle(pygame.sprite.Sprite):
  def __init__(self, ClassofVehicle, Color_Code, direction, pos):
    super().__init__()
    self.Class = ClassofVehicle
    self.Color_Code = Color_Code
    self.direction = direction
    self.pos = pos
    path = "Required_images/"+ClassofVehicle+"/"+ClassofVehicle+"_"+Color_Code+".png"
    self.image = pygame.image.load(path)
    self.rect = self.image.get_rect()
    if self.direction == "Right":
      self.rect.midright = pos
    else:
      self.rect.midleft = pos
    self.index = len(objects[direction])-1

  def movingVehicles(self):
    if self.direction == "Right":
      if self.index == 0 or (self.rect.x + self.rect.width < objects[self.direction][self.index].rect.x - HEADWAY_GAP):
        self.rect.x += 1
    elif self.direction == "Left":
      if self.index == 0 or (self.rect.x - self.rect.width > objects[self.direction][self.index].rect.x + HEADWAY_GAP):
        self.rect.x -= 1
  
  def update(self):
    self.movingVehicles()

Object_timer = pygame.USEREVENT+1
pygame.time.set_timer(Object_timer, 1000)
objectsGroup = pygame.sprite.Group()

def generatingObjects():
  Dir = random.choice(directions)
  po = coord[Dir]
  color_code = random.choice([0,1,2])
  cla = random.choice(classofVehicle)
  object = Vehicle(cla,vehicleColor[color_code],Dir,po)
  objectsGroup.add(object)
  objects[Dir].append(object)

while True:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit()
      exit()

    if event.type == Object_timer:
      generatingObjects()


myWindow.fill((0,0,0))
objectsGroup.update()
objectsGroup.draw(myWindow)
pygame.display.update()
clock.tick(FPS)

I have created a sprite class and a sprite group. I also created an user defined event to generate vehicles for every 1000ms. To display the vehicles on the screen instead of looping through all the elements of the sprite group separately I used group_name.draw() method. But when I run the code, some of the images are getting stuck on the screen for some time and are moving after sometime. I tried to look for any error in the logic but I couldn't find it. If you have any knowledge or able to find the error, please help and also any kind of suggestions are appreciated.

Satya Pamidi
  • 143
  • 8

2 Answers2

1

It is a matter of Indentation. You have to draw the scene in the application loop instead of after the application loop:

while True:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit()
      exit()

    if event.type == Object_timer:
      generatingObjects()

# INDENTATION
#->|

  myWindow.fill((0,0,0))
  objectsGroup.update()
  objectsGroup.draw(myWindow)
  pygame.display.update()
  clock.tick(FPS)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

make sure to indent the elements in your while loop and use a pygame.display.flip() at the very end of your loop.

so it looks like

while True:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit()
      exit()

    if event.type == Object_timer:
      generatingObjects()

    myWindow.fill((0,0,0))
    objectsGroup.update()
    objectsGroup.draw(myWindow)
    clock.tick(FPS)
    pygame.display.flip()
  • `pygame.display.update()` and `pygame.display.flip()` is the same. See [Difference between pygame.display.update and pygame.display.flipDifference between pygame.display.update and pygame.display.flip](https://stackoverflow.com/questions/29314987/difference-between-pygame-display-update-and-pygame-display-flip) – Rabbid76 Oct 28 '21 at 15:49
  • true but pygame.display.update() is more often used to update a specific surface –  Oct 28 '21 at 15:51
  • The problem is not `pygame.display.update()`. The problem is the [Indentation](https://docs.python.org/3/reference/lexical_analysis.html). The explanation in your answer is wrong. – Rabbid76 Oct 28 '21 at 15:53
  • sorry yes that was confusing of me –  Oct 28 '21 at 15:54
  • why do i need to include the display part it in for loop. for loop is to know external events like quit or key pressing and user events, right? I have to display the images externally inside the while but not in for loop. – Satya Pamidi Oct 28 '21 at 16:06