Pygame window not responding error when i try to run a code

hippozhipos
import pygame
import random

black  = (0,0,0)
white = (255,255,255)
green = (0,255,0)
red = (255,0,0)

screen = pygame.display.set_mode((1200, 600))
title = pygame.display.set_caption("Speeding cars")
clock = pygame.time.Clock()
clock.tick(60)

class Car:
    def __init__(self, x, y, height, width):
        self.x = x
        self.y = y
        self.height = height
        self.width = width

    def drive(self, change):
        self.change = random.randint(1, 2)
        self.x += self.change
        pygame.draw.rect(screen, black, (self.x, self.y, self.height, self.width))

    def again(self):
        while True:
            if self.x + 50 > 1200:
                for car in cars:
                    car.drive(False)

cars = []

for i in range(1, 6):
    cars.append(Car(0, i*100, 50, 25))

driving = True

while driving:

    screen.fill(white)

    for car in cars:
        car.drive(False)
        car.again()


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

    pygame.display.flip()

Whenever i try to run this code, my pygame window stops responding. Something i have noticed is whenever i delete def again(self): block of code, my winodw works fine, however i want the blocks to come back again after they go off the screen. Thank you.

Rabbid76

You've implemented an endless loop in again:

def again(self):
   while True:
       if self.x + 50 > 1200:
           for car in cars:
               car.drive(False)

Note, again is called in the main application loop and it is called for each car:

while driving:
   # [...]

   for car in cars:
       # [...]
       car.again()

If you want to set all the cars at its initial position, when 1 car has reached the end of the track, then you've to verify if any car has reached the end of the track and to est the .x coordinate of all cars 0:

finished = [car for car in cars if car.x + 50 > 1200]
if len(finished) > 0:
    for car in cars:
        car.x = 0 

or a bit shorter

if any([car for car in cars if car.x + 50 > 1200]):
    for car in cars:
        car.x = 0 

Do the evaluation in the main application loop:

driving = True
while driving:

    screen.fill(white)

    for car in cars:
        car.drive(False)

    if any([car for car in cars if car.x + 50 > 1200]):
        for car in cars:
            car.x = 0 

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

    pygame.display.flip()

If you want to keep the individual position of each car, then just set a single car at the start, when it reaches the end of the track (car.x -= 1200):

class Car:
    # [...]

    def again(self):
        if self.x + 50 > 1200:
            self.x -= 1200 
driving = True
while driving:

    screen.fill(white)

    for car in cars:
        car.drive(False)
        car.again()

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

    pygame.display.flip()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I have this error with pygame , when i try to run code from Python Crash Course (Exception has occurred: AttributeError )

When I try to run my code with the "java" command in the cmd window, I get an error

error when i try to run app on emulator android studio

What is happening here? When I try to run this code, the following error appears

Pygame Python window stops responding

Pygame: window not responding

I keep receiving an attribute error in Pygame when attempting to create a window

When I try pygame, window closes when I open it

firebase show error when i try to run functions locally

express error when try to run the server in visual studio code

When I run the code chunks everything works but when I try to Knit I get an error

LabVIEW not responding when I run this code. C++ code issue or LabVIEW issue?

pygame window not responding when clicked

gettting this error when i try to run eclipse in ubuntu please help

Error Twig when I try to run Symfony 3 project on server

Error on jest when I try to run the test

pygame window keeps not responding

Segmentation fault error when I try to run this program

When I run my Python Turtle game, the Graphics Window stops responding

CSS file not found error 404 when I try to run flask code

Lua: I get error when I try to run a script in Windows

I receive this error 'str' object has no attribute 'text' when I try to run the following code

When I try to run this code I am getting time limit exceeded error and it's taking 1.01 ms to execute

UnhandledPromiseRejectionWarning: Error: Cannot find module '../database/db' when i try to run my code

Pygame window not responding when I try to exit the window in the middle of execution or when I spam click on the window

Python Not Responding when I Run it

Pygame is opening a not responding window

When I try to run code in cmd, it opens vs code

Error coming every time I try to run my code