c# 贪吃蛇源码
using UnityEngine;
using System.Collections;
using System.Diagnostics;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.UI;
public class SnakeMove : MonoBehaviour {
List<Transform> Body = new List<Transform> ();//存放Transform数据类型的数组Body
public GameObject BodyObject;/ /定义一个游戏物体 蛇
public GameObject sFood;//// 定义一个游戏物体 食物
//updown Y轴 left down是x轴 forward back是z 轴
Vector3 postion = Vector3.up; //Vector3.up的简称 Vertor3(0,1,0)
private bool s = false;
// Use this for initialization
public float speed=0.1f;
public float time = 0;
//public float time0 =1;
// public float xlimit = 25.5f;
// public float ylimit = 25.5f;
public int xlimit = 25;
public int ylimit = 25;
//伤害数值
public int Value;
//目标位置
private Vector3 mTarget;
//屏幕坐标
private Vector3 mScreen;
//文本宽度
public float ContentWidth = 100;
//文本高度
public float ContentHeight = 50;
//GUI坐标
private Vector2 mPoint;
//炫酷的字体
GUIStyle frontStyle = new GUIStyle();
public Text text;
Vector3 VPostion;
public GameObject body1;
public ArrayList list = new ArrayList();
private bool isEated = false;
void Start () {
//Time.timeScale=1;
//time = Time.time + time;
//InvokeRepeating 从第一秒开始,每隔四秒调用一次
InvokeRepeating ("Food", 1, 4);1秒后 调用Food 之后每4秒调用一次
InvokeRepeating ("Move", speed, speed);
//获取目标位置
mTarget =transform.position;
//获取屏幕坐标
mScreen =Camera.main.WorldToScreenPoint(mTarget);
//将屏幕坐标转化为GUI坐标
mPoint = new Vector2(mScreen.x,Screen.height-mScreen.y);
//
Value =0;
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0))
Time.timeScale=1;
if (Input.GetKeyDown//(获取键按下) (KeyCode.D)&&postion!=Vector3.left)
{
postion = Vector3.right;
}
if (Input.GetKeyDown (KeyCode.A)&&postion!=Vector3.right)
{
postion = Vector3.left;
}
if (Input.GetKeyDown (KeyCode.S)&&postion!=Vector3.up)
{
postion = Vector3.down;
}
if (Input.GetKeyDown (KeyCode.W)&&postion!=Vector3.down)
{
postion = Vector3.up;
}
//Time.tiem 系统时间
// if (time<=Time.time)
// {
// transform.Translate (postion);
// time += 1;
// //time 越小 速度越快
// }
//Random r = new Random ();
//OnTriggerEnter();
}
void Move()
{
//transform.Translate (postion);
VPostion = transform.position;
transform.Translate (postion); //Transform.Translate平移 向某方向移动物体多少距离
if (isEated)
{
GameObject g = (GameObject)Instantiate(body1,VPostion,Quaternion.identity);
g.GetComponent<MeshRenderer> ().material.color = new Color (Random.Range (0, 1.0f), Random.Range (0, 1.0f), Random.Range (0, 1.0f));
list.Insert (0, g);//将一个项插入指定索引处的 IList<(Of <(T>)>)。
//将元素插入 ArrayList 的指定索引处。 可在任意位置插入。
isEated = false;
}
else if (list.Count>0)
{
// //最后一个元素的位置赋值给新的位置
// //最后一个元素插入在第一个元素地址
// //删除最后一个元素
((GameObject)list[list.Count-1]).transform.position = VPostion;
list.Insert (0, list [list.Count - 1]);//在0的位置插入一个
list.RemoveAt (list.Count-1);//移除 ArrayList 的指定索引处的元素。
}
}
void Food()
{
System.Random r = new System.Random ();
float x = r.Next (-xlimit,xlimit);
float y = r.Next (-ylimit,ylimit);
// float x = Random.Range(-xlimit,xlimit);
// float y = Random.Range (-ylimit, ylimit);
Instantiate (sFood, new Vector2 (x, y), Quaternion.identity);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Food"))
{
if(!isEated)
Value++;
isEated = true;
Destroy (other.gameObject);
}
else
{
Time.timeScale=0;
SceneManager.LoadScene (0);
}
text.text = "Score :" + Value;
}
void OnGUI()
{
//保证目标在摄像机前方
if (mScreen.z > 0)
{
//GUI.color = Color.blue;
//内部使用GUI坐标进行绘制
frontStyle.fontSize=40;
frontStyle.normal.background = null;//设置背景填充
frontStyle.normal.textColor = new Color (100, 0, 128);//设置字体颜色
GUI.Label(new Rect(30,0,ContentWidth,ContentHeight),"分数为"+Value.ToString(),frontStyle);
// mPoint.x,mPoint.y
}
}
}
c# 贪吃蛇源码的更多相关文章
- Winfrom 极简版贪吃蛇源码
该源码是我在百度知识库借助前辈的的经验,加上自己的一点小改动写的一个非常简陋的贪吃蛇小程序.如果你们有更好的改动方案,欢迎评论. 进入主题吧! 1.创建一个桌面应运程序,拖一个定时器控件.这样,程序界 ...
- H5 贪吃蛇源码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js贪吃蛇源码
1.注意,自己引入jquery,这个demo基于jquery的,我的jquery是写的本地的 2.没有写注释,看不懂的再问我吧, <!DOCTYPE html><html> & ...
- C语言实现贪吃蛇源码
先放效果 源代码 //2016-2-12 //zhaoyu //Gmail:zhaoyu1995.com@gmail.com //Language: C //Platform:Code::Blocks ...
- [C语言]贪吃蛇_结构数组实现
一.设计思路 蛇身本质上就是个结构数组,数组里存储了坐标x.y的值,再通过一个循环把它打印出来,蛇的移动则是不断地刷新重新打印.所以撞墙.咬到自己只是数组x.y值的简单比较. 二.用上的知识点 结构数 ...
- Android源码50例汇总,欢迎各位下载(转载)
下载中心好资料很多,藏在各个角落,小弟在此帮大家做了一个整理,做了一个下载目录,方便大家选择性下载. 源码实例如下: <Android应用开发揭秘>源代码推荐 http://down.51 ...
- JS小游戏:贪吃蛇(附源码)
javascript小游戏:贪吃蛇 此小游戏采用的是面向对象的思想,将蛇,食物,和游戏引擎分为3个对象来写的. 为方便下载,我把js写在了html中, 源码中暂时没有注释,等有空我在添加点注释吧. 游 ...
- Unity 3D游戏-贪吃蛇类游戏源码:重要方法和功能的实现
贪吃蛇类游戏源码 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 头部移动方式 2 生成 Shit 道具 ...
- iOS补位动画、沙漏效果、移动UITableViewCell、模拟贪吃蛇、拖拽进度等源码
iOS精选源码 JHAlertView - 一款黑白配色的HUD之沙漏效果 继承UIButton的自定义按钮SPButton 用递归算法实现iOS补位动画 iOS 长按移动UITableViewCel ...
随机推荐
- 论文笔记之:Learning to Track: Online Multi-Object Tracking by Decision Making
Learning to Track: Online Multi-Object Tracking by Decision Making ICCV 2015 本文主要是研究多目标跟踪,而 online ...
- Dtrace for Linux 2016
http://www.brendangregg.com/blog/2016-10-27/dtrace-for-linux-2016.html
- Java Language and Virtual Machine Specifications
The Java Language Specification, Java SE 8 Edition HTML | PDF The Java Virtual Machine Specification ...
- 利用C#Marshal类实现托管和非托管的相互转换
Marshal 类 命名空间:System.Runtime.InteropServices 提供了一个方法集,这些方法用于分配非托管内存.复制非托管内存块.将托管类型转换为非托管类型,此外还提供了在与 ...
- nginx配置-http和https
#user nobody;worker_processes 1;error_log logs/error.log;#error_log logs/error.log notice;#error_log ...
- HttpModule和Http Handler (比较与区别)
HttpModule和Http Handler (比较与区别) HttpModule概述 暂时先不考虑我们自己实现Http Module的情况.在.Net中,Http Module 是实现了IHttp ...
- 1 TKinter小窗口及标题
说明 :本博客户关于tkinter的知识参考:2014 年辛星 Python 界面编程教程第二版 创建一个GUI程序的步骤: 创建一个GUI程序 1.导入Tkinter模块 2.创建控件 3.指定这个 ...
- mysql性能的检查和调优方法
mysql性能的检查和调优方法 发布时间:2009 年 10 月 4 日 发布者: OurMySQL 来源:sudone.com 才被阅读:3,524 次 才1条评论 我一直是使用my ...
- 非ROOT用户启动Tomcat
[root@Z ~]# adduser tomcat [root@Z ~]# chown -R tomcat:tomcat /usr/local/tomcat/ [root@Z ~]# chown - ...
- C# 常见集合之前的转换
1,从System.String[]转到List<System.String> System.String[] str={"str","string" ...