下一步,我们要在上一篇的基础上解决这么几个问题:
1.使用opengl绘图。利用pyglet.gl来绘制箭头,后面会使用到。
2.如何判断选中。使用collision manage
3.需要实现一个数组和图形node的绑定。
opengl绘图
试着绘制一个跟随鼠标指针的直线。
class MainLayer(Layer):
is_event_handler = True
def __init__(self):
super(Layer, self).__init__()
self.isDraggin = False
def draw(self, *args, **kwargs):
if self.isDraggin:
glLineWidth = 3
glColor3ub(255, 255, 255)
glBegin(GL_LINE_STRIP)
glVertex2f(self.ox, self.oy)
glVertex2f(self.x, self.y)
glEnd()
def on_mouse_release(self, x, y, button, modifiers):
self.isDraggin = False
def on_mouse_press(self, x, y, button, modifiers):
self.ox = x
self.oy = y
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
self.x = x
self.y = y
self.isDraggin = True
CollisionManage
在上面的代码基础上,对一个sprite进行拖动
class Ball(cocos.sprite.Sprite):
def __init__(self, center_x, center_y, radius):
super(Ball, self).__init__(‘circle6.png‘)
self.position = (center_x, center_y)
self.cshape = cm.CircleShape(eu.Vector2(center_x, center_y), radius)
创建一个collman对象:
self.collman = cm.CollisionManagerBruteForce()
self.collman.add(self.ball)
self.isSelected = False
改动一下之前的事件handler, 对对象的选择可以使用cm的objs_touching_point方法或者cshape的touches_point方法
def on_mouse_release(self, x, y, button, modifiers):
self.isDraggin = False
if self.isSelected:
self.ball.do(MoveTo((x, y), duration = 2))
self.ball.cshape.center = eu.Vector2(x, y)
def on_mouse_press(self, x, y, button, modifiers):
self.ox = x
self.oy = y
under_mouse = self.collman.objs_touching_point(x, y)
if len(under_mouse) == 0:
print("touch none")
self.isSelected = False
else:
print("touch :{}".format(under_mouse))
self.isSelected = True
if self.ball.cshape.touches_point(x, y):
print("ball is touched")
绑定
在之前的代码时有问题的:
def draw_field(self):
for child in self.field1.get_children():
if hasattr(child, "card"):
child.kill()
for child in self.field2.get_children():
if hasattr(child, "card"):
child.kill()
for index,card in enumerate(self.ctrl.field1):
cocoscard = CocosCard(card)
cocoscard.position = 50 + index*110,0
self.field1.add(cocoscard)
for index,card in enumerate(self.ctrl.field2):
cocoscard = CocosCard(card)
cocoscard.position = 50 + index*110,0
self.field2.add(cocoscard)
我是先把原来绘制的删除了之后,又重新生成了相应的Card对象。实际上应该是,没有变化就不用重新绘制了。
现在的问题是,图形界面上有这一组组的Card对象,它们是根据fireplace引擎中的一个个数组来变化的。所以需要将数组里的对象与图形界面中的Card对象绑定起来。
这里我使用python的dict, 当原来数组中的对象不再存在的时候,将对应的Card删除。
def draw_field(self):
for child in self.field1.get_children():
if hasattr(child, "card"):
#child.kill()
if child.card not in self.ctrl.field1:
child.kill()
for child in self.field2.get_children():
if hasattr(child, "card"):
#child.kill()
if child.card not in self.ctrl.field2:
child.kill()
for card in self.ctrl.field1:
if card not in self.field1dict:
cocoscard = CocosCard(card)
self.field1.add(cocoscard)
self.field1dict[card] = cocoscard
for card in self.ctrl.field2:
if card not in self.field2dict:
cocoscard = CocosCard(card)
self.field2.add(cocoscard)
self.field2dict[card] = cocoscard
for index, card in enumerate(self.field1.get_children()):
card.position = 50 + index*110,0
for index, card in enumerate(self.field2.get_children()):
card.position = 50 + index*110,0
时间: 2024-10-08 15:38:36