计算机游戏程序设计 实验指导书 实验一.docx
《计算机游戏程序设计 实验指导书 实验一.docx》由会员分享,可在线阅读,更多相关《计算机游戏程序设计 实验指导书 实验一.docx(59页珍藏版)》请在冰豆网上搜索。
计算机游戏程序设计实验指导书实验一
计算机游戏程序设计
实验指导书
实验一GUI游戏界面的实现
一、实验目的与要求
1.熟悉及掌握GUI的高级控件,以及用法。
2.掌握GUI自定义皮肤用法
3.熟悉GUILayout的使用。
4.熟悉2D贴图的绘制和帧动画的实现方法。
二、实验内容及步骤
1.熟悉GUI高级控件,练习使用GUI的高级控件制作2~3个游戏界面。
2.练习使用GUI自定义皮肤,实现游戏界面的字体,背景颜色等设置。
3.熟悉GUILayout的使用,联系使用GUILayout的水平线性布局和垂直线性布局,并加适当偏移。
4.熟悉2D贴图的绘制和帧动画的实现方法,练习在界面中绘制静态图片和动画。
三、实验仪器与软件
1.PC计算机
2.Unity3D软件
四、实验报告要求
1.熟悉GUI高级控件,练习使用GUI的高级控件制作2~3个游戏界面。
Label控件:
usingUnityEngine;
usingSystem.Collections;
publicclasskongjian:
MonoBehaviour{
publicTextureimageTexture;
privateintimageWidth;
privateintimageHeight;
privateintscreenWidth;
privateintscreenHeight;
//Usethisforinitialization
voidStart(){
//得到屏幕宽高
screenWidth=Screen.width;
screenHeight=Screen.height;
//得到图片宽高
imageWidth=imageTexture.width;
imageHeight=imageTexture.height;
}
voidOnGUI(){
//将文字内容显示在屏幕中
GUI.Label(newRect(100,10,100,30),"hellounity");
//将贴图显示在屏幕中
GUI.Label(newRect(100,120,imageWidth,imageHeight),imageTexture);
}
//Updateiscalledonceperframe
voidUpdate(){
}
}
Button
usingUnityEngine;
usingSystem.Collections;
publicclasskongjian:
MonoBehaviour{
publicTexturebuttonTexture;
privatestringstr;
//Usethisforinitialization
voidStart(){
str="请点击按钮!
";
}
voidOnGUI(){
GUI.Label(newRect(10,10,Screen.width,30),str);
if(GUI.Button(newRect(10,50,buttonTexture.width,buttonTexture.height),buttonTexture)){
//点击按钮修改提示信息
str="您点击了图片按钮";
}
//设置按钮中文字的颜色
GUI.color=Color.green;
//设置按钮的背景色
GUI.backgroundColor=Color.black;
}
//Updateiscalledonceperframe
voidUpdate(){
}
}
2.练习使用GUI自定义皮肤,实现游戏界面的字体,背景颜色等设置。
usingUnityEngine;
usingSystem.Collections;
publicclassNewBehaviourScript:
MonoBehaviour{
//自定义皮肤
publicGUISkinliuna;
//单选是否选中
privateboolchoose=false;
//拖动窗口的位置
privateRectwindowRect=newRect(20,20,120,50);
//输入框中默认显示
privatestringedit="请输入字符串";
//Usethisforinitialization
voidStart(){
}
voidOnGUI()
{
//设置GUI皮肤为我们自定义皮肤
GUI.skin=liuna;
//绘制自定义按钮
GUI.Button(newRect(100,100,100,100),"点我");
//单项选择
choose=GUI.Toggle(newRect(10,50,100,30),choose,"单项选择");
//输入框
edit=GUI.TextField(newRect(200,10,200,20),edit,25);
//注册窗口
windowRect=GUI.Window(0,windowRect,setWindow,"这是一个窗口");
//设置GUI皮肤为系统定义皮肤
GUI.skin=null;
//绘制系统自带按钮
}
voidsetWindow(intwindowID)
{
//创建一个可以自由拖动的窗口
GUI.DragWindow();
//绘制自定义按钮
}
//Updateiscalledonceperframe
voidUpdate(){
}
}
3.熟悉GUILayout的使用,练习使用GUILayout的水平线性布局和垂直线性布局,并加适当偏移。
usingUnityEngine;
usingSystem.Collections;
publicclassNewBehaviourScript1:
MonoBehaviour{
//Usethisforinitialization
voidStart(){
}
voidOnGUI(){
//开始一个显示区域
GUILayout.BeginArea(newRect(100,100,200,60));
//开始最外层横向布局
GUILayout.BeginHorizontal();
//嵌套一个纵向布局
GUILayout.BeginVertical();
GUILayout.Box("One");
//两个box中间偏移10像素
GUILayout.Space(10);
GUILayout.Box("Two");
//结束嵌套的纵向局部
GUILayout.EndVertical();
//两个纵向布局中间偏移20像素
GUILayout.Space(20);
//嵌套一个纵向布局
GUILayout.BeginVertical();
GUILayout.Box("Three");
//两个box中间偏移10像素
GUILayout.Space(10);
GUILayout.Box("Four");
//结束嵌套的纵向局部
GUILayout.EndVertical();
//结束最外层横向布局
GUILayout.EndHorizontal();
//结束显示区域
GUILayout.EndArea();
}
//Updateiscalledonceperframe
voidUpdate(){
}
}
4.熟悉2D贴图的绘制和帧动画的实现方法,练习在界面中绘制静态图
2D贴图
usingUnityEngine;
usingSystem.Collections;
publicclassNewBehaviourScript:
MonoBehaviour{
privateTexture2DtexSingle;
//privateObject[]texAll;
voidOnGUI()
{
if(GUI.Button(newRect(0,10,100,50),"加载一张图片")){
if(texSingle==null){
texSingle=(Texture2D)Resources.Load("flower0/0");
}
}
if(texSingle!
=null){
GUI.DrawTexture(newRect(110,10,120,120),texSingle,ScaleMode.StretchToFill,true,0);
}
}
//Usethisforinitialization
voidStart(){
}
//Updateiscalledonceperframe
voidUpdate(){
}
}
帧动画
usingUnityEngine;
usingSystem.Collections;
publicclassNewBehaviourScript1:
MonoBehaviour{
//动画数组
privateObject[]animUp;
privateObject[]animDown;
privateObject[]animLeft;
privateObject[]animRight;
//地图贴图
privateTexture2Dmap;
//当前人物动画
privateObject[]tex;
//人物X坐标
privateintx;
//人物Y坐标
privateinty;
//帧序列
privateintnowFram;
//动画帧的总数
privateintmFrameCount;
//限制一秒多少帧
privatefloatfps=10;
//限制帧的时间
privatefloattime=0;
//Usethisforinitialization
voidStart(){
x=200;
y=200;
//得到帧动画中的所有图片资源
animUp=Resources.LoadAll("up");
animDown=Resources.LoadAll("down");
animLeft=Resources.LoadAll("left");
animRight=Resources.LoadAll("right");
//得到地图资源
map=(Texture2D)Resources.Load("map/ground");
//设置默认动画
tex=animUp;
}
voidOnGUI()
{
//绘制贴图
GUI.DrawTexture(newRect(0,0,Screen.width,Screen.height),map,ScaleMode.StretchToFill,true,0);
//绘制帧动画
DrawAnimation(tex,newRect(x,y,32,48));
//点击按钮移动人物
if(GUILayout.RepeatButton("向上"))
{
y-=2;
tex=animUp;
}
if(GUILayout.RepeatButton("向下"))
{
y+=2;
tex=animDown;
}
if(GUILayout.RepeatButton("向左"))
{
x-=2;
tex=animLeft;
}
if(GUILayout.RepeatButton("向右"))
{
x+=2;
tex=animRight;
}
//点击上下左右键移动人物
if(Input.GetKey(KeyCode.UpArrow))
{
y-=2;
tex=animUp;
}
if(Input.GetKey(KeyCode.DownArrow))
{
y+=2;
tex=animDown;
}
if(Input.GetKey(KeyCode.LeftArrow))
{
x-=2;
tex=animLeft;
}
if(Input.GetKey(KeyCode.RightArrow))
{
x+=2;
tex=animRight;
}
}
voidDrawAnimation(Object[]tex,Rectrect)
{
//绘制当前帧
GUI.DrawTexture(rect,(Texture2D)tex[nowFram],ScaleMode.StretchToFill,true,0);
//计算限制帧时间
time+=Time.deltaTime;
//超过限制帧则切换图片
if(time>=2.0/fps){
//帧序列切换
nowFram++;
//限制帧清空
time=0;
//超过帧动画总数从第0帧开始
if(nowFram>=tex.Length)
{
nowFram=0;
}
}
}
//Updateiscalledonceperframe
voidUpdate(){
}
}
实验二Unity游戏脚本
软件122班刘娜122513
一、实验目的与要求
1.熟悉及掌握MonoDevelop脚本编辑器的使用方法。
2.Unity脚本的生命周期。
3.熟练使用脚本来操作游戏对象。
二、实验内容及步骤
1.熟悉MonoDevelop脚本编辑器的使用方法,联系实现脚本调试。
2.编程实现创建游戏对象(立方体,球体),给游戏对象命名,改变颜色,添加刚体组件。
3.分别通过名称、标签获得游戏对象,通过标签获得多个游戏对象。
4.通过脚本控制游戏对象,改变游戏对象的位置,旋转游戏对象,缩放游戏对象。
三、实验仪器与软件
1.PC计算机
2.Unity3D软件
四、实验报告要求
1.熟悉MonoDevelop脚本编辑器的使用方法,联系实现脚本调试。
创建C#脚本:
在脚本中编辑代码
2.编程实现创建游戏对象(立方体,球体),给游戏对象命名,改变颜色,添加刚体组件。
usingUnityEngine;
usingSystem.Collections;
publicclassNewBehaviourScript:
MonoBehaviour{
//Usethisforinitialization
voidStart(){
}
//Updateiscalledonceperframe
voidUpdate(){}
voidOnGUI(){
if(GUI.Button(newRect(0,0,100,100),"创建立方体")){
GameObjectobjCube=GameObject.CreatePrimitive(PrimitiveType.Cube);
objCube.AddComponent("Rigidbody");
objCube.name="Cube";
objCube.renderer.material.color=Color.blue;
}
if(GUI.Button(newRect(100,0,100,100),"创建球体")){
GameObjectobjSphere=GameObject.CreatePrimitive(PrimitiveType.Sphere);
objSphere.AddComponent("Rigidbody");
objSphere.name="Sphere";
objSphere.renderer.material.color=Color.green;
}
}
}
受到重力降落:
3.分别通过名称、标签获得游戏对象,通过标签获得多个游戏对象。
通过名称获得游戏对象:
usingUnityEngine;
usingSystem.Collections;
publicclassNewBehaviourScript1:
MonoBehaviour{
privateGameObjectobjCube;
privateGameObjectobjSphere;
privateboolisCubeRoate=false;
privateboolisSphereRoate=false;
privatestringCubeInfo="旋转立方体";
privatestringSphereInfo="旋转球体";
//Usethisforinitialization
voidStart(){
objCube=GameObject.Find("Cube");
objSphere=GameObject.Find("Sphere");
}
//Updateiscalledonceperframe
voidUpdate(){
if(isCubeRoate){
if(objCube){//档立方体不为null时旋转
objCube.transform.Rotate(0.0f,Time.deltaTime*200,0.0f);
}
}
if(isSphereRoate){
if(objSphere){//档立方体不为null时旋转
objSphere.transform.Rotate(0.0f,Time.deltaTime*200,0.0f);
}
}
}
voidOnGUI(){
if(GUI.Button(newRect(0,0,100,100),CubeInfo)){
isCubeRoate=true;
CubeInfo="停止旋转";
}else{
isCubeRoate=false;
CubeInfo="旋转立方体";
}
if(GUI.Button(newRect(0,100,100,100),SphereInfo)){
isSphereRoate=true;
SphereInfo="停止旋转";
}else{
isSphereRoate=false;
SphereInfo="旋转球体";
}
}
}
通过标签来获取游戏对象
给Cube添加一个标签代码改为:
voidStart(){
objCube=GameObject.FindWithTag("tag1");
objSphere=GameObject.Find("Sphere");
}
通过标签获取多个游戏对象:
voidStart(){
objCube=GameObject.FindGameObjectsWithTag("tag1");
objSphere=GameObject.Find("Sphere");
}
4.通过脚本控制游戏对象,改变游戏对象的位置,旋转游戏对象,缩放游戏对象。
改变游戏对象的位置:
usingUnityEngine;
usingSystem.Collections;
publicclassNewBehaviourScript2:
MonoBehaviour{
privatefloatx=0.0f;
privatefloaty=0.0f;
privatefloatz=0.0f;
privateGameObjectobj;
//Usethisforinitialization
voidStart(){
obj=GameObject.Find("Cube");
}
//Updateiscalledonceperframe
voidUpdate(){
}
voidOnGUI(){
GUI.Box(newRect(0,0,100,100),"移动立方体x轴");
x=GUILayout.HorizontalSlider(x,-10.0f,10.0f,GUILayout.Width(400));
GUI.Box(newRect(0,100,100,100),"移动立方体y轴");
y=GUILayout.HorizontalSlider(y,-10.0f,10.0f,GUILayout.Width(400));
GUI.Box(newRect(0,200,100,100),"移动立方体z轴");
z=GUILayout.HorizontalSlider(z,-10.0f,10.0f,GUILayout.Width(400));
obj.transform.position=ne