用原生js写小游戏--Flappy Bird

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
    <style type="text/css">
         body {
            margin: 0;
            padding: 0;
        }

        #game {
            width: 800px;
            height: 600px;
            border: 1px solid #000;
            background: url(images/sky.png);
            overflow: hidden;
            position: relative;
        }

        #game .pipeD {
            background: url(images/pipe1.png) top center;
            position: absolute;
        }

        #game .pipeU {
            background: url(images/pipe2.png) bottom center;
            position: absolute;
        }

        #bird {
            width: 34px;
            height: 25px;
          /*  border-radius: 10px;
            background-color: red;*/
            position: absolute;
            top: 100px;
            left: 100px;
            background: url(images/birds.png) -8px -10px no-repeat;
        }
    </style>

</head>

<body>

    <div id="game">
        <div id="bird"></div>
    </div>
</body>
<script type="text/javascript">
    var birdElement = document.getElementById("bird");
    var game = document.getElementById("game");
    var gameover = false;
    var g = 1;
    // var i = 0;
    var timer=null;
    var bird = {
        x: birdElement.offsetLeft,
        y: birdElement.offsetTop,
        speedX: 5,
        speedY: 0,
        start: birdElement
    };
    var sky = {
        x: 0
    };

 game.onclick=function(){

    setInterval(function () {
        //游戏没有结束的时候运行代码
        if (!gameover) {

            //整个游戏背景x轴移动的距离
            sky.x = sky.x - bird.speedX;
            game.style.backgroundPositionX = sky.x + "px";
            //小鸟下落时y轴的坐标
            bird.speedY = bird.speedY + g;
            //设置一个变量用来接收小鸟下落时y轴的坐标,用来设置小鸟下降时的速度
            var step = bird.speedY;
            bird.y = bird.y + step;

            //用一个变量来设定小鸟下落的最低高度,用来 判断游戏是否结束
            var overY = game.offsetHeight - birdElement.offsetHeight;

            //小鸟的y轴坐标大于最低高度,所以游戏停止
            if (bird.y > overY) {
                bird.y = overY;
                stop(sky.x);
            }
            //小鸟的y轴坐标小于0,说明碰到顶部边框,所以游戏结束
            if (bird.y < 0) {
                bird.y = 0;
                stop(sky.x);
            }
            //设置游戏开始时小鸟出现的位置
            bird.start.style.top = bird.y + "px";
            // console.log(bird.x)
        }
    }, 30);

    //添加键盘事件,实现键盘上下键控制小鸟
    document.onkeyup = function (e) {
        if (e.keyCode === 38) {
            bird.speedY = -10;
        }
    }

    function Pipe(positonX) {
        //管子的坐标
        this.x = positonX;
        this.upPipeY = 0;
        this.width = 52;
        this.upPipeH = random(100,275);
        this.downPipeY = this.upPipeH + 200;
        this.downPipeH = game.offsetHeight - this.downPipeY;
        // 动态添加管子
        var divUp = document.createElement("div");
        divUp.className = "pipeU";
        divUp.style.width = this.width + "px";
        divUp.style.height = this.upPipeH + "px";
        divUp.style.left = this.x + "px";
        divUp.style.top = this.upPipeY + "px";
        game.appendChild(divUp);
        var divDown = document.createElement("div");
        divDown.className = "pipeD";
        divDown.style.width = this.width + "px";
        divDown.style.height = this.downPipeH + "px";
        divDown.style.left = this.x + "px";
        divDown.style.top = this.downPipeY + "px";
        game.appendChild(divDown);
        //因为定时器会混乱this的指向问题,所以提前保存this的指向,这里的this指向调用该方法的实例
        var that = this;
        // 设置定时器让管子向后移动
        this.timer=setInterval(function () {
            //这里1控制的是管子移动的速度  数字越大 难度越高
            that.x = that.x - 5;
            //简单实现管子无缝滚动,视觉差
            if (that.x < -52) {
                that.x = 800;
            }
            if (!gameover) {
                divUp.style.left = that.x + "px";
                divDown.style.left = that.x + "px";
            }
            // 设置变量,进行游戏碰撞检测,并停止游戏
            var downBoom = (bird.x + 34 > that.x) && (bird.x < that.x + 52) && (bird.y + 25 > that.downPipeY);
            var upBoom = (bird.x + 34 > that.x) && (bird.x < that.x + 52) && (bird.y < that.upPipeH);
            if (downBoom || upBoom) {
                //gameover = true;
                stop(sky.x);
            }
        }, 30);
    }
    //执行上面的函数方法
    var arr=[];
    for (var i = 0; i < 4; i++) {
        arr[i]=new Pipe(i * 200 + 400);
    }
    //封装一个用来停止游戏的方法,
    function stop(Sky){
        var a;
        gameover=true;
        clearInterval(timer);
        for(var i=0;i<arr.length;i++){
            clearInterval(arr[i].timer);
        }
        if(Math.abs(Sky)<=352){
            a=0;
            alert("成绩:"+a)
        }else{
            a=Math.floor((Math.abs(Sky)-300)/200+1);
            // a=Math.ceil(((Math.abs(Sky)-300))/200);
            alert("成绩:"+a)
        }
        // console.log(Sky)
    }
}

