-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathooo-clicker.lua
More file actions
328 lines (261 loc) · 7.74 KB
/
ooo-clicker.lua
File metadata and controls
328 lines (261 loc) · 7.74 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
-- A simple CC: Tweaked game where you have to click on O's to gain points
-- Licensed under the MIT License
-- Made by Jannnn13
local version = "1.0.0"
local cfgPath = customConfigPath or ".oooClickerConfig.json"
local function pullClick()
local _, _, x, y = os.pullEvent("mouse_click")
return {
x = x,
y = y
}
end
local function readConfig()
local cfgTemplate = {
highScore = 0,
theme = {
darkMode = false,
blackWhiteMode = false
}
}
if not fs.exists(cfgPath) then
local file = fs.open(cfgPath, "w")
file.write(textutils.serializeJSON(cfgTemplate))
file.close()
end
local file = fs.open(cfgPath, "r")
local content = file.readAll()
file.close()
local cfg
pcall(function()
cfg = textutils.unserializeJSON(content)
end)
if (not cfg) or (type(cfg) ~= "table") then
cfg = cfgTemplate
end
return cfg
end
local cfg = readConfig(cfgPath)
local function saveConfig(cfg)
local file = fs.open(cfgPath, "w")
file.write(textutils.serializeJSON(cfg))
file.close()
end
local function getColor()
local rt = {}
if term.isColor() or not cfg.theme.blackWhiteMode then
if cfg.theme.darkMode then
rt.bg = colors.black
rt.playButton = colors.lime
rt.onlineButton = colors.orange
rt.settingsButton = colors.lightGray
rt.exitButton = colors.red
rt.text = colors.blue
else
rt.bg = colors.white
rt.playButton = colors.lime
rt.onlineButton = colors.orange
rt.settingsButton = colors.gray
rt.exitButton = colors.red
rt.text = colors.orange
end
else
if cfg.theme.darkMode then
rt.bg = colors.black
rt.playButton = colors.white
rt.onlineButton = colors.white
rt.settingsButton = colors.white
rt.exitButton = colors.white
rt.text = colors.white
else
rt.bg = colors.white
rt.playButton = colors.black
rt.onlineButton = colors.black
rt.settingsButton = colors.black
rt.exitButton = colors.black
rt.text = colors.black
end
end
return rt
end
local function pen(text, color)
local previousTextColor = term.getTextColor()
term.setTextColor(color)
term.write(text)
term.setTextColor(previousTextColor)
end
local function waitForButtonClick(buttons)
while true do
local coor = pullClick()
if coor and coor.x and coor.y then
for _, button in ipairs(buttons) do
local text = button.text
local width = #text
if width > 0 and (coor.y == button.y) and (coor.x >= button.x) and (coor.x < button.x + width) then
return text, button.id
end
end
end
end
end
local function onOff(boolean)
if boolean then
return "On"
else
return "Off"
end
end
local function onOffColor(boolean)
if boolean then
return getColor().playButton
else
return getColor().exitButton
end
end
local function res()
local x, y = term.getSize()
return {
x = x,
y = y
}
end
if (res().x < 11) or (res().y < 6) then
error("Screen is too small! Minimum size is 11x6.", 0)
end
local previousColors = {
bg = term.getBackgroundColor(),
text = term.getTextColor()
}
local game = {}
function game.titleScreen()
term.setBackgroundColor(getColor().bg)
term.clear()
term.setCursorPos(1, 1)
if res().y > 6 then
pen("OOO", getColor().text)
term.setCursorPos(1, 2)
pen("Clicker", getColor().text)
else
pen("OOO Clicker", getColor().text)
end
term.setCursorPos(1, res().y - 2)
pen("Play", getColor().playButton)
term.setCursorPos(1, res().y - 1)
pen("Settings", getColor().settingsButton)
term.setCursorPos(1, res().y)
pen("Exit", getColor().exitButton)
local clickedButton = waitForButtonClick({
{ text = "Play", x = 1, y = res().y - 2 },
{ text = "Settings", x = 1, y = res().y - 1 },
{ text = "Exit", x = 1, y = res().y }
})
if clickedButton == "Play" then
local looseCause, score = game.mainGame()
game.scoreViewer(looseCause, score)
elseif clickedButton == "Settings" then
game.settings()
elseif clickedButton == "Exit" then
term.setBackgroundColor(previousColors.bg)
term.setTextColor(previousColors.text)
term.clear()
term.setCursorPos(1, 1)
return "Game Exit"
end
end
function game.settings()
term.setBackgroundColor(getColor().bg)
term.clear()
term.setCursorPos(1, 1)
pen("X", getColor().exitButton)
term.setCursorPos(res().x - #version, 1)
pen("v" .. version, getColor().text)
term.setCursorPos(1, 2)
pen("Dark Mode:", getColor().text)
term.setCursorPos(1, 3)
pen(onOff(cfg.theme.darkMode), onOffColor(cfg.theme.darkMode))
local buttons = {
{ text = "X", id = "X", x = 1, y = 1 },
{ text = onOff(cfg.theme.darkMode), id = "darkMode", x = 1, y = 3 },
}
local _, clickedButton = waitForButtonClick(buttons)
if clickedButton == "X" then
return
elseif clickedButton == "darkMode" then
cfg.theme.darkMode = not cfg.theme.darkMode
saveConfig(cfg)
game.settings()
elseif clickedButton == "blackWhiteMode" then
cfg.theme.blackWhiteMode = true
saveConfig(cfg)
game.settings()
end
end
function game.scoreViewer(looseCause, score)
if score > cfg.highScore then
cfg.highScore = score
saveConfig(cfg)
end
term.setBackgroundColor(getColor().bg)
term.clear()
term.setCursorPos(1, 1)
pen(looseCause, getColor().exitButton)
term.setCursorPos(1, 2)
pen("Score: " .. score, getColor().text)
term.setCursorPos(1, 3)
pen("HS: " .. cfg.highScore, getColor().text)
term.setCursorPos(1, 5)
pen("Play Again", getColor().playButton)
term.setCursorPos(1, 6)
pen("Exit", getColor().exitButton)
local clickedButton = waitForButtonClick({
{ text = "Play Again", x = 1, y = 5 },
{ text = "Exit", x = 1, y = 6 }
})
if clickedButton == "Play Again" then
local looseCause, score = game.mainGame()
game.scoreViewer(looseCause, score)
elseif clickedButton == "Exit" then
return
end
end
function game.mainGame()
local score = 0
local looseCause = ""
while true do
parallel.waitForAny(function()
term.setBackgroundColor(getColor().bg)
term.clear()
term.setCursorPos(1, 1)
pen("X", getColor().exitButton)
term.setCursorPos(res().x - #tostring(score), 1)
pen(tostring(score), getColor().text)
local random = {}
random.x = math.random(1, res().x)
random.y = math.random(2, res().y)
term.setCursorPos(random.x, random.y)
pen("O", getColor().text)
local clickedButton = waitForButtonClick({
{ text = "X", x = 1, y = 1 },
{ text = "O", x = random.x, y = random.y }
})
if clickedButton == "X" then
looseCause = "Cancelled."
return
end
end, function()
sleep(1.5)
looseCause = "Time's up!"
end)
if looseCause ~= "" then
break
else
score = score + 1
end
end
return looseCause, score
end
while true do
if game.titleScreen() == "Game Exit" then
break
end
end