当汽车到达屏幕边缘时,如何卸下汽车?
我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当汽车超出范围时,您必须从列表中删除护理。使用
pygame.Rect.Rect.Colleidect.Colliderect
屏幕上仍然有一部分汽车。将汽车映像加载到
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 frameSee 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: