博客地址:https://www.cnblogs.com/zylyehuo/

项目名称 guess_card_game

参考源码链接:
https://www.manning.com/books/unity-in-action

效果图预览

step1: 创建一个3D项目

step2: 修改2D相关设置

点击Edit/Project Settings 再选择Editor选项 修改Default Behaviour Mode的Mode为2D

修改选项卡为2D模式

step3: 准备工作

创建需要的文件夹

记住场景名字

在Sprites文件夹下准备相应的图片

卡牌图案: 150*200px

卡背: 能盖住卡牌即可

重置按钮: 无要求

背景: 无要求

step4: 相关代码

MemoryCard.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class MemoryCard : MonoBehaviour
{
[SerializeField] private GameObject cardBack;
public void OnMouseDown()
{
if (cardBack.activeSelf && controller.canReveal) {
cardBack.SetActive(false);
controller.CardRevealed(this);
}
} public void Unreveal() {
cardBack.SetActive(true);
} [SerializeField] private SceneController controller;
private int _id;
public int id
{
get { return _id; }
}
public void SetCard(int id, Sprite image)
{
_id = id;
GetComponent<SpriteRenderer>().sprite = image;
} // Start is called before the first frame update
void Start()
{ }
// Update is called once per frame
void Update()
{ } }

SceneController.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class SceneController : MonoBehaviour
{
public const int gridRows = 2;
public const int gridCols = 4;
public const float offsetX = 2;
public const float offsetY = 2.5f; private MemoryCard _firstRevealed;
private MemoryCard _secondRevealed;
private int _score = 0;
public bool canReveal {
get { return _secondRevealed == null; }
} [SerializeField] private MemoryCard originalCard;
[SerializeField] private Sprite[] images;
[SerializeField] private TextMesh scoreLabel; public void CardRevealed(MemoryCard card) {
if (_firstRevealed == null)
{
_firstRevealed = card;
}
else {
_secondRevealed = card;
StartCoroutine(CheckMatch());
}
} private IEnumerator CheckMatch() {
if (_firstRevealed.id == _secondRevealed.id)
{
_score++;
Debug.Log("Score:" + _score);
}
else {
yield return new WaitForSeconds(.5f);
_firstRevealed.Unreveal();
_secondRevealed.Unreveal();
} _firstRevealed = null;
_secondRevealed = null;
} // Start is called before the first frame update
void Start()
{
Vector3 startPos = originalCard.transform.position; int[] numbers = {0, 0, 1, 1, 2, 2, 3, 3 };
numbers = ShuffleArray(numbers); for (int i = 0; i < gridCols; i++) {
for (int j = 0; j < gridRows; j++) {
MemoryCard card;
if (i == 0 && j == 0)
{
card = originalCard;
}
else {
card = Instantiate(originalCard) as MemoryCard;
} int index = j * gridCols + i;
int id = numbers[index];
card.SetCard(id, images[id]); float posX = (offsetX * i) + startPos.x;
float posY = - (offsetY * j) + startPos.y;
card.transform.position = new Vector3(posX,posY,startPos.z);
}
} } private int[] ShuffleArray(int[] numbers) {
int[] newArray = numbers.Clone() as int[];
for (int i = 0; i < newArray.Length; i++) {
int tmp = newArray[i];
int r = Random.Range(i,newArray.Length);
newArray[i] = newArray[r];
newArray[r] = tmp;
}
return newArray;
} // Update is called once per frame
void Update()
{ }
}

UIButton.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class UIButton : MonoBehaviour
{
[SerializeField] private GameObject targetObject;
[SerializeField] private string targetMessage;
public Color highlightColor = Color.cyan;
public void OnMouseEnter()
{
SpriteRenderer sprite = GetComponent<SpriteRenderer>();
if (sprite != null)
{
sprite.color = highlightColor;
}
}
public void OnMouseExit()
{
SpriteRenderer sprite = GetComponent<SpriteRenderer>();
if (sprite != null)
{
sprite.color = Color.white;
}
} public void OnMouseDown()
{
transform.localScale = new Vector3(1.1f, 1.1f, 1.1f);
}
public void OnMouseUp()
{
transform.localScale = Vector3.one;
if (targetObject != null)
{
targetObject.SendMessage(targetMessage);
}
} // Start is called before the first frame update
void Start()
{ } // Update is called once per frame
void Update()
{ }
}

step5: 创建场景及对象

1、将背景从Project中拖到场景中

2、选择一张卡牌作为对象,拖到Hierarchy窗口,并修改名称为 Memory Card

3、将卡背拖到Memory Card下方作为子对象

4、创建空对象,依次修改名称

5、脚本挂载

Memory Card

card_back

Controller

Score

start-button

