简版Google小恐龙 ------ unity+C#

carlston06 2023-06-24 16:15:18

目录

跳的代码

自动移动

结束动画

碰撞判定


C#小白的期末作业。。。

谷歌小恐龙游戏是一个浏览器自带的小游戏。

首先打开谷歌Chrome,在地址栏输入:chrome://dino/


在unity里导入小恐龙所要的图片;

 给小恐龙加盒子碰撞器Box Collider2D,物理引擎Rigidbody 2D

同样路也要加盒子碰撞器Box Collider2D 来防止小恐龙掉下

跳的代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using UnityEngine;

public class game1 : MonoBehaviour
{
    Rigidbody2D rb;
    public float jump;  //跳的高度的值
    bool isjumping;
    public GameManager gm;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        isjumping = false;
    } 

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isjumping == false)
        {
            rb.velocity = new Vector2(0, jump);
            isjumping = true;
        }
    }
//小恐龙只有碰撞后才可以跳,这样就可以防止小恐龙一直跳
    private void OnCollisionEnter2D(Collision2D collision)
    {
        isjumping = false;

//小恐龙碰撞仙人掌Cactus时弹出GameOver画面
        if (collision.gameObject.tag == "Cactus")
        {
            gm.GameOver();
        }
        
    }
}

自动移动

using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Timers;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    public float movementSpeed;    //移动的速度
    public float startPosition;    //循环开始的位置
    public float endPosition;      //循环结束的位置

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

    // Update is called once per frame
    void Update()
    {
        transform.position = new Vector2(transform.position.x - movementSpeed * Time.deltaTime , transform.position.y);

        if (transform.position.x <= endPosition)
        {
            if(gameObject.tag == "Cactus")
            {
                Destroy(gameObject);
            }
            else
            {
                transform.position = new Vector2(startPosition, transform.position.y);
            }  
        }
    }
}

仙人掌的自动生成

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public GameObject cactus;
    public GameObject cactusSpawnPosition;
    public float spawnTime;                    //生成的时间
    float timer;
    public GameObject GameOverScene;

    // Start is called before the first frame update
    void Start()
    {
        Time.timeScale = 1;
    }

    // Update is called once per frame
    void Update()
    {
        timer += Time.deltaTime;

        if(timer >= spawnTime)
        {
            Instantiate(cactus, cactusSpawnPosition.transform);
            timer = 0;
        }

    }
//GameOver 调用
    public void GameOver ()
    {
        Time.timeScale = 0;
        GameOverScene.SetActive(true);
    }
//GameOverButton按钮的“lv1”场景的调用
    public void Restart ()
    {
        SceneManager.LoadScene("lv1");
    }
}

结束动画

 把GameOverText和GameOverButton放入面板GameOver里

效果:

 

 

碰撞判定

给仙人掌定义Cactus的标签

当小恐龙碰撞Cactus时弹GameOver

我们在开始的小恐龙的脚本里写就可以了

   void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isjumping == false)
        {
            rb.velocity = new Vector2(0, jump);
            isjumping = true;
        }
    }
//小恐龙只有碰撞后才可以跳,这样就可以防止小恐龙一直跳
    private void OnCollisionEnter2D(Collision2D collision)
    {
        isjumping = false;

//小恐龙碰撞仙人掌Cactus时弹出GameOver画面
        if (collision.gameObject.tag == "Cactus")
        {
            gm.GameOver();
        }
        

后在自动生成的脚本里再加入

 void Start()
    {
        Time.timeScale = 1;
    }

来调节帧数倍速

哈哈~~这就可以运行简版的Google小恐龙了

...全文
746 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
SoftwareTeacher 2023-06-25
精选
  • 打赏
  • 举报
回复
5.00元

赞! 可以把小恐龙变为小兔子么? 需要修改程序的哪些方面?

carlston06 2023-06-25
  • 举报
回复
@SoftwareTeacher 在unity里把小恐龙的图片换成小兔子就可以了
22级moyuqaq0303 2023-06-25
  • 打赏
  • 举报
回复

喔,好厉害good

1,365

社区成员

发帖
与我相关
我的任务
社区描述
柳职院电子信息工程学院同学们的学习园地
社区管理员
  • c_university_1974
  • qq_39231145
  • zhuisir
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

各位加入社区的同学,请完善社区信息,把社区昵称改为【班级-姓名】,社区签名改为【班级-学号-姓名】的格式

如【社区昵称】20计应1班  张某某(班级用简称)

     【社区签名】2020级计算机应用技术1班 20201234567 张某某 (班级用全称)

试试用AI创作助手写篇文章吧