0

I made a an intersection simulation where there is Vehicle and a rectangle in the middle to detect when the vehicle touch the rectangle.

The "detector" or rectangle is defined as follows:

    DetL = pygame.Rect(565,300,250,250)

The Vehicle class is defined as follows:

class Vehicle(pygame.sprite.Sprite):
def __init__(self, lane, vehicleClass, direction_number, direction,):
    pygame.sprite.Sprite.__init__(self)
    self.lane = lane
    self.vehicleClass = vehicleClass
    self.speed = speeds[vehicleClass]
    self.direction_number = direction_number
    self.direction = direction
    self.x = x[direction][lane]
    self.y = y[direction][lane]
    self.crossed = 0
    vehicles[direction][lane].append(self)
    self.index = len(vehicles[direction][lane]) - 1
    path = "images/" + direction + "/" + vehicleClass + ".png"
    self.image = pygame.image.load(path)
    #I intend on drawing a rectangle on every vehicle for collision detection purposes,
    #No error came up in line 153, but when i call Vehicle.rect on line 236 it says Vehicle 
    #has no attribute rect
    self.rect = self.image.get_rect()
    if(len(vehicles[direction][lane])>1 
    and vehicles[direction][lane][self.index-1].crossed==0): 
        if(direction=='right'):
            self.stop = vehicles[direction][lane][self.index-1].stop 
            - vehicles[direction][lane][self.index-1].image.get_rect().width 
            - stoppingGap     
               
        elif(direction=='left'):
            self.stop = vehicles[direction][lane][self.index-1].stop 
            + vehicles[direction][lane][self.index-1].image.get_rect().width 
            + stoppingGap
            
        elif(direction=='down'):
            self.stop = vehicles[direction][lane][self.index-1].stop 
            - vehicles[direction][lane][self.index-1].image.get_rect().height 
            - stoppingGap
            
        elif(direction=='up'):
            self.stop = vehicles[direction][lane][self.index-1].stop 
            + vehicles[direction][lane][self.index-1].image.get_rect().height 
            + stoppingGap
            
    else:
        self.stop = defaultStop[direction]
        
    if(direction=='right'):
        temp = self.image.get_rect().width + stoppingGap    
        x[direction][lane] -= temp
    elif(direction=='left'):
        temp = self.image.get_rect().width + stoppingGap
        x[direction][lane] += temp
    elif(direction=='down'):
        temp = self.image.get_rect().height + stoppingGap
        y[direction][lane] -= temp
    elif(direction=='up'):
        temp = self.image.get_rect().height + stoppingGap
        y[direction][lane] += temp
    simulation.add(self)

While the "Detector" function is defined as follows:

 def detection():
  if DetL.colliderect(Vehicle.rect):
    print("test")

The detector function then is called just before the end of my code:

   detection()
    pygame.draw.rect(screen, (255,255,255),DetL)
    pygame.draw.rect(screen, (255,255,255), Vehicle.rect)
    pygame.display.update()
Main()

I'm at a loss here. I have tried replacing if DetL.colliderect(Vehicle.rect) with if DetL.colliderect(Vehicle) it won't work. i also tried if DetL.colliderect(Vehicle.image) because i figured that self.rect places rectangle in self.image. But it seems that i'm wrong there.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • You may want to use Vehicle().rect (to get an instance of Vehicle) instead of Vehicle.rect. – Jobo Fernandez Aug 14 '22 at 10:50
  • 2
    This is not a pygame problem. Read about [Classes](https://docs.python.org/3/tutorial/classes.html). You have to create an [Instance Objects](https://docs.python.org/3/tutorial/classes.html#instance-objects) of the `Vehicle`. – Rabbid76 Aug 14 '22 at 10:51
  • you need to make an instance of the vehicle class and then use that when drawing or checking for collision. example: `car=Vehicle()` – nigh_anxiety Aug 14 '22 at 10:56
  • `Car= Vehicle` just makes `Car` another reference to the class definition. You need an instance of the class, `Car = Vehicle()` – nigh_anxiety Aug 14 '22 at 11:06
  • @nigh_anxiety i tried doing that: car = Vehicle() def detection(): if DetL.colliderect(Car): print("test") but then this came up: Vehicle.__init__() missing 4 required positional arguments: 'lane', 'vehicleClass', 'direction_number', and 'direction' i tried fillling those positional argument : Car = Vehicle(Vehicle.lane,Vehicle.vehicleClass,Vehicle.direction_number,Vehicle.direction) but then this came up: type object 'Vehicle' has no attribute 'lane' – GeorgiaMyBeloved Aug 14 '22 at 11:12
  • @nigh_anxiety i'm sorry that was a typo. i dont know how to quote a piece of code in stack overflow comment so i typed it by hand – GeorgiaMyBeloved Aug 14 '22 at 11:13
  • Use the back tick \` character to quote code. comments don't allow formatting though. I guess I should have included before, when you call `Car = Vehicle()`, you also need to include any parameter in the \_\_init\_\_ function other than self that doesn't have a default value, so it would be `Car = Vehicle(lane, vehicleClass, direction_number, direction), except you need to pass in values, either literal values (ex: direction= 'right') or populated variables. It looks like vehicleClass is enumerated elsewhere. if you borrowed this class from somewhere you may be missing other needed code – nigh_anxiety Aug 14 '22 at 14:02

0 Answers0