博客地址: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. 即时通讯技术文集(第39期):推送技术合集(Part1) [共18篇]

    为了更好地分类阅读 52im.net 总计1000多篇精编文章,我将在每周三推送新的一期技术文集,本次是第 39 期. [- 1 -] iOS的推送服务APNs详解:设计思路.技术原理及缺陷等 [链接 ...

  2. IM开发者的零基础通信技术入门(十):零基础,史上最强5G技术扫盲

    本文引用了"鲜枣课堂"的<史上最强5G科普>文章内容.为了更好的内容呈现,在引用和收录时内容有改动,转载时请注明原文来源. 1.内容概述 ➊ 5G技术的关注度越来越高: ...

  3. Jmeter使用(1)_返回结果作为下一个的参数

    一.用户登录返回结果 {{"code":200, "token":"dbfab2d6c79e4981a86775f"}} 二.查询信息接口h ...

  4. 10.3 - AM - 模拟赛 总结

    复盘 T1 很水,一道异或求和,但是某两位仁兄因没打括号而死. T2 很水,一道字符串处理,但是我和某位仁兄因没特判而死(虽然没有 hack 掉我,所以我理论上还是满分). T3 不水,看了很久,没想 ...

  5. SpringBoot(六) - 阿里巴巴的EasyExcel

    1.依赖 <!-- 阿里EasyExcel start --> <dependency> <groupId>com.alibaba</groupId> ...

  6. java多线程---总结(1)

    线程创建.start.run 一.创建线程方式 java创建线程的方式,主要有三种:类Thread.接口Runnable.接口Callable. 1.Thread和Runnable进行比较 他们之间的 ...

  7. kNN(K- Nearest Neighbor)基本原理

  8. CV高手是怎么炼成的?

    你平时都怎么复制粘贴的?是否每次都是复制一段粘贴一段?是否厌倦了每次只能复制粘贴一次的限制?那这篇文章就是为你量身订做的. CopyQ简介 CopyQ is clipboard manager – a ...

  9. RocketMQ原理—2.源码设计简单分析上

    大纲 1.NameServer的启动脚本 2.NameServer启动时会解析哪些配置 3.NameServer如何初始化Netty网络服务器 4.NameServer如何启动Netty网络服务器 5 ...

  10. 干掉visio,这个画图神器真的绝了!!!

    前言 看过我以往文章的小伙伴可能会发现,我的大部分文章都有很多配图.我的文章风格是图文相结合,更便于大家理解. 最近有很多小伙伴发私信问我:文章中的图是用什么工具画的.他们觉得我画的图风格挺小清新的, ...