#消消乐
import viz
import math
import vizact
import vizmat
import random
import vizcam
import viztask
import vizshape
import vizproximity
import vizinfo
import vizdlg
viz.setMultiSample(4)
viz.fov(80)
viz.go()
#viz.MainWindow.ortho(-10,10,-1,10,-10,10)
grid = vizshape.addGrid()#网格
grid.color(viz.RED)
grid.setAxisAngle(0,0,0,90)
world_axes = vizshape.addAxes()#坐标轴
world_axes.alpha(0.4)
#背景
ground=viz.addChild('tut_ground.wrl')
ground.collidePlane()
ground.setPosition(0,0,0,)
pic = viz.addTexture('getting started/1.jpg')
pic.wrap(viz.WRAP_T, viz.CLAMP_TO_BORDER)
pic.wrap(viz.WRAP_S,viz.CLAMP_TO_BORDER)
viz.MainView.setPosition([4, 4, 3])
viz.MainView.setAxisAngle(1,0,0,90)
line_number=7#行数
row_number=9#列数
#开局提示信息
start_message="""Are You Ready?"""
start_dialog = vizdlg.MessageDialog(message=start_message, title='Wellcome!!!', accept='Yes', cancel='No')
start_dialog.setScreenAlignment(viz.ALIGN_CENTER)
def show_start_dialog():
while True:
yield start_dialog.show()
if start_dialog.accepted:
pass
else:
viztask.schedule(show_start_dialog())
yield viztask.waitTime(10000)#关闭提示窗口的时间#有待改进
viztask.schedule(show_start_dialog())
#通关提示信息
pass_message="""You Win!!!"""
pass_dialog = vizdlg.MessageDialog(message=pass_message, title='Continue!!!', accept='Yes', cancel='No')
pass_dialog.setScreenAlignment(viz.ALIGN_CENTER)
def show_pass_dialog():
while True:
yield pass_dialog.show()
if pass_dialog.accepted:
scorePanel.score=0
scorePanel.setText('Score: {}'.format(scorePanel.score))
else:
viztask.schedule(show_pass_dialog())#在这调用另一个程序?!
yield viztask.waitTime(10000)#关闭提示窗口的时间
#通关判断
def prompt_pass(score):
if score>500:
viztask.schedule(show_pass_dialog())
import vizinfo
#打印得分
scorePanel = vizinfo.InfoPanel('Score: 0', icon=False)
scorePanel.score=0#初始得分
def score(n):
if n==3:
scorePanel.score += n
elif n==4:
scorePanel.score += n+1
elif n==5:
scorePanel.score += n+2
elif n==6:
scorePanel.score += n+3
else:
scorePanel.score += 2*n
scorePanel.setText('Score: {}'.format(scorePanel.score))
prompt_pass(scorePanel.score)
#球类
class ball():
def __init__(self,object,pos,color):
self.object=viz.add('getting started/ball.fbx')
self.pos=pos
self.color=color
def setBasicData(self):#为实例赋予坐标、颜色
self.object.setPosition(self.pos)
self.object.setEuler(0,260,0)
self.object.color(self.color)
self.object.setScale(0.3,0.3,0.3)
#self.object.texture(pic)
#self.object.appearance(viz.TEXDECAL)
example_object=[]
example_pos=[]
#添加实例对象
def add(i,j):
new_ball=ball('ball',[i,0,j],random.choice([viz.RED,viz.WHITE,viz.YELLOW,viz.GREEN]))#创建类的实例
new_ball.setBasicData()#为类赋予特定的位置、颜色、比例等状态信息
example_object.append([new_ball,new_ball.object])#vizChild与类的实例对象对应的列表[example,object]
example_pos.append([new_ball,new_ball.pos])
#遍历生成
[add(i,j) for i in range(row_number) for j in range(line_number)]
#根据viz对象获取实例
def objectgetexample(object):
for i in example_object:
if i[1]==object:
return i[0]
#根据位置获取实例
def posgetexample(pos):
for i in example_pos:
if i[1]==pos:
return i[0]
#获取当前列剩余球坐标
def posGetRowPos(x):
row_pos=[]
for e in example_pos:
if x==e[1][0]:# and pos[0]<e[1][2]
row_pos.append(e[1])
return row_pos
#获取上下左右的坐标
def get_wasd(pos):
up=[pos[0],0,pos[2]+1]
down=[pos[0],0,pos[2]-1]
left=[pos[0]-1,0,pos[2]]
right=[pos[0]+1,0,pos[2]]
return up,down,left,right
#找到与被选球相邻且相同颜色的球,在递归地找到其他相同颜色的球
found_pos=[]#当前所有同色球
def find_all_same_color_balls(example):
if example.pos not in found_pos:
found_pos.append(example.pos)
#yield viztask.waitTime(2)#这个怎么用
for pos in get_wasd(example.pos):
e=posgetexample(pos)
if e and e.color==example.color and pos not in found_pos:
found_pos.append(pos)
find_all_same_color_balls(posgetexample(pos))
print "选中的位置",example.pos,"周边同色位置",found_pos
#删除相连同色球
def remove_balls(found_pos):
score(len(found_pos))
for pos in found_pos:
example=posgetexample(pos)
example.object.remove()#删除球
example_object.pop(example_object.index([example,example.object]))
example_pos.pop(example_pos.index([example,example.pos]))
#新球生成,e代表实例
def create_new_e(x,n):
for y in range(n):
add(x,line_number+y)
def move(pos1,pos2):
e=posgetexample(pos1)
flyToPoint = vizact.moveTo(pos2,speed=2.5)
e.object.addAction(flyToPoint)#将球下移
e.pos=pos2#更新球的坐标
for e_pos_n in example_pos:
if e_pos_n[0]==e:
e_pos_n[1]=pos2#更新实例到坐标列表的元素
def one_boj_move(obj1,pos2):
e=objectgetexample(obj1)
#e2=objectgetexample(obj2)
flyToPoint = vizact.moveTo(pos2,speed=2.5)
e.object.addAction(flyToPoint)#将球下移
e.pos=pos2#更新球的坐标
for e_pos_n in example_pos:
if e_pos_n[0]==e:
e_pos_n[1]=pos2
def fill_x(x,n):#补全x列空出的n个位置
for i in range(n):
move([x,0,line_number+i],[x,0,line_number+i-n])
#下移
def move_down(found_pos):
index_x=[]
for pos in found_pos:
x=pos[0]
if x not in index_x:
index_x.append(x)
row_pos=posGetRowPos(x)#移除同色球后剩余的当前列球
for i,pos_n in enumerate(row_pos):#for i, v in enumerate(lt): 遍历列表中的元素,并自带下标
move(pos_n,[x,0,i])
n=line_number-len(posGetRowPos(x))#当前列空出的位置数
create_new_e(x,n)#为x列更新n个球
fill_x(x,n)
viz.mouse(viz.OFF)
def pickBall():
global found_pos
object=viz.pick()
if object.valid():
example=objectgetexample(object)
find_all_same_color_balls(example)
#print "pick color",example.color,"pick all same col pos",found_pos
if len(found_pos)>2:#found_pos是同色位置列表
remove_balls(found_pos)
move_down(found_pos)
found_pos=[]
pick_obj=[]
def pickBall2():
global found_pos,pick_obj
object=viz.pick()
if object.valid():
pick_obj.append(object)
print "len",len(pick_obj)
if len(pick_obj)==2:
pos1=objectgetexample(pick_obj[0]).pos;pos2=objectgetexample(pick_obj[1]).pos
#print "pos",pos1,pos2
one_boj_move(pick_obj[0],pos2)
one_boj_move(pick_obj[1],pos1)#两球位置对调
for object in pick_obj:
example=objectgetexample(object)
find_all_same_color_balls(example)
print "pick color",example.color,"pick all same col pos",found_pos
if len(found_pos)>2:#found_pos是同色位置列表
remove_balls(found_pos)
move_down(found_pos)
found_pos=[]
pick_obj=[]
if len(pick_obj)>2:
pick_obj=[]
vizact.onmousedown(viz.MOUSEBUTTON_LEFT, pickBall2)
'''
for i in range(row_number):
for j in range(line_number):
add(i,j)
'''
viz---消消乐4.0
最新推荐文章于 2024-08-26 18:56:03 发布