Java Script Implementation Pixel Bird

Source: Internet
Author: User


implement pixel birds with Java script

<! DOCTYPE html>

<meta charset= "UTF-8" >

<title>flybird</title>

<link rel= "stylesheet" type= "Text/css" href= "Css/bird.css"/>

<body>

<div class= "Canvas" >

<!--game Start-up

<div class= "Game_start" >

<div class= "Head" >

<span class= ' Small_bird ' ></span>

</div>

<div class= "Btns" >

<span class= "Start_btn" ></span>

<span class= "Submit_btn" ></span>

</div>

</div>

<!--moving Bars--

<div class= "Slider" >

</div>

<!--birds--

<div id= "Flybird" class= "Bird" >

</div>

<!--pipes--

<div class= "Conduti_group" >

</div>

<!--game Ends-

<div class= "Gameover" >

<div class= "Overimg" >

</div>

<div class= "Overmessage" >

<span class= ' Thisscore ' >1</span>

<span class= ' Historyscore ' >2</span>

</div>

<div class= "Game_restart" >

</div>

</div>

<!--points-

<div id= "scroing" class= "scroing" >

</div>

</div>

<script src= "Js/game1.js" type= "Text/javascript" >

</script>

</body>



<css part >

/* Background Canvas */

*{

Cursor:pointer;


}

. canvas {

position:relative;

width:343px;

height:480px;

Margin:auto;

Overflow:hidden;

Background:url (.. /images/bg.jpg) No-repeat;

}



/* Start Game */


. game_start {

Display:block;

}


. head {

Position:absolute;

top:100px;

left:60px;

Animation:birdmove 2s infinite;

}


. Head. Small_bird {

Display:inline-block;

width:40px;

height:30px;

Position:absolute;

right:0px;

top:20px;

animation:birdbg 0.5s Infinite;

}


/*btns Button Group */


. btns {

width:100%;

Text-align:center;

Position:absolute;

top:250px;

}


. btns. start_btn {

width:85px;

height:29px;

Display:inline-block;

Background:url (.. /images/start.jpg) No-repeat;

margin-left:10px;

}


. btns. submit_btn {

width:85px;

height:29px;

Display:inline-block;

Background:url (.. /images/submit.jpg) No-repeat;

margin-left:10px;

}


/* Move Bar */


. Slider {

width:686px;

Height:auto;

font-size:0px;

Position:absolute;

left:0;

top:423px;

Animation:slidermove 5s infinite linear;

}


. Slider img {

Display:inline-block;

}


/* Bird */


. Bird {

Position:absolute;

top:250px;

left:50px;

width:50px;

height:50px;

Display:none;

}

/* Piping */

. conduititem{

width:62px;

height:423px;

/*background:red;*/

/*opacity:0.7;*/

Position:absolute;

top:0;

/*right: -100px;*/

}

. top_conduit{

Position:absolute;

width:100%;

top:0;

Background:url (.. /images/up_mod.png);

}

. Top_conduit div{

Background:url (.. /images/up_pipe.png) 0 Bottom no-repeat;

}

. bottom_conduit{

Position:absolute;

width:100%;

bottom:0;

Background:url (.. /images/down_mod.png);

}

. Bottom_conduit div{

Background:url (.. /images/down_pipe.png) 0 Top no-repeat;

}


/* End of Game */


. gameover {

Display:none;

}


. Gameover. overimg {

Position:absolute;

left:60px;

top:100px;

Animation:overmove 1s 1;


}


. Gameover. overmessage {

Background:url (.. /images/message.jpg) No-repeat;

width:269px;

height:135px;

Position:absolute;

bottom:200px;

left:40px;

Animation:messagemove 1s 1;


}


. Gameover. overmessage. Thisscore {

Position:absolute;

right:30px;

top:35px;

Font-size:2.5em;

Color:black;

}


. Gameover. overmessage. Historyscore {

Position:absolute;

right:30px;

top:85px;

Font-size:2em;

color:red;

}


. Gameover. Game_restart {

Position:absolute;

left:120px;

top:300px;

Animation:okmove 1s 1;

}


/* points */


. scroing {

Position:absolute;

Text-align:center;

width:100%;

top:40px;

}

/* Animation */


@keyframes Birdmove {

0% {

top:100px;

}

50% {

top:130px;

}

100% {

top:100px;

}

}


@keyframes birdbg {

0% {

Background:url (.. /images/bird0.png) No-repeat;

}

100% {

Background:url (.. /images/bird1.png) No-repeat;

}

}


@keyframes Slidermove {

0% {

left:0

}

100% {

Left: -343px;

}

}

/*gameover*/

@keyframes Overmove {

0% {

top:0;

}

100% {

top:110px;

}

}


/*message*/

@keyframes Messagemove {

0% {

bottom:0px;

}

100% {

bottom:210px;

}

}


/*ok*/

@keyframes Okmove {

0% {

left:0px;

}

100% {

left:130px;

}

}


the point is coming.
<js Part >

Get

var bird = document.getElementById (' Flybird ');

var bird_ = Document.queryselector ('. Bird img ');

var Game_start = Document.queryselector ('. Game_start ');

var Game_restart = Document.queryselector ('. Game_restart ');

var gameover_ = Document.queryselector ('. Gameover ');

var thisscore = Document.queryselector ('. Thisscore ');

var historyscore_ = Document.queryselector ('. Historyscore ');


var speed = 0,

Speedmax = 8;

var score = 0;


var downtimer = null; Up timer

var uptimer = null; Down timer

var conduittimer = null//Pipe time

var floortesttimer = null;

var crashtesttimer = null;

var isgame = true;

