老莫的笔记本  
  
查看: 1163|回复: 0

Canvas 游戏起步封装【胖子制作】

[复制链接]

662

主题

878

帖子

5145

积分

超级版主

Rank: 8Rank: 8

积分
5145
发表于 2018-12-16 23:28:53 | 显示全部楼层 |阅读模式
1.html
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  6. <title>像素鸟测试</title>
  7. <meta name="description" content="">
  8. <meta name="keywords" content="">
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
  10. <link href="style/reset.css" rel="stylesheet">
  11. <link href="style/common.css" rel="stylesheet">
  12. <style>
  13.     #canvas{
  14.         border:1px solid #000;
  15.     }
  16. </style>
  17. </head>
  18. <body>
  19.     <canvas id="canvas"></canvas>
  20.     <script src="js/Game.js"></script>
  21.     <script>
  22.         var game = new Game({
  23.             id :'#canvas',
  24.             Rjsonurl : 'R.json'
  25.         });
  26.     </script>
  27. </body>
  28. </html>
复制代码

2.js/Game.js
  1. (function(){
  2.     // 一个类 是一个闭包
  3.     var Game = window.Game = function(params){
  4.         this.canvas = document.querySelector(params.id);
  5.         this.ctx = this.canvas.getContext("2d");
  6.         this.Rjsonurl = params.Rjsonurl;
  7.         // 帧编号
  8.         this.fno = 0;
  9.         // 设置画布的宽和高
  10.         this.init();
  11.         var self = this;
  12.         // 读取资源  这里有个注意的点,这是异步回调函数 ,方法得写在这里而不是外面
  13.         this.loadAllResource(function() {
  14.             // 这里表示所有资源读取完毕
  15.             self.start();
  16.         });
  17.     }
  18.     // 初始化,设置画布的宽度和高度
  19.     Game.prototype.init = function(){
  20.         // 读取视口的高度
  21.         var windowW = document.documentElement.clientWidth;
  22.         var windowH = document.documentElement.clientHeight;
  23.         // 验收
  24.         if(windowW>414)
  25.         {
  26.             windowW = 414;
  27.         }else if(windowW<320){
  28.             windowW = 320;
  29.         }
  30.         if(windowH >736){
  31.             windowH = 736;
  32.         }else if(windowH <500){
  33.             windowH = 500;
  34.         }
  35.         // 让canvas 匹配视口 不需要特意模拟手机
  36.         this.canvas.width = windowW;
  37.         this.canvas.height = windowH;
  38.     }
  39.     // 读取资源
  40.     Game.prototype.loadAllResource=function(callback){
  41.         this.R={}; //用于存放图片对象
  42.         var self =this;
  43.         var xhr = new XMLHttpRequest();
  44.         xhr.open('get', this.Rjsonurl ,true );
  45.         xhr.send(null);
  46.         xhr.onreadystatechange = function()
  47.         {
  48.             if(xhr.readyState==4){
  49.                 var Robj = JSON.parse(xhr.responseText);
  50.                 var loadNum = 0;
  51.                 for(var i=0; i<Robj.images.length; i++)
  52.                 {
  53.                     // 创建一个同名的类
  54.                     self.R[Robj.images[i].name] = new Image();
  55.                     // 图片预加载的请求.src=
  56.                     self.R[Robj.images[i].name].src = Robj.images[i].url
  57.                     // 监听
  58.                     self.R[Robj.images[i].name].onload = function (){
  59.                         // 计数
  60.                         loadNum++
  61.                         // 加载提示文字
  62.                         self.ctx.clearRect(0,0,self.canvas.width,self.canvas.height); //清屏
  63.                         self.ctx.font = "24px serif"; //字体样式
  64.                         self.ctx.textAlign ="center"; //对齐方式
  65.                         self.ctx.fillText('已加载'+loadNum+'项目',self.canvas.width/2,self.canvas.height*(1-0.618)/2);//文字与位置
  66.                         if(loadNum==Robj.images.length){
  67.                             callback();
  68.                         }
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.     }
  74.     // 启动游戏
  75.     Game.prototype.start = function(){
  76.         var self = this;
  77.         this.timer = setInterval(function(){
  78.             self.ctx.clearRect(0,0,self.canvas.width,self.canvas.height); //清屏
  79.             self.fno++;
  80.             self.ctx.fillText('FNO:'+self.fno,100,100);
  81.         },20)
  82.     }
  83.    
  84. })();
复制代码


3.资源文件 R.json
  1. {
  2.     "images":[
  3.         {"name" : "bg_day","url":"img/bg_day.png"},
  4.         {"name" : "bg_night","url":"img/bg_night.png"},
  5.         
  6.         {"name" : "bird0_0","url":"img/bird0_0.png"},
  7.         {"name" : "bird0_1","url":"img/bird0_1.png"},
  8.         {"name" : "bird0_2","url":"img/bird0_2.png"},
  9.         {"name" : "bird1_0","url":"img/bird1_0.png"},
  10.         {"name" : "bird1_1","url":"img/bird1_1.png"},
  11.         {"name" : "bird1_2","url":"img/bird1_2.png"},
  12.         {"name" : "bird2_0","url":"img/bird2_0.png"},
  13.         {"name" : "bird2_1","url":"img/bird2_1.png"},
  14.         {"name" : "bird2_2","url":"img/bird2_2.png"}
  15.     ]
  16. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表