如何在python3中使用事件自定义地图?

发布于 2025-01-20 05:36:55 字数 1383 浏览 0 评论 0原文

我无法设置更改平台游戏中绘制地图的块包。从理论上讲,紧迫数字应该起作用。所有文件都存储在具有适当层次结构的文件夹中。

写它的最佳格式是什么?方法,课? I do not want to prescribe conditions for 100+ options

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_1:
            block_pack = 'dirt'
        elif event.key == pygame.K_2:
            block_pack = 'grass'
        elif event.key == pygame.K_3:
            block_pack = 'planet'
        elif event.key == pygame.K_4:
            block_pack = 'sand'
        elif event.key == pygame.K_5:
            block_pack = 'snow'
        elif event.key == pygame.K_6:
            block_pack = 'stone'
        elif event.key == pygame.K_7:
            block_pack = 'enemies'
        elif event.key == pygame.K_8:
            block_pack = 'tiles'
        elif event.key == pygame.K_9:
            block_pack = 'items'
        elif event.key == pygame.K_0:
            block_pack = 'bg'
block_img = pygame.image.load(f'PNG/Ground/{block_pack}/{block_pack}.png')
blockLeft_img = pygame.image.load(f'PNG/Ground/{block_pack}/{block_pack}Left.png')
blockRight_img = pygame.image.load(f'PNG/Ground/{block_pack}/{block_pack}Right.png')
blockMid_img = pygame.image.load(f'PNG/Ground/{block_pack}/{block_pack}Mid.png')

hierarchy

I can't set up changing the pack of blocks for drawing maps in the platformer. In theory, pressing numbers should work. All files are stored in a folder with the appropriate hierarchy.

what is the best format to write it in? method, class? I do not want to prescribe conditions for 100+ options

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_1:
            block_pack = 'dirt'
        elif event.key == pygame.K_2:
            block_pack = 'grass'
        elif event.key == pygame.K_3:
            block_pack = 'planet'
        elif event.key == pygame.K_4:
            block_pack = 'sand'
        elif event.key == pygame.K_5:
            block_pack = 'snow'
        elif event.key == pygame.K_6:
            block_pack = 'stone'
        elif event.key == pygame.K_7:
            block_pack = 'enemies'
        elif event.key == pygame.K_8:
            block_pack = 'tiles'
        elif event.key == pygame.K_9:
            block_pack = 'items'
        elif event.key == pygame.K_0:
            block_pack = 'bg'
block_img = pygame.image.load(f'PNG/Ground/{block_pack}/{block_pack}.png')
blockLeft_img = pygame.image.load(f'PNG/Ground/{block_pack}/{block_pack}Left.png')
blockRight_img = pygame.image.load(f'PNG/Ground/{block_pack}/{block_pack}Right.png')
blockMid_img = pygame.image.load(f'PNG/Ground/{block_pack}/{block_pack}Mid.png')

hierarchy

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

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

发布评论

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

评论(1

因为看清所以看轻 2025-01-27 05:36:55

是的,dict将是您在这里最好的选择。我知道您有很多选择(100多个),但是您将不得不以某种方式映射。 dicts快速易于访问。以下代码将是一个选项:

keypress_options = {
   pygame.K_1: 'dirt',
   pygame.K_2: 'grass',
   pygame.K_3: 'planet',
   pygame.K_4: 'sand',
   pygame.K_5: 'snow',
   pygame.K_6: 'stone',
   pygame.K_7: 'enemies',
   pygame.K_8: 'tiles',
   pygame.K_9: 'items',
   pygame.K_0: 'bg'
}

然后您可以使用get访问它,以找出该键是否映射到任何内容

if option := keypress_options.get(event.key):
   do this

Yeah, a dict would be your best bet here. I know you have a lot of options (100+), but you're going to have to map it in some way. Dicts are fast and easy to access. The following code would be an option:

keypress_options = {
   pygame.K_1: 'dirt',
   pygame.K_2: 'grass',
   pygame.K_3: 'planet',
   pygame.K_4: 'sand',
   pygame.K_5: 'snow',
   pygame.K_6: 'stone',
   pygame.K_7: 'enemies',
   pygame.K_8: 'tiles',
   pygame.K_9: 'items',
   pygame.K_0: 'bg'
}

and then you can access it using get to find out if that keypress is mapped to anything

if option := keypress_options.get(event.key):
   do this
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文