The bird moves down

function down () {

Speed + = 1;

if (speed >= Speedmax) {

Speed = Speedmax;

}

BIRD_.SRC = ' images/down_bird1.png ';

Bird.style.top = bird.offsettop + speed + ' px ';

Floortest ();

}

Birds up

function up () {

Speed-= 1;

if (speed <= 0) {

Speed = 0;

Clearinterval (Uptimer);

Downtimer = SetInterval (down, 30);

}

BIRD_.SRC = ' images/up_bird1.png ';

Bird.style.top = bird.offsettop-speed + ' px ';

}

The function of the bird beating

function Birdjump () {

Speed = Speedmax;

if (isgame) {

Clearinterval (Uptimer);

Clearinterval (Downtimer); First clear down the down timer

Uptimer = setinterval (UP, 30)

}

}

Touch Bottom Collision Detection

function Floortest () {

var T1 = bird.offsettop;

Console.log (t1);

var B1 = t1 + bird.offsetheight;

if (T1 >= 396) {

Game Over

Gameover ();

}

}

Collision detection

function crashtest (obj, Objpar) {

Bird

var L1 = Bird.offsetleft;

var r1 = L1 + bird.offsetwidth;

var T1 = bird.offsettop;

var B1 = t1 + bird.offsetheight;

Pipeline

var L2 = Objpar.offsetleft;

var r2 = L2 + obj.offsetwidth;

var t2 = obj.offsettop;

var b2 = t2 + obj.offsetheight;

if (R1 > L2 && L1 < R2 && B1 > t2 && T1 < B2) {

Gameover ();

}

}

Clear All Timers

function Cleartimer () {

var timer = setinterval (function () {}, 30);

for (var i = 0; i < timer; i++) {

Clearinterval (i);

}

}


Game Over

function Gameover () {

Isgame = false;

Clearinterval (Conduittimer);

Clearinterval (Downtimer);

Clearinterval (Crashtesttimer);

Cleartimer ();

Bird.style.webkitTransition = "All 0.5s linear";

Bird.style.top = ' 397px ';

BIRD_.SRC = ' images/down_bird1.png ';

Document.removeeventlistener (' Click ', Birdjump, false);

Document.queryselector ('. Gameover '). style.display = ' block ';


var historyscore=localstorage.getitem (' Maxscore ');

if (historyscore==null| | Historyscore<score) {

Localstorage.setitem (' Maxscore ', score);

Historyscore=score;

}

Historyscore_.innerhtml=historyscore;

}

Game_restart.onclick = function () {

canvas.innerhtml = History.go (' 0 ');

}


Random function

function rand (min, max) {

Return parseint (Math.random () * (max-min)) + min;

}

var canvas = Document.queryselector ('. Canvas ');


function Scorefn (score) {

var scorethis = score.tostring ();

Scorethis=scorethis.split (");

var scorediv = document.queryselector (' #scroing ');

scorediv.innerhtml = ";

for (var i = 0; i < scorethis.length; i++) {

var img = document.createelement (' img ');

IMG.SRC = ' images/' + scorethis[i] + '. jpg ';

Scorediv.appendchild (IMG);

}


function Teo () {

var scorethis=score.tostring ();

Scorethis=scorethis.split ();

var scorediv=document.queryselector (' #scroing ');

var contentimg

//}

var scoreimg=document.queryselector (' #scroing img ');


}

Creating pipelines

function Createconduit () {

var conduitgroup = Document.queryselector ('. Conduti_group ');

var conduititem = document.createelement (' div ');

Conduititem.classname = ' Conduititem ';

Conduitgroup.appendchild (Conduititem);

var topheight = rand (60, 213);

var bottomheight = 273-topheight;


conduititem.innerhtml = "<div class= ' top_conduit ' >" +

"<div style= ' height:" + topheight + "PX; ></div> "+

"</div>" +

"<div class= ' bottom_conduit ' >" +

"<div style= ' height:" + bottomheight + "PX; ></div> "+

"</div>";


var maxwidth = canvas.offsetwidth;

ConduitItem.style.left = maxwidth + ' px ';

Conduititem.toscore = true;

Conduititem.movetimer = setinterval (function () {


MaxWidth-= 3;

ConduitItem.style.left = maxwidth + ' px ';

if (maxwidth <=-70) {

Clearinterval (Conduititem.movetimer);

Conduitgroup.removechild (Conduititem);

}

Calculate points

if (maxwidth <= && conduititem.toscore) {

score++;

Scorefn (score);

thisscore.innerhtml = score;

Conduititem.toscore = false;

}

}, 30)


}


Game initialization

function init () {

var start_btn = Document.queryselector ('. start_btn ');

var Game_start = Document.queryselector ('. Game_start ');


Add Click event Start button

Start_btn.onclick = function () {

Game_start.style.display = ' None ';

Bird.style.display = ' block '


Creating pipelines

Conduittimer = SetInterval (Createconduit, 2000);


Collision detection

Crashtesttimer = setinterval (function () {

var conduititem = Document.queryselector ('. Conduti_group '). Queryselectorall ('. Conduititem ');

for (var i = 0; i < conduititem.length; i++) {

var top_conduit = Conduititem[i].queryselector ('. Top_conduit ');

var bottom_conduit = Conduititem[i].queryselector ('. Bottom_conduit ');

Crashtest (Bottom_conduit, conduititem[i]);

Crashtest (Top_conduit, conduititem[i]);

}

}, 1000/60)

Bird Landing

Downtimer = SetInterval (down, 30);

Document.addeventlistener (' Click ', Birdjump, false);

}

}

Init ();

Java Script Implementation Pixel Bird

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.