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. 虚拟机设置成桥接模式x86_openwrt也可以上网

    一.虚拟机桥接设置 1. 2.选择 虚拟机 >>设置 三.ip设置,要在同一网段,能够分配到路由的ip地址 1.主机ip 2.虚拟机中x86_openwrt ip 四.openwrt设置 ...

  2. msp430学习笔记-时钟及延时函数

    引用:http://blog.chinaunix.net/uid-24343357-id-3271380.html MCLK默认时钟源是DCOCLK,SMCLK默认时钟源也是DCOCLK,DCOCLK ...

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

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

  4. BASIC-14_蓝桥杯_时间转换

    示例代码: #include <stdio.h> int main(void){ int t = 0 , h = 0 , m = 0 , s = 0 ; scanf("%d&qu ...

  5. ubuntu修改Bash命令行提示符

    用户通过远程登陆,初始的PS1为\s-\v\$, bash为/bin/bash,然后先后执行/etc/profile和~/.bash_profile中的命令. /etc/profile文件中调用执行/ ...

  6. ASP.NET Web Pages:Chart 帮助器

    ylbtech-.Net-ASP.NET Web Pages:Chart 帮助器 1.返回顶部 1. ASP.NET Web Pages - Chart 帮助器 Chart 帮助器 - 众多有用的 A ...

  7. 拼图游戏js

    实现算法: 1. JavaScript动态生成拼图:通过生成16个div,且除最后一个div不使用背景图片以外,其他div都设置拼图图片为背景.然后通过调整background-position来实现 ...

  8. [UE4]添加蒙太奇动画

    选择蒙太奇所使用的骨骼

  9. 模型融合策略voting、averaging、stacking

    原文:https://zhuanlan.zhihu.com/p/25836678 1.voting 对于分类问题,采用多个基础模型,采用投票策略选择投票最多的为最终的分类. 2.averaging 对 ...

  10. Asynchronous programming with Tornado

    Asynchronous programming can be tricky for beginners, therefore I think it’s useful to iron some bas ...