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的更多相关文章

  1. 2DToolkit官方文档中文版打地鼠教程

    初始设置 创建一个Unity项目,并导入2D Toolkit插件. 导入完成后,在Project窗口会显示TK2DROOT文件夹(后续版本文件夹名称或许会有变动). 导入素材游戏,你可以从这里下载.下 ...

  2. springmvc demo

    [说明]今天上午稍稍偏了一下方向,看了看servlet的相关知识,下午做maven+spring+springMVC的整合,晚上成功实现了一个小demo(可以在jsp动态页面上获得通过地址栏输入的参数 ...

  3. 原生js打地鼠

    我们要做的是一个打地鼠的游戏,只用原生js 1.导入需要的图片 2.编写页面css样式demo.css *{ margin:0; padding:0; } .game{ position: relat ...

  4. phaser2->3:来个打地鼠试水

    本文中phaser具体版本 phaser2:2.8.1 phaser3:3.17.0 一.实现效果二.实现细节三.项目总结四.参考文档 一.实现效果 源码地址(phaser2&phaser3) ...

  5. 通过一个demo了解Redux

    TodoList小demo 效果展示 项目地址 (单向)数据流 数据流是我们的行为与响应的抽象:使用数据流能帮我们明确了行为对应的响应,这和react的状态可预测的思想是不谋而合的. 常见的数据流框架 ...

  6. 很多人很想知道怎么扫一扫二维码就能打开网站,就能添加联系人,就能链接wifi,今天说下这些格式,明天做个demo

    有些功能部分手机不能使用,网站,通讯录,wifi基本上每个手机都可以使用. 在看之前你可以扫一扫下面几个二维码先看看效果: 1.二维码生成 网址 (URL) 包含网址的 二维码生成 是大家平时最常接触 ...

  7. 在线浏览PDF之PDF.JS (附demo)

    平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html#skill 下载地址:http://mozilla.gith ...

  8. 【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo

    Maven+springboot+阿里大于短信验证服务 纠结点:Maven库没有sdk,需要解决 Maven打包找不到相关类,需要解决 ps:最近好久没有写点东西了,项目太紧,今天来一篇 一.本文简介 ...

  9. vue双向数据绑定原理探究(附demo)

    昨天被导师叫去研究了一下vue的双向数据绑定原理...本来以为原理的东西都非常高深,没想到vue的双向绑定真的很好理解啊...自己动手写了一个. 传送门 双向绑定的思想 双向数据绑定的思想就是数据层与 ...

随机推荐

  1. 消息中间件 ActiveMQ的简单使用

    一.AactiveMQ的下载和安装 1. 下载ActiveMQ 地址:http://activemq.apache.org/activemq-5152-release.html 我这里下载的是wind ...

  2. android 自定义radiogroup的两种方式

    这里先备注下 listview+radiobutton实现  浅显易懂 http://www.haolizi.net/example/view_3312.html 在radiogoup原生态源码的基础 ...

  3. 手动安装Android Support Library(23.0.1)

    在搭建React-Native开发环境的时候,使用Android Sdk Manager无法找到Android Support Library这一项. 所以google了一下,找到了解决办法. 访问A ...

  4. Druid 连接池 JDBCUtils 工具类的使用

    Druid工具介绍 它不仅仅是一个数据库连接池,它还包含一个ProxyDriver,一系列内置的JDBC组件库,一个SQL Parser. 支持所有JDBC兼容的数据库,包括Oracle.MySQL. ...

  5. jquery粘贴操作

    今天忘记记录一个点了,关于input字体默认浅色,聚焦变深的问题. 图一,默认浅色 图二,聚焦出现下拉框“最近搜索”记录,点击“程序员” 图三,input值变为“程序员”,颜色没有变深(复制粘贴也不变 ...

  6. Hibernate inverse反转

    inverse: inverse: 指定由哪一方来维护之间的关联关系 false默认,表示不放弃,是主动放 true:表示把关联关系的维护反转(放弃),对集合对象的修改不会被反映到数据库中 容易出现的 ...

  7. java 总结代码块

    判断str2在str中出现了多少次: //msg: // // 世界上最痛苦的事 莫过于有眼睛却发现不了美 有耳朵却不会欣赏音乐 有心灵却无法理解什么是最真 // 世界上最痛苦的事 莫过于错过了不该错 ...

  8. sqoop操作之HIVE导出到ORACLE

    示例数据准备 hive中创建dept表 create table dept( deptno int, dname string, loc string ) row format delimited f ...

  9. 蓝瓶的钙,好喝的钙——windows,我要蓝屏的

    原文地址:http://80x86.io/post/windows-blue-screen-0x00000050-page_fault_in_nonpaged_area 这里只截取一部分. windo ...

  10. 中国Linux开源镜像站大全

    本文来源:各大开源软件.发行版镜像页面.       请注意这是一个总结,如果您自己搭建了一个小型开源镜像,这里并没有.以下列出的是包含大量不同镜像的站点.       具体配置中,我建议您使用大企业 ...