用户:
小麦做的面包查看:0 回复:0 评论:0 创建时间:2022-10-30T10:22:45
源代码:
game.py
import json
from random import choice
from arcade import *
from easygui import choicebox
def conv(pos):
return 87.5+pos[0]*75,87.5+pos[1]*75
class Game(arcade.Window):
def __init__(self, width, height, title):
super().__init__(width, height, title)
self.setup()
def setup(self):
set_background_color(color.WHITE)
self.game_map = GAME_MAP
self.init=self.game_map['init']
self.target = self.game_map['target']
self.hinder = self.game_map['hinder']
self.select = None
self.move_num=0
try:
self.best_move=self.game_map['move']
except:
self.best_move='-'
def on_draw(self):
start_render()
draw_rectangle_outline(50+37.5*self.game_map['size'][0], 50+37.5*self.game_map['size'][1], 75*self.game_map['size'][0], 75*self.game_map['size'][1], color.BLACK)
draw_text(' 游戏玩法\n左键选中方块后使用方向键滚动方块\n方块有三种面(红,黄,蓝)(黄占4面)\n把所有方块按正确朝向放到虚线框里胜利\n黑色方块是障碍物',
50, 55+75*self.game_map['size'][1], color.BLACK, font_name=('simhei', 'PingFang'), font_size=13)
draw_text(f'已移动次数:{self.move_num} 历史最少移动次数:{self.best_move}',
50, 20, color.BLACK, font_name=('simhei', 'PingFang'), font_size=13)
for box in self.target:
x, y = conv(box)
d = box[2]
if d == 1:
draw_rectangle_outline(x, y, 60, 60, color.RED, 2)
elif 2 <= d <= 5:
draw_rectangle_outline(x, y, 60, 60, color.YELLOW, 2)
elif d == 6:
draw_rectangle_outline(x, y, 60, 60, color.BLUE, 2)
if d == 2:
draw_line(x-33, y+30, x+33, y+30, color.RED, 2)
if d == 3:
draw_line(x+30, y+33, x+30, y-33, color.RED, 2)
if d == 4:
draw_line(x+33, y-30, x-33, y-30, color.RED, 2)
if d == 5:
draw_line(x-30, y-33, x-30, y+33, color.RED, 2)
for box in self.init:
x, y = conv(box)
d=box[2]
if d==1:
draw_rectangle_filled(x, y, 60, 60, color.RED)
elif 2 <= d <= 5:
draw_rectangle_filled(x, y, 60, 60, color.YELLOW)
elif d == 6:
draw_rectangle_filled(x, y, 60, 60, color.BLUE)
if d == 2:
draw_line(x-33, y+30, x+33, y+30, color.RED, 5)
if d == 3:
draw_line(x+30, y+33, x+30, y-33, color.RED, 5)
if d == 4:
draw_line(x+33, y-30, x-33, y-30, color.RED, 5)
if d == 5:
draw_line(x-30, y-33, x-30, y+33, color.RED, 5)
try:
if self.init[self.select] == box:
draw_point(x, y, color.BLACK, 40)
except:
pass
for box in self.hinder:
x, y = conv(box)
draw_rectangle_filled(x, y, 60, 60, color.BLACK)
if not self.judge_game():
draw_text('游戏胜利!', 50+37.5*self.game_map['size'][0]-125, 50+37.5*self.game_map['size'][1]-25,
color.BLACK,font_name=('simhei', 'PingFang'),font_size=50)
with open('maps.json','w') as f:
try:
if self.move_num > cmaps[name]['move']:
cmaps[name]['move'] = self.move_num
except:
cmaps[name]['move'] = self.move_num
json.dump(cmaps,f)
def on_mouse_press(self, x, y, buttom, mod):
if self.judge_game():
for box in self.init:
cx,cy=conv(box)
if cx-30 < x < cx+30 and cy-30 < y <cy+30:
if self.select==self.init.index(box):
self.select=None
else:
self.select=self.init.index(box)
def on_key_press(self, symbol, modifiers):
if self.judge_game():
flag = 1
if not self.select is None:
if symbol == key.RIGHT and self.init[self.select][0] < self.game_map['size'][0]-1:
for box in self.init:
if self.init[self.select][0]+1 == box[0] and self.init[self.select][1] == box[1]:
flag = 0
for box in self.hinder:
if self.init[self.select][0]+1 == box[0] and self.init[self.select][1] == box[1]:
flag = 0
if flag:
self.move_num+=1
self.init[self.select][0]+=1
c = self.init[self.select][2]
if c==1:
self.init[self.select][2] = 3
elif c == 3:
self.init[self.select][2] = 6
elif c == 5:
self.init[self.select][2] = 1
elif c == 6:
self.init[self.select][2] = 5
if symbol == key.LEFT and self.init[self.select][0] > 0:
for box in self.init:
if self.init[self.select][0]-1 == box[0] and self.init[self.select][1] == box[1]:
flag = 0
for box in self.hinder:
if self.init[self.select][0]-1 == box[0] and self.init[self.select][1] == box[1]:
flag = 0
if flag:
self.move_num += 1
self.init[self.select][0] -= 1
c = self.init[self.select][2]
if c == 1:
self.init[self.select][2] = 5
elif c == 3:
self.init[self.select][2] = 1
elif c == 5:
self.init[self.select][2] = 6
elif c == 6:
self.init[self.select][2] = 3
if symbol == key.UP and self.init[self.select][1] < self.game_map['size'][1]-1:
for box in self.init:
if self.init[self.select][1]+1 == box[1] and self.init[self.select][0] == box[0]:
flag = 0
for box in self.hinder:
if self.init[self.select][1]+1 == box[1] and self.init[self.select][0] == box[0]:
flag = 0
if flag:
self.move_num += 1
self.init[self.select][1] += 1
c = self.init[self.select][2]
if c == 1:
self.init[self.select][2] = 2
elif c == 2:
self.init[self.select][2] = 6
elif c == 4:
self.init[self.select][2] = 1
elif c == 6:
self.init[self.select][2] = 4
if symbol == key.DOWN and self.init[self.select][1] > 0:
for box in self.init:
if self.init[self.select][1]-1 == box[1] and self.init[self.select][0] == box[0]:
flag = 0
for box in self.hinder:
if self.init[self.select][1]-1 == box[1] and self.init[self.select][0] == box[0]:
flag = 0
if flag:
self.move_num += 1
self.init[self.select][1] -= 1
c = self.init[self.select][2]
if c == 1:
self.init[self.select][2] = 4
elif c == 2:
self.init[self.select][2] = 1
elif c == 4:
self.init[self.select][2] = 6
elif c == 6:
self.init[self.select][2] = 2
def judge_game(self):
f2=1
for target_box in self.target:
f1=0
for box in self.init:
if target_box==box:
f1=1
if not f1:
f2=0
if f2:
return 0
return 1
if __name__ == '__main__':
with open('maps.json') as f:
maps = json.load(f)
with open('maps.json') as f:
cmaps = json.load(f)
name = choicebox('请选择关卡名', '复原方块', choices=list(maps))
GAME_MAP = maps[name]
game = Game(100+75*GAME_MAP['size'][0], 150+75*GAME_MAP['size'][1], '复原方块')
print('游戏玩法:左键选中方块后使用方向键滚动方块,方块有三种面(红,黄,蓝)(黄占4面),把所有方块按正确朝向放到虚线框里胜利,黑色方块是障碍物')
run()
setup.py
import json,os
if not os.path.exists('maps.json'):
with open('maps.json', 'w') as f:
json.dump({}, f)
class maps:
def __init__(self):
try:
with open('maps.json') as f:
self.maps=json.load(f)
print(self.maps)
except:
with open('maps.json', 'w') as f:
json.dump({}, f)
self.maps = {}
print(self.maps)
def add(self, name, init: list, target: list, hinder=[], size=(6, 6)): # [[,,],[,,]],[[,,],[,,]]
add_map={'init':init,'target':target,'hinder':hinder,'size':size}
self.maps[name]=add_map
with open('maps.json','w') as f:
json.dump(self.maps,f)
def pop(self,index):
self.maps.pop(index)
with open('maps.json', 'w') as f:
json.dump(self.maps, f)
def clean(self):
self.maps={}
with open('maps.json', 'w') as f:
json.dump(self.maps, f)
def copy(self, name='maps_.json'):
with open(name,'w') as f:
json.dump(self.maps,f)
map_file = maps()
map_file.clean()
map_file.add('第1关', [[0, 0, 1]],
[[4, 0, 1]], [], (5, 1))
map_file.add('第2关', [[1, 1, 5], [2, 1, 4], [1, 2, 2], [2, 2, 3]],
[[1, 1, 1], [2, 1, 1], [1, 2, 1], [2, 2, 1]], [], (4, 4))
map_file.add('第3关', [[0, 2, 1], [1, 2, 1], [2, 2, 1], [3, 2, 1]],
[[1, 1, 1], [2, 1, 1], [1, 2, 1], [2, 2, 1]], [], (4, 4))
map_file.add('第4关', [[1, 1, 2], [2, 1, 2], [1, 2, 4], [2, 2, 4]],
[[1, 1, 3], [2, 1, 5], [1, 2, 3], [2, 2, 5]], [], (4, 4))
安装方式:运行setup.py
游玩方式:运行game.py