我目前正在开发一个pygame游戏,我的朋友问我是否可以实现一个konami代码。 我认为这是一个伟大的想法,所以我实施了它
我做这件事的方式肯定会奏效,因为我不必处理任何错误。 然而代码比预期的要大一点,所以我想知道如何使它变小并且仍然工作,因为我不知道有什么好的方法使它变小
守则:
konami_code = ['', '', '', '', '', '', '', '', '', '']
key = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.KEYUP:
if key[pygame.K_UP]:
if konami_code[0] == '':
konami_code[0] = 'UP'
elif konami_code[0] == 'UP' \
and konami_code[1] == '':
konami_code[1] = 'UP'
else:
konami_code = ['', '', '', '', '', '', '', '', '', '']
if key[pygame.K_DOWN]:
if konami_code[0] == 'UP' \
and konami_code[1] == 'UP' \
and konami_code[2] == '':
konami_code[2] = 'DOWN'
elif konami_code[0] == 'UP' \
and konami_code[1] == 'UP' \
and konami_code[2] == 'DOWN' \
and konami_code[3] == '':
konami_code[3] = 'DOWN'
else:
konami_code = ['', '', '', '', '', '', '', '', '', '']
if key[pygame.K_LEFT]:
if konami_code[0] == 'UP' \
and konami_code[1] == 'UP' \
and konami_code[2] == 'DOWN' \
and konami_code[3] == 'DOWN' \
and konami_code[4] == '':
konami_code[4] = 'LEFT'
elif konami_code[0] == 'UP' \
and konami_code[1] == 'UP' \
and konami_code[2] == 'DOWN' \
and konami_code[3] == 'DOWN' \
and konami_code[4] == 'LEFT' \
and konami_code[5] == 'RIGHT' \
and konami_code[6] == '':
konami_code[6] = 'LEFT'
else:
konami_code = ['', '', '', '', '', '', '', '', '', '']
if key[pygame.K_RIGHT]:
if konami_code[0] == 'UP' \
and konami_code[1] == 'UP' \
and konami_code[2] == 'DOWN' \
and konami_code[3] == 'DOWN' \
and konami_code[4] == 'LEFT' \
and konami_code[5] == '':
konami_code[5] = 'RIGHT'
elif konami_code[0] == 'UP' \
and konami_code[1] == 'UP' \
and konami_code[2] == 'DOWN' \
and konami_code[3] == 'DOWN' \
and konami_code[4] == 'LEFT' \
and konami_code[5] == 'RIGHT' \
and konami_code[6] == 'LEFT' \
and konami_code[7] == '':
konami_code[7] = 'RIGHT'
else:
konami_code = ['', '', '', '', '', '', '', '', '', '']
if key[pygame.K_b]:
if konami_code[0] == 'UP' \
and konami_code[1] == 'UP' \
and konami_code[2] == 'DOWN' \
and konami_code[3] == 'DOWN' \
and konami_code[4] == 'LEFT' \
and konami_code[5] == 'RIGHT' \
and konami_code[6] == 'LEFT' \
and konami_code[7] == 'RIGHT' \
and konami_code[8] == '':
konami_code[8] = 'B'
else:
konami_code = ['', '', '', '', '', '', '', '', '', '']
if key[pygame.K_a]:
if konami_code[0] == 'UP' \
and konami_code[1] == 'UP' \
and konami_code[2] == 'DOWN' \
and konami_code[3] == 'DOWN' \
and konami_code[4] == 'LEFT' \
and konami_code[5] == 'RIGHT' \
and konami_code[6] == 'LEFT' \
and konami_code[7] == 'RIGHT' \
and konami_code[8] == 'B' \
and konami_code[9] == '':
konami_code[9] = 'A'
else:
konami_code = ['', '', '', '', '', '', '', '', '', '']
if konami_code[0] == 'UP' \
and konami_code[1] == 'UP' \
and konami_code[2] == 'DOWN' \
and konami_code[3] == 'DOWN' \
and konami_code[4] == 'LEFT' \
and konami_code[5] == 'RIGHT' \
and konami_code[6] == 'LEFT' \
and konami_code[7] == 'RIGHT' \
and konami_code[8] == 'B' \
and konami_code[9] == 'A':
print('code completed')
您可以将konami代码存储为值列表。然后将用户输入与该列表进行比较:
我认为在这里问这个问题并不合适(也许代码审查是一个更好的地方),但我对您的代码做了一些改进:
最重要的是,现在你可以在序列列表中输入你想要的任何序列,它将匹配用户输入,不管列表有多长。除了一些调整的情况下,一个长长的名单可以与字体大小(自动化的过程),也许一些时间可以添加
下面是一个更短的选项:
相关问题 更多 >
编程相关推荐