打地鼠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的双向绑定真的很好理解啊...自己动手写了一个. 传送门 双向绑定的思想 双向数据绑定的思想就是数据层与 ...
随机推荐
- Application 应用对象
在整个服务器运行过程中,application对象只有一个 下面的例子是访问次数的统计 <%@ page language="java" contentType=" ...
- Django 博客项目01 数据库设计与验证码校验+Ajax登录
数据库设计 from django.db import models from django.contrib.auth.models import AbstractUser class UserInf ...
- SQL 将非标准日期格式转换成标准格式,进行条件判断
a.JLDate为非标准日期格式: 例: 2011-8-28 0:00:000011-8-28 0:00:000111-8-4 0:00:00 select CONVERT(varchar(50),C ...
- java web程序 上机考试登陆界面设计实现
今天是java web上机.做一个登陆注册的界面.要求:jsp.mysql数据库,js做一个美观的界面.功能.可以添加 更多啊.我做的界面被老师狠狠的扣了分.问题在于.当用户没有输入任何信息(没有输入 ...
- 2018-2019 20165226 Exp6 信息搜集与漏洞扫描
2018-2019 20165226 Exp6 信息搜集与漏洞扫描 目录 一.实验内容说明及基础问题回答 二.实验过程 Task1 各种搜索技巧的应用 检测特定类型的文件 搜索网址目录结构 trace ...
- Python 标准库 ConfigParser 模块 的使用
Python 标准库 ConfigParser 模块 的使用 demo #!/usr/bin/env python # coding=utf-8 import ConfigParser import ...
- Linux 简单命令查询CPU、内存、网卡等信息
[转自]Linux查询CPU.内存.网卡等信息 看CPU信息(型号)# cat /proc/cpuinfo | grep name | cut -f2 -d: |uniq -c 1 Int ...
- Linux下不同颜色文件的类型
蓝色表示目录: 绿色表示可执行文件: 红色表示压缩文件: 浅蓝色表示链接文件:主要是使用ln命令建立的文件 灰色表示其它文件: 红色闪烁表示链接的文件有问题了: 黄色是设备文件,包括block, ch ...
- spyder快捷键
ctrl+1:注释/反注释 ctrl+4/5:注释/反注释 tab/ shift+tab:缩进/反缩进 F5:全运行 F9:单行运行 F11:全屏 ctrl+I:显示帮助
- 6.28笔记-servlet3.0注解配置、文件上传、过滤器、监听器
一.servlet3.0注解配置 使用javaEE6.0 支持servlet3.0 value的值就是访问路径 urlPatterns的值也是访问路径 @WebServlet(name="D ...