-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamestates.py
More file actions
140 lines (135 loc) · 5.91 KB
/
gamestates.py
File metadata and controls
140 lines (135 loc) · 5.91 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
"""
Menu/UI functions for main
"""
from utils import *
from logic import Game
from ansi import *
def menu():
screenX, screenY = 0, 0
logo = headline("Snake")
update = False
while True:
if get_terminal_size().columns != screenX or get_terminal_size().lines != screenY or update:
if not update:
hardClear()
update = False
screenX, screenY = get_terminal_size()
centeredLogo = centerMultiline(logo, screenX)
print(CMT + centeredLogo)
print("Press Space to start Game".center(screenX))
print("Press x to access Settings".center(screenX))
if keyboard.is_pressed("space"):
return
if keyboard.is_pressed("x"):
settings()
print(CLR)
update = True
def settings():
with open("settings.json", "r") as f:
SETTINGS_rendermode, SETTINGS_savehs, SETTINGS_speedadj = json.load(f)
colorDict = {True: COLOR_SETTINGS_FOREGROUND+COLOR_SETTINGS_BACKGROUND, False: COLORRESET+COLORRESET}
screenX, screenY = 0, 0
selection = 0
print(CLR)
update = False
while True:
if get_terminal_size().columns != screenX or get_terminal_size().lines != screenY or update:
update = False
screenX, screenY = get_terminal_size()
print(CMT + "Settings".center(screenX))
print("To navigate use arrow keys ↑↓, to select press enter <╝. Press c to cancel".center(screenX))
print()
print(f"Render mode: <{colorDict[selection==0]}{SETTINGS_rendermode[0][SETTINGS_rendermode[1]]}{COLORRESET}>".center(screenX))
print(f"Save Highscore: <{colorDict[selection==1]}{SETTINGS_savehs[0][SETTINGS_savehs[1]]}{COLORRESET}>".center(screenX))
print(f"increasing speed: <{colorDict[selection==2]}{SETTINGS_speedadj[0][SETTINGS_speedadj[1]]}{COLORRESET}>".center(screenX))
if keyboard.is_pressed("up arrow"):
selection = max(0, selection-1)
while keyboard.is_pressed("up arrow"):
pass
update = True
if keyboard.is_pressed("down arrow"):
selection = min(2, selection+1)
while keyboard.is_pressed("down arrow"):
pass
update = True
if keyboard.is_pressed("enter"):
if selection == 0:
SETTINGS_rendermode = [SETTINGS_rendermode[0], 1-SETTINGS_rendermode[1]]
elif selection == 1:
SETTINGS_savehs = [SETTINGS_savehs[0], 1-SETTINGS_savehs[1]]
elif selection == 2:
SETTINGS_speedadj = [SETTINGS_savehs[0], 1-SETTINGS_speedadj[1]]
while keyboard.is_pressed("enter"):
pass
update = True
if keyboard.is_pressed("c"):
with open("settings.json", "w") as f:
f.write(json.dumps([SETTINGS_rendermode, SETTINGS_savehs, SETTINGS_speedadj], indent=4))
return
def start():
with open("settings.json", "r") as f:
SETTINGS_rendermode, SETTINGS_savehs, SETTINGS_speedadj = json.load(f)
detailedrender = SETTINGS_rendermode[1] == SETTINGS_rendermode[0].index("detailed")
hardClear()
screenX, screenY = 0, 0
gameController = Game(20, 10*{True: 2, False: 1}[detailedrender])
gameController.spawnTreat()
keyboard.add_hotkey("w", gameController.changeDir, args=("w"))
keyboard.add_hotkey("up arrow", gameController.changeDir, args=("w"))
keyboard.add_hotkey("a", gameController.changeDir, args=("a"))
keyboard.add_hotkey("left arrow", gameController.changeDir, args=("a"))
keyboard.add_hotkey("s", gameController.changeDir, args=("s"))
keyboard.add_hotkey("down arrow", gameController.changeDir, args=("s"))
keyboard.add_hotkey("d", gameController.changeDir, args=("d"))
keyboard.add_hotkey("right arrow", gameController.changeDir, args=("d"))
while True:
if gameController.move() == -1:
keyboard.remove_all_hotkeys()
return gameController.score
if get_terminal_size().columns != screenX or get_terminal_size().lines != screenY:
screenX, screenY = get_terminal_size()
hardClear()
print(CMT)
gameController.displayGame()
sleep(1/gameController.speed)
def gameover(score):
with open("settings.json", "r") as f:
SETTINGS_rendermode, SETTINGS_savehs, SETTINGS_speedadj = json.load(f)
screenX, screenY = 0, 0
logo = headline("Game Over!")
print(CLR)
if SETTINGS_savehs[1] == SETTINGS_savehs[0].index("yes"):
if os.path.exists("data.json"):
with open("data.json", "r") as f:
hs = json.load(f)[0]
if score > hs:
with open("data.json", "w") as f:
f.write(json.dumps([score]))
else:
with open("data.json", "w") as f:
f.write(json.dumps([score]))
while True:
if get_terminal_size().columns != screenX or get_terminal_size().lines != screenY:
screenX, screenY = get_terminal_size()
centeredLogo = centerMultiline(logo, screenX)
highscore = score
if os.path.exists("data.npy"):
with open("data.json", "r") as f:
read = json.load(f)[0]
if score > read:
with open("data.json", "w") as f:
f.write(json.dumps([score]))
else:
highscore = read
else:
with open("data.json", "w") as f:
f.write(json.dumps([score]))
print(CMT + centeredLogo)
print(f"Score: {score} Highscore: {highscore}".center(screenX))
print()
print("Press q to exit".center(screenX))
print("Press r to restart".center(screenX))
if keyboard.is_pressed("q"):
exit()
if keyboard.is_pressed("r"):
return