Code Monkey home page Code Monkey logo

breakout-game's Introduction

点此处预览

基于 Canvas 的打砖块游戏,运用到面向对象**,对游戏元素和游戏场景进行抽象。运用事件发布/订阅完成场景切换。

碰撞检测原理

检测是否碰撞

通过计算圆心到矩形边上的最短距离来判断是否碰撞。

碰撞时回弹方向

每一帧都记录球的坐标,在碰撞时,通过上一帧球相对于矩形的位置,确定回弹方向。

参考文章

已知缺陷

  • 球只会以固定的角度镜面反弹,玩家不能有目的地控制球的回弹方向(角度)。同时镜面反弹有时会导致游戏进入死局。

  • 如果物体在帧与帧之间移动了足够远的距离,它就可能跑到另一个物体的内部,甚至直接穿过另一个物体。典型的例子就是当球碰撞到挡板侧边的同时,快速移动挡板,球就会进入到挡板内部无限反弹。

  • 如果球在某一帧同时撞击到多个物体,就会检测到多次撞击,然后形成无法确定的运动方向。

  • 游戏帧率和球的运动速度没有做到联动,球的速度是固定不变,所以球的实际运动速度会受帧率影响,帧数越高跑得越快。

  • 这个问题受限于碰撞检测算法,因为如果强行做到这一点,那么当低帧数时,为了保持同样的运动速度,势必要加大球的单位速度,然后就会引发上面第 2 点提到的问题。

第一点问题可以使用带弧度的挡板图片素材,然后通过撞击点的位置,给予不同的反弹角度。 后面三点问题则需要更高级的碰撞检测算法才可以解决。

breakout-game's People

Contributors

heiye9 avatar

Stargazers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.