• 本文原创发布于博客园”优梦创客”的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号”优梦创客”(微信号:unitymaker)
  • 您可以自由转载,但必须加入完整的版权声明!

是男人就下100层这是一个很受欢迎的小游戏。设计者所取的游戏名称以及它特有的挑战性令几乎所有的男同胞们都一如既往的去暴机,这个游戏同样也吸引了广大的女性,相当有挑战性。

  • 中文名 是男人就下100层
  • 原版名称 man down
  • 游戏类型 休闲游戏

使用方向键左右控制小人,要防止被托上去挤死,防止掉下去摔死以及防止踩到陷阱。

  • 导入游戏资源,确保Sprites文件夹里的资源的Texture Type为Sprite

1

  • 打开Sprite Mode的Sprite Editor,对图片进行切割,点击Apply进行保存。

image

image

  • 将云彩(cloud、darkcloud)、背景图(game background)拖进场景,其中背景图为背景(Background),其他作为前景(Foreground)。

image

image

image

将主角拖入场景之中并将其命名为Player,将主角Player设置在Foreground前景层。
image

打开动画控制器,创建主角的动画walk动画和Idel动画。把主角图片拖到时间轴上其中walk动画每0.01秒一张。Idel动画只需要拖一张。
image

image

  • 给主角添加刚体(Rigidbody 2D)、添加碰撞器(box Collider 2D)

image

  • 给主角添加脚本Player。给角色设置一个最大速度maxSpeed。设置一个力量force。保存Rigidbody 2D
    通过Horizontal取得水平上的按键0=什么也没按。1=右键。2=左键.

    1. public float MaxSpeed = 4;
    2. public float Force = 4;
    3. public Rigidbody2D mybody;
    1. void PlayerMove()
    2. {
    3. var x = Input.GetAxis("Horizontal");
    4. var xforce = 0.0f;
    5. if(x > 0)
    6. {
    7. xforce = Force*x;
    8. myAnimator.SetBool("walk", true);
    9. this.gameObject.transform.localScale = new Vector3(1, 1, 1);
    10. }
    11. else if(x < 0)
    12. {
    13. xforce = Force*x;
    14. myAnimator.SetBool("walk", true);
    15. this.gameObject.transform.localScale = new Vector3(-1, 1, 1);
    16. }
    17. else
    18. {
    19. xforce = 0;
    20. myAnimator.SetBool("walk", false);
    21. }
    22. var xspeed = mybody.velocity.x;
    23. if(Mathf.Abs(xspeed) >= MaxSpeed) {
    24. if(xforce * xspeed > 0) {
    25. xforce = 0;
    26. }
    27. }
    28. mybody.AddForce(new Vector2(xforce, 0));
    29. }

将云彩拖入场景之中并将其命名为cloud和darkcloud将云彩cloud和darkcloud设置在Background背景层。
image

给云彩添加碰撞体Box Collider 2D,添加脚本CloudSpawn,建立cloud预制体和主角预制体player_0。将预制体Cloud和player_0拖入,Cloud 、player中。确保min x、max x、Distance Y、LastCloud Y的值。

image

  1. void CreateClouds()
  2. {
  3. var worldSize = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0));
  4. var worldSize2 = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 0));
  5. minX = worldSize2.x + 1f;
  6. maxX = worldSize.x - 1f;
  7. lastCloudY = 0;
  8. for (var i = 0; i < clouds.Count; i++)
  9. {
  10. var cloud = clouds [i];
  11. Vector3 pos;
  12. float x = 0;
  13. if (controlCloud == 0)
  14. {
  15. x = Random.Range(0, maxX);
  16. controlCloud = 1;
  17. }
  18. else if (controlCloud == 1)
  19. {
  20. x = Random.Range(minX, 0);
  21. controlCloud = 2;
  22. }
  23. else if (controlCloud == 2)
  24. {
  25. x = Random.Range(1, maxX);
  26. controlCloud = 3;
  27. }
  28. else if (controlCloud == 3)
  29. {
  30. x = Random.Range(minX, -1);
  31. controlCloud = 0;
  32. }
  33. pos = new Vector3(x, lastCloudY, 0);
  34. cloud.transform.localPosition = pos;
  35. lastCloudY -= distanceY;
  36. }
  37. }

设置游戏角色player碰到darkCloud就会死亡

image

游戏背景需要不断滚动,先把游戏背景图片再复制两份再把复制的背景拼接好注意不要留缝隙,再给图片添加碰撞体(Box Collider 2D)和脚本BG Scaler

image

image

  1. void Start () {
  2. SpriteRenderer sp = this.gameObject.GetComponent<SpriteRenderer>();
  3. float width = sp.bounds.size.x;
  4. float swidth = Screen.width;
  5. float sheight = Screen.height;
  6. Debug.Log(swidth+" : "+sheight);
  7. float sz = Camera.main.orthographicSize;
  8. float sc = sheight/swidth;
  9. float cameraHeight = sz*2;
  10. float cameraWidth = cameraHeight/(sc);
  11. float spriteScale = cameraWidth/width;
  12. this.transform.localScale = new Vector3(spriteScale, 2, 0);
  13. }

设置游戏角色player碰到darkcloud就死亡和碰到游戏上下bound死亡。

  1. if (other.gameObject.tag == "darkCloud")
  2. {
  3. var cs = Camera.main.GetComponent<CameraScript> ();
  4. cs.enabled = false;
  5. transform.localPosition = new Vector3 (1000, 1000, 0);
  6. //GameController.Instance.ShowGameOver (score, CoinCount);
  7. AudioSource.PlayClipAtPoint(dealSound, transform.position);
  8. LifeCount--;
  9. GameManager.Instance.CheckGameState (LifeCount, CoinCount, score);
  10. }
  11. else if (other.gameObject.tag == "bound")
  12. {
  13. var cs = Camera.main.GetComponent<CameraScript>();
  14. cs.enabled = false;
  15. transform.localPosition = new Vector3(1000, 1000, 0);
  16. //GameController.Instance.ShowGameOver(score, CoinCount);
  17. AudioSource.PlayClipAtPoint(dealSound, transform.position);
  18. LifeCount--;
  19. GameManager.Instance.CheckGameState(LifeCount, CoinCount, score);
  20. }

先新建一个空节点命名为Music,在文件夹中找到音乐天空之城将其拉入Music选择循环播放(Loop),适当选择音量(Volime)。

image

image

  • 增加血量扣血
  • 增加道具吃道具加分
  • 添加敌人
  • 当层数越来越多时候速度越来越快

版权声明:本文为raymondking123原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/raymondking123/articles/8424652.html