JS小游戏-像素鸟#源码#Javascript

2024-06-22 20:44

本文主要是介绍JS小游戏-像素鸟#源码#Javascript,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、游戏图片

像素鸟小游戏
在这里插入图片描述

2、HTML部分

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>body{margin: 0;}.game{position: relative;width: 800px;height: 600px;margin: 0 auto;overflow: hidden;}.sky {position: absolute;width: 200%;height: 100%;background-image: url('./img/sky.png');margin: 0 auto;}.game .bird {background: url("./img/bird.png");position: absolute;width: 33px;height: 26px;left: 150px;top: 150px;}.game .bird.swing1{background-position: -8px -10px;}.game .bird.swing2{background-position: -60px -10px;}.game .bird.swing3{background-position: -113px -10px;}.pipeDown{position: absolute;background-image: url('./img/pipeUp.png');width: 52px;height: 100px;left: 500px;bottom: 112px;}.pipeUp{position: absolute;background-image: url('./img/pipeDown.png');background-position: bottom;width: 52px;height: 100px;left: 500px;top: 0;}.ground{position: absolute;background-image: url('./img/land.png');width: 200%;height: 112px;left: 0;bottom: 0;}.score{position: absolute;width: 100px;height: 36px;background-color: lightblue;right: 0;top: 0;text-align: center;line-height: 36px;font-size: 24px;z-index: 100;}p{text-align: center;}</style>
</head>
<body><div class="game"><div class="sky"></div><div class="bird swing1"></div><div class="ground"></div><div class="score">0</div></div><p>按w或者按上开始游戏</p><script src="./JS/Rectangle.js"></script><script src="./JS/Sky.js"></script><script src="./JS/Land.js"></script><script src="./JS/Bird.js"></script><script src="./JS/Pipe.js"></script><script src="./JS/Game.js"></script>
</body>
</html>

3、JS部分

