Git Product home page Git Product logo

canvasgames's Introduction

canvasGames

自写canvas简易射鸡游戏,

https://github.com/supperjet/H5-Animation 处学习 万分感激...

demo https://codepen.io/koongMem/full/ALWAYz/

部分js参考,图片资源借用了一下~~~~

不知道怎么改私有呀。。。。

通用运算公式

角度旋转

dx = mouse.x - object.x;
dy = mouse.y - object.y;
object.rotation = Math.atan2(dy,dx)*180/Math.PI

平滑运动

value = center + Math.sin(angle)*range;
angle += speed;

正圆运动

x_position = centerX + Math.sin(angle)*radius;
y_position = centerY + Math.cos(angle)*radius;
angle += speed;

椭圆运动

x_position = centerX + Math.cos(angle)*radiusX;
y_position = centerY + Math.sin(angle)*radiusY;
angle += speed;

两点间距离

dx = x2 - x1;
dy = y2 - y1;
dist = Math.sqrt(dx*dx + dy*dy);

任意方向速度

vx = speed * Math.cos(angle);
vy = speed * Math.sin(angle);

任意方向加速度

ax = force * Math.cos(angle);
ay = force * Math.xin(angle);

改变速度

vx += ax;
vx += ay;

改变位置

object.x += vx;
object.y += vy;
/*1.移除一个超过边界的物体*/
if(object.x - object.width/2 > right || 
   object.x + object.width/2 < left ||
   object.y - object.height/2 > bottom ||
   object.y + object.height/2 < top){/*移除物体代码*/}

/* 2.重现一个超出边界的物体*/
if(object.x - object.width/2 > right || 
   object.x + object.width/2 < left ||
   object.y - object.height/2 > bottom ||
   object.y + object.height/2 < top){
    /*重新设置对象的位置和速度*/
   }

/*3. 边界环绕*/
if(object.x - object.width/2 > right){
    object.x = left - object.width/2;
}elseif(object.x + object.width/2 < left){
    object.x = object.width/2 + right;
}

if(object.y - object.height/2 > bottom){
    object.y = top - object.height/2;
}elseif(object.y + object.height/2 < top){
    object.y = object.height/2 + bottom;
}

canvasgames's People

Contributors

koongmem avatar

Watchers

 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.