-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMinecraft--2D.pyw
2909 lines (2676 loc) · 127 KB
/
Minecraft--2D.pyw
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Hello Python
项目名:我的世界2D
项目开始时间:2022-02-06 19:02
"""
import ast
import os
import random
import shutil
import time
import sys
import pygame
import pygame.freetype
import win32api
import wx
from role_add import *
class Button_font(object):
""" 定义一个字体按钮类"""
def __init__(self):
""" 定义初始化方法"""
self.MC_MUSIC = 'music\\clink.mp3' # 按钮音效路径
self.instead_0 = None
self.instead_1 = None
self.instead_2 = None
self.instead_3 = None
self.FONT_SIZE = None # 字体大小
self.NMAE_XY = None # 显示位置
self.BUTTON_NAME = None # 按钮名称
self.BUTTON_COLOR_F = None # 按钮颜色(当鼠标移到按钮之外时)
self.BUTTON_COLOR_T = None # 按钮颜色(当鼠标移到按钮之内时)
def button(self):
""" 创建字体按钮"""
# 获取字体区域
xw, yw = self.NMAE_XY # 截取字体显示位置————(xw, yw)
x0, y0 = self.FONT_SIZE * len(self.BUTTON_NAME)-1 + xw, self.FONT_SIZE-1 + yw
if xw <= x <= x0 and yw <= y <= y0:
# 当鼠标移动到字体区域之内时更换字体颜色
self.instead_0, self.instead_1, self.instead_2, self.instead_3 = \
self.NMAE_XY, self.BUTTON_NAME, self.BUTTON_COLOR_T, self.FONT_SIZE
else:
# 当鼠标移动到字体区域之外时更换字体颜色
self.instead_0, self.instead_1, self.instead_2, self.instead_3 = \
self.NMAE_XY, self.BUTTON_NAME, self.BUTTON_COLOR_F, self.FONT_SIZE
FONT_XY.render_to(
screen, self.instead_0, self.instead_1, fgcolor=self.instead_2, size=self.instead_3
)
def press(self, n=1):
""" 定义按钮按下"""
xw, yw = self.NMAE_XY
x0, y0 = self.FONT_SIZE * len(self.BUTTON_NAME)-1 + xw, self.FONT_SIZE-1 + yw
if xw <= x <= x0 and yw <= y <= y0:
""" 点击后的事件"""
if n:
self.click_music()
return True
def click_music(self):
""" 定义按钮音效"""
click = pygame.mixer.Sound(self.MC_MUSIC)
click.set_volume(MUSIC_BUTTON)
click.play()
class Button_image(object):
""" 定义一个图像按钮"""
def __init__(self):
""" 定义初始化方法"""
self.MC_MUSIC = 'music\\clink.mp3' # 按钮音效路径
self.instead_0 = None
self.instead_1 = None
self.instead_2 = None
self.IMAGE_OFF = None # 当鼠标移到按钮之外时的图像
self.IMAGE_ON = None # 当鼠标移到按钮之内时的图像
self.IMAGE_SIZE_OFF = None # 当鼠标移到按钮之外时的图像大小
self.IMAGE_SIZE_ON = None # 当鼠标移到按钮之内时的图像大小
self.IMAGE_OFF_XY = None # 图像显示位置(当鼠标移到按钮之外时)
self.IMAGE_ON_XY = None # 图像显示位置(当鼠标移到按钮之内时)
def button(self):
""" 创建图像按钮"""
xn, yn = self.IMAGE_SIZE_OFF # 截取图像大小————(xn, yn)
xw, yw = self.IMAGE_OFF_XY # 截取图像显示位置————(xw, yw)
# 获取图像区域
x0, y0 = xn-1 + xw, yn-1 + yw
if xw <= x <= x0 and yw <= y <= y0:
# 当鼠标移动到图像区域之内时更换图像
self.instead_0, self.instead_1, self.instead_2 = self.IMAGE_ON, self.IMAGE_SIZE_ON, self.IMAGE_ON_XY
else:
# 当鼠标移动到图像区域之外时更换图像
self.instead_0, self.instead_1, self.instead_2 = self.IMAGE_OFF, self.IMAGE_SIZE_OFF, self.IMAGE_OFF_XY
insert_image(self.instead_0, self.instead_1, self.instead_2)
def press(self, n=1):
""" 定义按钮按下"""
xn, yn = self.IMAGE_SIZE_OFF
xw, yw = self.IMAGE_OFF_XY
x0, y0 = xn-1 + xw, yn-1 + yw
if xw <= x <= x0 and yw <= y <= y0:
""" 点击后的事件"""
if n:
self.click_music()
return True
def click_music(self):
""" 定义按钮音效"""
click = pygame.mixer.Sound(self.MC_MUSIC)
click.set_volume(MUSIC_BUTTON)
click.play()
class InputBox(object):
""" 定义一个输入框"""
def __init__(self):
""" 定义初始化方法"""
self.input_open = [] # 是否打开输入框
self.point_open = [] # 指针开关
self.point_open_0, self.point_open_1 = (0, 0) # 均匀指针闪烁
self.point_time = 10 # 指针闪烁时长
self.point_place = [] # 指针位置
self.text = [['', ''] for _ in range(4)] # 初始化4个输入框的文本存储————[[持续文本数据, 文本数据], ...]
self.sx, self.sy = (None, None) # 截取输入框大小————(sx, sy)
self.gx, self.gy = (None, None) # 截取输入框位置————(gx, gy)
self.NUMBER = None # 输入框索引
self.INPUT_SIZE = None # 输入框大小
self.INPUT_XY = None # 输入框位置
self.INPUT_COLOR_INSIDE = None # 当鼠标移动到输入框之内时的颜色
self.INPUT_COLOR_OUTSIDE = None # 当鼠标移动到输入框之外时的颜色
self.INPUT_SIZE_INSIDE = None # 当鼠标移动到输入框之内时的输入框粗细
self.INPUT_SIZE_OUTSIDE = None # 当鼠标移动到输入框之外时的输入框粗细
self.INPUT_COLOR_BG_IF_LIST = None # 输入框背景颜色
self.POINT_COLOR = None # 指针颜色
self.POINT_SIZE = None # 指针粗细
self.POINT_KIND_IF = None # 指针样式
self.TEXT_COLOR = None # 文字颜色
def house(self):
""" 绘制一个输入框"""
self.sx, self.sy = self.INPUT_SIZE
self.gx, self.gy = self.INPUT_XY
x0, y0 = self.sx-1 + self.gx, self.sy-1 + self.gy
# 输入框背景
if self.INPUT_COLOR_BG_IF_LIST[0]:
ca, cb, cc = self.INPUT_COLOR_BG_IF_LIST[1]
if self.INPUT_COLOR_BG_IF_LIST[2]:
shade_bg((self.sx, self.sy), (ca, cb, cc, self.INPUT_COLOR_BG_IF_LIST[3]), (self.gx, self.gy))
else:
pygame.draw.rect(screen, (ca, cb, cc), (self.gx, self.gy, self.sx, self.sy), 0)
if self.gx <= x <= x0 and self.gy <= y <= y0:
# 当鼠标移动到输入框区域之内时更换输入框颜色
pygame.draw.rect(screen, self.INPUT_COLOR_INSIDE, (self.gx, self.gy, self.sx, self.sy),
self.INPUT_SIZE_INSIDE)
self.point_open[self.NUMBER] = True
else:
# 当鼠标移动到输入框之外时更换输入框颜色
pygame.draw.rect(screen, self.INPUT_COLOR_OUTSIDE, (self.gx, self.gy, self.sx, self.sy),
self.INPUT_SIZE_OUTSIDE)
self.point_open[self.NUMBER] = False
def point(self):
""" 绘制一个指针"""
if self.input_open[self.NUMBER]:
if self.point_open_0 % self.point_time == 0:
self.point_open_0 = self.point_time - 1
self.point_open_1 += 1
if self.point_open_1 == self.point_time:
self.point_open_0 = 0
self.point_open_1 = 0
else:
a = ((self.sy-3)/2) * (len(self.text[self.NUMBER][0]) + self.point_place[self.NUMBER])
if self.POINT_KIND_IF:
if a >= self.sx-6 - (self.sy-6)/2:
a = self.sx-6 - (self.sy-6)/2
gy, zx, sy = self.gy-self.POINT_SIZE-3 + self.sy, (self.sy-6)/2, self.POINT_SIZE
else:
if a >= self.sx-6:
a = self.sx-6
gy, zx, sy = self.gy+3, self.POINT_SIZE, self.sy-6
pygame.draw.rect(screen, self.POINT_COLOR, (self.gx+3 + a, gy, zx, sy), 0)
self.point_open_0 += 1
def press_frame_event(self):
""" 点击输入框事件"""
if self.point_open[self.NUMBER]:
if self.input_open[self.NUMBER]:
self.input_open[self.NUMBER] = False
for i in range(len(self.point_place)):
self.point_place[i] = 0
else:
self.input_open[self.NUMBER] = True
else:
self.input_open[self.NUMBER] = False
for i in range(len(self.point_place)):
self.point_place[i] = 0
def words(self, event):
""" 记录文本数据"""
if event.type == pygame.KEYDOWN:
try:
i = self.input_open.index(True)
if event.key == pygame.K_RETURN:
pass
elif event.key == pygame.K_BACKSPACE:
if self.point_place[i] == 0:
self.text[i][0] = self.text[i][0][:self.point_place[i]-1]
else:
self.text[i][0] = self.text[i][0][:self.point_place[i]-1] + \
self.text[i][0][self.point_place[i]:]
elif event.key == pygame.K_LEFT:
if self.point_place[i] > -len(self.text[i][0]):
self.point_place[i] -= 1
elif event.key == pygame.K_RIGHT:
if self.point_place[i] < 0:
self.point_place[i] += 1
else:
if self.point_place[i] == 0:
self.text[i][0] += event.unicode
else:
self.text[i][0] = self.text[i][0][:self.point_place[i]] + \
event.unicode + \
self.text[i][0][self.point_place[i]:]
except:
pass
def bill(self):
""" 显示文本数据"""
if self.POINT_KIND_IF:
a = int(self.sx / (self.sy/2))
else:
a = int(self.sx / (self.sy/2-1))
b = 0
if a <= len(self.text[self.NUMBER][0]):
b = len(self.text[self.NUMBER][0]) - a
FONT_XY.render_to(
screen, (self.gx+3, self.gy+3), self.text[self.NUMBER][0][b:a+b], fgcolor=self.TEXT_COLOR, size=self.sy-3
)
def start(self):
""" 加载输入框"""
try:
self.input_open[self.NUMBER]
self.house()
self.point()
self.bill()
except:
a = len(self.input_open)
b = self.NUMBER+1
for i in range(b-a):
self.input_open.append(False)
self.point_open.append(False)
self.text.append(['', ''])
self.point_place.append(0)
class Message(object):
""" 定义消息框"""
def message_error(self, TEXT, NOTE):
""" 错误消息框"""
message = wx.MessageDialog(None, caption=TEXT,
message=NOTE,
style=wx.OK | wx.ICON_ERROR)
message.ShowWindowModal()
def message_info(self, TEXT, NOTE):
""" 消息框"""
message = wx.MessageDialog(None, caption=TEXT,
message=NOTE,
style=wx.OK | wx.ICON_INFORMATION)
message.ShowWindowModal()
class Make_one_world(wx.Frame):
""" 定义创建新世界的GUI"""
def __init__(self, parent, id):
""" 定义初始化方法"""
wx.Frame.__init__(self, parent, id, '创建新世界',
pos=(460, 270), size=(400, 130))
# 创建面板
panel = wx.Panel(self)
# 创建“确定”和“取消”按钮,并绑定事件
self.bt_confirm = wx.Button(panel, label='确定')
self.bt_confirm.Bind(wx.EVT_BUTTON, self.OnclickSubmit)
self.bt_cancel = wx.Button(panel, label='取消')
self.bt_cancel.Bind(wx.EVT_BUTTON, self.OnclickCancel)
# 创建文本,左对齐
self.label_user = wx.StaticText(panel, label="世界名:")
self.text_user = wx.TextCtrl(panel, style=wx.TE_LEFT)
# 添加容器,容器中控件按横向并排排列
hsizer_user = wx.BoxSizer(wx.HORIZONTAL)
hsizer_user.Add(self.label_user, proportion=0, flag=wx.ALL, border=5)
hsizer_user.Add(self.text_user, proportion=1, flag=wx.ALL, border=5)
hsizer_pwd = wx.BoxSizer(wx.HORIZONTAL)
hsizer_button = wx.BoxSizer(wx.HORIZONTAL)
hsizer_button.Add(self.bt_confirm, proportion=0, flag=wx.ALIGN_CENTER, border=5)
hsizer_button.Add(self.bt_cancel, proportion=0, flag=wx.ALIGN_CENTER, border=5)
# 添加容器,容器中控件按纵向并排排列
vsizer_all = wx.BoxSizer(wx.VERTICAL)
vsizer_all.Add(hsizer_user, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=45)
vsizer_all.Add(hsizer_pwd, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=45)
vsizer_all.Add(hsizer_button, proportion=0, flag=wx.ALIGN_CENTER | wx.TOP, border=15)
panel.SetSizer(vsizer_all)
number = 0 # 新的世界 + number,————"0 <= number <= 10"
for i in range(len(Load_file.world_name)):
name_all = Load_file.world_name[i][0][6:]
if name_all == "新的世界 (" + str(number) + ")" or name_all == "新的世界":
number += 1
if number == 0:
self.text_user.SetValue("新的世界")
else:
self.text_user.SetValue("新的世界 (" + str(number) + ")")
def datawoulds(self, username):
""" 创建一个存档"""
adding = 'saves\\' + str(username)
try:
os.mkdir(adding)
Message.message_info("创建世界", "创建成功!")
def start_create(world_sd):
open(world_sd, 'a+')
start_create(adding + '\\' + 'block_image.dat')
start_create(adding + '\\' + 'block_list.dat')
start_create(adding + '\\' + 'world.dat')
start_create(adding + '\\' + 'character.dat')
except:
Message.message_error("错误", "创建失败!")
def OnclickSubmit(self, event):
""" 点击确定按钮,执行方法"""
username = self.text_user.GetValue()
if username == '':
Message.message_error('错误', '世界名不能为空!')
elif username != '':
if len(username.encode('GBK')) > 28:
Message.message_error('错误', '世界名长度不能超过28个字节!')
else:
Make_one_world.datawoulds(self, username)
def OnclickCancel(self, event):
""" 点击取消按钮,执行方法"""
self.text_user.SetValue("")
class Kill_one_world(wx.Frame):
""" 定义删除世界的GUI"""
def __init__(self, parent, id):
""" 定义初始化方法"""
wx.Frame.__init__(self, parent, id, title="删除世界",
pos=(450, 400), size=(400, 220))
self.world_data = Load_file.world_name[Load_file.fcd][0]
panel = wx.Panel(self)
title = wx.StaticText(panel, label='确定要删除?', pos=(60, 30))
font = wx.Font(24, wx.DEFAULT, wx.FONTSTYLE_NORMAL, wx.NORMAL)
title.SetFont(font)
wx.StaticText(panel, label='Name="' + str(self.world_data[6:]) + '"', pos=(80, 80))
fons = wx.Font(10, wx.DEFAULT, wx.FONTSTYLE_NORMAL, wx.NORMAL)
self.bt_confirm = wx.Button(panel, label='确定')
self.bt_confirm.Bind(wx.EVT_BUTTON, self.OnclickSubmit)
self.bt_cancel = wx.Button(panel, label='取消')
self.bt_cancel.Bind(wx.EVT_BUTTON, self.OnclickCancel)
hsizer_button = wx.BoxSizer(wx.HORIZONTAL)
hsizer_button.Add(self.bt_confirm, proportion=0, flag=wx.ALIGN_CENTER, border=5)
hsizer_button.Add(self.bt_cancel, proportion=0, flag=wx.ALIGN_CENTER, border=5)
vsizer_all = wx.BoxSizer(wx.VERTICAL)
vsizer_all.Add(hsizer_button, proportion=0, flag=wx.ALIGN_CENTER | wx.TOP, border=130)
panel.SetSizer(vsizer_all)
def OnclickSubmit(self, event):
""" 点击确定按钮,执行方法"""
try:
shutil.rmtree(self.world_data)
Message.message_info("删除世界", "删除成功!")
except:
Message.message_error("错误", "世界不存在!")
def OnclickCancel(self, event):
""" 点击取消按钮,执行方法"""
pass
class Start_interface(object):
""" 定义一个开始界面类"""
def __init__(self):
""" 定义初始化方法"""
# 背景图片路径
self.BG_IMAGE = [
'bg', 'logo', 'loading', 'world_gui'
]
# 背景音乐列表
self.MUSIC_LIST = [
'music\\C418 - Aria Math.mp3', 'music\\C418 - Haggstrom.mp3',
'music\\C418 - Haunt Muskie.mp3', 'music\\C418 - Mice on Venus.mp3',
'music\\C418 - Minecraft.mp3', 'music\\C418 - Minecraft_0.mp3',
'music\\C418 - Sweden.mp3', 'music\\C418-Menu.mp3'
]
self.click = False # 跳转(开始界面|False)或(选择世界|True)界面
self.click_set = False # 跳转(开始界面|False)或(游戏设置|True)界面
self.x, self.y = screen.get_size() # 获取窗口大小
def bg_music(self):
""" 播放背景音乐"""
# 定义背景音乐播放的函数
def bg_music(music_file, volume):
# 开始播放
pygame.mixer.music.load(os.path.join(os.getcwd(), music_file))
pygame.mixer.music.set_volume(volume) # 设置音量
# 当一首音乐被播放完以后
if pygame.mixer.music.get_busy() == False:
# 随机获取一首音乐路径并播放
bg_music(random.choice(self.MUSIC_LIST), MUSIC_MAIN)
pygame.mixer.music.play()
def bg_image(self):
""" 加载背景图片"""
insert_image(self.BG_IMAGE[0], WINDOW_SIZE, (0, 0))
mc_logo = insert_image_change(self.BG_IMAGE[1], (756, 121))
mc_logo.set_colorkey((238, 243, 250))
screen.blit(mc_logo, (self.x/2-756/2, 50))
for i in range(5):
FONT_XY.render_to(screen, (self.x/2-756/2+80-i, 185-i), 'Python--Edition--2D', fgcolor=(80, 80, 80), size=35)
pygame.draw.rect(screen, (128, 128, 128), (self.x/2-756/2+75, 215, 345, 1), 0)
def button_1(self, inherit):
""" 定义“开始游戏”按钮"""
Button_font.FONT_SIZE = fx = 30
Button_font.BUTTON_NAME = fy = "开始游戏"
Button_font.NMAE_XY = (self.x/2-fx*len(fy)/2, self.y/2-len(fy)/2-90)
Button_font.BUTTON_COLOR_F = (0, 200, 255)
Button_font.BUTTON_COLOR_T = (0, 255, 255)
Button_image.IMAGE_OFF = 'button_bg'
Button_image.IMAGE_ON = 'button_mo'
Button_image.IMAGE_SIZE_OFF = (250, 50)
Button_image.IMAGE_SIZE_ON = (252, 52)
Button_image.IMAGE_OFF_XY = (self.x/2-fx*len(fy)/2-70, self.y/2-len(fy)/2-100)
Button_image.IMAGE_ON_XY = (self.x/2-fx*len(fy)/2-71, self.y/2-len(fy)/2-101)
if inherit:
Button_image.button()
Button_font.button()
def button_2(self, inherit):
""" 定义“退出游戏”按钮"""
Button_font.BUTTON_NAME = fy = "退出游戏"
Button_font.NMAE_XY = (self.x/2-Button_font.FONT_SIZE*len(fy)/2, self.y/2-len(fy)/2+90)
Button_font.BUTTON_COLOR_F = (200, 0, 0)
Button_font.BUTTON_COLOR_T = (255, 0, 0)
Button_image.IMAGE_OFF = 'button_bg'
Button_image.IMAGE_ON = 'button_mo'
Button_image.IMAGE_SIZE_OFF = (250, 50)
Button_image.IMAGE_SIZE_ON = (252, 52)
Button_image.IMAGE_OFF_XY = (self.x/2-Button_font.FONT_SIZE*len(fy)/2-70, self.y/2-len(fy)/2+80)
Button_image.IMAGE_ON_XY = (self.x/2-Button_font.FONT_SIZE*len(fy)/2-71, self.y/2-len(fy)/2+79)
if inherit:
Button_image.button()
Button_font.button()
def button_3(self, inherit):
""" 定义“游戏设置”按钮"""
Button_font.BUTTON_NAME = fy = "游戏设置"
Button_font.NMAE_XY = (self.x/2-Button_font.FONT_SIZE*len(fy)/2, self.y/2-len(fy)/2)
Button_font.BUTTON_COLOR_F = (200, 0, 200)
Button_font.BUTTON_COLOR_T = (255, 0, 255)
Button_image.IMAGE_OFF = 'button_bg'
Button_image.IMAGE_ON = 'button_mo'
Button_image.IMAGE_SIZE_OFF = (250, 50)
Button_image.IMAGE_SIZE_ON = (252, 52)
Button_image.IMAGE_OFF_XY = (self.x/2-Button_font.FONT_SIZE*len(fy)/2-70, self.y/2-len(fy)/2-10)
Button_image.IMAGE_ON_XY = (self.x/2-Button_font.FONT_SIZE*len(fy)/2-71, self.y/2-len(fy)/2-11)
if inherit:
Button_image.button()
Button_font.button()
def press_1(self):
""" 点击“开始游戏”按钮"""
self.button_1(False)
if Button_image.press():
self.click = True
def press_2(self):
""" 点击“退出游戏”按钮"""
self.button_2(False)
if Button_image.press():
sys.exit()
def press_3(self):
""" 点击“游戏设置”按钮"""
self.button_3(False)
if Button_image.press():
self.click_set = True
class Selection_interface(object):
""" 定义一个选择世界界面类"""
def bg_image(self):
""" 加载背景图片"""
screen.blit(kill_all, (0, 0)) # 清空屏幕
Start_interface.click = True # 当按下开始游戏时
insert_image(Start_interface.BG_IMAGE[2], WINDOW_SIZE, (0, 0))
insert_image(
Start_interface.BG_IMAGE[3], (Start_interface.x-300, Start_interface.y-260),
(Start_interface.x/2-(Start_interface.x-300)/2, Start_interface.y/2-(Start_interface.y-260)/2)
)
# 显示标题
FONT_XY.render_to(screen, (Start_interface.x/2-75*4/2, 50), "选择世界", fgcolor=(200, 200, 0), size=75)
def key(self):
""" 键盘事件(快捷键)"""
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_ESCAPE]: # 当按下“Esc”键
Button_font.click_music()
Start_interface.click = False # 返回到主界面
def button_1(self, inherit):
""" 定义“返回”按钮"""
Button_font.FONT_SIZE = fx = 40
Button_font.NMAE_XY = (30, Start_interface.y-fx-30)
Button_font.BUTTON_NAME = "返回"
Button_font.BUTTON_COLOR_F = (0, 200, 255)
Button_font.BUTTON_COLOR_T = (0, 255, 255)
Button_image.IMAGE_OFF = 'button_bg'
Button_image.IMAGE_ON = 'button_oh'
Button_image.IMAGE_SIZE_OFF = (110, 60)
Button_image.IMAGE_SIZE_ON = (112, 62)
Button_image.IMAGE_OFF_XY = (15, Start_interface.y-fx-44)
Button_image.IMAGE_ON_XY = (14, Start_interface.y-fx-45)
if inherit:
Button_image.button()
Button_font.button()
def button_2(self, inherit):
""" 定义“创建新世界”按钮"""
Button_font.FONT_SIZE = fx = 30
Button_font.NMAE_XY = (Start_interface.x/4, Start_interface.y-fx-40)
Button_font.BUTTON_NAME = "创建新世界"
Button_font.BUTTON_COLOR_F = (0, 200, 0)
Button_font.BUTTON_COLOR_T = (0, 255, 0)
Button_image.IMAGE_OFF = 'button_bg'
Button_image.IMAGE_ON = 'button_oh'
Button_image.IMAGE_SIZE_OFF = (170, 50)
Button_image.IMAGE_SIZE_ON = (172, 52)
Button_image.IMAGE_OFF_XY = (Start_interface.x/4-11, Start_interface.y-fx-51)
Button_image.IMAGE_ON_XY = (Start_interface.x/4-12, Start_interface.y-fx-52)
if inherit:
Button_image.button()
Button_font.button()
def button_3(self, inherit):
""" 定义“删除世界”按钮"""
Button_font.NMAE_XY = (Start_interface.x/2, Start_interface.y-Button_font.FONT_SIZE-40)
Button_font.BUTTON_NAME = "删除世界"
Button_font.BUTTON_COLOR_F = (200, 0, 0)
Button_font.BUTTON_COLOR_T = (255, 0, 0)
Button_image.IMAGE_OFF = 'button_bg'
Button_image.IMAGE_ON = 'button_oh'
Button_image.IMAGE_SIZE_OFF = (140, 50)
Button_image.IMAGE_SIZE_ON = (142, 52)
Button_image.IMAGE_OFF_XY = (Start_interface.x/2-11, Start_interface.y-Button_font.FONT_SIZE-51)
Button_image.IMAGE_ON_XY = (Start_interface.x/2-12, Start_interface.y-Button_font.FONT_SIZE-52)
if inherit:
Button_image.button()
Button_font.button()
def press_1(self):
""" 点击“返回”按钮"""
self.button_1(False)
if Button_image.press():
Start_interface.click = False
def press_2(self):
""" 点击“创建新世界”按钮"""
self.button_2(False)
if Button_image.press():
app = wx.App()
Make_one_world(parent=None, id=-1).Show()
app.MainLoop()
def press_3(self):
""" 点击“删除世界”按钮"""
self.button_3(False)
if Button_image.press():
if len(Load_file.world_name) != 0:
app = wx.App()
Kill_one_world(parent=None, id=-1).Show()
app.MainLoop()
class Load_call_world(object):
""" 定义一个加载世界类"""
def __init__(self):
""" 定义初始化方法"""
self.JX, self.JY, self.JZ = (64, Start_interface.y-100, Start_interface.x/2-576/2) # 物品列表位置
self.tx, self.ty = (0, 1) # 物品列表间隔
self.block_use = [None, None, None, None, None, None, None, None, None] # 主物品栏
# 此内里的函数开关
self.select_box_open = True
self.block_throw_open = True
self.block_use_key_open = True
self.convenience_open = True
self.read_font_open = True
self.event_repeat_open = True
self.table_open = False
def event_repeat(self):
""" 加载物品列表"""
insert_image('inventory', (576, 64), (self.tx*self.JX+self.JZ, self.ty*self.JY))
if Get_into_world.image_number > 8:
Get_into_world.image_number = 0
elif Get_into_world.image_number < 0:
Get_into_world.image_number = 8
insert_image('choice', (68, 68), (self.tx*self.JX+self.JZ-2 + 64*Get_into_world.image_number,
self.ty*self.JY-2))
self.block_add_use()
def block_add_use(self):
""" 把已选择的物品添加到主物品栏"""
list_name_id = []
for name_primary, name_id in dic_list[Object_paper.object_point].items():
list_name_id.append(name_id[0])
try:
self.block_use[Get_into_world.image_number] = list_name_id[int(InputBox.text[0][1])]
InputBox.text[0][1] = ''
except:
pass
block_index = 0
for block in self.block_use:
insert_image(
block, (45, 45),
(self.tx*self.JX+self.JZ+10 + 64*block_index, self.ty*self.JY+10)
)
block_index += 1
def block_use_key_num(self):
""" 当按下数字键时"""
keys_pressed = pygame.key.get_pressed() # 键盘事件
if keys_pressed[pygame.K_1]:
Get_into_world.image_number = 0
elif keys_pressed[pygame.K_2]:
Get_into_world.image_number = 1
elif keys_pressed[pygame.K_3]:
Get_into_world.image_number = 2
elif keys_pressed[pygame.K_4]:
Get_into_world.image_number = 3
elif keys_pressed[pygame.K_5]:
Get_into_world.image_number = 4
elif keys_pressed[pygame.K_6]:
Get_into_world.image_number = 5
elif keys_pressed[pygame.K_7]:
Get_into_world.image_number = 6
elif keys_pressed[pygame.K_8]:
Get_into_world.image_number = 7
elif keys_pressed[pygame.K_9]:
Get_into_world.image_number = 8
def block_use_key(self):
""" 选定主物品栏的物品"""
if InputBox.input_open != []:
if not InputBox.input_open[0]:
self.block_use_key_open = False
self.block_use_key_num()
elif self.block_use_key_open:
self.block_use_key_num()
def block_throw(self):
""" 摧毁物品"""
keys_pressed = pygame.key.get_pressed() # 键盘事件
if keys_pressed[pygame.K_q]: # 当按下"Q"
self.block_use[Get_into_world.image_number] = None
def read_font(self):
""" 显示字体"""
CEPN_XY = (10, 10) # 显示坐标的位置
mx, my = Get_into_world.block_mxy()
# 显示当前坐标
FONT_XY.render_to(screen, CEPN_XY,
'坐标:(x=' + str(Get_into_world.ctcs_x + mx // Get_into_world.image_size) +
', y=' + str(Get_into_world.ctcs_y - my // Get_into_world.image_size) +
', z=' + str(Get_into_world.image_size) + ')',
(255-CA, 255-CB, 255-CC), size=15)
# 显示当前世界时间
FONT_XY.render_to(screen, (10, 30),
'时间:'+str(Get_into_world.day_time),
(255-CA, 255-CB, 255-CC), size=15)
def button_1(self, inherit):
""" 定义————[保存并退出世界]按钮"""
Button_font.FONT_SIZE = fx = 15
Button_font.BUTTON_NAME = fy = "保存并退出世界"
Button_font.NMAE_XY = (Start_interface.x-fx*len(fy)-10, 10)
Button_font.BUTTON_COLOR_F = (255-CA, 255-CB, 255-CC)
Button_font.BUTTON_COLOR_T = (255, 0, 0)
if inherit:
Button_font.button()
def select_box(self):
""" 绘制选定框"""
mx, my = Get_into_world.block_mxy()
pygame.draw.rect(
screen, (255-CA, 255-CB, 255-CC),
(mx, my, Get_into_world.image_size, Get_into_world.image_size), 1
)
def convenience(self, event):
""" 显示或隐藏信息"""
if event.type == pygame.KEYUP:
if event.key == pygame.K_o: # 当按下"O"
if self.read_font_open:
self.read_font_open = False
self.select_box_open = False
else:
self.read_font_open = True
self.select_box_open = True
elif event.key == pygame.K_p: # 当按下"P"
if self.event_repeat_open:
self.event_repeat_open = False
else:
self.event_repeat_open = True
elif event.key == pygame.K_i: # 当按下"I"
if self.table_open:
self.table_open = False
self.select_box_open = True
else:
self.table_open = True
self.select_box_open = False
def table(self):
""" 显示网格"""
for i in range(int(HIGHLY/Get_into_world.image_size)+1):
pygame.draw.rect(screen, (0, 255-CB, 255-CC),
(0, i*Get_into_world.image_size, WIDTH, 1), 0)
for i in range(int(WIDTH/Get_into_world.image_size)+1):
pygame.draw.rect(screen, (0, 255-CB, 255-CC),
(i*Get_into_world.image_size, 0, 1, HIGHLY), 0)
mx, my = Get_into_world.block_mxy()
pygame.draw.rect(
screen, (255-CA, 255-CB, 255-CC),
(mx, my+Get_into_world.image_size/2, Get_into_world.image_size, 1), 0
)
pygame.draw.rect(
screen, (255-CA, 255-CB, 255-CC),
(mx+Get_into_world.image_size/2, my, 1, Get_into_world.image_size), 0
)
class Get_into_world(object):
""" 定义一个进入世界类"""
def __init__(self):
""" 定义初始化方法"""
self.block_list, self.block_image = [], [] # 放置物体的所有id和坐标
self.image_number = 0 # 物品地址
self.image_size = 32 # 物体视距(z)
self.ctcs_x, self.ctcs_y = (0, 0) # 坐标计数器
self.SPEED_M, self.SPEED_L = (1, 3) # 坐标轴正常移动速度, 坐标轴最快移动速度
self.end_the_world = False # 退出当前世界条件
self.vibity_0, self.vibity_1 = (0, 0) # 区块加载范围
self.time_num = 0 # 时间时序计数器
self.time_speed = 64 # 时间流逝速度(帧)
self.ca, self.cb, self.cc, self.ue = (245, 255, 255, 1) # 天空颜色
self.day_time = 12 # 24小时进制————{1时=100分; 5分=64帧}
self.board = 0 # 是否静止当前时间————(0-默认,1-静止,2-向左流逝,3-向右流逝)
self.img_size, self.img_xy = [], [] # 所有物体的大小及位置
# 此类里的函数开关
self.kill_block_open = True
self.place_block_open = True
self.key_block_open = True
self.visibility_open = True
self.move_coordinate_system_open = True
self.mouse_end_world_open = True
# 定义物体的音效文件
self.sound_effect = [
['music\\1.mp3', 'music\\2.mp3', 'music\\8.mp3', 'music\\11.mp3', 'music\\18.mp3', 'music\\43.mp3',
'music\\cloth5.mp3'],
['music\\7.mp3', 'music\\25.mp3', 'music\\26.mp3', 'music\\27.mp3', 'music\\28.mp3', 'music\\cloth2.mp3',
'music\\cloth3.mp3', 'music\\wood3.mp3'],
['music\\12.mp3', 'music\\13.mp3'],
['music\\14.mp3', 'music\\15.mp3', 'music\\29.mp3'],
['music\\16.mp3', 'music\\cloth.mp3'],
['music\\3.mp3', 'music\\4.mp3', 'music\\5.mp3', 'music\\6.mp3','music\\9.mp3', 'music\\10.mp3',
'music\\17.mp3', 'music\\30.mp3','music\\33.mp3', 'music\\34.mp3', 'music\\cloth4.mp3']
]
def block_mxy(self):
""" 方格算法"""
mx, my = x // self.image_size * self.image_size, y // self.image_size * self.image_size
return mx, my
def block_music(self, imaget):
""" 播放物体音效"""
for name_primary, name_id in dic_list[Object_paper.object_point].items():
if name_id[0] == imaget:
if name_id[1] == 1:
list_music = self.sound_effect[0]
elif name_id[1] == 2:
list_music = self.sound_effect[1]
elif name_id[1] == 3:
list_music = self.sound_effect[2]
elif name_id[1] == 4:
list_music = self.sound_effect[3]
elif name_id[1] == 5:
list_music = self.sound_effect[4]
elif name_id[1] == 6:
list_music = self.sound_effect[5]
if name_id[1] != 0:
music_file = random.choice(list_music) # 随机获取一个音效文件路径
block_music = pygame.mixer.Sound(music_file)
block_music.set_volume(MUSIC_BLOCK)
block_music.play()
def kill_window(self):
""" 定义填充物体"""
kill = pygame.Surface((self.image_size, self.image_size), flags=pygame.SRCALPHA)
pygame.Surface.convert(kill)
kill.fill(pygame.Color(CA, CB, CC, 255))
return kill
def kill_block(self):
""" 鼠标左键填充物体"""
mx, my = self.block_mxy()
kill = self.kill_window()
screen.blit(kill, (mx, my)) # 填充一个物体
# 删除列表中的一个物体坐标
if [mx, my] in self.block_list:
block_image_zc = [] # 物体的位置
# 循环填充一个位置的物体直到物体被填完
while True:
try:
# 记录当前位置的物体id
block_image_zc = self.block_image[self.block_list.index([mx, my])]
del self.block_image[self.block_list.index([mx, my])]
self.block_list.remove([mx, my])
except:
# 播放填充的物体音效
self.block_music(block_image_zc)
break
def place_block(self):
""" 鼠标右键放置物品"""
mx, my = self.block_mxy()
self.kill_block() # 先填充
# 后放置
try:
image = Load_call_world.block_use[self.image_number]
# 添加一个物体坐标和物体id到列表
self.block_list.append([mx, my])
self.block_image.append(image)
self.block_music(image) # 播放当前物体音效
except:
pass
def key_block(self):
""" 获取键盘事件"""
key_repeat = pygame.key.get_pressed()
if key_repeat[pygame.K_x]: # 当按下"X"键
# 循环放置
while True:
self.place_block()
if key_repeat[pygame.K_x]: # 当"X"键弹起
break
elif key_repeat[pygame.K_z]: # 当按下"Z"键
# 循环填充
while True:
self.kill_block()
if key_repeat[pygame.K_z]: # 当"Z"键弹起
break
def start_moving(self, block_poe):
""" 刷新所有方块"""
# 清空物体坐标列表,添加新的物体坐标
del self.block_list[0:]
for ope_data in block_poe:
self.block_list.append(ope_data)
screen.blit(kill_all, (0, 0)) # 清空屏幕
# 所有的物体坐标和物体id————[[x, y, id], [x, y, id], ...]
block_nexy_all = []
for i in range(len(self.block_list)):
block_nexy_all.append([
self.block_list[i][0], self.block_list[i][1],
self.block_image[i]
])
# 根据物体id和坐标放置物体
del self.img_size[:], self.img_xy[:]
for block_data in block_nexy_all:
# 区域加载器
if block_data[0] < 0+self.vibity_1 or block_data[1] < 0+self.vibity_1 or \
block_data[0] > WIDTH-self.vibity_1 or block_data[1] > HIGHLY-self.vibity_1:
pass
else:
road = insert_image(
block_data[2], (self.image_size, self.image_size), (block_data[0], block_data[1])
)
if road != None:
self.img_size.append(road.get_rect())
self.img_xy.append([block_data[0], block_data[1]])
def visibility(self):
""" 区块加载范围"""
keys_pressed = pygame.key.get_pressed() # 键盘事件
if keys_pressed[pygame.K_PERIOD]: # 当按下"<"
self.vibity_0 += 1
elif keys_pressed[pygame.K_COMMA]: # 当按下">"
self.vibity_0 -= 1
if HIGHLY-self.vibity_1*2 < 0:
self.vibity_0 -= 1
elif HIGHLY-self.vibity_1*2 > HIGHLY:
self.vibity_0 += 1
self.vibity_1 = self.image_size * self.vibity_0
pygame.draw.rect(
screen, (0, 0, 255),(
self.vibity_1-1, self.vibity_1-1, WIDTH-self.vibity_1*2+2, HIGHLY-self.vibity_1*2+2
), 1
)
def move_coordinate_system(self):
""" 移动坐标系——(x, y, z)"""
xy_speed = False # 最快速度的条件
ZX, ZY = (0, 101) # (z)的取值范围————{z|z=(x, y), x>=0, (x, y)∈R}
keys_pressed = pygame.key.get_pressed() # 键盘事件
if keys_pressed[pygame.K_w]: # 当按下"W"
# 把所有物体的坐标加一格像素并添加到这个列表
block_poe = []
for block_xy in self.block_list:
if keys_pressed[pygame.K_LCTRL]: # 当按下"Ctrl"键
block_poe.append([block_xy[0], block_xy[1] + self.image_size * self.SPEED_L]) # 加速
xy_speed = True
else:
block_poe.append([block_xy[0], block_xy[1] + self.image_size * self.SPEED_M]) # 正常速度
self.start_moving(block_poe) # 调用后所有物体移动一格
# 记录当前坐标
if xy_speed:
self.ctcs_y += self.SPEED_L
else:
self.ctcs_y += self.SPEED_M
elif keys_pressed[pygame.K_s]: # 当按下"S"
block_poe = []
for block_xy in self.block_list:
if keys_pressed[pygame.K_LCTRL]: # 当按下"Ctrl"键
block_poe.append([block_xy[0], block_xy[1] - self.image_size * self.SPEED_L])
xy_speed = True
else:
block_poe.append([block_xy[0], block_xy[1] - self.image_size * self.SPEED_M])
self.start_moving(block_poe)
if xy_speed:
self.ctcs_y -= self.SPEED_L
else:
self.ctcs_y -= self.SPEED_M
if keys_pressed[pygame.K_a]: # 当按下"A"
block_poe = []
for block_xy in self.block_list:
if keys_pressed[pygame.K_LCTRL]: # 当按下"Ctrl"键
block_poe.append([block_xy[0] + self.image_size * self.SPEED_L, block_xy[1]])
xy_speed = True
else:
block_poe.append([block_xy[0] + self.image_size * self.SPEED_M, block_xy[1]])
self.start_moving(block_poe)
if xy_speed:
self.ctcs_x -= self.SPEED_L
else:
self.ctcs_x -= self.SPEED_M
elif keys_pressed[pygame.K_d]: # 当按下"D"
block_poe = []
for block_xy in self.block_list:
if keys_pressed[pygame.K_LCTRL]: # 当按下"Ctrl"键
block_poe.append([block_xy[0] - self.image_size * self.SPEED_L, block_xy[1]])
xy_speed = True
else:
block_poe.append([block_xy[0] - self.image_size * self.SPEED_M, block_xy[1]])
self.start_moving(block_poe)
if xy_speed:
self.ctcs_x += self.SPEED_L
else:
self.ctcs_x += self.SPEED_M
#### 物体视距范围(z)||默认(1~100)
if keys_pressed[pygame.K_LSHIFT] and self.image_size + 1 != ZY: # 当按下"Shift"键
self.image_size += 1
block_poe = []
for block_xy in self.block_list:
block_poe.append([(block_xy[0] / (self.image_size - 1)) * self.image_size,
(block_xy[1] / (self.image_size - 1)) * self.image_size])
self.start_moving(block_poe)
elif keys_pressed[pygame.K_SPACE] and self.image_size - 1 != ZX: # 当按下空格
self.image_size -= 1
block_poe = []
for block_xy in self.block_list:
block_poe.append([(block_xy[0] / (self.image_size + 1)) * self.image_size,
(block_xy[1] / (self.image_size + 1)) * self.image_size])
self.start_moving(block_poe)
def screen_repeat(self):
""" 刷新屏幕"""