-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadyPlayerOne.py
More file actions
237 lines (202 loc) · 6.46 KB
/
ReadyPlayerOne.py
File metadata and controls
237 lines (202 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#! /usr/bin/env python
import sys
import time
import logging
import pygame
from pygame import locals
from collections import deque
logging.getLogger().addHandler(logging.StreamHandler())
log = logging.getLogger()
log.setLevel(logging.DEBUG)
# === BEGIN CONSTANTS ===
# Escape Room Time:
Minute = 45
Second = 0
# Player
PlayerID="ONE"
#PlayerID="TWO"
PlayerReady=False
P1Ready=PlayerReady
P2Ready=True
#Colors
Black = (0,0,0)
Red = (255, 0, 0)
Green = (0, 255, 0)
Blue = (0, 0, 255)
Purple = (80, 25, 128)
White = (255, 255, 255)
# === END CONSTANTS ===
running = True
pygame.init()
pygame.joystick.init() # main joystick device system
# try:
# j = pygame.joystick.Joystick(0) # create a joystick instance
# j.init() # init instance
# print 'Enabled joystick: ' + j.get_name()
# except pygame.error:
# print 'no joystick found.'
#
# if not pygame.joystick.get_count():
# log.critical("No Joystick Found")
# sys.exit()
j = None
Joysticks = [pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count())]
for Joystick in Joysticks:
log.debug(Joystick.get_name())
if "NES" in Joystick.get_name():
j = pygame.joystick.Joystick(Joystick.get_id())
j.init()
break
log.info("Enabled joystick: {}".format(j.get_name()))
BTN = {
0 : 'A',
1 : 'B',
2 : 'SELECT',
3 : 'START'
}
def exit(button, queue=deque(maxlen=4)):
queue.append(button)
if tuple(queue) == ("SELECT", "START",
"B", "A"):
return True
def konami(button, queue=deque(maxlen=11)):
queue.append(button)
if tuple(queue) == ("UP", "UP",
"DOWN", "DOWN",
"LEFT", "RIGHT",
"LEFT", "RIGHT",
"B", "A", "START"):
global PlayerReady
global P1Ready
PlayerReady=True
P1Ready=True
print("@@@@@PLAYER READY@@@@@")
def get_direction(x,y):
LocalDir=""
if y <= -0.95:
# UP
LocalDir+="UP"
if y >= 0.95:
# DOWN
LocalDir+="DOWN"
if x >= 0.95:
# RIGHT
LocalDir+="RIGHT"
if x <= -0.95:
# LEFT
LocalDir+="LEFT"
return LocalDir
def AllPlayersReady():
global P1Ready
global P2Ready
print("P1: {0},P2 {1}".format(P1Ready,P2Ready))
if P1Ready & P2Ready:
return True
else:
return False
#Half 720p windowed
size = width, height = 640, 360 #Make sure background image is same size
screen = pygame.display.set_mode(size)
#Fullscreen 720p
# size = width, height = 1280, 720 #Make sure background image is same size
# screen = pygame.display.set_mode(size,pygame.FULLSCREEN)
border = height/20
background=Purple
# #Fonts
FontSize=height/5
Font = pygame.font.SysFont("Trebuchet MS", FontSize)
Clock = pygame.time.Clock()
CLOCKTICK = pygame.USEREVENT+1
pygame.time.set_timer(CLOCKTICK, 1000) # fired once every Second
# Initial Screen
# Time, HH:MM
print("{0:02}:{1:02}").format(Minute,Second)
screen.fill(background)
TimeFont = Font.render("{0:02}:{1:02}".format(Minute,Second),True, White)
TimeFontR = TimeFont.get_rect()
TimeFontR.center=(width/2,(FontSize/2)+border)
if PlayerReady:
print("Player {} Ready".format(PlayerID))
PlayerFont = Font.render("Player {} Ready".format(PlayerID),True,Blue)
else:
print("Player {} NOT Ready".format(PlayerID))
PlayerFont = Font.render("Player {} Not Ready".format(PlayerID),True,Red)
PlayerFontR = PlayerFont.get_rect()
PlayerFontR.center=(width/2,height/2)
screen.blit(TimeFont, TimeFontR)
screen.blit(PlayerFont, PlayerFontR)
pygame.display.flip()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == CLOCKTICK: # count up the clock
#Timer
if Minute == 0:
if Second == 0:
done = True
pygame.quit()
if Second == 0:
Minute -= 1
Second = 59
# Minute -= 1
# redraw time
print("{0:02}:{1:02}").format(Minute,Second)
screen.fill(background)
TimeFont = Font.render("{0:02}:{1:02}".format(Minute,Second),True, White)
TimeFontR = TimeFont.get_rect()
TimeFontR.center=(width/2,(FontSize/2)+border)
if PlayerReady:
print("Player {} Ready".format(PlayerID))
PlayerFont = Font.render("Player {} Ready".format(PlayerID),True,Blue)
else:
print("Player {} NOT Ready".format(PlayerID))
PlayerFont = Font.render("Player {} Not Ready".format(PlayerID),True,Red)
PlayerFontR = PlayerFont.get_rect()
PlayerFontR.center=(width/2,height/2)
screen.blit(TimeFont, TimeFontR)
screen.blit(PlayerFont, PlayerFontR)
pygame.display.flip()
Second -= 1
if AllPlayersReady():
running=False
if event.type == pygame.locals.JOYAXISMOTION: # 7
x, y = j.get_axis(0), j.get_axis(1)
JoyDir=get_direction(x, y)
print(JoyDir)
if JoyDir=="":
continue
else:
konami(JoyDir)
#print 'x and y : ' + str(x) +' , '+ str(y)
elif event.type == pygame.locals.JOYBUTTONDOWN: # 10
if event.button in BTN:
print('Button: {} down').format(BTN[event.button])
if konami(BTN[event.button]):
player_ready()
else:
print("Button: Unknown down")
elif event.type == pygame.locals.JOYBUTTONUP: # 11
if event.button in BTN:
print('Button: {} up').format(BTN[event.button])
else:
print("Button: Unknown up")
Clock.tick(60) # ensures a maximum of 60 frames per Second
while not running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(1)
elif event.type == pygame.locals.JOYBUTTONDOWN: # 10
if event.button in BTN:
print('Button: {} down').format(BTN[event.button])
if exit(BTN[event.button]):
pygame.quit()
sys.exit()
else:
print("Button: Unknown down")
elif event.type == pygame.locals.JOYBUTTONUP: # 11
if event.button in BTN:
print('Button: {} up').format(BTN[event.button])
else:
print("Button: Unknown up")