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.游戏背景滚动
最终的游戏演示界面如下:
控制:移动 – (←↑↓→)箭头
射击 – 空格
- <!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 images
- this.background = new Image();
- // Set images src
- this.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 variables
- this.x = x;
- this.y = y;
- }
- this.speed = 0;
- this.canvasWidth = 0;
- this.canvasHeight = 0;
- // Define abstract function to be implemented in child objects
- this.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 function
- this.draw = function() {
- // Pan background
- this.y += this.speed;
- this.context.drawImage(imageRepository.background, this.x, this.y);
- // Draw another image at the top edge of the first image
- this.context.drawImage(imageRepository.background, this.x, this.y - this.canvasHeight);
- // If the image scrolled off the screen, reset
- if (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 element
- this.bgCanvas = document.getElementById('background');
- // Test to see if canvas is supported
- if (this.bgCanvas.getContext) {
- this.bgContext = this.bgCanvas.getContext('2d');
- // Initialize objects to contain their context and canvas
- // information
- Background.prototype.context = this.bgContext;
- Background.prototype.canvasWidth = this.bgCanvas.width;
- Background.prototype.canvasHeight = this.bgCanvas.height;
- // Initialize the background object
- this.background = new Background();
- this.background.init(0,0); // Set draw point to 0,0
- return true;
- } else {
- return false;
- }
- };
- // Start the animation loop
- this.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();
- }
- 最后的运行效果如下:
博主其他系列博文推荐:
2.手把手教你使用FineUI开发一个b/s结构的取送货管理信息系统系列博文索引
Step by Step 使用HTML5开发一个星际大战游戏(1)的更多相关文章
- Step by Step 使用HTML5开发一个星际大战游戏(2)
HTML5 Canvas Game: 玩家飞船 本系列博文翻译自以下文章 http://blog.sklambert.com/html5-canvas-game-the-player-ship/ L ...
- 使用HTML5开发Kinect体感游戏
一.简介 我们要做的是怎样一款游戏? 在前不久成都TGC2016展会上,我们开发了一款<火影忍者手游>的体感游戏,主要模拟手游章节<九尾袭来 >,用户化身四代,与九尾进行对决, ...
- Cocos2d-x-Lua 开发一个简单的游戏(记数字步进白色块状)
Cocos2d-x-Lua 开发一个简单的游戏(记数字步进白色块状) 本篇博客来给大家介绍怎样使用Lua这门语言来开发一个简单的小游戏-记数字踩白块. 游戏的流程是这种:在界面上生成5个数1~5字并显 ...
- 使用Cocos2dx-JS开发一个飞行射击游戏
一.前言 笔者闲来无事,某天github闲逛,看到了游戏引擎的专题,引起了自己的兴趣,于是就自己捣腾了一下Cocos2dx-JS.由于是学习,所谓纸上得来终觉浅,只是看文档看sample看demo,并 ...
- HTML5 - 开发一个自己的websocket服务器
应用:node.js 主要步骤: 创建文件夹 创建app.js(server入口,app为自定义命名) npm init -y (快速创建一个package.json文件) 依赖包安装:nodejs- ...
- 从零开始手把手教你使用javascript+canvas开发一个塔防游戏01地图创建
项目演示 项目演示地址: 体验一下 项目源码: 项目源码 代码结构 本节做完效果 游戏主页面 index.html <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- 用Python做一个飞机大战游戏
基于pygame的一款小游戏 这是我上半年做的一款小游戏,但是一直忘记了,现在才上传代码. github项目地址:StarMan 代码基于pygame,Python版本3.5.2运行正常. 游戏很简单 ...
- Nginx 模块开发(1)—— 一个稍稍能说明问题模块开发 Step By Step 过程
1. Nginx 介绍 Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,它的发音为“engine X”, 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/S ...
- Step by Step: 基于MFC下的COM组件开发-Helloworld
http://blog.csdn.net/sybifei/article/details/45008745 [这篇文章有问题, 仅供参考] http://blog.csdn.net/define_us ...
随机推荐
- 动态规划:状压DP-斯坦纳树
最小生成树是最小斯坦纳树的一种特殊情况 最小生成树是在给定的点集和边中寻求最短网络使所有点连通 而最小斯坦纳树允许在给定点外增加额外的点,使生成的最短网络开销最小 BZOJ2595 题意是给定一个棋盘 ...
- [洛谷P3501] [POI2010]ANT-Antisymmetry
洛谷题目链接:[POI2010]ANT-Antisymmetry 题目描述 Byteasar studies certain strings of zeroes and ones. Let be su ...
- 【UOJ131/NOI2015D2T2-品酒大会】sam求后缀树
题目链接:http://uoj.ac/problem/131 题意:给出一个字符串,第i个字符对应的值为a[i], 对于i∈[0,n),求最长公共前缀大于等于i的字串对个数,并求这些字符串对开头对应值 ...
- [bzoj1823][JSOI2010]满汉全席——2-SAT
题目大意 题目又丑又长我就不贴了,说一下大意,有n种菜,m个评委,每一个评委又有两种喜好,每种菜有满汉两种做法,只能选一种.判断是否存在一种方案使得所有评委至少喜欢一种菜品.输入包含多组数据. 题解 ...
- 常用的find命令
find命令 find [路径名] –name/-size/-perm find [路径名] –name “*p” 在路径搜索p结尾的文件夹及文件 find [路径名] –name “[ab]*” 在 ...
- Navigator与UserAgent笔记
关于Navigator与UserAgent笔记 1.Navigator笔记 Navigator对象主要是包含有关客户端浏览器的一些信息,Navigator对象是由JavaScript runtime ...
- css样式表中的样式覆盖顺序(转)
有时候在写CSS的过程中,某些限制总是不起作用,这就涉及了CSS样式覆盖的问题,如下 Css代码 #navigator { height: 100%; width: 200; position: ...
- C# WinForm开发 取消窗体关闭按钮
//禁用窗体的关闭按钮 private const int CP_NOCLOSE_BUTTON = 0x200; protected override CreateParams CreateParam ...
- 运行微信支付demo
首先要说说写这篇文章的初衷:集成支付宝支付运行demo都是可以正常运行的,但是我下载下来微信支付的demo,却发现一大堆报错,而且相关文章几乎没有,可能大家觉得没必要,也许你觉得很简单:但是技术大牛都 ...
- linux下redis的最佳实践(Master-Slave)
本文演示了redis在同一台linux上的安装及运行多个实例,并演示了主从复制,以及如何进行主从的切换. 1. 下载 $ wget http://download.redis.io/releases/ ...