如何在汽车到达屏幕边缘时将其移走?
How do I remove a car when it reaches the edge of screen?
我是 Python 的初学者,正在尝试通过构建 Frogger Clone 来学习。我使用 class 创建了汽车图像,并循环创建了 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()
我如何创建一个 if 条件来检查汽车的 x 位置何时到达边界并将其从当前汽车列表中删除。
当汽车出界时,您必须从列表中删除护理。使用pygame.Rect.colliderect
测试屏幕上是否还有汽车的一部分。
在 Car
class 的构造函数中加载汽车图像,而不是在每个帧中加载
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))
参见 How to remove items from a list while iterating?。遍历汽车列表的浅表副本,如果汽车不在屏幕上,则将其从原始汽车列表中移除:
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)
我是 Python 的初学者,正在尝试通过构建 Frogger Clone 来学习。我使用 class 创建了汽车图像,并循环创建了 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()
我如何创建一个 if 条件来检查汽车的 x 位置何时到达边界并将其从当前汽车列表中删除。
当汽车出界时,您必须从列表中删除护理。使用pygame.Rect.colliderect
测试屏幕上是否还有汽车的一部分。
在 Car
class 的构造函数中加载汽车图像,而不是在每个帧中加载
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))
参见 How to remove items from a list while iterating?。遍历汽车列表的浅表副本,如果汽车不在屏幕上,则将其从原始汽车列表中移除:
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)