一·前言
在本教程中,我们将一步一步带领大家制作一个经典的贪吃蛇游戏。无论你是刚入门的编程新手,还是有一定经验的开发者,都能通过这个项目提升自己的技能,深入了解游戏开发的基本流程和技巧。让我们一起开启这段有趣的游戏制作之旅吧!
二·完整项目和资源
注意:因个人原因,本次游戏后续内容将不再更新,完整内容见如下资源项目
通过网盘分享的文件:snake
链接: https://pan.baidu.com/s/14O5RhI1GgzpdZ6nS8F7WVw?pwd=1209 提取码: 1209
--来自百度网盘超级会员v5的分享
三·详细操作步骤
1.移动方式的选择
(1)锁定蛇头的z轴旋转
2.使用物理系统进行移动的注意事项
需要在fixedupdate()中进行物理行为操作(保证固定时间操作)
3.使用代码获取刚体组件
(1)创建脚本SnakeMove
(2)编写代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SnakeMove : MonoBehaviour
{
private Rigidbody2D SnakeRig2D;
// Start is called before the first frame update
void Start()
{
SnakeRig2D = GetComponent<Rigidbody2D>();
}
}
4.获取键盘的输入
(1)打开SnakeMove脚本
(2)编辑代码
public class SnakeMove : MonoBehaviour
{
private Rigidbody2D SnakeRig2D;
private Vector2 SnakeDirection;
// Start is called before the first frame update
void Start()
{
SnakeRig2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxisRaw("Horizontal");
float y = Input.GetAxisRaw("Vertical");
SnakeDirection = new Vector2(x, y);
}
}
5.改变刚体的速度控制物体移动
(1)打开SnakeMove脚本
(2)编辑代码
public class SnakeMove : MonoBehaviour
{
······
public float SnakeSpeed = 1;
······
private void FixedUpdate()
{
movesnake();
}
······
private void movesnake()
{
SnakeRig2D.velocity = SnakeDirection*SnakeSpeed;
}
······
}
6.改变输入方式为改变方向做铺垫
(1)实现按键只能控制一个方向移动
void Update()
{
//float x = Input.GetAxisRaw("Horizontal");
//float y = Input.GetAxisRaw("Vertical");
if(Input.GetKey(KeyCode.W))
{
SnakeDirection = new Vector2(0,1);
}
if(Input.GetKey(KeyCode.S))
{
SnakeDirection = new Vector2(0, -1);
}
if(Input.GetKey(KeyCode.D))
{
SnakeDirection = new Vector2(1, 0);
}
if(Input.GetKey(KeyCode.A))
{
SnakeDirection = new Vector2(-1, 0);
}
}
(2)控制贪吃蛇的转向(保证蛇头图案也可以改变方向,同时不能直接执行180掉转移动)
void Update()
{
//float x = Input.GetAxisRaw("Horizontal");
//float y = Input.GetAxisRaw("Vertical");
if(Input.GetKey(KeyCode.W)&&SnakeRig2D.velocity!=Vector2.down)
{
SnakeDirection = new Vector2(0,1);
transform .rotation =Quaternion.Euler(0,0,0) ;
}
if(Input.GetKey(KeyCode.S)&&SnakeRig2D.velocity!=Vector2.up)
{
SnakeDirection = new Vector2(0, -1);
transform.rotation = Quaternion.Euler(0, 0, -180);
}
if(Input.GetKey(KeyCode.D) && SnakeRig2D.velocity != Vector2.left)
{
SnakeDirection = new Vector2(1, 0);
transform.rotation = Quaternion.Euler(0, 0, -90);
}
if(Input.GetKey(KeyCode.A) && SnakeRig2D.velocity != Vector2.right)
{
SnakeDirection = new Vector2(-1, 0);
transform.rotation = Quaternion.Euler(0, 0, 90);
}
}
7.制作贪吃蛇的食物
(1)创建空物体命名为FoodMaker
(2)创建一个子image UI,命名为Food
(3)创建一个Prefabs预制体文件夹存放Food
(4)创建一个脚本Generate Food
(5)编辑代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenerateFood : MonoBehaviour
{
public GameObject FoodPre;
public Sprite[] Foodsprites;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
(6)挂载脚本以及脚本资源
8.生成食物并指定食物的父物体
public class GenerateFood : MonoBehaviour
{
public GameObject FoodPre;
public Sprite[] Foodsprites;
private Transform FoodMakeTrans;
// Start is called before the first frame update
void Start()
{
FoodMakeTrans = transform;
}
// Update is called once per frame
void Update()
{
GameObject food= GameObject.Instantiate(FoodPre);
food.transform.parent = FoodMakeTrans;
}
}
9.在随机位置生成食物
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GenerateFood : MonoBehaviour
{
public GameObject FoodPre;
public Sprite[] Foodsprites;
private Transform FoodMakeTrans;
// Start is called before the first frame update
void Start()
{
FoodMakeTrans = transform;
makefood();
}
// Update is called once per frame
void Update()
{
}
private void makefood()
{
int xlocation = Random.Range(-310, 600);
int ylocation = Random.Range(-330, 330);
GameObject food = GameObject.Instantiate(FoodPre, new Vector2(xlocation, ylocation), Quaternion.identity);
food.transform.SetParent(FoodMakeTrans, false);
FoodPre.GetComponent<Image>().sprite = Foodsprites[Random.Range(0, Foodsprites.Length)];
}
}
10.贪吃蛇与食物的碰撞处理——吃食物
(1)给Food预制体添加碰撞器,不需要勾选触发器图片存在错误
(2)给SnakeHead添加碰撞器并且勾选触发器
(3)给Food预制体添加Food标签
(4)打开SnakeMove代码
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag=="Food")
{
Destroy(collision.gameObject);
}
}
11.实现食物被吃掉后再次生成
public class GenerateFood : MonoBehaviour
{
private static GenerateFood _instance;
······
public static GenerateFood Instance { get { return _instance; } }
···
{
_instance = this;
}
······
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SnakeMove : MonoBehaviour
{
······
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag=="Food")
{
Destroy(collision.gameObject);
GenerateFood.Instance.makefood();
}
}
······
}
四·结语
我们已经完成了贪吃蛇游戏的基本界面搭建和部分功能组件的创建。后续还需要通过编写脚本代码来实现游戏的逻辑功能,如蛇的移动、吃食物、得分计算、碰撞检测等。希望这个教程能帮助你顺利完成贪吃蛇游戏的制作,如果你在制作过程中遇到任何问题,欢迎在评论区留言交流。