2D小游戏--猜对应卡牌(unity)的更多相关文章

  1. 玩Python小游戏猜数字,在游戏中掌握基础,你还能学不会?

    学python怎么离得开案例呢? 今天再继续给大家分享一个Python教程里的猜数字游戏     我最近也是在学python,从事编程工作几年了,但是python还是今年才开始玩的,不得不说,这真是一 ...

  2. Python小游戏 -- 猜单词

    Python初学者小游戏:猜单词 游戏逻辑:就像我们曾经英语学习机上的小游戏一样,电脑会从事先预置的词库中抽取单词,然后给出单词的字母数量,给定猜解次数,然后让玩家进行猜测,并给出每次猜测的正确字母与 ...

  3. Python小游戏 -- 猜数字

    Python初学者小游戏:猜数字 游戏逻辑:电脑随机生成一个数字,然后玩家猜数字,电脑提示猜的数字大了还是小了,供玩家缩小数字范围,达到既定次数后,玩家失败.若在次数内猜对,玩家获胜. 涉及知识点:r ...

  4. 解压小游戏之打砖块(C#+unity)

    z这个小游戏很简洁,很简单,非常适合一个人玩,特别减压

  5. Python小游戏——猜数字教程(random库教程)

    今天来开发一个简单的数字逻辑游戏,猜数字(数字炸弹) 首先开发游戏第一件事,了解需求. 猜数字游戏规则: 计算机随机生成一个指定范围的数字,由玩家来猜测, 之后计算机会根据玩家提供数字来与自己生成的数 ...

  6. 利用while循环写的简单小游戏猜数字

    猜数字的大小游戏 C:\Users\Administrator>python Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 23 2018, 23:31:1 ...

  7. Python小游戏——猜数字

    1 print("--------------我爱鱼-----------") 2 temp = input("不妨猜一下甲鱼现在心里想的是哪个数字:") 3 ...

  8. python写的第一个简单小游戏-猜数字

    #Filename:game1.py guess=10 running=True while running: try: answer=int(raw_input('Guess what i thin ...

  9. 使用canvas通过js制作一个小型英雄抓怪兽的2D小游戏

    首先,这是一个HTML: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  10. Python学习-55 小游戏- 猜大小

    #游戏开始,首先玩家选择大小,选择完成后开始摇骰子(11<=总值<=18为大,3<=总值<=10为小) import random def roll_dice(numbers= ...

随机推荐

  1. Appium_WebDriverAgent安装

      一.WebDriverAgent安装到ios测试设备 a) 切换到appium 的appium-webdriveragent目录(/Applications/Appium.app/Contents ...

  2. 安装Rational rose软件简单教程

    安装Rational rose软件简单教程 一.下载文件 百度云盘下载链接:https://pan.baidu.com/s/1QWmTgxKlQtE5flP7dXIF5w 提取码:wjnq 二.装虚拟 ...

  3. Mongodb 基础与安装

    官网链接:https://docs.mongodb.com/ 参考链接:https://www.runoob.com/mongodb/mongodb-linux-install.html 1.什么是M ...

  4. SpringBoot集成Jwt(详细步骤+图解)

    SpringBoot集成Jwt(详细步骤+图解)Jwt简介 JSON Web Token是目前最流行的跨域认证解决方案,,适合前后端分离项目通过Restful API进行数据交互时进行身份认证 Jwt ...

  5. Kotlin:【接口】【抽象类】

  6. vscode python remote debug极速入门

    本文适用范围 主要适用于debug python 程序,尤其是深度学习刚入门需要使用remote 连接到linux进行程序运行,想调试一下的同学. 当然非深度学习也可以参考食用本文哈哈哈. 极速入门版 ...

  7. 【译】轻松评估 AI 应用程序的质量

    原文 | Wendy Breiding 翻译 | 郑子铭 在构建利用 AI 的应用程序时,能够有效地评估 SLM(小型语言模型)或 LLM(大型语言模型)的响应从未如此重要. 评估是指评估 AI 模型 ...

  8. Yarn提交Flink任务参数介绍

    一.参数介绍 作业模式:yarn-per-job 基本参数 描述 -ynm(Custom Yarnname) 自定义的Yarn名字 -yqu 指定Yarn队列名 -yn (TaskManager)  ...

  9. FLink09的RichFlatMap和RichMap使用

    一.数据源配置 pom文件:https://www.cnblogs.com/robots2/p/16048648.html 二.RichFlatMap代码,输入单行输出多行 package net.x ...

  10. Flink 部署和整体架构

    一.Flink运行部署模式和流程 部署模式: 1.Local 本地部署,直接启动进程,适合调试使用 2.Standalone Cluster集群部署,flink自带集群模式 3.On Yarn 计算资 ...