用Python做的魔方游戏【超简陋版】
本帖最后由 Ewan-Ahiouy 于 2023-7-4 12:46 编辑用Python做的魔方游戏【超简陋版】
游戏背景
最近学习了Pygame,想做一个游戏练练手,就做了......{:5_108:}
正好喜欢魔方,就做了一个魔方游戏{:10_256:}
但是没有学过3D,就勉强做了个2D版的{:10_284:}
虽说好做了些,但还是花费了楼主几天时间{:10_250:}
所以......{:5_96:}
评点分不过分吧{:10_254:}
求求了!{:5_105:}
因为蒟蒻不会输入框,所以用按键表示魔方的各个操作,完整如下表{:10_305:}
按键rufdlbme
表示的操作RUFDLBMe
按键tigckv
表示的操作rufdlb
按键xyzs
表示的操作xyzs
如果你需要用这些操作的反操作( 就是比如 R' 这样的操作 ),则需要按下 n 后按下需要的按键即可( 如进行 R' 操作,需要按下 n ,在按下 r ,这样打出的就是 R' 的操作了 {:10_268:}
n 键其实就是正操作与反操作的转换键{:10_330:}
有些人可能看不懂这些 $%^&%{.;..}#%$%^ 字母是什么,可以看看下图{:10_305:}
https://s1.ax1x.com/2023/07/01/pCBhRVP.jpg
https://s1.ax1x.com/2023/07/01/pCBhgbt.jpg
https://s1.ax1x.com/2023/07/01/pCBh6KA.jpg
https://s1.ax1x.com/2023/07/01/pCBhcDI.jpg
游戏截图
( 图床大法真好!{:5_95:} 感谢发明者~ )
https://s1.ax1x.com/2023/07/01/pCBftk8.png
https://s1.ax1x.com/2023/07/01/pCBfG0P.png
https://s1.ax1x.com/2023/07/01/pCBfJTf.png
附加文件
就是个图标而已{:10_256:}
Code
import pygame
import sys
pygame.init()
class moFang:
""" 1: Yellow, 2: White, 3: Red, 4: Orange, 5: Blue, 6: Green """
def __init__(self, shang: str, xia: str, qian: str, hou: str, zuo: str, you: str):
self.shang =
self.xia =
self.qian =
self.hou =
self.you =
self.zuo =
self.caoZuoList =
self.zhiLing = ["R", "R'", "R2", "U", "U'", "U2", "F", "F'", "F2", "L", "L'", "L2", "D", "D'", "D2", "B", "B'", "B2", "M", "M'", "M2", "E", "E'", "E2", "x", "x'", "x2", "y", "y'", "y2", "z", "z'", "z2", "u", "u'", "u2", "r", "r'", "r2", "f", "f'", "f2", "l", "l'", "l2", "d", "d'", "d2", "b", "b'", "b2", "s", "s'", "s2"]
def xiaZhuan(self, List):
List, List, List, List, List, List, List, List = List, List, List, List, List, List, List, List
return List
def R(self):
tmp1, tmp2, tmp3 = self.shang, self.shang, self.shang
self.shang, self.shang, self.shang = self.qian, self.qian, self.qian
self.qian, self.qian, self.qian = self.xia, self.xia, self.xia
self.xia, self.xia, self.xia = self.hou, self.hou, self.hou
self.hou, self.hou, self.hou = tmp1, tmp2, tmp3
self.you = self.xiaZhuan(self.you)
def U(self):
tmp = self.qian.copy()
self.qian = self.you.copy()
self.you = self.hou[::-1]
self.hou = self.zuo[::-1]
self.zuo = tmp
self.shang = self.xiaZhuan(self.shang)
def F(self):
self.qian = self.xiaZhuan(self.qian)
tmp = self.shang
self.shang = , self.zuo, self.zuo]
self.zuo, self.zuo, self.zuo = self.xia[::-1]
self.xia = , self.you, self.you]
self.you, self.you, self.you = tmp
def D(self):
self.xia = self.xiaZhuan(self.xia)
tmp = self.qian.copy()
self.qian = self.zuo.copy()
self.zuo = self.hou.copy()
self.hou = self.you.copy()
self.you = tmp
def L(self):
self.zuo = self.xiaZhuan(self.zuo)
tmp1, tmp2, tmp3 = self.shang, self.shang, self.shang
self.shang, self.shang, self.shang = self.hou, self.hou, self.hou
self.hou, self.hou, self.hou = self.xia, self.xia, self.xia
self.xia, self.xia, self.xia = self.qian, self.qian, self.qian
self.qian, self.qian, self.qian = tmp1, tmp2, tmp3
def B(self):
self.hou = self.xiaZhuan(self.hou)
tmp = self.shang
self.shang = , self.you, self.you]
self.you, self.you, self.you = self.xia
self.xia = , self.zuo, self.zuo]
self.zuo, self.zuo, self.zuo = tmp
def M(self):
tmp1, tmp2, tmp3 = self.shang, self.shang, self.shang
self.shang, self.shang, self.shang = self.hou, self.hou, self.hou
self.hou, self.hou, self.hou = self.xia, self.xia, self.xia
self.xia, self.xia, self.xia = self.qian, self.qian, self.qian
self.qian, self.qian, self.qian = tmp1, tmp2, tmp3
def E(self):
tmp = self.qian.copy()
self.qian = self.zuo.copy()
self.zuo = self.hou.copy()
self.hou = self.you.copy()
self.you = tmp
def x(self):
self.mingLing("R M' L'")
def y(self):
self.mingLing("U E' D'")
def z(self):
self.mingLing("F y M y' B'")
def r(self):
self.mingLing("R M'")
def u(self):
self.mingLing("U E'")
def f(self):
self.mingLing("z D")
def d(self):
self.mingLing("D E")
def l(self):
self.mingLing("x' R")
def b(self):
self.mingLing("z' F")
def s(self):
self.mingLing("b' B")
def mingLing(self, mingling: str):
minglingList = mingling.split(" ")
for i in minglingList:
if i in self.zhiLing:
xiabiao = self.zhiLing.index(i) + 1
xiabiao = xiabiao // 3 - 1 if xiabiao % 3 == 0 else xiabiao // 3
if "'" in i:
for i in range(3):
self.caoZuoList()
elif "2" in i:
self.caoZuoList()
self.caoZuoList()
else:
self.caoZuoList()
width, height = 600, 600
color = [(255, 255, 0), (255, 255, 255), (255, 0, 0), (255, 140, 0), (0, 0, 255), (0, 255, 0)]
icon = pygame.image.load('imgs/icon.png')
mofang = moFang(
"""\
1 1 1
1 1 1
1 1 1\
""",
"""\
2 2 2
2 2 2
2 2 2\
""",
"""\
3 3 3
3 3 3
3 3 3\
""",
"""\
4 4 4
4 4 4
4 4 4\
""",
"""\
5 5 5
5 5 5
5 5 5\
""",
"""\
6 6 6
6 6 6
6 6 6\
"""
)
mianPos = [(0, 0, 90, 90), (0, 0, 90, 30), (0, 60, 90, 30), (0, 0, 30, 90), (60, 0, 30, 90)]
jianPan = [
pygame.K_r, pygame.K_u, pygame.K_f, pygame.K_d, pygame.K_l, pygame.K_b, pygame.K_m, pygame.K_e,
pygame.K_t, pygame.K_i, pygame.K_g, pygame.K_c, pygame.K_k, pygame.K_v,
pygame.K_x, pygame.K_y, pygame.K_z, pygame.K_s
]
caoZuo = [
"R", "U", "F", "D", "L", "B", "M", "E",
"r", "u", "f", "d", "l", "b",
"x", "y", "z", "s"
]
jiayin = False
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("魔方")
pygame.display.set_icon(icon)
def zhuangTai(zhuangtai: list, x: int, y: int):
my_x, my_y = x, y
for i in zhuangtai:
for j in i:
pygame.draw.rect(window, color, (my_x, my_y, 30, 30))
my_x += 30
my_y += 30
my_x = x
for i in range(5):
pygame.draw.rect(window, (0, 0, 0), (mianPos + x, mianPos + y, mianPos, mianPos), 5)
while True:
pygame.time.Clock().tick(60)
window.fill((255, 255, 255))
zhuangTai(mofang.shang, 255, 105)
zhuangTai(mofang.qian, 255, 105 + 90 + 10)
zhuangTai(mofang.xia, 255, 105 + 90 * 2 + 10 * 2)
zhuangTai(mofang.hou, 255, 105 + 90 * 3 + 10 * 3)
zhuangTai(mofang.zuo, 155, 105 + 90 + 10)
zhuangTai(mofang.you, 155 + 10 * 2 + 90 + 90, 105 + 90 + 10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
elif event.key == pygame.K_n:
jiayin = not jiayin
elif event.key in jianPan:
s = caoZuo
if jiayin:
s += "'"
mofang.mingLing(s)
pygame.display.update()
求评分!{:9_221:}
Update
2023/7/1
发表
2023/7/2
看见了 不二如是 大神的 申精#文章格式建议【官方指导】 和 小甲鱼 老师的 精华帖申请指南及奖励,赶紧更改了下格式嘿嘿
p y g l e t zhangjinxuan 发表于 2023-7-1 17:40
p y g l e t
? 不玩魔方,才刚来,零基础的我也看不懂代码,但觉得好厉害 666{:10_245:} 牛!
3D:https://www.glowscript.org/docs/VPythonDocs/index.html 另外你提到输入框的问题,Pygame是没有输入框滴
可以把这些动作做成按钮,按钮就用一个长方形,用pygame.font渲染字体,blit上去,然后处理MOUSEBUTTONUP事件 歌者文明清理员 发表于 2023-7-1 23:51
另外你提到输入框的问题,Pygame是没有输入框滴
可以把这些动作做成按钮,按钮就用一个长方形,用pygame.f ...
那得放多少个按钮啊{:10_245:} 66666666 工资:https://fishc.com.cn/forum.php?mod=viewthread&tid=230198&page=1&extra= 厉害! 很厉害呀,评分! yinda_peng 发表于 2023-7-2 09:20
很厉害呀,评分!
不敢当{:10_278:} liuhongrun2022 发表于 2023-7-2 09:18
厉害!
不敢当不敢当{:10_278:}
一个蒟蒻而已 还在学习中,感谢分享 Ewan-Ahiouy 发表于 2023-7-2 09:32
不敢当不敢当
一个蒟蒻而已
我比你还蒻
%%% {:5_106:} {:10_275:} 好强{:5_92:} Ewan-Ahiouy 发表于 2023-7-2 06:50
那得放多少个按钮啊
就是这样呀