打地鼠Demo
using UnityEngine;
using System.Collections; public enum MoleStates
{
NormalState,// 初始状态
UpState,// 钻出来状态
DownState,//钻进去状态
HitState//打击状态
}
public struct Data
{
public MoleStates currentState; // 地鼠动画状态
public int currentIndex;// 地鼠动画帧下标
}
public class GameScript : MonoBehaviour {
// 这些public 的变量需要在unity中设置
public Texture2D backGround;
public Texture2D moleNormalState; // 地鼠状态
public Texture2D mouseNormalState;// 鼠标状态
public int moleWidth;//
public int moleHeight;
public Texture2D mouseHitState;// 鼠标打击状态
public Texture2D [] vertigoFrame; // 眩晕动画帧数
public Texture2D [] moleAnimationFrame;//地鼠动画帧数
public float animationSpeed;// 地鼠动画播放速度
public int gameTime;//游戏倒计时
public float hitAnimationSpeed;//打击动画播放速度
public float moleSpeed;// 地鼠随机出来的速度 private int row;//地图行,列
private int col;
Texture2D mouseImage;// 中间变量,存储锤子图片
int curVetigoFrame;// float tempTime; // 累加每次deltaTime
float randomTime; // 地鼠没有出来前,时间累加 用于地鼠图片的计时器
float hitTime; // 眩晕效果计时器
float secondTime;// 游戏没有结束前,时间累加 游戏总时间计时器 Data [,] moleDatas;//结构体数据 读取对应的图片下标和地鼠状态
//bool hasMole;
int score; float hitX;//眩晕效果 左上角坐标
float hitY;
int hitIndex;//打击眩晕效果帧动画下标
bool isOver;
bool isHitAnimation;//是否显示打击眩晕效果 void OnGUI()
{
GUI.DrawTexture( new Rect(0f, 0f, Screen.width, Screen.height),backGround ); for(int i = ; i < row; ++i)
{
for(int j = ; j < col ; ++j)
{
GUI.Label( new Rect( j * moleWidth + (( Screen.width - moleWidth * col ) >> ), i * moleHeight + (( Screen.height - moleHeight * row ) >> ),moleWidth, moleHeight ), moleAnimationFrame[ moleDatas[i,j].currentIndex ]);
}
}
// 绘制眩晕效果
GUI.Label (new Rect(hitX,hitY,100f, 100f),vertigoFrame[hitIndex] );
float mouseX = Input.mousePosition.x;
float mouseY = Input.mousePosition.y;
// 绘制锤子动画
GUI.Label( new Rect( mouseX - , Screen.height - mouseY - , 100f, 100f ), mouseImage ); // 设置文本颜色,设置字体颜色
GUI.skin.label.normal.textColor = Color.blue;
GUI.skin.label.fontSize = ;
//绘制得分
GUI.Label( new Rect(10f,10f,200f,90f), string.Format("Score:{0}", score) );
//绘制倒计时
GUI.Label( new Rect(10f,100f,200f,90f), string.Format("Time:{0}", gameTime) );
// 游戏结束后 绘制lable
if(isOver)
{
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
GUI.skin.label.fontSize = ;
GUI.Label( new Rect(0f, 0f, Screen.width, Screen.height),string.Format( "Game Over!" ) );
} }
void Awake() //初始化
{
row = ;
col = ;
tempTime = 0f;
randomTime = 0f;
hitTime = 0f;
secondTime = 0f;
//hasMole = false;
score = ;
hitIndex = ;
isOver = false;
isHitAnimation = false;
mouseImage = mouseNormalState;
moleDatas = new Data[row, col];
// 初始化地鼠数据
for(int i = ; i < row; ++i)
{
for(int j = ; j < col ; ++j)
{
moleDatas[i,j].currentIndex = ;
moleDatas[i,j].currentState = MoleStates.NormalState;
}
}
}
void Update()
{
if (!isOver)
{
// 鼠标事件监听
MouseListener();
// 控制current动画帧
ControlCurrentAnimFrame();
// 产生随机地鼠
RandomMole();
// 打击眩晕效果动画
HitAinmation();
// 时间大于一秒后,让游戏时间减一
if (secondTime > 1f)
{
// 时间归0
secondTime = 0f;
gameTime--;
// 当游戏时间小于1时游戏结束
if (gameTime < )
{
isOver = true;
}
}
else
{
// 否则继续运行
secondTime += Time.deltaTime;
}
} }
// 打击眩晕效果动画
void HitAinmation()
{
// 如果打中则播放动画
if (isHitAnimation)
{
// 打击时间 > 动画播放速度
if (hitTime > hitAnimationSpeed)
{
hitTime = 0f;
// 一帧一帧的播放
hitIndex++;
// 播到最后一帧 置0动画停止
if (hitIndex > vertigoFrame.Length - )
{
hitIndex = ;
isHitAnimation = false;
}
}
else
{
hitTime += Time.deltaTime;
}
}
}
// 鼠标事件监听
void MouseListener()
{
if ( Input.GetMouseButtonDown() )
{
// 将锤子变为打击状态
mouseImage = mouseHitState;
float mouseX = Input.mousePosition.x;
float mouseY = Screen.height - Input.mousePosition.y;// 屏幕左上角y坐标为屏幕的高,所以叫减去当前鼠标点击的y坐标
// 获取打中的是哪个地鼠 行和列
int r = (int) ((mouseY - ( Screen.height - moleHeight * row ) / ) / moleHeight );
int c = (int) ((mouseX - ( Screen.width - moleHeight * col ) / ) / moleWidth ); // 根据状态打地鼠
if(moleDatas[r,c].currentState == MoleStates.DownState || moleDatas[r,c].currentState == MoleStates.UpState )
{
moleDatas[r,c].currentState = MoleStates.HitState;
}
}
if( Input.GetMouseButtonUp() )
{
mouseImage = mouseNormalState;
}
} // 产生随机地鼠
void RandomMole()
{
// 随机时间 > 一只地鼠动画所需播放速度
if ( randomTime > moleSpeed )
{
randomTime = 0f;
int i = Random.Range( , row);
int j = Random.Range( , col); if( moleDatas[i,j].currentState == MoleStates.NormalState )
{
moleDatas[i,j].currentState = MoleStates.UpState;
} }
else
{
randomTime += Time.deltaTime;
}
} // 控制current动画帧
void ControlCurrentAnimFrame()
{
for(int i = ; i < row; ++i)
{
for(int j = ; j < col ; ++j)
{
// 控制动画播放时间
if( tempTime > animationSpeed )
{
tempTime = 0f;
switch( moleDatas[i,j].currentState )
{
case MoleStates.NormalState:
{
moleDatas[i,j].currentIndex = ;
}
break;
case MoleStates.UpState:
{
//hasMole = true;
moleDatas[i,j].currentIndex ++;
if( moleDatas[i,j].currentIndex > moleAnimationFrame.Length - )
{
moleDatas[i,j].currentIndex = moleAnimationFrame.Length - ;
moleDatas[i,j].currentState = MoleStates.DownState;
}
}
break;
case MoleStates.DownState:
{
moleDatas[i,j].currentIndex --;
if ( moleDatas[i,j].currentIndex < )
{
moleDatas[i,j].currentState = MoleStates.NormalState;
//hasMole = false;
}
}
break;
case MoleStates.HitState:
{
curVetigoFrame ++;
score += ;
Debug.Log( string.Format("score = {0}",score) );
hitY = i * moleHeight + (( Screen.height - moleHeight * row ) >> );
hitX = j * moleWidth + (( Screen.width - moleWidth * col ) >> );
isHitAnimation = true;
moleDatas [i, j].currentState = MoleStates.NormalState;
//hasMole = false;
}
break;
default: break;
}
}
else
{
tempTime += Time.deltaTime; }
}
} }
}
打地鼠Demo的更多相关文章
- 2DToolkit官方文档中文版打地鼠教程
初始设置 创建一个Unity项目,并导入2D Toolkit插件. 导入完成后,在Project窗口会显示TK2DROOT文件夹(后续版本文件夹名称或许会有变动). 导入素材游戏,你可以从这里下载.下 ...
- springmvc demo
[说明]今天上午稍稍偏了一下方向,看了看servlet的相关知识,下午做maven+spring+springMVC的整合,晚上成功实现了一个小demo(可以在jsp动态页面上获得通过地址栏输入的参数 ...
- 原生js打地鼠
我们要做的是一个打地鼠的游戏,只用原生js 1.导入需要的图片 2.编写页面css样式demo.css *{ margin:0; padding:0; } .game{ position: relat ...
- phaser2->3:来个打地鼠试水
本文中phaser具体版本 phaser2:2.8.1 phaser3:3.17.0 一.实现效果二.实现细节三.项目总结四.参考文档 一.实现效果 源码地址(phaser2&phaser3) ...
- 通过一个demo了解Redux
TodoList小demo 效果展示 项目地址 (单向)数据流 数据流是我们的行为与响应的抽象:使用数据流能帮我们明确了行为对应的响应,这和react的状态可预测的思想是不谋而合的. 常见的数据流框架 ...
- 很多人很想知道怎么扫一扫二维码就能打开网站,就能添加联系人,就能链接wifi,今天说下这些格式,明天做个demo
有些功能部分手机不能使用,网站,通讯录,wifi基本上每个手机都可以使用. 在看之前你可以扫一扫下面几个二维码先看看效果: 1.二维码生成 网址 (URL) 包含网址的 二维码生成 是大家平时最常接触 ...
- 在线浏览PDF之PDF.JS (附demo)
平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html#skill 下载地址:http://mozilla.gith ...
- 【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo
Maven+springboot+阿里大于短信验证服务 纠结点:Maven库没有sdk,需要解决 Maven打包找不到相关类,需要解决 ps:最近好久没有写点东西了,项目太紧,今天来一篇 一.本文简介 ...
- vue双向数据绑定原理探究(附demo)
昨天被导师叫去研究了一下vue的双向数据绑定原理...本来以为原理的东西都非常高深,没想到vue的双向绑定真的很好理解啊...自己动手写了一个. 传送门 双向绑定的思想 双向数据绑定的思想就是数据层与 ...
随机推荐
- JavaScriptSerializer类 对象序列化为JSON,JSON反序列化为对象 。
JavaScriptSerializer 类由异步通信层内部使用,用于序列化和反序列化在浏览器和 Web 服务器之间传递的数据.说白了就是能够直接将一个C#对象传送到前台页面成为javascript对 ...
- CentOS启动网络提示connect: Network is unreachable(配置静态路由)
ls /etc/sysconfig/network-script/ifcfg-eth0 一.看是否在上述目录下存在ifcfg-eth0 这个文件,若存在则按下面的步骤操作: 1.手工配置ip看能不能配 ...
- 【ActiveMQ入门-4】ActiveMQ学习-异步接收
总体说明: 1. 一个生产者/发布者:可以向多个目的地发送消息: 2. 每个目的地(destination)可以有多个订阅者或消费者: 如下图所示: 程序结构: 1. Publisher.java ...
- IaaS,PaaS,SaaS 的区别和联系
原文:http://www.ruanyifeng.com/blog/2017/07/iaas-paas-saas.html 越来越多的软件,开始采用云服务. 云服务只是一个统称,可以分成三大类. Ia ...
- Java 类的生命周期
类从被加载到JVM内存中开始,到卸载出内存为止,它的整个生命周期包括: 加载(Loading)-->验证(Verification)-->准备(Preparation)-->解析(R ...
- 半联结&反联结!
半联结是在两个数据集(表)之间的联结,其中第一个数据集中的数据行在决定是否返回时会根据在另一个数据集中出现或不出现至少一个相匹配的数据行来确定.“不出先”匹配行——这是半联结的一种特殊形式,称为反联结 ...
- MySQL数据库InnoDB存储引擎
MySQL数据库InnoDB存储引擎Log漫游 http://blog.163.com/zihuan_xuan/blog/static/1287942432012366293667/
- 现在的 Linux 内核和 Linux 2.6 的内核有多大区别?
作者:larmbr宇链接:https://www.zhihu.com/question/35484429/answer/62964898来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转 ...
- IO流程及优化
http://blog.csdn.net/xypzwl/article/details/51416883 一.存储设备的存储原理 机械硬盘: 机械硬盘使用磁性物质作为存储介质,用N.S极性来代表0或1 ...
- 存储设备的DDP功能详解
http://blog.csdn.net/u013394982/article/details/18259015 DDP功能,即Dynamic Disk Pool,它是除了现有的RAID0,1,10, ...