Loading...

这是笔者公选课《游戏程序设计基础》的作业,一个对解谜游戏和编程学习结合的设想

在这个 demo 中,player 需要阅读两块路牌中的信息,特别是第二块记录了箱子类定义的信息:

1
2
3
4
5
6
7
8
class box {
def _init_(self):
self.contain = [ ]
self.opened = false
def open(self):
self.opened = true }
the_box = box()
the_box.contain.append(key)

这段代码用 python 实现定义了一个 box 类,并创建了一个游戏里箱子的 instance(就是玩家面前的箱子。)对于 box 类玩家首先需要阅读出它有两个 Class Attribute:contain 属性用列表的形式储存 box 的内容物,而 opened 属性默认为关闭。玩家要做的就是先调用 open()的方法打开箱子,并用 python 中列表的“[__]”方法来访问箱子中的钥匙以通过这个场景。


这个 DEMO 的三个脚本

  1. CheckInput:实现输入正确的代码后对物件进行操作(如打开箱子,销毁箱子)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using UnityEngine;
using UnityEngine.UI;
public class CheckInput : MonoBehaviour
{
// 输入框组件的引用
public InputField inputField;
// 文本框组件的引用
public Text inputText;
// 箱子模型的引用
public GameObject box;
// 箱子打开的动画组件的引用
public Animator boxAnimator;
// 获得物品的名字

// 正确的文字
public string correctText1;
public string correctText2;

// refenrence to computer object
public GameObject pc;

// 检测输入框的值是否正确
public void CheckInputValue()
{
// 获取输入框的值
string input = inputField.text;
// 如果输入框的值等于正确的文字
if (input == correctText1)
{

boxAnimator.SetTrigger("Open");
Destroy(pc);
}
if (input == correctText2){

Destroy(box);
}
}

void Update()
{
CheckInputValue();

}

}

  1. Playercontroller:实现了角色的左右移动(包括对指定动画进行调用切换)和跳跃功能(本 DEMO 中未使用)。左右移动的代码照抄了 M_Studio 的教程,跳跃功能则是笔者照藤摸瓜自己实现的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System.Security.Cryptography;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class PlayerController : MonoBehaviour

{
Rigidbody2D rb;
Animator anim;
public float speed;
float xVelocity;

public bool isOnGround;

public float jumpForce;

public bool jumpInput;

public bool Start_jump;

public bool Start_landing;

// Start is called before the first frame update
void Start()

{
rb=GetComponent<Rigidbody2D>();

anim=GetComponent<Animator>();

}

// Update is called once per frame
void Update()
{


Anime_trans();

Movement();

Jump();

}

void Movement()
{
xVelocity=Input.GetAxisRaw("Horizontal");

rb.velocity=new Vector2(xVelocity*speed,rb.velocity.y);

if(xVelocity!=0)
{
transform.localScale=new Vector3(xVelocity,1,1);

}

}

void Anime_trans()
{

anim.SetFloat("speed",Mathf.Abs(rb.velocity.x));//runing anime

}

void Jump()

{

jumpInput=Input.GetKeyDown(KeyCode.UpArrow);

Start_jump=(jumpInput==true&&isOnGround==true);

if(Start_jump==true)
{
rb.AddForce(Vector2.up*jumpForce,ForceMode2D.Impulse);

}
//implemnt of landing
if (rb.velocity.y<0)
{
Start_landing=true;
}

else
{
Start_landing=false;
}

}

}

  1. RoadSigh:实现了通过对玩家与告示牌的检测(这里利用的是对图层的检测,为每个需要这个方法的 object 设定了不同且独一无二的的图层,并与玩家的模型的进行检测)对 DEMO 中 TIPS 的 UI 与 Terminal(玩家输入框)的隐藏和显示。(这个脚本是笔者编程刚入门的时候写的,相当之糟糕,把一个方法小改复制粘贴重复用了四次…..)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
public class RoadSign : MonoBehaviour
{

public Canvas signCanvas1;
public Canvas signCanvas2;

public Canvas signCancas3;
public Canvas Terminal;

public bool checkaround;
public GameObject aroundcheck;
public float checkRadius;

public LayerMask keiji1;
public LayerMask keiji2;
public LayerMask keiji3;
public LayerMask keiji4;




// 在游戏开始时,隐藏画布
private void Start()
{
signCanvas1.enabled = false;
signCanvas2.enabled = false;
Terminal.enabled = false;
}

// 在每一帧更新时,检测玩家是否靠近或点击路牌
private void Update()
{
To_show_or_hide_1();
To_show_or_hide_2();
To_show_or_hide_3();
To_show_or_hide_4();
}

// check if show the canvas
void To_show_or_hide_1(){

checkaround=Physics2D.OverlapCircle(aroundcheck.transform.position,checkRadius,keiji1);
if (checkaround == true){
signCanvas1.enabled = true;
}
if (checkaround == false){
signCanvas1.enabled = false;
}
}
void To_show_or_hide_2(){

checkaround=Physics2D.OverlapCircle(aroundcheck.transform.position,checkRadius,keiji2);
if (checkaround == true){
signCanvas2.enabled = true;
}
if (checkaround == false){
signCanvas2.enabled = false;
}
}
void To_show_or_hide_3(){

checkaround=Physics2D.OverlapCircle(aroundcheck.transform.position,checkRadius,keiji3);
if (checkaround == true){
Terminal.enabled = true;
}
if (checkaround == false){
Terminal.enabled = false;
}
}


void To_show_or_hide_4(){

checkaround=Physics2D.OverlapCircle(aroundcheck.transform.position,checkRadius,keiji4);
if (checkaround == true){
signCancas3.enabled = true;
}
if (checkaround == false){
signCancas3.enabled = false;
}
}
private void OnDrawGizmosSelected() {
Gizmos.color=Color.blue;
Gizmos.DrawWireSphere(aroundcheck.transform.position,checkRadius);
}
}