当汽车到达屏幕边缘时,如何卸下汽车?

发布于 2025-01-29 06:01:38 字数 1432 浏览 1 评论 0原文

我是Python的初学者,并试图通过建立Frogger克隆来学习。我使用类创建了汽车的图像,并将其循环以创建6辆汽车。我有一个问题,因为当它们到达屏幕边界时,如何将它们删除。

import random

import pygame

pygame.init()
screen = pygame.display.set_mode((600, 600))
# Dictionary of Car Images
cars = {
    1: pygame.image.load('images/police.png'),
    2: pygame.image.load('images/motorcycle.png'),
    3: pygame.image.load('images/sports1.png')
}
current_cars = []


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



    def move(self):
        self.x += 5
        screen.blit(cars.get(self.a), (self.x, self.y))

# Creates 6 cars
for i in range(6):
    a = random.randint(1,3)
    current_cars.append(Car(a,0, 100 + (100*i)))

start = pygame.time.get_ticks()
clock = pygame.time.Clock()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((0, 0, 0))
    now = pygame.time.get_ticks()
    # Creates new enemy after every 6 seconds.
    if now - start > 6000:
        start = now
        a = random.randint(1,3)
        b = random.randrange(1,501,100)
        object = Car(a, 0, 100 + b)
        current_cars.append(object)
       
        # moves all the cars in list
    for i in range(len(current_cars)):
        current_cars[i].move()

    clock.tick(11)
    pygame.display.update()

如何创建一个条件,以检查汽车的X位置何时到达边界并将其从当前汽车列表中删除。

I am a beginner in Python and trying to learn by building a Frogger Clone. I have created images of cars using a class, and looping them to create 6 cars. I have a problem in that how do i remove them when they reach the boundary of screen.

import random

import pygame

pygame.init()
screen = pygame.display.set_mode((600, 600))
# Dictionary of Car Images
cars = {
    1: pygame.image.load('images/police.png'),
    2: pygame.image.load('images/motorcycle.png'),
    3: pygame.image.load('images/sports1.png')
}
current_cars = []


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



    def move(self):
        self.x += 5
        screen.blit(cars.get(self.a), (self.x, self.y))

# Creates 6 cars
for i in range(6):
    a = random.randint(1,3)
    current_cars.append(Car(a,0, 100 + (100*i)))

start = pygame.time.get_ticks()
clock = pygame.time.Clock()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((0, 0, 0))
    now = pygame.time.get_ticks()
    # Creates new enemy after every 6 seconds.
    if now - start > 6000:
        start = now
        a = random.randint(1,3)
        b = random.randrange(1,501,100)
        object = Car(a, 0, 100 + b)
        current_cars.append(object)
       
        # moves all the cars in list
    for i in range(len(current_cars)):
        current_cars[i].move()

    clock.tick(11)
    pygame.display.update()

How do i create an if condition to check when x position of a car reaches the boundary and remove it from the list of current cars.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风念你 2025-02-05 06:01:38

当汽车超出范围时,您必须从列表中删除护理。使用 pygame.Rect.Rect.Colleidect.Colliderect屏幕上仍然有一部分汽车。

将汽车映像加载到car类的构造函数中,而不是在每个帧中,

class Car():
    def __init__(self, a, x, y):
        self.image = cars.get(a)
        self.x = x
        self.y = y

    def move(self):
        self.x += 5
        screen.blit(self.image, (self.x, self.y))

请参见在迭代时如何从列表中删除项目?。通过汽车清单的浅副本迭代,如果不在屏幕上的汽车列表中,将汽车从原始汽车列表中删除:

while running:
    # [...]

    for car in current_cars:     # iterate list of cars
        car.move()

    screen_rect = screen.get_rect()
    for car in current_cars[:]:  # iterate shallow copy of car list
        car_rect = car.image.get_rect(topleft = (car.x, car.y))
        if not screen_rect.collidrect(car_rect):
            current_cars.remove(car)

You have to remove the care from the list when the car goes out of bounds. Use pygame.Rect.colliderect to test if there is still a part of the car on the screen.

Load the car image in the constructor of the Car class instead of in each frame

class Car():
    def __init__(self, a, x, y):
        self.image = cars.get(a)
        self.x = x
        self.y = y

    def move(self):
        self.x += 5
        screen.blit(self.image, (self.x, self.y))

See How to remove items from a list while iterating?. Iterate through a shallow copy of the car list and remove the cars from the original car list if they are not on screen:

while running:
    # [...]

    for car in current_cars:     # iterate list of cars
        car.move()

    screen_rect = screen.get_rect()
    for car in current_cars[:]:  # iterate shallow copy of car list
        car_rect = car.image.get_rect(topleft = (car.x, car.y))
        if not screen_rect.collidrect(car_rect):
            current_cars.remove(car)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文