Canvas绘制小球(面向对象思想)
来源:互联网 发布:hl线切割软件下载 编辑:程序博客网 时间:2024/05/16 03:03
面向对象其实理解起来,说容易也容易,说难也确实很难,而且,明白什么是面向对象,却未必能写出来好的代码:
下面是一个简单的例子,大家可以参考一下:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style type="text/css"> * { margin: 0; padding: 0; } body { text-align: center; } button { margin: 80px auto 0; padding: 5px 10px; } canvas { margin: 10px auto 0; background: #000; } </style></head><body><button>走你</button><button>停止</button><br><canvas height="300" width="500"></canvas><script> window.onload = function(){ //UI设置 var canvas = document.getElementsByTagName('canvas')[0], btnGo = document.getElementsByTagName('button')[0], btnStop = document.getElementsByTagName('button')[1]; // 获取画布 var ctx = canvas.getContext("2d"); //画布尺寸 var canvasWidth = canvas.width; var canvasHeight = canvas.height; //变量声明 var planetArr = []; var timer = null //游戏设置 var playGames; //类定义 var Planet = function (x, y, vx, vy, r) { this.x = x; this.y = y; this.vx = vx; this.vy = vy; this.r = r; }; Planet.prototype.move = function () { this.x += this.vx; this.y += this.vy; if (this.x > canvasWidth - this.r) { this.x = canvasWidth - this.r; this.vx = -this.vx; } else if (this.x < this.r) { this.x = this.r; this.vx = -this.vx; } if (this.y > canvasHeight - this.r) { this.y = canvasHeight - this.r; this.vy = -this.vy; } else if (this.y < this.r) { this.y = this.r; this.vy = -this.vy; } ctx.beginPath(); ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false); ctx.fill(); }; //重置和重启游戏 function startGame() { //游戏设置,初始化 var x, y, vx, vy, r, planet; for (var i = 0; i < 100; i++) { x = Math.random() * canvasWidth; y = Math.random() * canvasWidth; vx = Math.random() * 5 * (Math.random() > 0.5 ? 1 : -1); vy = Math.random() * 5 * (Math.random() > 0.5 ? 1 : -1); r = Math.random() * 8 + 2; planet = new Planet(x, y, vx, vy, r); planetArr.push(planet); } ctx.fillStyle = 'white'; // 游戏事件绑定 //开始游戏循环 animate(); }; //初始化游戏环境 function init() { btnGo.onclick = function (e) { playGames = true; animate(); }; btnStop.onclick = function () { playGames = false; } startGame(); }; //动画循环 function animate() { //清除 ctx.clearRect(0, 0, canvasWidth, canvasHeight); //动画设置 for (var i = 0; i < planetArr.length; i++) { planetArr[i].move(); } if (playGames) { //设置循环时间 clearTimeout(timer); timer = setTimeout(animate, 33); } } //开始游戏 init(); }</script></body></html>
阅读全文
0 0
- Canvas绘制小球(面向对象思想)
- Canvas绘制页面小球跳动
- Canvas绘制平抛小球效果
- 绘制canvas彩色泡泡小球碰撞
- h5 canvas绘制的旋转小球
- Canvas绘制小球运动,检测碰撞变色
- [面向对象的案例]在canvas画布内实现小球的随机移动
- OO思想 (面向对象思想)
- 面向对象思想(一)
- 面向对象思想(一)
- 面向对象思想(二)
- js面向对象之躁动的小球()
- ES6面向对象--实现绚丽小球(跟随鼠标)
- 面向对象的思想(上)
- 面向对象的思想(下)
- 面向对象设计思想(C#)
- 面向对象设计思想(C#)
- 面向对象设计思想(C#)
- java自动拆装箱不可避免遇到的一些问题
- C# 用代码添加textBox组件
- 160个破解练习之CrackMe 008
- Java基础之多态的详细解释
- 第8周 项目3- 对称矩阵压缩存储的实现与应用(2)
- Canvas绘制小球(面向对象思想)
- audio在ios微信浏览器中不支持自动播放问题
- Masonry学习之UIScrollView
- Spring的@Transactional事务处理详解
- DateUtil
- nested exception is java.sql.SQLException: 索引中丢失 IN 或 OUT 参数::X
- java soket编程
- JAVA API文档
- 基于深度学习的风格迁移转换的两种实现——style-transfer和fast-neural-style-tensorflow