typescript 简版跳一跳
typescript 简版跳一跳
学习typescript,第一步应该是学习官方文档,理解最基础的语法。第二步开始用typescript实现一些js+css 或者canvas类型的游行。现在开始我们用ts写跳一跳
核心点:1.场景的随机创建
2.旗子的跳动
3.落脚点的判断,重点要提及的是射线判断法,参见博客
4.场景平移
5.游戏重置
6.销毁场景外方块
Ts代码:
- //1.创建底座:
- //2.创建跳棋:
- //3.点击移动
- //4.开始按压动画,
- //5.放开动画
- //6.跳动
- //7.是否跳对
- //8.移动场景
- //9.创建新底座
- module Jump {
- interface Pos {
- x: number;
- y: number;
- }
- enum Flag {
- on, in, out
- }
- enum Direction {
- left,
- right
- }
- let direction: Direction = Direction.right;
- let diamondsList: diamonds[] = [];
- let mask: JQuery<HTMLElement> = $(".game-p");
- let chess: Chess;
- let score:number=0;
- class MathHelp {
- /**
- * 返回范围内随机数[min,max]
- * @param min 最小值
- * @param max 最大值
- */
- static RandRange(min: number, max: number): number {
- return Math.floor(Math.random() * (max - min + 1) + min);
- }
- /**
- * 根据角度求对边长度。Math.sin(randian)
- * @param r 斜边长
- * @param angle 角度
- */
- static righttriangle(r: number, angle: number): number {
- return Math.sin(Math.PI / 180 * angle) * r;
- }
- /**
- * 射线法判断点是否在多边形内部
- * @param p 待判断的点
- * @param poly 多边形顶点
- */
- static rayCasting(p: Pos, poly: Pos[]): Flag {
- var px = p.x,
- py = p.y,
- flag = false
- for (var i = 0, l = poly.length, j = l - 1; i < l; j = i, i++) {
- var sx = poly[i].x,
- sy = poly[i].y,
- tx = poly[j].x,
- ty = poly[j].y
- // 点与多边形顶点重合
- if ((sx === px && sy === py) || (tx === px && ty === py)) {
- return Flag.on;
- }
- // 判断线段两端点是否在射线两侧
- if ((sy < py && ty >= py) || (sy >= py && ty < py)) {
- // 线段上与射线 Y 坐标相同的点的 X 坐标
- var x = sx + (py - sy) * (tx - sx) / (ty - sy)
- // 点在多边形的边上
- if (x === px) {
- return Flag.on;
- }
- // 射线穿过多边形的边界
- if (x > px) {
- flag = !flag
- }
- }
- }
- // 射线穿过多边形边界的次数为奇数时点在多边形内
- return flag ? Flag.in : Flag.out;
- }
- }
- class diamonds {
- private width: number = 180;
- private height: number = 180;
- id: string = "p" + new Date().getTime();;
- node: JQuery<HTMLElement>;
- left: number = 0;
- top: number = 0;
- constructor(isauto: boolean = true, isRe = true, left: number = 0, top: number = 0) {
- this.left = left;
- this.top = top;
- if (isauto) {
- let dl = MathHelp.RandRange(200, 300);
- let x = MathHelp.righttriangle(dl, 57);
- let y = MathHelp.righttriangle(dl, 33);
- let lastbox = diamondsList[diamondsList.length - 1];
- if (isRe) {
- if (direction == Direction.left) {
- direction = Direction.right;
- } else if (direction == Direction.right) {
- direction = Direction.left;
- }
- }
- if (direction == Direction.right) {
- this.left = lastbox.left + x;
- } else {
- this.left = lastbox.left - x;
- }
- this.top = lastbox.top - y;
- }
- this.node = $(`<div id='${this.id}' class='gb' style="top:${this.top}px;left:${this.left}px">
- <div class='box' style="background:url(img/c${MathHelp.RandRange(1, 4)}.png)"></div>
- <div class='shadw'></div>
- </div>`);
- mask.append(this.node);
- }
- GetPointList(): Pos[] {
- var result = [{
- x: this.left,
- y: this.top + 57
- }, {
- x: this.left + (this.width / 2),
- y: this.top + 4
- }, {
- x: this.left + this.width,
- y: this.top + 57
- }, {
- x: this.left + (this.width / 2),
- y: this.top + (57 * 2 - 4)
- }];
- return result;
- }
- move(p: Pos) {
- this.node.css({
- left: p.x + "px",
- top: p.y + "px",
- transition: "",
- transform: ""
- });
- }
- }
- class Chess {
- private width: number = 180;
- private height: number = 182;
- left: number = 49;
- top: number = 531;
- node: JQuery<HTMLElement>;
- constructor() {
- this.node =$(`<div class="chess gb"></div>`);
- mask.append(this.node);
- }
- jump(pt: Pos, call: Function) {
- let numb = 0;
- let t1 = setInterval(() => {
- if (numb == 0) {
- this.node.animate({
- left: pt.x + "px",
- top: pt.y + "px"
- }, 504);
- }
- this.node.css({
- "background-position": "-" + this.width * numb + "px" + " 0px"
- });
- numb++;
- if (numb >= 11) {
- window.clearInterval(t1);
- call();
- }
- }, 42)
- }
- GetCenter(): Pos {
- return {
- x: this.left + this.width / 2,
- y: this.top + this.height
- }
- }
- move(p: Pos) {
- this.node.css({
- left: p.x + "px",
- top: p.y + "px",
- transition: "",
- transform: ""
- });
- }
- }
- class Game {
- static distince = 0;
- static time: number;
- /**
- * 初始化游戏场景
- */
- static scence() {
- let d1 = new diamonds(false, false, 50, 650);
- diamondsList.push(d1);
- let d = new diamonds(true, false);
- diamondsList.push(d);
- Game.loadlisten();
- chess = new Chess();
- $(".againBtn").on("click",()=>{
- //重置
- Game.GameRest();
- });
- }
- /**
- * 重置游戏
- */
- static GameRest(){
- $(".gameEnd").css({
- "z-index":-1
- });
- diamondsList=[];
- score=0;
- $(".jfb").html(score.toString());
- $(".gb").remove();
- direction=Direction.right;
- Game.scence();
- }
- static CreateSP() {
- let d = new diamonds();
- diamondsList.push(d);
- Game.loadlisten();
- }
- private static loadlisten() {
- document.addEventListener('touchstart', Game.touch, false);
- document.addEventListener('touchmove', Game.touch, false);
- document.addEventListener('touchend', Game.touch, false);
- }
- private static removelisten() {
- document.removeEventListener('touchstart', Game.touch, false);
- document.removeEventListener('touchmove', Game.touch, false);
- document.removeEventListener('touchend', Game.touch, false);
- }
- private static touch(e: Event) {
- e.preventDefault();
- let currentDiamonds = diamondsList[diamondsList.length - 2];
- if (e.type == "touchstart") {
- //挤压形变动画
- let scaley = 1;
- let chessy = 0;
- Game.distince = 0;
- Game.time = setInterval(() => {
- if (scaley > 0.7) {
- scaley -= 0.01;
- }
- if (chessy < 30) {
- chessy++;
- chess.node.css({
- "transform": " scaleX(" + (1 + chessy * 0.006) + ") scaleY(" + (1 - (chessy * 0.007)) + ")"
- })
- }
- Game.distince++;
- currentDiamonds.node.css({
- "transform-origin": "bottom center",
- "transform": "scaleY(" + scaley + ")"
- });
- }, 50);
- } else if (e.type == "touchend") {
- //1.底座还原动画
- //2.旗子动画轨迹
- //3.落脚点判断
- //4.场景平移,创建新底座,移除画外底座
- currentDiamonds.node.css({
- "transform": "scaleY(1)"
- });
- clearInterval(Game.time);
- Game.removelisten();
- Game.Jump(Game.distince);
- }
- }
- private static Jump(distince: number) {
- let dl = distince * 17;
- let x = MathHelp.righttriangle(dl, 57);
- let y = MathHelp.righttriangle(dl, 33);
- if (direction == Direction.left) {
- x = -x;
- }
- chess.left = chess.left + x;
- chess.top = chess.top - y;
- chess.jump({ x: chess.left, y: chess.top }, () => {
- let p = chess.GetCenter();
- let last = diamondsList[diamondsList.length - 1];
- let poly = last.GetPointList();
- //判断是否跳在基座上
- let result = MathHelp.rayCasting(p, poly);
- if (result == Flag.in) {
- direction = Math.random() > 0.5 ? 1 : 0;
- Game.moveblock();
- score++;
- $(".jfb").html(score.toString());
- } else {
- //game over
- $(".gameEnd").css({
- "z-index":999
- });
- $(".score").html(score.toString());
- console.log("game over!");
- }
- })
- }
- /**
- * 移动场景
- */
- private static moveblock() {
- let last = diamondsList[diamondsList.length - 1];
- let p1: Pos;
- let x: number = 0;
- let y: number = 0;
- //以左右标准基座为左边平移
- if (direction == Direction.left) {
- p1 = { x: 50, y: 650 };
- } else {
- p1 = { x: 400, y: 650 };
- }
- x = p1.x - last.left;
- y = p1.y - last.top;
- $(".gb").css({
- "transform": "translate(" + x + "px," + y + "px)",
- "transition": "transform 0.9s"
- });
- setTimeout(() => {
- //更新class中left,top属性
- $.each(diamondsList, (a, b) => {
- b.left = b.left + x;
- b.top = b.top + y;
- b.move({ x: b.left, y: b.top });
- });
- chess.left = chess.left + x;
- chess.top = chess.top + y;
- chess.move({
- x: chess.left,
- y: chess.top
- });
- Game.Destroy();
- //创建新底座
- Game.CreateSP();
- }, 1100)
- }
- //销毁画外的底座
- private static Destroy(){
- diamondsList.forEach((item,i,list)=>{
- if(item.top>1008){
- diamondsList.splice(i,1);
- $("#"+item.id).remove();
- }
- })
- }
- }
- Game.scence();
- }
html:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>Typescript跳一跳</title>
- <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
- <script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
- <link rel="stylesheet" type="text/css" href="css/ts.css" />
- <script type="text/javascript">
- var isios = false;
- ! function(userAgent) {
- var screen_w = parseInt(window.screen.width),
- scale = screen_w / 640;
- if(/Android (\d+\.\d+)/.test(userAgent)) {
- var version = parseFloat(RegExp.$1);
- document.write(version > 2.3 ? '<meta name="viewport" content="width=640, initial-scale = ' + scale + ',user-scalable=1, minimum-scale = ' + scale + ', maximum-scale = ' + scale + ', target-densitydpi=device-dpi">' : '<meta name="viewport" content="width=640, target-densitydpi=device-dpi">');
- } else {
- isios = true;
- document.write('<meta name="viewport" content="width=640, initial-scale = ' + scale + ' ,minimum-scale = ' + scale + ', maximum-scale = ' + scale + ', user-scalable=no, target-densitydpi=device-dpi">');
- }
- }(navigator.userAgent);
- </script>
- </head>
- <body>
- <!--游戏页面-->
- <div class="game">
- <div class="game-p">
- <!--<div class="chess gb"></div>
- <div id="one" class="gb">
- <div class="box"></div>
- <div class="shadw"></div>
- </div>-->
- </div>
- <img class="logo" src="img/logo.png" />
- <div class="jfb">0</div>
- <div class="toolp">距离开启宝箱还有5步</div>
- </div>
- <div class="gameEnd">
- <div class="getScore">
- <p class="score">10</p>
- <button class="againBtn">再来一局</button>
- </div>
- </div>
- <script src="js/myjump.js" type="text/javascript" charset="utf-8"></script>
- </body>
- </html>
css:
- html,
- body {
- margin:;
- padding:;
- height: 100%;
- min-height: 1008px;
- }
- .game {
- height: 100%;
- width: 100%;
- position: relative;
- left:;
- top:;
- }
- .logo {
- position: absolute;
- left: 30px;
- top: 26px;
- }
- .jfb {
- position: absolute;
- top: 30px;
- right: 60px;
- font-size: 100px;
- font-weight:;
- color: #4f4e3f;
- text-align: center;
- line-height: 100px;
- }
- .toolp {
- position: absolute;
- bottom: 5%;
- left: 30%;
- width: 40%;
- height: 50px;
- border-radius: 50px;
- color: #FFFFFF;
- text-align: center;
- font-size: 20px;
- line-height: 50px;
- background-color: #4a764e;
- }
- .game-p {
- height: 100%;
- width: 100%;
- position: absolute;
- left:;
- top:;
- background-color: #8aad8e;
- }
- .gb{
- position: absolute;
- left: 50px;
- top: 650px;
- }
- .box{
- height: 180px;
- width: 180px;
- background-image:url(../img/c1.png);
- background-size: 100% 100%;
- z-index:;
- position: absolute;
- }
- .shadw{
- position: absolute;
- left: 120px;
- top:;
- height: 133px;
- width: 234px;
- background-image: url(../img/s2.png);
- background-size: 100% 100%;
- opacity: 0.3;
- transform: translateY(35px) translateX(-180px);
- transform-origin: bottom center;
- }
- .chess{
- width: 182px;
- height: 227px;
- background-image: url(../img/e3.png);
- background-position:0 0;
- position: absolute;
- top: 531px;
- left: 49px;
- z-index:;
- }
- .gameEnd{
- position: absolute;
- top:;
- left:;
- width: 100%;
- height: 100%;
- overflow: hidden;
- background-color: rgba(0,0,0,.8);
- z-index: -1;
- }
- .getScore{
- width: 492px;
- height: 760px;
- background: url(../img/getScore.png) no-repeat;
- background-size: 100% auto;
- position: absolute;
- top:;
- right:;
- left:;
- bottom:;
- margin: auto;
- }
- .score{
- color: #dcc226;
- font-size: 130px;
- text-align: center;
- margin-top: 120px;
- font-weight:;
- }
- .againBtn{
- width: 309px;
- height: 87px;
- background: url(../img/bg.png) no-repeat;
- background-size: 100% 100%;
- font-size: 40px;
- color: #dcc226;
- text-align: center;
- border: none;
- font-weight:;
- position: absolute;
- left: 50%;
- margin-left: -154.5px;
- }
源码:留下邮箱,看到就发。
typescript 简版跳一跳的更多相关文章
- 用Kotlin破解Android版微信小游戏-跳一跳
前言 微信又更新了,从更新日志上来看,似乎只是一次不痛不痒的小更新.不过,很快就有人发现,原来微信这次搞了个大动作——在小程序里加入了小游戏.今天也是朋友圈被刷爆的缘故. 看到网上 有人弄了一个破解版 ...
- 微信小程序跳一跳辅助程序(手动版)
最近,微信官方推出了demo小程序游戏<跳一跳>,这个游戏操作简单,容易上手,却又不容易获得高分,受到很多人的喜爱(emm...这游戏有毒).自己也尝试了玩了几次,作为一个手残+脑残的资深 ...
- 微信跳一跳辅助Demo
[原创] 前几天没事干看别人一直在玩微信上线的那一个跳一跳小游戏,玩着玩着老是掉下去,闲着没事呗 就想了想做一个辅助程序的呗.不过先做的手动版的.自动版的有点麻烦.就不发了.用的Java写的,也就一个 ...
- AI之微信跳一跳
需要环境:1,Python3.6 2,android手机 3,ADB驱动,下载地址https://adb.clockworkmod.com/ 步骤: 配置Python3,ADB安装目录到环境变量pat ...
- 用Python代码实现微信跳一跳作弊器
最近随着微信版本的更新,在进入界面有个跳一跳的小游戏,在网上看到技术篇教你用Python来玩微信跳一跳 ( 转载自 " 工科给事中的技术博客 " ) 本文旨在总结,技术全靠大神完成 ...
- java语言实现简单接口工具--粗简版
2016注定是变化的一年,忙碌.网红.项目融资失败,现在有点时间整整帖子~~ 目标: 提高工作效率与质量,能支持平台全量接口回归测试与迭代测试也要满足单一接口联调测试. 使用人员: 测试,开发 工具包 ...
- python练习_购物车(简版)
python练习_购物车(简版) 需求: 写一个python购物车可以输入用户初始化金额 可以打印商品,且用户输入编号,即可购买商品 购物时计算用户余额,是否可以购买物品 退出结算时打印购物小票 以下 ...
- 挑战App Store,微信通过“跳一跳”秀了一下“小程序”的肌肉
2017年即将结束的时候,微信放了一个大招.随着最新的微信v6.6.1版本更新,基于小程序的"小游戏"板块正式上线.微信上首发的这款"小游戏"叫"跳一 ...
- .NET开发一个微信跳一跳辅助程序
昨天微信更新了,出现了一个小游戏"跳一跳",玩了一下 赶紧还蛮有意思的 但纯粹是拼手感的,玩了好久,终于搞了个135分拿了个第一名,没想到过一会就被朋友刷下去了,最高的也就200来 ...
随机推荐
- D - 湫湫系列故事——减肥记II
虽然制定了减肥食谱,但是湫湫显然克制不住吃货的本能,根本没有按照食谱行动! 于是,结果显而易见… 但是没有什么能难倒高智商美女湫湫的,她决定另寻对策——吃没关系,咱吃进去再运动运动消耗掉不就好了? 湫 ...
- Java分享笔记:关于Java反射机制
[1] 关于Java反射机制的概述 1.反射机制的概念:允许程序在执行期,借助于反射相关的API,取得任何类的内部信息,并能直接操作任意对象内部的属性和方法. 2.与反射相关的主要的API:java. ...
- php post提交xml文件
<?php header("Content-type: text/xml;"); // xml code demo $xmlData = '<?xml version= ...
- ECSHOP和SHOPEX快递单号查询中通插件V8.6专版
发布ECSHOP说明: ECSHOP快递物流单号查询插件特色 本ECSHOP快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅 ...
- uva 509 RAID!(磁盘数据)
来自 https://blog.csdn.net/su_cicada/article/details/80085318 习题4-7 RAID技术(RAID!, ACM/ICPC World Final ...
- vue---day03
1. Vue的生命周期 - 创建和销毁的时候可以做一些我们自己的事情 - beforeCreated - created - beforeMount - mounted - beforeUpdate ...
- JOI2017 春季合宿:Railway Trip
自己的AC做法似乎离正解偏了十万八千里而且复杂了不少--不管怎样还是记录下来吧. 题意: 题目链接: JOISC2017 F - AtCoder JOISC2017 F - LOJ \(N\)个车站排 ...
- Xshell启动时显示丢失MSVCP110.dll解决方法
成功安装xshell之后,在运行时却弹出“无法启动此程序,因为计算机中丢失MSVCP110.dll.尝试重新安装该程序以解决此问题”,很多人按照提示重装了还是出现同样的问题,本集教程将具体讲解如何处理 ...
- LeetCode题目解答
LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...
- Kotlin 1 函数
#2 函数 函数声明和平时我见到的有点不太一样,使用关键字fun来声明.(感觉好欢乐的样子···O(∩_∩)O~~) 下面的示例,简单的声明了一个函数: // 这是函数声明 fun this_is_a ...