-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpygame-test.py
77 lines (62 loc) · 2.66 KB
/
pygame-test.py
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
import pygame, string
from pathlib import Path
pygame.init()
pygame.joystick.init()
clock = pygame.time.Clock() # To limit framerate, prevents single button presses
# being registered multiple times
joystick_count = pygame.joystick.get_count()
selection = 0
if joystick_count <= 0:
print("No joysticks detected. Bye.")
exit()
if joystick_count > 1:
print("Multiple joysticks detected.")
print("Please select the joystick you want to use by typing the appropriate number:")
joystick_not_selected = True
while joystick_not_selected:
for i in range(joystick_count):
print("{}:\t{}".format(i, pygame.joystick.Joystick(i).get_name()))
joystick_number = input("Enter joystick number: ")
if not str.isnumeric(joystick_number):
print("Try writing a number this time.")
continue
selection = int(joystick_number)
if selection not in range(joystick_count):
print("That wasn't a number we're expecting. Try again.")
continue
joystick_not_selected = False
joy = pygame.joystick.Joystick(selection)
joy.init()
button_mappings = ["LIGHT", "MEDIUM", "HEAVY", "SPECIAL"]
mapping_key = ""
next_button = True
player = 1 if int(input("Player Number? (1, 2): ")) > 1 else 0
while len(mapping_key) < 4:
if next_button:
print(button_mappings[len(mapping_key)], end=': ', flush=True)
next_button = False
for event in pygame.event.get():
if event.type == pygame.JOYBUTTONDOWN:
button = event.__dict__["button"] # See https://www.pygame.org/docs/ref/event.html
print("Button {}".format(button))
mapping_key += string.ascii_lowercase[button]
next_button = True
break
clock.tick(60) # 60 frames per second
print("mapping_key:", mapping_key)
default_path = "defaultpath.txt"
default_path_exists = Path(default_path).is_file()
my_path = open(default_path).read() if default_path_exists else input("please provide the path for SFWinKey.TXT: ")
with open(my_path if default_path_exists else "defaultconfig.txt", encoding="utf-8") as f:
file = f.readlines()
file[player] = mapping_key + "wxyz" + "\n"
if default_path_exists:
if input("write to \"" + my_path + "\" ? (Y/ n): ") not in 'yY':
my_path = input("please enter a new path: ")
if input("set \"" + my_path + "\" as the new default? (Y/ n): ") in 'yY':
open(default_path, 'w+').write(my_path)
print("new default path saved successfully.")
else:
open(default_path, 'w+').write(my_path)
open(my_path, 'w+', encoding="utf-8").write("".join(file))
print("saved successfully.")