-
Notifications
You must be signed in to change notification settings - Fork 2
/
MineBlock 2D.py
287 lines (244 loc) · 9.98 KB
/
MineBlock 2D.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
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
from tkinter import *
import os, webbrowser, World_gen, data, sys
with open("options.txt", "r") as f:
exec(f.read())
# Command line launcher bypass
if len(sys.argv) > 1:
worlds = os.listdir("Worlds")
if sys.argv[1] in worlds:
import main
main.main(sys.argv[1])
else:
print("World dosn't exist.")
class Window(Frame):
def __init__(self, master=None):
# parameters that you want to send through the Frame class.
Frame.__init__(self, master)
#reference to the master widget, which is the tk window
self.master = master
#with that, we want to then run init_window, which doesn't yet exist
self.init_window()
def init_window(self):
self.master.title("MineBlock 2D Launcher")
img = PhotoImage(file=os.path.join("Texture Packs", "Default", "5.png"))
self.master.tk.call('wm', 'iconphoto', root._w, img)
self.pack(fill=BOTH, expand=1)
# creating a menu instance
menu = Menu(self.master)
self.master.config(menu=menu)
# create the file object
file = Menu(menu)
file.add_command(label="Exit", command=self.client_exit)
file.add_command(label="Game folder", command=self.game_folder)
menu.add_cascade(label="File", menu=file)
# create the options object
options = Menu(menu)
options.add_command(label="Options", command=O.start)
options.add_command(label="Reset to defaults", command=O.reset)
menu.add_cascade(label="Options", menu=options)
# create the help object
hel = Menu(menu)
hel.add_command(label="Readme", command=self.readme)
hel.add_command(label="Website", command=self.download)
hel.add_command(label="Changelog", command=self.changelog)
menu.add_cascade(label="Help", menu=hel)
# New world button
button = Button(self, text="New World", width=60, command=W.new_world)
button.place(x=0, y=0)
self.refresh()
def refresh(self):
worlds = os.listdir("Worlds")
if len(worlds) >= 1:
button = Button(self, text=worlds[0][0:-5], width=50, command=lambda: World.start_world(0))
button.place(x=0, y=50)
button = Button(self, text="Delete", width=6, command=lambda: World.delete_world(0))
button.place(x=350, y=50)
else:
button = Button(self, width=50)
button.place(x=0, y=50)
button = Button(self, width=6)
button.place(x=350, y=50)
if len(worlds) >= 2:
button = Button(self, text=worlds[1][0:-5], width=50, command=lambda: World.start_world(1))
button.place(x=0, y=80)
button = Button(self, text="Delete", width=6, command=lambda: World.delete_world(1))
button.place(x=350, y=80)
else:
button = Button(self, width=50)
button.place(x=0, y=80)
button = Button(self, width=6)
button.place(x=350, y=80)
if len(worlds) >= 3:
button = Button(self, text=worlds[2][0:-5], width=50, command=lambda: World.start_world(2))
button.place(x=0, y=110)
button = Button(self, text="Delete", width=6, command=lambda: World.delete_world(2))
button.place(x=350, y=110)
else:
button = Button(self, width=50)
button.place(x=0, y=110)
button = Button(self, width=6)
button.place(x=350, y=110)
if len(worlds) >= 4:
button = Button(self, text=worlds[3][0:-5], width=50, command=lambda: World.start_world(3))
button.place(x=0, y=140)
button = Button(self, text="Delete", width=6, command=lambda: World.delete_world(3))
button.place(x=350, y=140)
else:
button = Button(self, width=50)
button.place(x=0, y=140)
button = Button(self, width=6)
button.place(x=350, y=140)
if len(worlds) >= 5:
button = Button(self, text=worlds[4][0:-5], width=50, command=lambda: World.start_world(4))
button.place(x=0, y=170)
button = Button(self, text="Delete", width=6, command=lambda: World.delete_world(4))
button.place(x=350, y=170)
else:
button = Button(self, width=50)
button.place(x=0, y=170)
button = Button(self, width=6)
button.place(x=350, y=170)
if len(worlds) >= 6:
button = Button(self, text=worlds[5][0:-5], width=50, command=lambda: World.start_world(5))
button.place(x=0, y=200)
button = Button(self, text="Delete", width=6, command=lambda: World.delete_world(5))
button.place(x=350, y=200)
else:
button = Button(self, width=50)
button.place(x=0, y=200)
button = Button(self, width=6)
button.place(x=350, y=200)
def client_exit(self):
exit()
def game_folder(self):
os.startfile(".")
def readme(self):
webbrowser.open("file://" + os.path.realpath("Readme.html"))
def download(self):
webbrowser.open("http://mineblock2d.ml/")
def changelog(self):
webbrowser.open("http://mineblock2d.ml/changelog.html")
class World():
# Load world
def start_world(n):
global root
root.destroy()
worlds = os.listdir("Worlds")
import main
main.main(worlds[n])
root = Tk()
root.geometry("400x300")
global app
app = Window(root)
root.mainloop()
# Delete world
def delete_world(n=None):
worlds = os.listdir("Worlds")
global WORLD
WORLD = worlds[n][0:-5]
W.root = Tk()
W.root.title("Delete?")
W.root.geometry("200x100")
label = Label(W.root, text="Are you shure you want to delete \n" + WORLD)
label.place(x=0, y=10)
Button(W.root, text="Cancel", command=W.root.destroy).place(x=10, y=70)
Button(W.root, text="Ok",command=W.rm).place(x=160, y=70)
W.root.mainloop()
def rm(self):
W.root.destroy()
path = os.path.join("Worlds", WORLD + ".json")
os.remove(path)
app.refresh()
# New world
def new_world(self):
self.root = Tk()
self.root.title("New World")
self.root.geometry("300x100")
self.root.bind("<Return>", W.create)
label = Label(self.root, text="World Name")
label.place(x=10, y=10)
self.e = Entry(self.root)
self.e.place(x=160, y=10)
button = Button(self.root, text="Create", command=W.create)
button.pack(side=BOTTOM)
self.root.mainloop()
def create(self, a=None):
world = self.e.get() + ".json"
worlds = os.listdir("Worlds")
if world not in worlds and world != ".json":
World_gen.main(world)
self.root.destroy()
app.refresh()
else:
label = Label(self.root, text="World allredy exists.")
label.place(x=50, y=30)
class Options(Frame):
def start(self):
self.root = Tk()
self.root.geometry("300x250")
self.root.title("MineBlock 2D Settings")
button = Button(self.root, text="Apply", command=self.apply)
button.pack(side=BOTTOM)
label = Label(self.root, text="Screen width")
label.place(x=10, y=10)
self.width = Entry(self.root)
self.width.place(x=160, y=10)
self.width.insert(0, SCREEN_WIDTH)
label = Label(self.root, text="Screen height")
label.place(x=10, y=40)
self.height = Entry(self.root)
self.height.place(x=160, y=40)
self.height.insert(0, SCREEN_HEIGHT)
label = Label(self.root, text="Sky color (R, G, B)")
label.place(x=10, y=80)
self.sky = Entry(self.root)
self.sky.place(x=160, y=80)
self.sky.insert(0, str(SKY))
label = Label(self.root, text="Skin filename")
label.place(x=10, y=110)
self.skin = Entry(self.root)
self.skin.place(x=160, y=110)
self.skin.insert(0, SKIN)
label = Label(self.root, text="Texture pack folder")
label.place(x=10, y=140)
self.pack = Entry(self.root)
self.pack.place(x=160, y=140)
self.pack.insert(0, PACK)
root.mainloop()
def apply(self):
with open("options.txt", "r") as f:
opts = f.read().split("\n")
for i, o in enumerate(opts):
if len(o) != 0:
if o.split()[0] == "SCREEN_HEIGHT":
opts[i] = "SCREEN_HEIGHT = " + self.height.get()
if o.split()[0] == "SCREEN_WIDTH":
opts[i] = "SCREEN_WIDTH = " + self.width.get()
if o.split()[0] == "SKY":
opts[i] = "SKY = " + self.sky.get()
if o.split()[0] == "SKIN":
opts[i] = 'SKIN = "' + self.skin.get() + '"'
if o.split()[0] == "PACK":
opts[i] = 'PACK = "' + self.pack.get() + '"'
with open("options.txt", "w") as f:
f.write("\n".join(opts))
def reset(self):
O.root = Tk()
O.root.title("Reset to defaults?")
O.root.geometry("200x100")
label = Label(O.root, text="Are you shure you want to reset all \noptions to defaults?")
label.place(x=0, y=10)
Button(O.root, text="Cancel", command=O.root.destroy).place(x=10, y=70)
Button(O.root, text="Ok",command=O.reset_options).place(x=160, y=70)
O.root.mainloop()
def reset_options(self):
O.root.destroy()
with open("options.txt", "w") as f:
f.write(data.options)
# Part of main window
root = Tk()
root.geometry("400x300")
W = World()
O = Options()
app = Window(root)
root.mainloop()