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:
JavaScript
x
118
118
1
konami_code = ['', '', '', '', '', '', '', '', '', '']
2
3
key = pygame.key.get_pressed()
4
for event in pygame.event.get():
5
if event.type == pygame.KEYUP:
6
if key[pygame.K_UP]:
7
if konami_code[0] == '':
8
konami_code[0] = 'UP'
9
10
elif konami_code[0] == 'UP'
11
and konami_code[1] == '':
12
konami_code[1] = 'UP'
13
14
else:
15
konami_code = ['', '', '', '', '', '', '', '', '', '']
16
17
if key[pygame.K_DOWN]:
18
if konami_code[0] == 'UP'
19
and konami_code[1] == 'UP'
20
and konami_code[2] == '':
21
konami_code[2] = 'DOWN'
22
23
elif konami_code[0] == 'UP'
24
and konami_code[1] == 'UP'
25
and konami_code[2] == 'DOWN'
26
and konami_code[3] == '':
27
konami_code[3] = 'DOWN'
28
29
else:
30
konami_code = ['', '', '', '', '', '', '', '', '', '']
31
32
if key[pygame.K_LEFT]:
33
if konami_code[0] == 'UP'
34
and konami_code[1] == 'UP'
35
and konami_code[2] == 'DOWN'
36
and konami_code[3] == 'DOWN'
37
and konami_code[4] == '':
38
konami_code[4] = 'LEFT'
39
40
elif konami_code[0] == 'UP'
41
and konami_code[1] == 'UP'
42
and konami_code[2] == 'DOWN'
43
and konami_code[3] == 'DOWN'
44
and konami_code[4] == 'LEFT'
45
and konami_code[5] == 'RIGHT'
46
and konami_code[6] == '':
47
konami_code[6] = 'LEFT'
48
49
else:
50
konami_code = ['', '', '', '', '', '', '', '', '', '']
51
52
if key[pygame.K_RIGHT]:
53
if konami_code[0] == 'UP'
54
and konami_code[1] == 'UP'
55
and konami_code[2] == 'DOWN'
56
and konami_code[3] == 'DOWN'
57
and konami_code[4] == 'LEFT'
58
and konami_code[5] == '':
59
konami_code[5] = 'RIGHT'
60
61
elif konami_code[0] == 'UP'
62
and konami_code[1] == 'UP'
63
and konami_code[2] == 'DOWN'
64
and konami_code[3] == 'DOWN'
65
and konami_code[4] == 'LEFT'
66
and konami_code[5] == 'RIGHT'
67
and konami_code[6] == 'LEFT'
68
and konami_code[7] == '':
69
konami_code[7] = 'RIGHT'
70
71
else:
72
konami_code = ['', '', '', '', '', '', '', '', '', '']
73
74
if key[pygame.K_b]:
75
if konami_code[0] == 'UP'
76
and konami_code[1] == 'UP'
77
and konami_code[2] == 'DOWN'
78
and konami_code[3] == 'DOWN'
79
and konami_code[4] == 'LEFT'
80
and konami_code[5] == 'RIGHT'
81
and konami_code[6] == 'LEFT'
82
and konami_code[7] == 'RIGHT'
83
and konami_code[8] == '':
84
konami_code[8] = 'B'
85
86
else:
87
konami_code = ['', '', '', '', '', '', '', '', '', '']
88
89
if key[pygame.K_a]:
90
if konami_code[0] == 'UP'
91
and konami_code[1] == 'UP'
92
and konami_code[2] == 'DOWN'
93
and konami_code[3] == 'DOWN'
94
and konami_code[4] == 'LEFT'
95
and konami_code[5] == 'RIGHT'
96
and konami_code[6] == 'LEFT'
97
and konami_code[7] == 'RIGHT'
98
and konami_code[8] == 'B'
99
and konami_code[9] == '':
100
konami_code[9] = 'A'
101
102
else:
103
konami_code = ['', '', '', '', '', '', '', '', '', '']
104
105
106
107
if konami_code[0] == 'UP'
108
and konami_code[1] == 'UP'
109
and konami_code[2] == 'DOWN'
110
and konami_code[3] == 'DOWN'
111
and konami_code[4] == 'LEFT'
112
and konami_code[5] == 'RIGHT'
113
and konami_code[6] == 'LEFT'
114
and konami_code[7] == 'RIGHT'
115
and konami_code[8] == 'B'
116
and konami_code[9] == 'A':
117
print('code completed')
118
Advertisement
Answer
You can store your konami code as a list of values. And then compare user input with that list:
JavaScript
1
28
28
1
import pygame
2
3
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]
4
5
pygame.init()
6
screen = pygame.display.set_mode([500, 500])
7
code = []
8
index = 0
9
running = True
10
while running:
11
key = pygame.key.get_pressed()
12
for event in pygame.event.get():
13
if event.type == pygame.QUIT:
14
running = False
15
elif event.type == pygame.KEYDOWN:
16
if event.key == CODE[index]:
17
code.append(event.key)
18
index += 1
19
if code == CODE:
20
index = 0
21
print('Bingo!')
22
else:
23
code = []
24
index = 0
25
26
pygame.display.quit()
27
pygame.quit()
28