gpt4 book ai didi

python - 在游戏循环中处理不同对象的优雅方式?

转载 作者:行者123 更新时间:2023-12-01 08:35:24 26 4
gpt4 key购买 nike

我是一个半新手编程,我正在使用 python 和 pygame 开发游戏。基本思想是玩家在大图 block map 上走动并做一些事情。所以会有各种各样的对象在做事情。移动的 NPC、生长的草、生长的灌木、吃草的动物等。

我编写了游戏的模拟版本作为示例。

import pygame

class Plant():

def __init__(self,name,location,size,edible):
self.name = name
self.location = location
self.size = size
self.edible = edible

class Person():

def __init__(self,location,name):
self.location = location
self.name = name

class Game():

def __init__(self):
pygame.init()
self.mainScreen = pygame.display.set_mode(200,200)
self.objects = []
self.running = False # start stopped

def launch(self):
self.running=True
self.running_loop()

def running_loop(self):
while self.running:
self.mainScreen.fill((255,255,255))
# main game loop

for object in self.objects:

if type(object) == Person:
#do code related to persons
pass
elif type(object) == Plant:
# do code related to persons
pass

for object in self.objects:
pygame.draw(self.mainScreen,object)


# event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()


#create game instance
game = Game()

#create things in the game
person1 = Person((0,0),"Player")
plant1 = Plant("Bush",(1,1),2,False)
plant2 = Plant("Strawberries",(2,3),1,True)
game.objects.append(person1)
game.objects.append(plant1)
game.objects.append(plant2)


game.launch()

所以我的问题是关于这部分的:

def running_loop(self):
while self.running:

# main game loop

for object in self.objects:

if type(object) == Person:
#do code related to persons
pass
elif type(object) == Plant:
# do code related to persons
pass

for object in self.objects:
pygame.draw(self.mainScreen,object)

列表中会有很多不同的对象,包含游戏中的所有对象。但并非所有方法都具有相同的方法,因此使用了 if 类型(object)。我一直在想有一种更优雅的方法可以做到这一点。我可以进行多态性并只编写一个在每个实例中调用其他方法的更新方法吗?该解决方案的问题是我可能还需要导入 pygame 的 Rect 等类。由于这是可能变得非常困惑的项目之一,因此我试图从一开始就正确执行它。

最佳答案

在pygame中,通常建议继承pygame.sprite.Sprite并将实例放入 sprite groupsSprite 类有一个 update 方法,该方法应该被特定 sprite 对象的相关逻辑覆盖。您可以通过调用其 update 方法来更新 Sprite 组中的所有 Sprite ,该方法随后调用所有包含的 Sprite 的 update 方法,并且您还可以使用 绘制 Sprite >sprite_group.draw(display_surface)

还有订购和layered groups Sprite 是否应按预定顺序出现。

这是一个introduction to sprites and groups这是我的 pygame 模板之一:

import pygame as pg


class Player(pg.sprite.Sprite):

def __init__(self, pos, key_left, key_right, key_up, key_down):
super().__init__()
self.image = pg.Surface((30, 50))
self.image.fill(pg.Color('dodgerblue'))
self.rect = self.image.get_rect(topleft=pos)
self.vel = pg.math.Vector2(0, 0)
self.pos = pg.math.Vector2(self.rect.topleft)
self.speed = 5
self.key_left = key_left
self.key_right = key_right
self.key_up = key_up
self.key_down = key_down

def update(self):
self.pos += self.vel
self.rect.center = self.pos

def handle_event(self, event):
if event.type == pg.KEYDOWN:
if event.key == self.key_left:
self.vel.x = -self.speed
elif event.key == self.key_right:
self.vel.x = self.speed
elif event.key == self.key_up:
self.vel.y = -self.speed
elif event.key == self.key_down:
self.vel.y = self.speed
elif event.type == pg.KEYUP:
if event.key == self.key_left and self.vel.x < 0:
self.vel.x = 0
elif event.key == self.key_right and self.vel.x > 0:
self.vel.x = 0
elif event.key == self.key_up and self.vel.y < 0:
self.vel.y = 0
elif event.key == self.key_down and self.vel.y > 0:
self.vel.y = 0


class Game:

def __init__(self):
pg.init()
self.fps = 60
self.done = False
self.clock = pg.time.Clock()
self.screen = pg.display.set_mode((800, 600))
self.player1 = Player((100, 100), pg.K_a, pg.K_d, pg.K_w, pg.K_s)
self.player2 = Player((200, 200), pg.K_j, pg.K_l, pg.K_i, pg.K_k)
self.all_sprites = pg.sprite.Group(self.player1, self.player2)
self.background_color = pg.Color('gray12')

def run(self):
while not self.done:
self.dt = self.clock.tick(self.fps) / 1000
self.handle_events()
self.run_logic()
self.draw()

def handle_events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.done = True
for sprite in self.all_sprites:
sprite.handle_event(event)

def run_logic(self):
self.all_sprites.update()

def draw(self):
self.screen.fill(self.background_color)
self.all_sprites.draw(self.screen)
pg.display.flip()


if __name__ == '__main__':
Game().run()
pg.quit()

关于python - 在游戏循环中处理不同对象的优雅方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53753943/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com