function random(a,b){
    return Math.round(Math.random()*(a-b)+b)
}

</script>

效果如下

原文地址:https://www.cnblogs.com/zhouqingfeng/p/12047306.html

时间: 2024-08-26 16:56:28

用原生js写小游戏--Flappy Bird的相关文章

JS写小游戏(一):游戏框架

前言 前一阵发现一个不错的网站,都是一些用html5+css+js写的小游戏,于是打算学习一番,写下这个系列博客主要是为了加深理解,当然也有一些个人感悟,如果英文好可以直接Click Here. 概述 一般,小游戏都要关注两个问题:刷新和交互.因为游戏一般是动态的,所以需要不断刷新.JavaScript是单线程,如果用C语言写过贪吃蛇之类的小游戏,应该知道,单线程一般是挂起一个时间来达到动态效果.比如C语言的Sleep(),JS的setInterval()等.但是js还有一种更高性能的方法req

原生js写的贪吃蛇网页版游戏

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>原生js写的贪吃蛇网页版游戏</title> </head> <body><div><A href="http://www.999jiujiu.com/">h

用Phaser来制作一个html5游戏——flappy bird (一)

Phaser是一个简单易用且功能强大的html5游戏框架,利用它可以很轻松的开发出一个html5游戏.在这篇文章中我就教大家如何用Phaser来制作一个前段时间很火爆的游戏:Flappy Bird,希望大家看后也能做出自己的html5游戏.大家可以先点击这里来试玩一下我已经做好的这个游戏,感受一下Phaser的游戏效果,游戏的完整代码我已经放到github上了.支持的浏览器:IE9+.Firefox.Chrome.Opera.Safari以及移动端的能支持html5的浏览器,推荐使用谷歌浏览器,

原生JS写的ajax函数

参照JQuery中的ajax功能,用原生JS写了一个ajax,功能相对JQuery要少很多,不过基本功能都有,包括JSONP. 调用的方式分为两种: 1. ajax(url, {}); 2. ajax({}); 调用的方法参照JQuery的ajax,只是 不需要写$.ajax ,只需要写 ajax 就可以了. 代码如下: !function () { var jsonp_idx = 1; return ajax = function (url, options) { if (typeof url

用原生JS写移动动画案例及实际应用

js很强大 相信很多人都知道,那么它有哪些强大之处呢?有兴趣的人可以去查查,这里就不赘述了,因为不在本片文章讨论的范围. 我们要讲的是怎么用原生JS写移动动画?我们先举一个最简单的动画例子,很多网站的左边或右边会有个分享的框,鼠标放上去就还移出一个列表,里面是要分享的地址.鼠标移开,就会移进去. 要实现这个效果要怎么做呢? 可以想一想,鼠标经过和鼠标离开很好理解 用onmousemove事件和onmouseout事件就能完成. 那移动动画呢?我们可以一步一步思考, 首先,一开始是这样的 完成移动

原生js写的一个弧形菜单插件

弧形菜单是一种半弧式或者全弧形菜单,是一种不同于传统横向或者竖向菜单形式的菜单.最近在网上看到好多人写出了这种效果,于是也尝试自己写了一个. 实现方式:原生态js 主要结构: 1.参数合并 1 var defaultPra = { 2 mainMenuId: "ArcMenu",//主菜单id 3 menuBoxId:"menuBox",//菜单包裹id 4 position: "",//弧形菜单 5 customPosition:"0

原生js canvas 碰撞游戏的开发笔记

-----------------------------------------------福利--------------------------------------------- -----------------------------------------------分割线--------------------------------------------- 今天 我们研究下碰撞游戏 什么是碰撞游戏? 当然是东西碰到在一起啦 用前端逻辑来说 就是2个物品互相碰撞产生的事件 问

js消除小游戏(极简版)`

js小游戏极简版 (1) 基础布局 <div class = "box"> <p></p> <div class="div"></div> </div>     (2)简单的基础样式 * { margin: 0; padding: 0; } div.box { width: 1000px; height: 700px; border: 1px solid #008B8B; box-sizing:

使用原生js写ajax

// 使用原生js 封装ajax // 兼容xhr对象 function createXHR(){ if(typeof XMLHttpRequest != "undefined"){ // 非IE6浏览器 return new XMLHttpRequest(); }else if(typeof ActiveXObject != "undefined"){ // IE6浏览器 var version = [ "MSXML2.XMLHttp.6.0"