如何在python3中使用事件自定义地图?
我无法设置更改平台游戏中绘制地图的块包。从理论上讲,紧迫数字应该起作用。所有文件都存储在具有适当层次结构的文件夹中。
写它的最佳格式是什么?方法,课? 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')
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')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,
dict
将是您在这里最好的选择。我知道您有很多选择(100多个),但是您将不得不以某种方式映射。 dicts快速易于访问。以下代码将是一个选项:然后您可以使用
get
访问它,以找出该键是否映射到任何内容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:and then you can access it using
get
to find out if that keypress is mapped to anything