• [模拟炉石](三)使用卡牌


    在上一篇中,了解了cocos2d中的collision manage, 它的作用不仅可以实现选中,利用它的碰撞检测可以实现很多其它的东西。

    这一篇要解决的问题就是如何将自己的手牌打出以及让场上的仆从进攻。

    1.给卡牌添加拖拽效果。

        def on_mouse_press(self, x, y, buttons, modifiers):
            self.startDraggin = True
            under_mouse = self.model.collman.objs_touching_point(x, y)
            if len(under_mouse) != 0:
                self.model.selected = under_mouse.pop()
                if hasattr(self.model.selected,"field"):
                    self.model.selected = None
                else:
                    self.model.selected.changeColor()
            buttons = self.model.collmanForButton.objs_touching_point(x, y)
            if len(buttons) != 0:
                print("button is clicked, {}, {}".format(x, y))
                self.nextStep()

    这里使用了.objs_touching_point(x,y)来获得被点击的东西,但是有可能获得了field(Layer), 所以需要进行一个判断。

    后面的button,是我在右下角放了一个结束回合的按钮,但是为了和卡牌分开,我使用了另外一个collision manage。

        def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
            if self.startDraggin:
                if self.model.selected:
                    self.oPostion = self.model.selected.position
                    self.model.selected.updatePosition(x, y)

    拖拽的时候更新被选中的卡牌的位置。注意要同时更新卡牌的cshape位置。

    2.为了实现使用卡牌的效果,其实就是当卡牌被拖拽到场上的时候,进行使用。所以将field加入cm中管理,

        def on_mouse_release(self, x, y, button, modifiers):
            #back to orginal position
            if self.model.selected:
                #is card put on field?
                if self.model.collman.they_collide(self.model.selected,self.model.field1Shape):
                    self.putCardToField()
                else:
                    for other in self.model.collman.iter_colliding(self.model.selected):
                        if hasattr(other, "card"):
                            self.attackEnemy(other.card)
                            break
                self.putCardBack()
            self.model.draw_hand()
            self.model.draw_field()
            self.startDraggin = False

    首先they_collide(self.model.selected,self.model.field1Shape)判断是否是和场地碰撞了。如果是就触发self.putCardToField(),这个方法就是使用卡牌。

    3.仆从进攻,接着上面,如果是两个卡牌碰撞了,那么久触发self.attackEnemy(other.card)

    上面就已经基本实现了。

    需要改进的是:

    1.如果卡牌有目标,那么应该不触发拖拽卡牌效果而是拖出一个箭头。

    2.在卡牌碰撞的时候,如果出现了多张卡牌,那么使用目标应该选择最近的一张。

    3.当鼠标拖拽到目标上面的时候,目标应该有响应效果。

    这些就不单独再写成博客了。

  • 相关阅读:
    idea教程--面板介绍
    idea教程--使用mave创建普通java项目
    【Spring】Spring中的Bean
    【Spring】创建一个Spring的入门程序
    【Spring】 Spring的核心容器
    【玩转算法】1、总结,算法思想,加油
    【高级排序算法】3、归并排序法的优化
    【高级排序算法】2、归并排序法的实现-Merge Sort
    【高级排序算法】1、归并排序法
    【排序基础】6、插入排序法的改进与选择排序的效率比较
  • 原文地址:https://www.cnblogs.com/wenning/p/5104053.html
Copyright © 2020-2023  润新知