简体   繁体   English

如何让我的 pygame konami 代码更简单?

[英]how can I make my pygame konami code simpler?

I am currently working on a pygame game and my friends asked if i could implement a konami code.我目前正在开发 pygame 游戏,我的朋友问我是否可以实现 konami 代码。 I thought it was a great idea so i implemented it.我认为这是一个好主意,所以我实施了它。

I made it in a way it would certainly work because I wouldnt have to deal with any errors.我以一种肯定会起作用的方式做到了这一点,因为我不必处理任何错误。 However.. the code got a bit larger then expected so i was wondering how i could make it smaller and still working because i dont know any good way to make it smaller.但是..代码比预期的要大一些,所以我想知道如何使它更小并且仍然可以工作,因为我不知道有什么好方法可以使它更小。

the code:编码:

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')

You can store your konami code as a list of values.您可以将您的 konami 代码存储为值列表。 And then compare user input with that list:然后将用户输入与该列表进行比较:

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()

I don't think this is actually a proper question to ask here (maybe code review is a better place) but I made some improvements to Your code:我认为这实际上不是一个合适的问题(也许代码审查是一个更好的地方),但我对您的代码进行了一些改进:

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 = []

The main thing is that now You can input any sequence You want in the sequence list and it will match user input against it, no matter how long the list really.最主要的是,现在您可以在序列列表中输入您想要的任何序列,并且无论列表有多长,它都会匹配用户输入。 except some adjustment in case of a long list could be made with font size (automate the process) and maybe some timing could be added除了在长列表的情况下可以对字体大小进行一些调整(自动化过程),也许可以添加一些时间

here is an even shorter option:这是一个更短的选择:

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 = []

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM