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 ...
随机推荐
- 【BZOJ1010】【HNOI2008】玩具装箱
继续看黄学长代码 原题: P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1.. ...
- SHOW OPEN TABLES – what is in your table cache
One command, which few people realize exists is SHOW OPEN TABLES – it allows you to examine what tab ...
- 虚拟化之vmx配置文件
https://www.haiku-os.org/get-haikuhttp://sanbarrow.com/vmx.html contradictionn. 矛盾:否认:反驳homegrownadj ...
- Python教程:[69]strip()函数详解
strip()用于裁剪字符串首尾的某些字符,是一个用处非常多的函数,今天我们来通过例子来探讨一下它的基本用法: 假如有一个这样的字符串 strip()不带任何参数,可以删除首位的空格 但是strip( ...
- 微信浏览器里location.reload问题
微信浏览器里location.reload问题会导致有时候post数据丢失.建议不要用此方式,尽量ajax方式获取或不要为了获取新的UI而刷新页面 2015-12-26 00:51:34array ( ...
- runliuv, 安卓查看WIFI密码
用RE查看data/misc/wifi/wpa_supplicant.conf或者其他文件名以.conf结尾的文件
- php xdebug xampp eclipse
Eclipse配置 一:配置workspace 打开Eclipse for PHP Developers,需要设置workspace,这个必须设置到C:\xampp\htdocs目录,否则待会无法进行 ...
- 【转】java 自动装箱与拆箱
java 自动装箱与拆箱 这个是jdk1.5以后才引入的新的内容,作为秉承发表是最好的记忆,毅然决定还是用一篇博客来代替我的记忆: java语言规范中说道:在许多情况下包装与解包装是由编译器自行完成的 ...
- IntelliJ IDEA设置JDK
File→Project Structure→Project SDK→New 来自为知笔记(Wiz)
- bzoj2338 数矩形
给出N(N≤1500)个点,求选四个点作为顶点组成矩形的最大面积,保证有解. 对每两个点连边,按边长排序,枚举等长且中点相同的边作为对角线组成矩形,计算面积取最大值. 时间复杂度O(n2logn) # ...