简介:

最近在学习Unity3D,用了两天时间做了个小游戏打算放上了和大家分享一下,项目名定义为Flapping,是参考Flappy Bird做的,高手勿喷。

这是原本游戏效果图:

这是本项目效果图:

资源下载:

1. 完整源代码下载

2. PC发布版下载

3. Android发布版APK下载

源代码:

源代码里已经打好了注释,主要分为3个C#脚本。第一个是Player.cs,是本游戏最核心脚本,用来初始化场景和控制小鸟;第二个是Pillar.cs,处理柱子被小鸟撞后的特效;第三个是Menu.cs,这是菜单。

Player.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;public class Player : MonoBehaviour {// Use this for initializationvoid Start () {// 获取各个对象或组件goBird = transform.Find ("Bird").gameObject;srBird = goBird.GetComponent<SpriteRenderer>();goCamera = GameObject.Find ("Main Camera");goScores = GameObject.Find ("Scores");if (canPlay) {InitBackground ();InitBlocks ();// 重设分数PlayerPrefs.SetInt ("scores", 0);} else {// 显示分数if (goScores) {goScores.transform.GetComponent <Text>().text = "SCORES: "+PlayerPrefs.GetInt("scores");}}}public bool canPlay = true;       // 判断是否能开始public float screenHeight = 10;  // 屏幕高度public float moveSpeed = 5;     // 小鸟移动速度public float upSpeed = 19;        // 往上爬速度public float upHeight = 0.9f;  // 往上爬高度private float curUpHeight = 0; // 当前高度private bool upping = false;    // 判断是否正在攀爬public float flapInterval = 0.1f;   // 多长时间需要改变小鸟的样子private float curInterval = 0;     // 当前经过的时间public Sprite srUnflap;               // 小鸟正常样子public Sprite srFlapped;       // 小鸟正在攀爬的样子private GameObject goBird;      // 小鸟对象private SpriteRenderer srBird;   // 存放小鸟样子private GameObject goCamera;   // 摄像头private bool isDead = false; // 判断是否死亡private GameObject goScores;   // 分数对象private int scores = 0;     // 分数public AudioClip collisionAduio;   // 碰撞音效// Update is called once per framevoid Update () {// 判断是否死亡if (!isDead) {UpdateFlap ();// 如果没死就继续更新下一帧if (canPlay) {// 更新动作UpdateMovement ();// 更新分数UpdateScores ();// 更新会动的柱子UpdateMovingBlocks ();}} else {UpdateDieCompleted ();}}private void UpdateMovement() {float s = Time.deltaTime * moveSpeed;transform.Translate (new Vector3 (s, 0, 0));// 重设摄像头位置goCamera.transform.position = new Vector3(transform.position.x,goCamera.transform.position.y,goCamera.transform.position.z);if ((Input.GetKeyUp (KeyCode.Space) || Input.GetButtonUp("Fire1"))) {curUpHeight = 0;upping = true;// 旋转小鸟的角度为45度goBird.transform.eulerAngles = new Vector3 (0,0,45);}// 往上爬if (upping) {float us = upSpeed * Time.deltaTime;if (us + curUpHeight >= upHeight) {us = upHeight - curUpHeight;}transform.Translate (new Vector3(0,us,0));curUpHeight += us;if (curUpHeight >= upHeight) {upping = false;curUpHeight = 0;// 旋转小鸟的角度为正常goBird.transform.eulerAngles = new Vector3 (0,0,0);}}// 如果撞到天花板就往下坠float top = transform.localScale.y / 2 + transform.position.y;if (top >= screenHeight / 2) {upping = false;curUpHeight = 0;// 旋转小鸟的角度为正常goBird.transform.eulerAngles = new Vector3 (0,0,0);}// 如果碰到地板就死亡float bottom = -transform.localScale.y / 2 + transform.position.y;if (bottom <= -screenHeight / 2) {// 小鸟死亡BirdDie ();}}private void BirdDie() {isDead = true;goBird.transform.eulerAngles = new Vector3 (0,0,90);}private void UpdateDieCompleted() {// 如果碰到地板就死亡float bottom = transform.localScale.y / 2 + transform.position.y;if (bottom <= -screenHeight / 2) {DieCompleted ();}}private void DieCompleted() {Destroy (gameObject);// 载入Gameover场景SceneManager.LoadScene ("GameOver");SceneManager.UnloadSceneAsync ("Game_1");}// 改变小鸟样子private void UpdateFlap() {curInterval += Time.deltaTime;if (curInterval >= flapInterval && !upping) {if (srBird.sprite == srUnflap) {srBird.sprite = srFlapped;} else {srBird.sprite = srUnflap;}curInterval = 0;}}// 更新分数private void UpdateScores() {float tail = transform.position.x - transform.localScale.x / 2 - blockWidth;tail = (tail < 0) ? 0 : tail;int killBlocks = (int)(tail / (blockOffset) + ((tail > blockWidth) ? 1 : 0));scores = killBlocks * 10;goScores.transform.GetComponent <Text>().text = "SCORES: "+scores;// 保存分数PlayerPrefs.SetInt ("scores",scores);// 判断游戏是否已经结束if (scores >= (totalBlock * 10)) {DieCompleted ();}}// 柱子public GameObject block;public float blockWidth = 1;     //柱子宽度public float blockMinHeight = 1;    // 柱子最小高度public float blockOffset = 8;      // 柱子距离public float blockExitHeight = 4;   // 两柱之间给小鸟通过的缝的距离public float blockNarrow = 0.2f;  // 两柱之间给小鸟通过的缝的距离的递减数,越后的柱子的缝越小public int totalBlock = 40;          // 柱子数量public int blockClass = 5;          // 将所有柱子分为多少组   private List<GameObject> movingBlocks = new List<GameObject>(); // 会移动的柱子private float curBlockMoveHeight = 0;    // 柱子移动高度public float blockMoveSpeed = 1;      // 柱子移动速度private bool isBlockMovingUp= true;       // 判断是否正在移动private GameObject CreateBlock(Vector3 pos, Vector3 scale, Vector3 rotation) {GameObject go = GameObject.Instantiate (block).gameObject;go.transform.position = pos;go.transform.localScale = scale;go.transform.Rotate(rotation);return go;}// 初始化柱子private void InitBlocks() {int perClassBlock = totalBlock / blockClass;float x = 0.0f;System.Random rm = new System.Random (); // 随机数for (int i = 0; i < blockClass; ++i) {// 越后的柱子的缝越窄,这就表示游戏越难float narrow = blockExitHeight - i*blockNarrow;float avaliableHeight = screenHeight - narrow;for (int j = 0; j < perClassBlock; ++j, x+=blockOffset) {float height1,height2,y1,y2;height1 = (float)rm.NextDouble() * avaliableHeight;float blockMinHeight2 = blockMinHeight * 2;height1 = (height1 > (avaliableHeight - blockMinHeight2)) ? (avaliableHeight - blockMinHeight2) : height1;height1 = (height1 < blockMinHeight2) ? (blockMinHeight2) : height1;height2 = avaliableHeight - height1;y1 = (screenHeight - height1) / 2;y2 = (screenHeight - height2) / 2;GameObject go1 = CreateBlock (new Vector3(x,y1), new Vector3(1,height1/2,1), new Vector3(0,0,0));GameObject go2 = CreateBlock (new Vector3(x,-y2), new Vector3(1,height2/2,1), new Vector3(0,0,180));// 保存会动的柱子if (j >= perClassBlock - 2) {movingBlocks.Add (go1);movingBlocks.Add (go2);}}}}// 移动会动的柱子private void UpdateMovingBlocks() {float speed = blockMoveSpeed*Time.deltaTime;if (speed + curBlockMoveHeight >= blockMinHeight) {speed = blockMinHeight - curBlockMoveHeight;}curBlockMoveHeight += speed;speed = isBlockMovingUp ? speed : -speed;for (int i = 0; i < movingBlocks.Count; i += 2) {int j = i + 1;Vector3 pos1 = movingBlocks[i].transform.position;Vector3 pos2 = movingBlocks[j].transform.position;Vector3 scale1 = movingBlocks [i].transform.localScale;Vector3 scale2 = movingBlocks [j].transform.localScale;scale1.y -= speed;scale2.y += speed;pos1.y = (screenHeight - scale1.y*2)/2;pos2.y = -(screenHeight - scale2.y*2)/2;movingBlocks [i].transform.position = pos1;movingBlocks [j].transform.position = pos2;movingBlocks [i].transform.localScale = scale1;movingBlocks [j].transform.localScale = scale2;}isBlockMovingUp = curBlockMoveHeight >= blockMinHeight ? !isBlockMovingUp : isBlockMovingUp;curBlockMoveHeight = curBlockMoveHeight >= blockMinHeight ? 0 : curBlockMoveHeight;}// 背景public GameObject background;public float bgWidth = 20;public float bgStartPos = -10;public int bgCount = 50;// 创建背景private void CreateBackground(Vector3 pos, Vector3 scale) {GameObject go = GameObject.Instantiate (background).gameObject;go.transform.position = pos;go.transform.localScale = scale;}// 初始化背景private void InitBackground() {int n = bgCount;for (int i=0; i<n; ++i) {CreateBackground(new Vector3(bgStartPos+i*bgWidth,0,2),new Vector3(1,1,1));}}// 小鸟的碰撞处理void OnCollisionEnter2D(Collision2D col) {if (col.gameObject.tag == "Block") {AudioSource a = GetComponent<AudioSource>();if (!isDead) {BirdDie ();// 发出碰撞声音a.clip = collisionAduio;a.Stop ();}a.Play ();}}
}

