Réalisez le jeu du pendu avec UNITY

Dans ce tutoriel apprenez à créer le jeu du pendu avec Unity. Cette playlist est composée de plusieurs épisodes qui vous permettront de réaliser pas à pas ce jeu ludique.

Ci dessous les scripts du jeu, un peu plus bas le package Unity du jeu.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Words
{
private List<string> lstWords = new List<string>();
public string curWord;
public Words()
{
lstWords.Add("VAPOTEUR");
lstWords.Add("TRIOMPHE");
}
public string GetWord()
{
curWord = lstWords[Random.Range(0, lstWords.Count)];
return curWord;
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Words { private List<string> lstWords = new List<string>(); public string curWord; public Words() { lstWords.Add("VAPOTEUR"); lstWords.Add("TRIOMPHE"); } public string GetWord() { curWord = lstWords[Random.Range(0, lstWords.Count)]; return curWord; } }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Words 
{
    private List<string> lstWords = new List<string>();
    public string curWord;

    public Words()
    {
        lstWords.Add("VAPOTEUR");
        lstWords.Add("TRIOMPHE");
    }

    public string GetWord()
    {
        curWord = lstWords[Random.Range(0, lstWords.Count)];
        return curWord;
    }
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Game : MonoBehaviour
{
private Words word = new Words();
private string curWord;
public Text txt;
private string reponse;
private bool win = false;
public Sprite[] sp;
public AudioClip SfxCorrect, SfxFailed;
private AudioSource audiosource;
public GameObject Pendu;
private int i = 0;
public GameObject PanelEnd;
private void Awake()
{
curWord= word.GetWord();
audiosource = GetComponent<AudioSource>();
}
public void KeyboardPress(string letter)
{
Validation(letter);
}
private void Validation(string letter)
{
reponse = "";
win = false;
for (int i = 0; i < word.curWord.Length; i++)
{
if(txt.text.Substring(i,1) == "_")
{
if(word.curWord.Substring(i,1)==letter)
{
reponse += letter;
win = true;
}
else
{
reponse += "_";
}
}
else
{
reponse += txt.text.Substring(i, 1);
}
}
txt.text = reponse;
Verification();
}
void Verification()
{
if(win)
{
audiosource.PlayOneShot(SfxCorrect);
if(txt.text == curWord)
{
PanelEnd.SetActive(true);
PanelEnd.GetComponentInChildren<Text>().text = "BRAVO ! le mot était " + curWord;
StartCoroutine(Restart());
}
}
else
{
Pendu.GetComponent<Image>().sprite = sp[i];
i++;
audiosource.PlayOneShot(SfxFailed);
if(i==6)
{
PanelEnd.SetActive(true);
PanelEnd.GetComponentInChildren<Text>().text = "PERDU ! le mot etait " + curWord;
StartCoroutine(Restart());
}
}
}
IEnumerator Restart()
{
yield return new WaitForSeconds(5f);
UnityEngine.SceneManagement.SceneManager.LoadScene("Game");
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Game : MonoBehaviour { private Words word = new Words(); private string curWord; public Text txt; private string reponse; private bool win = false; public Sprite[] sp; public AudioClip SfxCorrect, SfxFailed; private AudioSource audiosource; public GameObject Pendu; private int i = 0; public GameObject PanelEnd; private void Awake() { curWord= word.GetWord(); audiosource = GetComponent<AudioSource>(); } public void KeyboardPress(string letter) { Validation(letter); } private void Validation(string letter) { reponse = ""; win = false; for (int i = 0; i < word.curWord.Length; i++) { if(txt.text.Substring(i,1) == "_") { if(word.curWord.Substring(i,1)==letter) { reponse += letter; win = true; } else { reponse += "_"; } } else { reponse += txt.text.Substring(i, 1); } } txt.text = reponse; Verification(); } void Verification() { if(win) { audiosource.PlayOneShot(SfxCorrect); if(txt.text == curWord) { PanelEnd.SetActive(true); PanelEnd.GetComponentInChildren<Text>().text = "BRAVO ! le mot était " + curWord; StartCoroutine(Restart()); } } else { Pendu.GetComponent<Image>().sprite = sp[i]; i++; audiosource.PlayOneShot(SfxFailed); if(i==6) { PanelEnd.SetActive(true); PanelEnd.GetComponentInChildren<Text>().text = "PERDU ! le mot etait " + curWord; StartCoroutine(Restart()); } } } IEnumerator Restart() { yield return new WaitForSeconds(5f); UnityEngine.SceneManagement.SceneManager.LoadScene("Game"); } }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Game : MonoBehaviour
{
    private Words word = new Words();
    private string curWord;
    public Text txt;
    private string reponse;
    private bool win = false;
    public Sprite[] sp;
    public AudioClip SfxCorrect, SfxFailed;
    private AudioSource audiosource;
    public GameObject Pendu;
    private int i = 0;
    public GameObject PanelEnd;
    
    private void Awake()
    {
        curWord= word.GetWord();
        audiosource = GetComponent<AudioSource>();
    }

    public void KeyboardPress(string letter)
    {
        Validation(letter);
    }

    private void Validation(string letter)
    {
        reponse = "";
        win = false;

        for (int i = 0; i < word.curWord.Length; i++)
        {
            if(txt.text.Substring(i,1) == "_")
            {
                if(word.curWord.Substring(i,1)==letter)
                {
                    reponse += letter;
                    win = true;
                }
                else
                {
                    reponse += "_";
                }
            }
            else
            {
                reponse += txt.text.Substring(i, 1);
            }
        }

        txt.text = reponse;
        Verification();
    }
    void Verification()
    {
        if(win)
        {
            audiosource.PlayOneShot(SfxCorrect);

            if(txt.text == curWord)
            {
                PanelEnd.SetActive(true);
                PanelEnd.GetComponentInChildren<Text>().text = "BRAVO ! le mot était " + curWord;
                StartCoroutine(Restart());
            }
        }
        else
        {
            Pendu.GetComponent<Image>().sprite = sp[i];
            i++;
            audiosource.PlayOneShot(SfxFailed);

            if(i==6)
            {
                PanelEnd.SetActive(true);
                PanelEnd.GetComponentInChildren<Text>().text = "PERDU ! le mot etait " + curWord;
                StartCoroutine(Restart());
            }
        }
    }

    IEnumerator Restart()
    {
        yield return new WaitForSeconds(5f);
        UnityEngine.SceneManagement.SceneManager.LoadScene("Game");
    }
}
A propos de upln 279 Articles
En informatique le problème se situe souvent entre la chaise et le clavier !

Soyez le premier à commenter

Poster un Commentaire

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.