baseGame class
/*** 基础的游戏类* 属性: 宽度 高度、横坐标、纵坐标、横向速度、纵向速度、对应的dom元素*/
class Rectangle {constructor(width,height,x,y,vx,vy,dom){this.width = width;this.height = height;this.x = x;this.y = y;this.vx = vx;this.vy = vy;this.dom = dom;this.render();}/*** 渲染*/render(){this.dom.style.width = this.width + "px";this.dom.style.height = this.height + "px";this.dom.style.left = this.x + "px";this.dom.style.top = this.y + "px";}onMove(){}/***  在duration时间下物体移动* @param {number} duration  间隔*/move(duration){this.x += this.vx * duration;this.y += this.vy * duration;if(this.onMove) this.onMove();this.render();}
}
sky ground class
const skyDom = document.querySelector('.sky');
skyStyle = getComputedStyle(skyDom);
const widthSky = parseFloat(skyStyle.width);
const heightSky = parseFloat(skyStyle.width);class Sky extends Rectangle{constructor(){super(widthSky, heightSky, 0, 0, -100, 0, skyDom);}onMove(){if(this.x <= -widthSky / 2) {this.x = 0;}}
}
const landDom = document.querySelector('.ground');
landStyle = getComputedStyle(landDom);
const widthLand = parseFloat(landStyle.width);
const heightLand = parseFloat(landStyle.width);class Land extends Rectangle{constructor(){super(widthLand, heightLand, 0, 488, -100, 0, landDom);}onMove(){if(this.x <= -widthLand / 2) {this.x = 0;}}
}
bird class
const birdDom = document.querySelector('.bird');
birdStyle = getComputedStyle(birdDom);
const widthBird = parseFloat(birdStyle.width);
const heightBird = parseFloat(birdStyle.height);class Bird extends Rectangle{gravity = 1000;   constructor(){super(widthBird, heightBird, 150, 200, 0, 100, birdDom);this.swingState = 1;this.bindEvent();}// 綁定鼠標按下bindEvent(){document.addEventListener('keydown', (e) => {if(e.key === 'w' || e.key === 'ArrowUp'){this.vy += -550;}})this.startSwing();}// 小鳥扇翅膀startSwing(){if(this.timer){return ;}this.timer = setInterval( () => {birdDom.classList.remove('swing'+this.swingState);this.swingState = (++this.swingState % 3) + 1;birdDom.classList.add('swing'+this.swingState);},300)}stopSwing(){clearInterval(this.timer);this.timer = null;}onMove(){if(this.y >= 463) {this.y = 463;this.vy =0;}if(this.y <= 0 ){this.y = 0;this.vy = 0;}}move(duration){super.move(duration);this.vy += this.gravity * duration;}}
pipe class
const game = document.querySelector('.game');
const gameWidth = game.clientWidth;
class Pipe extends Rectangle{isExited = true;constructor(height, top, speed, dom){super(52, height, gameWidth, top, speed, 0, dom);}onMove(){if(this.x < -this.width){this.dom.remove();this.isExited = false;}}}function getRandomNumber(min, max){return Math.floor(Math.random() * (max - min)) + min;
}class PipePare{stopTimer = null;constructor(speed){this.up = document.createElement('div');this.down = document.createElement('div');this.up.classList.add('pipeUp');this.down.classList.add('pipeDown');game.appendChild(this.up);game.appendChild(this.down);this.spaceHeihgt = 150;  // 柱子之間的空隙 const fristPipeHeight = getRandomNumber(0, 488-150);this.upPipe = new Pipe(fristPipeHeight, 0, speed, this.up);this.downPipe = new Pipe(338 - fristPipeHeight, 150 + fristPipeHeight, speed, this.down);}stop(){if(this.stopTimer){clearInterval(this.stopTimer);this.stopTimer = null;}this.down.remove();this.up.remove();}move(duration){this.stopTimer = setInterval( () => {this.upPipe.move(duration);this.downPipe.move(duration);},10)}isCollision(bird){const birdStyle = getComputedStyle(document.querySelector('.bird'))let y = Number.parseInt(birdStyle.top);if(this.upPipe.x < bird.x + bird.width && this.upPipe.x > bird.x){return y < this.upPipe.height || y + bird.height > this.upPipe.height + this.spaceHeihgt;}}
}class GamePipePair{pipeTimer = null;pipeArr = [];  // 用于显示的管道数组// 记录小鸟越过的数组scoreArr = [];constructor(speed){this.speed = speed;this.pair = new PipePare(speed);this.init();}init(){this.pipeTimer = setInterval( ()=> {let tmp = new PipePare(this.speed);tmp.move(0.01);this.pipeArr.push(tmp);this.scoreArr.push(tmp);}, 2000) }getScore(bird){return this.scoreArr.filter(item => item.upPipe.x <= bird.x).length;}stop(){this.pipeArr.forEach(item => {item.stop();})if(this.pipeTimer){clearInterval(this.pipeTimer);this.pipeTimer = null;}}collisionDetection(bird){  // 小鸟对象传入this.pipeArr = this.pipeArr.filter(item => item.upPipe.isExited); // 过滤掉了不存在的// 检查小鸟是否碰撞到柱子for(let i = 0 ;i < this.pipeArr.length;i++){if(this.pipeArr[i].isCollision(bird)){return true;}}return false;}}
game class
class Game {score = 0;land;sky;bird;backgroundTimer = null;pipeController = null;constructor(){this.land = new Land();this.sky = new Sky();this.bird = new Bird();this.state = 0;   // 0 遊戲結束  1遊戲開始進行中this.init();   }init(){document.onkeydown =  (e) => {if((e.key === 'w' || e.key === 'ArrowUp') && this.state === 0){console.log('開始遊戲');this.state = 1;this.startGame();}}}startGame(){this.pipeController = new GamePipePair(-100);if(this.backgroundTimer) return ;this.backgroundTimer = setInterval(() => {this.land.move(0.01);this.sky.move(0.01);this.bird.move(0.01);this.updateScore();if(this.pipeController){if(this.pipeController.collisionDetection(this.bird)){this.endGame();console.log('game ended');}}}, 10)}updateScore(){const score = document.querySelector('.score');this.score = this.pipeController.getScore(this.bird)score.innerHTML = this.score;}endGame(){if(this.backgroundTimer && this.state === 1){this.pipeController.stop();this.state = 0;this.bird.stopSwing();clearInterval(this.backgroundTimer);this.backgroundTimer = null;document.onkeydown = null;if(confirm(`游戏结束!! 你最后的得分是${this.score}分你想要再玩一局嘛 想玩点确定哦!!!`)){this.init();}}}}const birdGame = new Game();

4、源码+静态资源

像素鸟源码地址

这篇关于JS小游戏-像素鸟#源码#Javascript的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1085370

相关文章

java脚本使用不同版本jdk的说明介绍

《java脚本使用不同版本jdk的说明介绍》本文介绍了在Java中执行JavaScript脚本的几种方式,包括使用ScriptEngine、Nashorn和GraalVM,ScriptEngine适用... 目录Java脚本使用不同版本jdk的说明1.使用ScriptEngine执行javascript2.

Spring MVC如何设置响应

《SpringMVC如何设置响应》本文介绍了如何在Spring框架中设置响应,并通过不同的注解返回静态页面、HTML片段和JSON数据,此外,还讲解了如何设置响应的状态码和Header... 目录1. 返回静态页面1.1 Spring 默认扫描路径1.2 @RestController2. 返回 html2

Spring常见错误之Web嵌套对象校验失效解决办法

《Spring常见错误之Web嵌套对象校验失效解决办法》:本文主要介绍Spring常见错误之Web嵌套对象校验失效解决的相关资料,通过在Phone对象上添加@Valid注解,问题得以解决,需要的朋... 目录问题复现案例解析问题修正总结  问题复现当开发一个学籍管理系统时,我们会提供了一个 API 接口去

Java操作ElasticSearch的实例详解

《Java操作ElasticSearch的实例详解》Elasticsearch是一个分布式的搜索和分析引擎,广泛用于全文搜索、日志分析等场景,本文将介绍如何在Java应用中使用Elastics... 目录简介环境准备1. 安装 Elasticsearch2. 添加依赖连接 Elasticsearch1. 创

Spring核心思想之浅谈IoC容器与依赖倒置(DI)

《Spring核心思想之浅谈IoC容器与依赖倒置(DI)》文章介绍了Spring的IoC和DI机制,以及MyBatis的动态代理,通过注解和反射,Spring能够自动管理对象的创建和依赖注入,而MyB... 目录一、控制反转 IoC二、依赖倒置 DI1. 详细概念2. Spring 中 DI 的实现原理三、

SpringBoot 整合 Grizzly的过程

《SpringBoot整合Grizzly的过程》Grizzly是一个高性能的、异步的、非阻塞的HTTP服务器框架,它可以与SpringBoot一起提供比传统的Tomcat或Jet... 目录为什么选择 Grizzly?Spring Boot + Grizzly 整合的优势添加依赖自定义 Grizzly 作为

Java后端接口中提取请求头中的Cookie和Token的方法

《Java后端接口中提取请求头中的Cookie和Token的方法》在现代Web开发中,HTTP请求头(Header)是客户端与服务器之间传递信息的重要方式之一,本文将详细介绍如何在Java后端(以Sp... 目录引言1. 背景1.1 什么是 HTTP 请求头?1.2 为什么需要提取请求头?2. 使用 Spr

Java如何通过反射机制获取数据类对象的属性及方法

《Java如何通过反射机制获取数据类对象的属性及方法》文章介绍了如何使用Java反射机制获取类对象的所有属性及其对应的get、set方法,以及如何通过反射机制实现类对象的实例化,感兴趣的朋友跟随小编一... 目录一、通过反射机制获取类对象的所有属性以及相应的get、set方法1.遍历类对象的所有属性2.获取

Java中的Opencv简介与开发环境部署方法

《Java中的Opencv简介与开发环境部署方法》OpenCV是一个开源的计算机视觉和图像处理库,提供了丰富的图像处理算法和工具,它支持多种图像处理和计算机视觉算法,可以用于物体识别与跟踪、图像分割与... 目录1.Opencv简介Opencv的应用2.Java使用OpenCV进行图像操作opencv安装j

java Stream操作转换方法

《javaStream操作转换方法》文章总结了Java8中流(Stream)API的多种常用方法,包括创建流、过滤、遍历、分组、排序、去重、查找、匹配、转换、归约、打印日志、最大最小值、统计、连接、... 目录流创建1、list 转 map2、filter()过滤3、foreach遍历4、groupingB