Pillar.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Pillar : MonoBehaviour {// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}public int crushPiece = 5;     // 碎片数量public float pieceWidth = 0.8f; // 碎片宽高public GameObject piece;     // 碎片对象// 碰撞后弹出碎片private void Crush(Collision2D col) {System.Random rm = new System.Random ();for (int i = 0; i < crushPiece; ++i) {Vector3 pos = new Vector3(col.contacts [0].point.x, col.contacts [0].point.y, 0);Vector3 scale = new Vector3 (pieceWidth,pieceWidth, 1);float fr = (float)rm.NextDouble ();float angle = (float)fr * -180;Vector3 rotation = new Vector3 (0,0,angle);GameObject go = CreatePiece (pos,scale,rotation);go.transform.GetComponent<Rigidbody2D>().AddForce(new Vector2(-fr*2,3) * 100);Destroy (go, 3.0f);}}private GameObject CreatePiece(Vector3 pos, Vector3 scale, Vector3 rotate) {GameObject go = GameObject.Instantiate (piece);go.transform.position = pos;go.transform.localScale = scale;go.transform.eulerAngles = rotate;return go;}void OnCollisionEnter2D(Collision2D col) {if (col.gameObject.tag == "Player") {Crush (col);}}
}

Menu.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;public class Menu : MonoBehaviour {public void StartGame() {// 载入游戏SceneManager.LoadScene ("Game_1");SceneManager.UnloadSceneAsync ("Menu");SceneManager.UnloadSceneAsync ("GameOver");}public void QuitGame() {// 退出游戏Application.Quit ();}
}

用Unity3D开发2D小游戏 Flappy Bird相关推荐

  1. 我的第一个Unity的2D小游戏(Flappy Bird)

    前言 兜兜转转跑来学习unity了,学习利用的是unity2017.2版本,在看过网上所谓的一堆零基础入门的视频后(确实0基础,无外乎都从界面开始介绍,然后是脚本基础几个API的介绍,然后讲解了下UG ...

  2. 【新手上路】Java必备小游戏——Flappy Bird(飞翔的小鸟)

    <飞翔的小鸟>是一款曾经比较火热的小游戏,本文可以带你你从零开始,一步一步的开发出这款小游戏.如果你只是刚入门java的新手,不用担心,只要你简单掌握了该游戏所需要的javase基础知识, ...

  3. java小游戏------Flappy Bird(飞翔的小鸟含源码)

    前言:本小游戏可作为java入门阶段收尾创作. 需:掌握面向对象的使用,了解多线程和异常处理等知识. 如上图所示:我们需要绘制背景,小鸟,障碍物,当然也包括游戏开始界面以及死亡界面. 一:思路解析: ...

  4. unity3D做2D小游戏飞机大战

    **~~ 一.飞机 ~~ ** 在Assets下面右键Create新建flot命名为Scenes,在右键Create新建Scene命名为MainScene,选中它,点击Create->UI-&g ...

  5. 游戏Flappy Bird走红启示:没人知道玩家想要什么

    [导读]Flappy Bird现排名中国区App Store免费榜第四名.该游戏日平均广告收入达到了5万美元. 腾讯科技 王鑫 2月7日报道 游戏开发者一直在试图了解,到底玩家会喜欢什么样的游戏?免费 ...

  6. 基于Unity的2D小游戏 SpeedDown 开发笔记(学习bilibili@[M_Studio]的教学视频

    基于Unity的2D小游戏 SpeedDown 开发笔记(学习bilibili@M_Studio的教学视频) 主要内容:在Sunnyland游戏的设计基础上,新增了物理组件Joint系列.DrawGi ...

  7. 基于cocoCreator版本2.4.5整理一款2D小游戏快速开发的游戏框架

    前言:基于cocoCreator版本2.4.5整理一款2D小游戏快速开发的游戏框架. 一.cocosCreator的UI框架. 中心思想, 将所有的UI窗体分为3类管理(1级窗体, 2级窗体, 3级窗 ...

  8. Unity3D游戏开发之使用Unity3D开发2D游戏 (一)

    今天要和大家分享的是基于Unity3D开发2D游戏,博主一直钟爱于国产武侠RPG,这个我在开始写Unity3D游戏开发系列文章的时候就已经说过了,所以我们今天要做的就是利用Unity3D来实现在2D游 ...

  9. 基于Unity3D的AR小游戏开发【100011412】

    本科毕业设计(论文) GRADUATION DESIGN(THESIS) 基于 Unity3D 的增强现实游戏程序 摘要 增强现实(AR)作为一项新兴技术近年来被越来越多的人群所获知,AR 也渐渐走进 ...

最新文章

  1. ​ 长达35页!美国公布未来新兴科技趋势报告
  2. 【转】Linux 之 /etc/profile、~/.bash_profile 等几个文件的执行过程
  3. 比較++和+的运算符优先级
  4. BZOJ 4006 Luogu P3264 [JLOI2015]管道连接 (斯坦纳树、状压DP)
  5. 带你深入理解值传递(点进来才知道它是一篇使你收益的文章)
  6. java项目 配置文件_细数Java项目中用过的配置文件(properties篇)
  7. android判断是否json格式,android – 检查JSON中是否存在subObject
  8. 排序算法:堆排序算法实现及分析
  9. activemq的高级特性:消息存储持久化
  10. 【序列化与反序列化流】
  11. 常用的4种黑盒测试方法
  12. 量化指标公式源码_量化庄建仓(副图指标源码)下载 通达信源码
  13. EBS中的销售员SQL
  14. 程序员职业规划和学习规划
  15. GEE开发之Modis_LST地表温度数据分析
  16. java web 上传图片漏洞_Web安全:文件上传漏洞
  17. python2.7安装教程windowsxp_怎么在windows xp 下安装python 2.7
  18. 特斯拉与Uber达成协议,为Uber伦敦司机提供电动汽车
  19. 什么是粗粒度和细粒度权限
  20. 云测试中QA团队的作用

热门文章

  1. 在纸张上设计软件产品原型的方法
  2. listlength函数头文件_数据结构头文件
  3. Android控件全解手册 - 图片优化篇
  4. 中英文名言警句 励志
  5. 泛函分析 03.04 内积空间与Hilbert空间 - 正交基和正交列的完备性
  6. c语言枚举型与联合体
  7. 淘宝/天猫获取购买到的商品订单物流 API接口 代码分享
  8. phonegap的那些坑 -- 转载
  9. 硅谷银行一夜倒闭,海量创业公司遭殃,工资房租统统拿不出
  10. Java开发的超级马里奥小游戏410 相对简单 功能非常齐全 完整源码