Python 如何使pygame konami代码更简单?
我目前正在开发一个pygame游戏,我的朋友问我是否可以实现一个konami代码。 我认为这是一个伟大的想法,所以我实施了它 我做这件事的方式肯定会奏效,因为我不必处理任何错误。 然而代码比预期的要大一点,所以我想知道如何使它变小并且仍然工作,因为我不知道有什么好的方法使它变小 守则:Python 如何使pygame konami代码更简单?,python,list,Python,List,我目前正在开发一个pygame游戏,我的朋友问我是否可以实现一个konami代码。 我认为这是一个伟大的想法,所以我实施了它 我做这件事的方式肯定会奏效,因为我不必处理任何错误。 然而代码比预期的要大一点,所以我想知道如何使它变小并且仍然工作,因为我不知道有什么好的方法使它变小 守则: konami_code = ['', '', '', '', '', '', '', '', '', ''] key = pygame.key.get_pressed() for event in pygame
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')
我认为在这里问这个问题并不合适(也许代码审查是一个更好的地方),但我对您的代码做了一些改进:
import pygame
pygame.init()
pygame.font.init()
display = pygame.display.set_mode((500, 300))
font = pygame.font.SysFont('arial', 20)
sequence = ['up', 'up', 'down', 'right', 'left', 'z', 'x', 'z', 'up', 'down']
text_render = font.render(' '.join(sequence), False, (255, 255, 255))
input_sequence = []
clock = pygame.time.Clock()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
input_sequence.append('up')
if event.key == pygame.K_DOWN:
input_sequence.append('down')
if event.key == pygame.K_RIGHT:
input_sequence.append('right')
if event.key == pygame.K_LEFT:
input_sequence.append('left')
if event.key == pygame.K_z:
input_sequence.append('z')
if event.key == pygame.K_x:
input_sequence.append('x')
display.blit(text_render, (10, 10))
pygame.display.update()
if len(input_sequence) == len(sequence):
if input_sequence == sequence:
print('correct sequence')
pygame.quit()
exit()
else:
print('incorrect sequence, try again')
input_sequence = []
最重要的是,现在你可以在序列列表中输入你想要的任何序列,它将匹配用户输入,不管列表有多长。除了一些调整的情况下,一个长长的名单可以与字体大小(自动化的过程),也许一些时间可以添加
下面是一个更短的选项:
import pygame
pygame.init()
pygame.font.init()
display = pygame.display.set_mode((500, 300))
font = pygame.font.SysFont('arial', 20)
keys = {pygame.K_UP: 'up', pygame.K_DOWN: 'down', pygame.K_RIGHT: 'right', pygame.K_LEFT: 'left',
pygame.K_z: 'z', pygame.K_x: 'x'}
sequence = ['up', 'up', 'down', 'right', 'left', 'z', 'x', 'z', 'up', 'down']
text_render = font.render(' '.join(sequence), False, (255, 255, 255))
input_sequence = []
clock = pygame.time.Clock()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
for key, name in keys.items():
if event.key == key:
input_sequence.append(name)
break
display.blit(text_render, (10, 10))
pygame.display.update()
if len(input_sequence) == len(sequence):
if input_sequence == sequence:
print('correct sequence')
pygame.quit()
exit()
else:
print('incorrect sequence, try again')
input_sequence = []
您可以将konami代码存储为值列表。然后将用户输入与该列表进行比较:
import pygame
CODE = [pygame.K_UP, pygame.K_UP, pygame.K_DOWN, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT, pygame.K_LEFT, pygame.K_RIGHT, pygame.K_b, pygame.K_a]
pygame.init()
screen = pygame.display.set_mode([500, 500])
code = []
index = 0
running = True
while running:
key = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == CODE[index]:
code.append(event.key)
index += 1
if code == CODE:
index = 0
print('Bingo!')
else:
code = []
index = 0
pygame.display.quit()
pygame.quit()