#消消乐
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(90)
viz.go()
grid = vizshape.addGrid()#网格
grid.color(viz.RED)
grid.setAxisAngle(0,0,0,90)
grid.setPosition(0,0,0)
world_axes = vizshape.addAxes()#坐标轴
world_axes.setPosition(0,0,0)
world_axes.alpha(0.4)
#背景
ground=viz.addChild('tut_ground.wrl')
ground.collidePlane()
ground.setPosition(0,0,0,)
viz.MainView.setPosition([2, 4, 2])
viz.MainView.setAxisAngle(1,0,0,90)
d=0#y值与水平面的距离
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)
balls=[]
#初始界面
for i in range(9):
for j in range(7):
billball = viz.add('white_ball.wrl')
billball.setPosition(i-2,d,j-1)
billball.color(random.choice([viz.RED,viz.WHITE,viz.YELLOW,viz.GREEN]))
balls.append([billball,i,j,billball.getColor()])#列表存储:[球对象,横坐标,纵坐标,颜色值]
'''
if i ==0 or i==8 or j==0 or j==6:#界外黑球,无法与视界内的球相消#似乎可以不用
billball.color(viz.BLACK)
else :#视界内球,颜色相同达三个或以上,则相消
'''
#获取对象坐标值
def myGetPosition(object):
balls.sort(key = lambda x:x[0]!=object)
pos=balls[0][1:3]#[ball,i,j,color]
return pos
#获取对象四元组信息【ball,x,y,color】
def myGetBallsMassege(object):
balls.sort(key = lambda x:x[0]!=object)
ball_massege=balls[0]
return ball_massege
#在视界内的球中,先找到与被选球距离为1且相同颜色的球,在递归调用调用自身,找到其他相同颜色的球
same_color_balls=[]
def find_all_same_color_balls(object,color):
global same_color_balls#存储相邻同色球
for ball in balls:#ball是四元组,object是ball的第一个元素
x1=ball[1];y1=ball[2];x2=myGetPosition(object)[0];y2=myGetPosition(object)[1]
if ball not in same_color_balls and ball[3]==color and (x1-x2)**2+(y1-y2)**2==1 :
same_color_balls.append(ball)
find_all_same_color_balls(ball[0],color)
#移除小球
def remove_balls(balls_remove_list,type):#参数是四元组列表
score(len(balls_remove_list))
balls_remove_pos_list=[]
for ball in balls_remove_list:
balls_remove_pos_list.append(ball[1:3])
ball[0].remove()
balls.pop(balls.index(ball))
if type==1:
create_new_balls(balls_remove_pos_list)
else:
pass
#生成新的小球
def create_new_balls(pos_list):#pos是坐标列表
global balls
#viztask.waitTime(3)#?
for x in pos_list:
billball = viz.add('white_ball.wrl')
billball.setPosition(x[0]-2,d,x[1]-1)
billball.color(random.choice([viz.RED,viz.WHITE,viz.YELLOW,viz.GREEN]))
balls_four_massege=[billball,x[0],x[1],billball.getColor()]
balls.append(balls_four_massege)
#获取列表第二个元素
def tackSecond(elem):
return elem[1]
#下移更新法---添加球
def create_new_balls_by_movedown(pos_list,n):#n是该列消去的球数,用于计算下移球的初始位置
global balls
#viztask.waitTime(3)#?
for pos in pos_list:
billball = viz.add('white_ball.wrl')
billball.setPosition(pos[0]-2,d,pos[1]-1+n)
flyToPoint = vizact.moveTo([pos[0]-2,d,pos[1]-1],speed=2)
billball.addAction(flyToPoint)
billball.color(random.choice([viz.RED,viz.WHITE,viz.YELLOW,viz.GREEN]))
balls_four_massege=[billball,pos[0],pos[1],billball.getColor()]
balls.append(balls_four_massege)
#下移更新法---移动球
def move_down(balls_remove_list):
#print "####",balls_remove_list
remove_balls(balls_remove_list,2)
index_x=[]#被移除球的横坐标,每列取一个
list_x_row=[]#每个元素的属于x列的剩余元素的坐标集合列表,如[[1,2],[1,5],[1,3]]是一个元素
for move_ball in balls_remove_list:
index_xy=[]#被移动球的坐标,每列所有
x2=move_ball[1];y2=move_ball[2]#被移除球的坐标
for ball in balls:
x1=ball[1];y1=ball[2]
if x1==x2 and [x1,y1] not in index_xy:
index_xy.append([x1,y1])
if x1==x2 and x1 not in index_x:
index_x.append(x2)
index_xy.sort(key=tackSecond)#按y值由小到大排序,如[[1,2],[1,5],[1,3]]
if index_xy not in list_x_row and index_xy!=[]:
list_x_row.append(index_xy)#每一个元素的下标与index_x的下标对应,(如[[[1,2],[1,5],[1,3]],[[2,2],[2,5],[2,3]]]与[1,2]对应)
#print list_x_row
for x in index_x:
i=index_x.index(x)#横坐标为x的值在inde_x中的下标
row=list_x_row[i]#属于横坐标x的列,已排序
print "###",x,"i",i,row
n=1#x列的第n个球
for row_elem in row:
x2=row_elem[0];y2=row_elem[1];
for ball in balls:
x1=ball[1];y1=ball[2]
if x1==x2 and y1==y2:
flyToPoint = vizact.moveTo([x-2,d,n-2],speed=2)
ball[0].addAction(flyToPoint)#将球下移
balls[balls.index(ball)][2]=n-2#更新四元组列表被移动的球的纵坐标
n+=1
list_y_number=0
for move_ball in balls_remove_list:#求出属于x列的被移除球数
if move_ball[1]==x:
list_y_number+=1
print list_y_number
list_pos=[]#存储移空位置
for j in range(list_y_number):
list_pos.append([x,line_number-j-1])
create_new_balls_by_movedown(list_pos,list_y_number)
#固定视角
viz.mouse(viz.OFF)
#拾取选中
def pickBall():
global same_color_balls
object = viz.pick()
if object.valid():
print "###",object,object.getColor()
find_all_same_color_balls(object,object.getColor())
#print "same_color_balls",len(same_color_balls),same_color_balls
if len(same_color_balls)>2:
#remove_balls(same_color_balls,1)
move_down(same_color_balls)
same_color_balls=[]#在下一次查询相同球之前,将存储同色球的列表清空
vizact.onmousedown(viz.MOUSEBUTTON_LEFT, pickBall)
'''
#判断被选中目标的上左下右
def near_balls(object):
a=myGetBallsMassege(object)
balls.sort(key = lambda x:x[1:3]!=[a[1]+1,a[2]])
target_right = balls[0]
balls.sort(key = lambda x:x[1:3]!=[a[1]-1,a[2]])
target_left = balls[0]
balls.sort(key = lambda x:x[1:3]!=[a[1],a[2]+1])
target_up = balls[0]
balls.sort(key = lambda x:x[1:3]!=[a[1],a[2]-1])
target_down = balls[0]
return [target_up,target_down,target_left,target_right]#多值return组装在一个元组里
near_list=near_balls(object)#near_balls返回的每个元素都是列表
same_color_count=0
near_same_color_list=[]
def find_same_color_balls(near_list,color,n):
global near_same_color_list,same_color_count
up=near_list[0];down=near_list[1];left=near_list[2];right=near_list[3]#上下左右的球
if up[3]==color and n!=2 and up[2]!=6 and up not in near_same_color_list:#n==2是上一次搜索的球,不应该重复搜索
same_color_count+=1
near_same_color_list.append(up)
i=up[1]
j=up[2]
if i!=0 and i!=8 and j!=0 and j!=6:
find_same_color_balls(near_balls(up),color,1)
if down[3]==color and n!=1 and down[2]!=0 and down not in near_same_color_list:#n==2是上一次搜索的球,不应该重复搜索
same_color_count+=1
near_same_color_list.append(down)
i=down[1]
j=down[2]
if i!=0 and i!=8 and j!=0 and j!=6:
find_same_color_balls(near_balls(down),color,1)
if left[3]==color and n!=2 and left[2]!=6 and left not in near_same_color_list:#n==2是上一次搜索的球,不应该重复搜索
same_color_count+=1
near_same_color_list.append(left)
i=left[1]
j=left[2]
if i!=0 and i!=8 and j!=0 and j!=6:
find_same_color_balls(near_balls(left),color,1)
if right[3]==color and n!=2 and right[2]!=6 and right not in near_same_color_list:#n==2是上一次搜索的球,不应该重复搜索
same_color_count+=1
near_same_color_list.append(right)
i=right[1]
j=right[2]
if i!=0 and i!=8 and j!=0 and j!=6:
find_same_color_balls(near_balls(right),color,1)
print "#######",same_color_count
same_color_count=0
near_same_color_list=[]
'''
vizard--消消乐2.0
最新推荐文章于 2024-07-27 12:20:46 发布