• [模拟炉石](二)下一步要解决的问题


    下一步,我们要在上一篇的基础上解决这么几个问题:

    1.使用opengl绘图。利用pyglet.gl来绘制箭头,后面会使用到。

    2.如何判断选中。使用collision manage

    3.需要实现一个数组和图形node的绑定。






    opengl绘图


    试着绘制一个跟随鼠标指针的直线。


    1. class MainLayer(Layer):
    2.    is_event_handler = True
    3.    def __init__(self):
    4.        super(Layer, self).__init__()
    5.        self.isDraggin = False
    6.    def draw(self, *args, **kwargs):
    7.        if self.isDraggin:
    8.            glLineWidth = 3
    9.            glColor3ub(255, 255, 255)
    10.            glBegin(GL_LINE_STRIP)
    11.            glVertex2f(self.ox, self.oy)
    12.            glVertex2f(self.x, self.y)
    13.            glEnd()
    14.    def on_mouse_release(self, x, y, button, modifiers):
    15.        self.isDraggin = False
    16.    def on_mouse_press(self, x, y, button, modifiers):
    17.        self.ox = x
    18.        self.oy = y
    19.    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
    20.        self.x = x
    21.        self.y = y
    22.        self.isDraggin = True









    CollisionManage


    在上面的代码基础上,对一个sprite进行拖动

    1. class Ball(cocos.sprite.Sprite):
    2.    def __init__(self, center_x, center_y, radius):
    3.        super(Ball, self).__init__('circle6.png')
    4.        self.position = (center_x, center_y)
    5.        self.cshape = cm.CircleShape(eu.Vector2(center_x, center_y), radius)




    创建一个collman对象:

    1.        self.collman = cm.CollisionManagerBruteForce()
    2.        self.collman.add(self.ball)
    3.        self.isSelected = False




    改动一下之前的事件handler, 对对象的选择可以使用cm的objs_touching_point方法或者cshape的touches_point方法


    1.    def on_mouse_release(self, x, y, button, modifiers):
    2.        self.isDraggin = False
    3.        if self.isSelected:
    4.            self.ball.do(MoveTo((x, y), duration = 2))
    5.            self.ball.cshape.center = eu.Vector2(x, y)
    6.    def on_mouse_press(self, x, y, button, modifiers):
    7.        self.ox = x
    8.        self.oy = y
    9.        under_mouse = self.collman.objs_touching_point(x, y)
    10.        if len(under_mouse) == 0:
    11.            print("touch none")
    12.            self.isSelected = False
    13.        else:
    14.            print("touch :{}".format(under_mouse))
    15.            self.isSelected = True
    16.        if self.ball.cshape.touches_point(x, y):
    17.            print("ball is touched")






    绑定

    在之前的代码时有问题的:


    1.    def draw_field(self):
    2.        for child in self.field1.get_children():
    3.            if hasattr(child, "card"):
    4.                child.kill()
    5.        for child in self.field2.get_children():
    6.            if hasattr(child, "card"):
    7.                child.kill()
    8.        for index,card in enumerate(self.ctrl.field1):
    9.            cocoscard = CocosCard(card)
    10.            cocoscard.position = 50 + index*110,0
    11.            self.field1.add(cocoscard)
    12.        for index,card in enumerate(self.ctrl.field2):
    13.            cocoscard = CocosCard(card)
    14.            cocoscard.position = 50 + index*110,0
    15.            self.field2.add(cocoscard)

    我是先把原来绘制的删除了之后,又重新生成了相应的Card对象。实际上应该是,没有变化就不用重新绘制了。

    现在的问题是,图形界面上有这一组组的Card对象,它们是根据fireplace引擎中的一个个数组来变化的。所以需要将数组里的对象与图形界面中的Card对象绑定起来。

    这里我使用python的dict, 当原来数组中的对象不再存在的时候,将对应的Card删除。

    1.    def draw_field(self):
    2.        for child in self.field1.get_children():
    3.            if hasattr(child, "card"):
    4.                #child.kill()
    5.                if child.card not in self.ctrl.field1:
    6.                    child.kill()
    7.        for child in self.field2.get_children():
    8.            if hasattr(child, "card"):
    9.                #child.kill()
    10.                if child.card not in self.ctrl.field2:
    11.                    child.kill()
    12.        for card in self.ctrl.field1:
    13.            if card not in self.field1dict:
    14.                cocoscard = CocosCard(card)
    15.                self.field1.add(cocoscard)
    16.                self.field1dict[card] = cocoscard
    17.        for card in self.ctrl.field2:
    18.            if card not in self.field2dict:
    19.                cocoscard = CocosCard(card)
    20.                self.field2.add(cocoscard)
    21.                self.field2dict[card] = cocoscard
    22.        for index, card in enumerate(self.field1.get_children()):
    23.            card.position = 50 + index*110,0
    24.        for index, card in enumerate(self.field2.get_children()):
    25.            card.position = 50 + index*110,0
    26.    










  • 相关阅读:
    Python爬虫-05:Ajax加载的动态页面内容
    Python爬虫-04:贴吧爬虫以及GET和POST的区别
    Python-爬虫03:urllib.request模块的使用
    Python Numpy-基础教程
    8皇后算法
    迷宫算法
    归并排序
    查找算法
    排序算法
    设计模式
  • 原文地址:https://www.cnblogs.com/wenning/p/5096728.html
Copyright © 2020-2023  润新知