python - 当我尝试运行代码时,Pygame 窗口没有响应错误

标签 python pygame

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()

每当我尝试运行此代码时,我的 pygame 窗口就会停止响应。我注意到,每当我删除 def Again(self): 代码块时,我的 winodw 工作正常,但是我希望这些 block 在离开屏幕后再次返回。谢谢。

最佳答案

再次实现了无限循环:

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

注意,再次在主应用程序循环中被调用,并且为每辆车调用:

while driving:
   # [...]

   for car in cars:
       # [...]
       car.again()
<小时/>

如果您想将所有汽车设置在其初始位置,当 1 辆汽车到达轨道末端时,您必须验证是否有汽车到达轨道末端并估计 .x所有汽车的坐标0:

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

或者短一点

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

在主应用程序循环中进行评估:

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()
<小时/>

如果您想保留每辆车的单独位置,则只需在起点设置一辆车,当它到达赛道终点时(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()

关于python - 当我尝试运行代码时,Pygame 窗口没有响应错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59225132/

相关文章:

python - 音乐Y或音乐N

python - pandas_datareader,导入错误 : cannot import name 'urlencode'

python - Selenium 打开 Web 浏览器,但不打开目标 url (InvalidArgumentException : Message: invalid argument)

python - Django 不需要 ContentType

python - 是否可以在浏览器中运行 pygame 或 pyglet ?

python - 有没有一种方法可以更快地渲染点 OpenGL

python - Pygame 按住键会导致无限循环

python - 覆盖默认的用户模型方法

python - 将 bool numpy 数组的行编码为字节

python - pygame 跳过更新屏幕