嗨,我正在制作一款类似于复古游戏“猎鸭”的游戏,但飞机飞过屏幕。最初我开始使用函数进行编码,主要是因为它更容易,但我想将其转换为一个类,以便更容易实例化飞机等。但我对如何做到这一点感到困惑?我已经制作了两个函数来绘制和更新作为函数工作的平面,但我不确定如何将其转换为类/OOP 代码?之前的代码(此处显示的代码)使飞机在屏幕上生成,并以不同的 y 坐标从左到右、从右到左等随机飞行(一次在屏幕上显示 3 个)。
current_planes = []
def spawn_plane():
# chooses a random y position for the plane to spawn
plane_limit = 37
plane_deck = 250
plane_y = random.randint(plane_limit, plane_deck)
direction = random.choice(["left", "right"])
# decides randomly whether the plane will start on the left/right
if direction == "right":
plane_x = -103
velocity = plane_vel
flipped_plane = pygame.transform.flip(plane, True, False)
else:
plane_x = screen_width
velocity = -plane_vel
flipped_plane = pygame.transform.flip(plane, False, False)
# add plane's data to current_planes list
current_planes.append({
"x": plane_x,
"y": plane_y,
"velocity": velocity,
"image": flipped_plane,
})
# moves planes and deletes when leave screen
def update_planes():
global current_planes
for plane in current_planes[:]:
plane["x"] += plane["velocity"]
# remove planes
if plane["x"] > screen_width or plane["x"] < -103:
current_planes.remove(plane)
# draw planes
for plane in current_planes:
screen.blit(plane["image"], (plane["x"], plane["y"]))
hitbox_plane = (plane["x"] + 5, plane["y"] + 2, 103, 37)
pygame.draw.rect(screen, ("green"), hitbox_plane, 2)
# (in the main game loop)
# spawn planes every 60 frames
if frame_count % 60 == 0:
spawn_plane()
# draws and updates screen
draw_main_game()
update_planes()
pygame.display.update()
clock.tick(60)
frame_count += 1
我试图将此代码复制到一个类中,没想到它会起作用(它没有),但我只是不太理解类?我不确定我打算将什么作为参数等以及如何在游戏循环中调用它。这段代码->
class planes:
def __init__(self, plane_x, plane_y, width, height, plane):
self.plane_x = plane_x
self.plane_y = plane_y
self.width = width
self.height = height
self.plane = plane
self.vel = \[0,0\]
# func to spawn new plane
def spawn_plane():
# chooses a random y position for the plane to spawn
plane_limit = 37
plane_deck = 250
plane_y = random.randint(plane_limit, plane_deck)
direction = random.choice(["left", "right"])
# decides randomly whether the plane will start on the left/right
if direction == "right":
plane_x = -103
velocity = plane_vel
flipped_plane = pygame.transform.flip(plane, True, False)
else:
plane_x = screen_width
velocity = -plane_vel
flipped_plane = pygame.transform.flip(plane, False, False)
# add plane's data to current_planes list
current_planes.append({
"x": plane_x,
"y": plane_y,
"velocity": velocity,
"image": flipped_plane,
})
# moves planes and deletes when leave screen
def update_planes():
global current_planes
for plane in current_planes[:]:
plane["x"] += plane["velocity"]
# remove planes
if plane["x"] > screen_width or plane["x"] < -103:
current_planes.remove(plane)
# draw planes
for plane in current_planes:
screen.blit(plane["image"], (plane["x"], plane["y"]))
hitbox_plane = (plane["x"] + 5, plane["y"] + 2, 103, 37)
pygame.draw.rect(screen, ("green"), hitbox_plane, 2)`
任何帮助将不胜感激。 (我已经浓缩了我的程序代码以显示与我的问题相关的内容,但如果它没有意义,请告诉我)干杯
Plane
或 Planes
试图理解你的代码,我在“一架飞机”和“所有飞机”之间遇到了混淆。要非常小心,不要让你的头脑感到困惑。您可以使用一个类
Plane
来描述一架飞机,然后使用 list
中的 Plane
来表示您的飞机集合,或者使用第二个类 PlaneCollection
来表示您的飞机集合
现在,在您的第一次尝试中,您有一个名为
planes
的类,其中 s
就好像它是复数一样,并且您似乎不清楚此类的对象是否代表一个平面,或者代表哪个平面。整个飞机集合。
在您的第一个代码中,在尝试引入类之前,飞机由具有四个字段的字典表示:
{"x": plane_x,"y": plane_y,"velocity": velocity,"image": flipped_plane,}
。如果这对你有用,我建议创建一个只有四个字段的类 Plane
,x
、y
、velocity
、image
。
在类内部定义的函数将成为该类对象的方法。这样的函数应该始终使用代表该对象的名为
self
的第一个参数来定义。如果您想要在类内部定义一个函数,但可以在没有类的特定对象的情况下调用该函数,那么这是一个“类方法”,您可以通过在其定义之前编写 @classmethod
来声明它.
例如:
update
,这样当你有一个平面myplane
时,你可以调用myplane.update()
来更新这个平面。random
,它返回一个新的随机 Plane
对象。考虑到所有这些,你的课程可能看起来像这样:
class Plane:
def __init__(self, x, y, velocity, image):
self.x = x
self.y = y
self.velocity = velocity
self.image = image
@classmethod
def random(cls):
# chooses a random y position for the plane to spawn
plane_limit = 37
plane_deck = 250
plane_y = random.randint(plane_limit, plane_deck)
direction = random.choice(["left", "right"])
# decides randomly whether the plane will start on the left/right
if direction == "right":
velocity = ...
image = ...
else:
velocity = ...
image = ...
return Plane(plane_x, plane_y, velocity, image)
def update(self):
self.x += self.velocity
class PlaneCollection:
def __init__(self, height, width):
self.height = height
self.width = width
self.planes = []
def update(self):
for plane in self.planes:
plane.update()
self.planes = [plane for plane in self.planes if 0 <= plane.x < width]
def draw(self)
for plane in self.planes:
screen.blit(...)
...
...