I am currently working on a pygame game and my friends asked if i could implement a konami code. 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')
Advertisement
Answer
You can store your konami code as a list of values. 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()