Step by Step 使用HTML5开发一个星际大战游戏(1)
本系列博文翻译自以下文章
http://blog.sklambert.com/html5-canvas-game-panning-a-background/
Languages: HTML5, JavaScript
Code: https://github.com/straker/galaxian-canvas-game/tree/master/part1
1.游戏背景滚动
最终的游戏演示界面如下:
控制:移动 – (←↑↓→)箭头
射击 – 空格
The HTML5 Page
<!DOCTYPE html> <html><head><title>Space Shooter Demo</title><style>canvas {position: absolute;top: 0px;left: 0px;background: transparent;}</style></head><body onload="init()"><!-- The canvas for the panning background --><canvas id="background" width="600" height="360">Your browser does not support canvas. Please try again with a different browser.</canvas><script src="space_shooter_part_one.js"></script></body> </html>
以上代码创建了一个600宽,360高的画布。
基础
创建一个封装全部图像的js对象:
/*** Define an object to hold all our images for the game so images* are only ever created once. This type of object is known as a* singleton.*/ var imageRepository = new function() {// Define imagesthis.background = new Image();// Set images srcthis.background.src = "imgs/bg.png"; }
接下来,我们创建Drawable对象,所有以后的可以运动的物体对象都继承于它,Drawable对象包含一个空的draw方法。
/*** Creates the Drawable object which will be the base class for* all drawable objects in the game. Sets up default variables* that all child objects will inherit, as well as the default* functions.*/ function Drawable() {this.init = function(x, y) {// Default variablesthis.x = x;this.y = y;}this.speed = 0;this.canvasWidth = 0;this.canvasHeight = 0;// Define abstract function to be implemented in child objectsthis.draw = function() {}; }
接下来我们创建背景
Background对象,注意红色部分的代码,红色2句代码是背景移动的核心。
第一句让背景从纵坐标0开始向下移动,第二句
让背景从纵坐标(0-画布)高度开始向下移动,这样就产生了背景在不断向下移动的效果。最后一句蓝色代码是将
Background对象的原形设置为
Drawable对象,继承
Drawable中的变量和方法。
/*** Creates the Background object which will become a child of* the Drawable object. The background is drawn on the "background"* canvas and creates the illusion of moving by panning the image.*/ function Background() {this.speed = 1; // Redefine speed of the background for panning// Implement abstract functionthis.draw = function() {// Pan backgroundthis.y += this.speed;this.context.drawImage(imageRepository.background, this.x, this.y);// Draw another image at the top edge of the first imagethis.context.drawImage(imageRepository.background, this.x, this.y - this.canvasHeight);// If the image scrolled off the screen, resetif (this.y >= this.canvasHeight)this.y = 0;}; } // Set Background to inherit properties from Drawable Background.prototype = new Drawable();
最后一步
创建Game对象,Game对象获得web页面中定义的画布,初始化背景对象Background,设置背景对象的
context以及画布宽,画布高属性。
/*** Creates the Game object which will hold all objects and data for* the game.*/ function Game() {/** Gets canvas information and context and sets up all game* objects.* Returns true if the canvas is supported and false if it* is not. This is to stop the animation script from constantly* running on older browsers.*/this.init = function() {// Get the canvas elementthis.bgCanvas = document.getElementById('background');// Test to see if canvas is supportedif (this.bgCanvas.getContext) {this.bgContext = this.bgCanvas.getContext('2d');// Initialize objects to contain their context and canvas// informationBackground.prototype.context = this.bgContext;Background.prototype.canvasWidth = this.bgCanvas.width;Background.prototype.canvasHeight = this.bgCanvas.height;// Initialize the background objectthis.background = new Background();this.background.init(0,0); // Set draw point to 0,0return true;} else {return false;}};// Start the animation loopthis.start = function() {animate();}; }
以下是动画功能的实现,其中requestAnimFrame类似一个timer,会不定期的回调 animate()函数; animate()函数中调用
game.background.draw();不断的重绘背景图片的位置,以实现背景滚动的动画效果。
/*** The animation loop. Calls the requestAnimationFrame shim to* optimize the game loop and draws all game objects. This* function must be a gobal function and cannot be within an* object.*/ function animate() {requestAnimFrame( animate );game.background.draw(); } /*** requestAnim shim layer by Paul Irish* Finds the first API that works to optimize the animation loop,* otherwise defaults to setTimeout().*/ window.requestAnimFrame = (function(){return window.requestAnimationFrame ||window.webkitRequestAnimationFrame ||window.mozRequestAnimationFrame ||window.oRequestAnimationFrame ||window.msRequestAnimationFrame ||function(/* function */ callback, /* DOMElement */ element){window.setTimeout(callback, 1000 / 60);}; })();
最后启动程序:
/*** Initialize the Game and starts it.*/ var game = new Game(); function init() {if(game.init())game.start(); }
最后的运行效果如下:
博主其他系列博文推荐:
1.网络采集软件核心技术剖析系列博文索引
2.手把手教你使用FineUI开发一个b/s结构的取送货管理信息系统系列博文索引
转载于:https://www.cnblogs.com/ice-river/p/4146182.html
Step by Step 使用HTML5开发一个星际大战游戏(1)相关推荐
- 开发一个Canvas小游戏 实现一个游戏“引擎”
前言 这个游戏其实在三四年前就写了,中间还重构过好几次,之前都是用简单的面向对象和函数式编程来写,游戏中的元素关系到还是分的挺开,但是游戏的渲染,运算等逻辑分的不够清晰,整个逻辑基本都是自顶向下的流水 ...
- 如何开发一个扫雷小游戏?
如何用C#开发一个扫雷小游戏? 十分自豪的说,计算机编程就是变魔术,每一个coder都是一个魔术师. 初学C#的时候,我相信很多人都和我一样,学会了基本语法,掌握了基本的数据结构,也见过了不少微软提供 ...
- 用python开发一个推箱子游戏
好的,为了开发一个推箱子游戏,你需要了解一些基本的编程概念,如变量,循环,条件语句和函数. 首先,你需要定义游戏场景,即箱子和人物所在的空间.你可以使用二维数组表示游戏场景,每个元素都代表一个格子. ...
- python坦克大战游戏_Python开发的坦克大战游戏
python开发的坦克大战游戏importpygamefrom pygame.sprite importSpriteimportsysimporttimeimportrandom SCREEN_WID ...
- 用java写一个坦克大战游戏
写一个坦克大战游戏需要具备一定的Java编程基础和对游戏开发的了解.具体实现步骤如下: 需求分析:明确游戏的目标.玩家.障碍物.子弹等要素,并制定游戏规则. 设计游戏界面:设计游戏界面,包括坦克.障碍 ...
- 如何开发一个小程序游戏?
小程序游戏开发需要开发人员具备以下几点能力: 有一定的编程基础,例如 JavaScript.TypeScript 至少熟悉一种游戏开发引擎,比如 Cocos.Unity等 对游戏机制.游戏系统有一定的 ...
- Silverlight C# 游戏开发:Flyer01开发一个有趣的游戏
前面扯了很多理论,虽然很无聊但是对于开发游戏来说非常的有用,在早年的开发环境,没有这么多可视的工具,一切靠的是对画面的理解以及游戏感觉Coding代码,然后不厌其烦的测试修改测试修改. 在未来的一段时 ...
- Python开发一个滑雪小游戏
擅长领域:Python开发一个小游戏 今日重点:一步步分析and越过亚马逊的反爬虫机制 一.如何搭建开发环境环境 一起来学pygame吧 游戏开发30例(开篇词)--环境搭建+游戏效果展示 windo ...
- C/C++游戏开发:从零开始,用C++编写一个潜艇大战游戏!
C++编写的原汁原味的潜艇大战游戏,这是一个国外C++高手编写的潜艇大战,其玩法和界面效果均和windows电脑中自带的潜艇大战十分相似,从编译情况来看,游戏开发时未使用第三方控件,在VC6环境下,可 ...
最新文章
- 图像分割在医学影像学中的应用(一)
- docker安装nacos步骤
- python是什么类型的语言
- [转载] 朴素贝叶斯python实现预测_Python实现朴素贝叶斯分类器的方法详解
- Struts2 注解
- web.config点滴:更改login控件对密码安全性的要求
- 88上的数学题目之二
- 学会这个方法,轻松为PDF文件加密,快来码住
- 【TSP问题】基于人工鱼群算法求解TSP问题matlab 源码
- edius隐藏快捷键_EDIUS快捷键大全
- 网络工程师考试知识点总结
- 传奇行会战攻略和战术技巧
- 【java网络】常用网络模型BIO
- Linux下启动Tomcat项目
- MATLAB的appdesigner背景图片设置
- Springboot中下划线转驼峰配置
- 世纪三部曲(全9册) 读后感
- 代码覆盖度-代码覆盖度概念以及度量方法
- 分离数字的python编码_把数字拆分成2的幂的和
- 团队作业之一:团队介绍及选题背景与意义
热门文章
- win7下import pytorch报错AttributeError: function 'AddDllDirectory' not found
- VS code前端配置
- Python中字符串的连接
- java系列4:数组初始化(省略格式)
- 06Matplotlib数据可视化--6.2散点图
- 杭州滨江工作方案:将区块链等产业与“数字滨江”、“数字经济”紧密相连
- 2020年Tor Project的加密货币捐款增加23%达23万美元
- LunarCrush将比特币批评家Peter Schiff列为第二大比特币影响者
- 这些将在新一年改变你的风控内容
- 产品要想跑得赢,政策定价来帮您