Міністерство освіти і науки україни національний університет «львівська політехніка»
ІКТА
кафедра ЗІ
ЗВІТ
До лабораторної роботи №8
з курсу: «Технології програмування ч.2»
на тему: « РОЗРОБЛЕННЯ ЗАСТОСУВАНЬ»
Виконали: ст. гр. КБ-12
Монцібович Н.В
Войтович О.О
Прийняв: ______________
Львів - 2018
Мета роботи – навчитися створювати програми з графічним інтерфейсом. Вивчити основні засоби елементів керування та принципи додавання функцій обробників подій для створення графічної взаємодії з користувачем.
Список ідентифікаторів констант, змінних, функцій, використаних у програмі, та їх пояснення
LoadScene - запуск сцени
int[,] - робоче поле
GameObject[] knop - ігрове поле
GameObject paus - вікно паузи
GameObject GO - вікно програшу
GameObject VI - вікно виграшу
SetActive - зміна активності об’єкта
Random.Range – рандом
ToString() – перетворення в символьний рядок
Time.timeScale – встановлення швидкості ігрового часу
GetComponent<Text>().text – контроль властивості об’єкта - текст
Текст програми
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainWindow : MonoBehaviour
{
// Use this for initialization
void Start()
{
Screen.orientation = ScreenOrientation.LandscapeLeft; // Альбомна орієнтація
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class QuitFromGame : MonoBehaviour {
public void Quit()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false; // завершення роботи в редакторі
#else
Application.Quit(); // завершення роботи додатку
#endif
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
public class LoadGameStart : MonoBehaviour {
public void LoadByIndex()
{
SceneManager.LoadScene("HighLevel"); // запуск сцени HighLevel
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
public class HightLevel : MonoBehaviour {
void Start()
{
Screen.orientation = ScreenOrientation.LandscapeLeft; // Альбомна орієнтація
}
public void Level3()
{
SceneManager.LoadScene("3"); // Запуск сцени 3
}
public void Level5()
{
SceneManager.LoadScene("5"); // Запуск сцени 5
}
public void Level10()
{
SceneManager.LoadScene("10"); // Запуск сцени 10
}
public void menu()
{
SceneManager.LoadScene("MainWindow"); // Запуск сцени MainWindow
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine;
public class Level3 : MonoBehaviour {
int v = 6; // кількість вільних клітинок
int[,] m = new int[3, 3]; // робоче поле
public GameObject[] knop = new GameObject[9]; // ігрове поле
public GameObject paus; //вікно паузи
public GameObject GO; //вікно програшу
public GameObject VI; //вікно виграшу
// Use this for initialization
void Start()
{
Screen.orientation = ScreenOrientation.LandscapeLeft; // Альбомна орієнтація
v = 6; // вказуємо кількість вільних клітинок
paus.SetActive(false); //приховуємо вікно паузи
GO.SetActive(false); //приховуємо вікно паузи
VI.SetActive(false); //приховуємо вікно паузи
for (int i=0; i<9; ++i)
{ knop[i].GetComponent<Text>().text = ""; } // очищуємо ігрове поле
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
{ m[i, j] = 0; } // занульоїмо робоче поле
int b = 3; // вказуємо кількість бомб
while (b > 0)
{
int i = Random.Range(0,3); // вибираємо будь який рядок
int j = Random.Range(0,3); // вибираємо будь який стовбець
if (m[i, j] != -1) { m[i, j] = -1; b--; } // якщо бомба не стоїть на цій позиції ставимо її
}
}
// Update is called once per frame
void Update()
{
}
int bombs(int i, int j)
{ // перевіряємо кількість бомб навколо позиції з перевіркою виходу за межі поля
int x = 0;
int max = 2, min = 0;
if ((i - 1 >= min) && (j - 1 >= min))
if (m[i - 1, j - 1] == -1) { x++; }
if ( i - 1 >= 0)
if (m[i - 1, j] == -1) { x++; }
if ((i - 1 >= min) && (j + 1 <= max))
if (m[i - 1, j + 1] == -1) { x++; }
if (j - 1 >= min)
if (m[i, j - 1] == -1) { x++; }
if (j + 1 <= max)
if (m[i, j + 1] == -1) { x++; }
if ((i + 1 <= max) && (j - 1 >= min))
if (m[i + 1, j - 1] == -1) { x++; }
if (i + 1 <= max)
if (m[i + 1, j] == -1) { x++; }
if ((i + 1 <= max) && (j + 1 <= max))
if (m[i + 1, j + 1] == -1) { x++; }
return x;
}
public void button_1() // при натисканні на ділянку
{
int i = 0, j = 0;
if (m[i, j] == -1) { knop[0].GetComponent<Text>().text = "Boom!"; // якщо стоїть бомба показуємо це
GO.SetActive(true); // викликаємо вікно програшу
Handheld.Vibrate(); } // викликаємо вібрацію
else { knop[0].GetComponent<Text>().text = (bombs(i, j).ToString()); // якщо бомба не стоїть показуємо кількість бомб навколо
--v; Viner(v); } // віднімаємо вільне від бомб поле, перевіряємо чи такі ще є
}
public void button_2()
{
int i = 0, j = 1;
if (m[i, j] == -1)
{
knop[1].GetComponent<Text>().text = "Boom!";
GO.SetActive(true);
Handheld.Vibrate();
}
else
{
knop[1].GetComponent<Text>().text = (bombs(i, j).ToString());
--v; Viner(v);
}
}
public void button_3()
{
int i = 0, j = 2;
if (m[i, j] == -1)
{
knop[2].GetComponent<Text>().text = "Boom!";
GO.SetActive(true);
Handheld.Vibrate();
}
else
{
knop[2].GetComponent<Text>().text = (bombs(i, j).ToString());
--v; Viner(v);
}
}
public void button_4()
{
int i = 1, j = 0;
if (m[i, j] == -1) { knop[3].GetComponent<Text>().text = "Boom!";
GO.SetActive(true);
Handheld.Vibrate();
}
else { knop[3].GetComponent<Text>().text = (bombs(i, j).ToString()); --v; Viner(v); }
}
public void button_5()
{
int i = 1, j = 1;
if (m[i, j] == -1) { knop[4].GetComponent<Text>().text = "Boom!";
GO.SetActive(true);
Handheld.Vibrate();
}
else { knop[4].GetComponent<Text>().text = (bombs(i, j).ToString()); --v; Viner(v); }
}
public void button_6()
{
int i = 1, j = 2;
if (m[i, j] == -1) { knop[5].GetComponent<Text>().text = "Boom!";
GO.SetActive(true);
Handheld.Vibrate();
}
else { knop[5].GetComponent<Text>().text = (bombs(i, j).ToString()); --v; Viner(v); }
}
public void button_7()
{
int i = 2, j = 0;
if (m[i, j] == -1) { knop[6].GetComponent<Text>().text = "Boom!";
GO.SetActive(true);
Handheld.Vibrate();
}
else { knop[6].GetComponent<Text>().text = (bombs(i, j).ToString()); --v; Viner(v); }
}
public void button_8()
{
int i = 2, j = 1;
if (m[i, j] == -1) { knop[7].GetComponent<Text>().text = "Boom!";
GO.SetActive(true);
Handheld.Vibrate();
}
else { knop[7].GetComponent<Text>().text = (bombs(i, j).ToString()); --v; Viner(v); }
}
public void button_9()
{
int i = 2, j = 2;
if (m[i, j] == -1) { knop[8].GetComponent<Text>().text = "Boom!";
GO.SetActive(true);
Handheld.Vibrate();
}
else { knop[8].GetComponent<Text>().text = (bombs(i, j).ToString()); --v; Viner(v); }
}
public void restart() // при натисканні на кнопку
{
Start(); // викликаємо функцію Start()
}
public void menu() // при натисканні на кнопку
{
SceneManager.LoadScene("MainWindow"); // Запуск сцени MainWindow
}
public void resume() // при натисканні на кнопку
{
Time.timeScale = 1; // відновлюємо ігровий час
paus.SetActive(false); // приховуємо вікно паузи
}
public void pouse() // при натисканні на кнопку
{
Time.timeScale = 0; // зупиняємо ігровий час
paus.SetActive(true); // викликаємо вікно паузи
}
void Viner(int i) // при натисканні на кнопку
{
if (i == 0) VI.SetActive(true); // якщо усі поля окрім бомб були відкриті викликаємо вікно перемоги
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine;
public class Level5 : MonoBehaviour {
int v = 13; // кількість вільних клітинок
int[,] m = new int[5, 5]; // робоче поле
public GameObject[] knop = new GameObject[25]; // ігрове поле
public GameObject paus; //вікно паузи
public GameObject GO; //вікно програшу
public GameObject VI; //вікно виграшу
// Use this for initialization
void Start()
{
Screen.orientation = ScreenOrientation.LandscapeLeft; // Альбомна орієнтація
v = 13; // вказуємо кількість вільних клітинок
paus.SetActive(false); //приховуємо вікно паузи
GO.SetActive(false); //приховуємо вікно паузи
VI.SetActive(false); //приховуємо вікно паузи
for (int i = 0; i < 25; ++i)
{ knop[i].GetComponentInChildren<Text>().text = ""; } // очищуємо ігрове поле
for (int i = 0; i < 5; ++i)
for (int j = 0; j < 5; ++j)
{ m[i, j] = 0; } // занульоїмо робоче поле
int b = 12; // вказуємо кількість бомб
while (b > 0)
{
int i = Random.Range(0, 5); // вибираємо будь який рядок
int j = Random.Range(0, 5); // вибираємо будь який стовбець
if (m[i, j] != -1) { m[i, j] = -1; b--; } // якщо бомба не стоїть на цій позиції ставимо її
}
}
// Update is called once per frame
void Update()
{
}
int bombs(int i, int j)
{ // перевіряємо кількість бомб навколо позиції з перевіркою виходу за межі поля
int x = 0;
int max = 4, min = 0;
if ((i - 1 >= min) && (j - 1 >= min))
if (m[i - 1, j - 1] == -1) { x++; }
if (i - 1 >= 0)
if (m[i - 1, j] == -1) { x++; }
if ((i - 1 >= min) && (j + 1 <= max))
if (m[i - 1, j + 1] == -1) { x++; }
if (j - 1 >= min)
if (m[i, j - 1] == -1) { x++; }
if (j + 1 <= max)
if (m[i, j + 1] == -1) { x++; }
if ((i + 1 <= max) && (j - 1 >= min))
if (m[i + 1, j - 1] == -1) { x++; }
if (i + 1 <= max)
if (m[i + 1, j] == -1) { x++; }
if ((i + 1 <= max) && (j + 1 <= max))
if (m[i + 1, j + 1] == -1) { x++; }
return x;
}
public void button_1() // при натисканні на ділянку
{
pole(0,0,0); // викликаємо функцію з параметрами ігрового обєкта
}
public void button_2()
{
pole(0, 1, 1);
}
public void button_3()
{
pole(0, 2, 2);
}
public void button_4()
{
pole(0, 3, 3);
}
public void button_5()
{
pole(0, 4, 4);
}
public void button_6()
{
pole(1, 0, 5);
}
public void button_7()
{
pole(1, 1, 6);
}
public void button_8()
{
pole(1, 2, 7);
}
public void button_9()
{
pole(1, 3, 8);
}
public void button_10()
{
pole(1, 4, 9);
}
public void button_11()
{
pole(2, 0, 10);
}
public void button_12()
{
pole(2, 1, 11);
}
public void button_13()
{
pole(2, 2, 12);
}
public void button_14()
{
pole(2, 3, 13);
}
public void button_15()
{
pole(2, 4, 14);
}
public void button_16()
{
pole(3, 0, 15);
}
public void button_17()
{
pole(3, 1, 16);
}
public void button_18()
{
pole(3, 2, 17);
}
public void button_19()
{
pole(3, 3, 18);
}
public void button_20()
{
pole(3, 4, 19);
}
public void button_21()
{
pole(4, 0, 20);
}
public void button_22()
{
pole(4, 1, 21);
}
public void button_23()
{
pole(4, 2, 22);
}
public void button_24()
{
pole(4, 3, 23);
}
public void button_25()
{
pole(4, 4, 24);
}
public void restart() // при натисканні на кнопку
{
Start(); // викликаємо функцію Start()
}
public void menu() // при натисканні на кнопку
{
SceneManager.LoadScene("MainWindow"); // Запуск сцени MainWindow
}
public void resume() // при натисканні на кнопку
{
Time.timeScale = 1; // відновлюємо ігровий час
paus.SetActive(false); // приховуємо вікно паузи
}
public void pouse() // при натисканні на кнопку
{
Time.timeScale = 0; // зупиняємо ігровий час
paus.SetActive(true); // викликаємо вікно паузи
}
void Viner(int i) // при натисканні на кнопку
{
if (i == 0) VI.SetActive(true); // якщо усі поля окрім бомб були відкриті викликаємо вікно перемоги
}
void pole (int i, int j, int x)
{
if (m[i, j] == -1) { knop[x].GetComponentInChildren<Text>().text = "Boom!"; // якщо стоїть бомба показуємо це
GO.SetActive(true); // викликаємо вікно програшу
Handheld.Vibrate();} // викликаємо вібрацію
else { knop[x].GetComponentInChildren<Text>().text = (bombs(i, j).ToString()); // якщо бомба не стоїть показуємо кількість бомб навколо
--v; Viner(v);
} // віднімаємо вільне від бомб поле, перевіряємо чи такі ще є
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine;
public class Level10 : MonoBehaviour {
int v; // кількість вільних клітинок
int[,] m = new int[10, 10]; // робоче поле
public GameObject[] knop = new GameObject[100]; // ігрове поле
public GameObject paus; //вікно паузи
public GameObject GO; //вікно програшу
public GameObject VI; //вікно виграшу
// Use this for initialization
void Start()
{
Screen.orientation = ScreenOrientation.LandscapeLeft; // Альбомна орієнтація
v = 55; // вказуємо кількість вільних клітинок
paus.SetActive(false); //приховуємо вікно паузи
GO.SetActive(false); //приховуємо вікно паузи
VI.SetActive(false); //приховуємо вікно паузи
for (int i = 0; i < 100; ++i)
{ knop[i].GetComponentInChildren<Text>().text = ""; } // очищуємо ігрове поле
for (int i = 0; i < 10; ++i)
for (int j = 0; j < 10; ++j)
{ m[i, j] = 0; } // занульоїмо робоче поле
int b = 45; // вказуємо кількість бомб
while (b > 0)
{
int i = Random.Range(0, 10); // вибираємо будь який рядок
int j = Random.Range(0, 10); // вибираємо будь який стовбець
if (m[i, j] != -1) { m[i, j] = -1; b--; } // якщо бомба не стоїть на цій позиції ставимо її
}
}
// Update is called once per frame
void Update()
{
}
int bombs(int i, int j)
{ // перевіряємо кількість бомб навколо позиції з перевіркою виходу за межі поля
int x = 0;
int max = 9, min = 0;
if ((i - 1 >= min) && (j - 1 >= min))
if (m[i - 1, j - 1] == -1) { x++; }
if (i - 1 >= 0)
if (m[i - 1, j] == -1) { x++; }
if ((i - 1 >= min) && (j + 1 <= max))
if (m[i - 1, j + 1] == -1) { x++; }
if (j - 1 >= min)
if (m[i, j - 1] == -1) { x++; }
if (j + 1 <= max)
if (m[i, j + 1] == -1) { x++; }
if ((i + 1 <= max) && (j - 1 >= min))
if (m[i + 1, j - 1] == -1) { x++; }
if (i + 1 <= max)
if (m[i + 1, j] == -1) { x++; }
if ((i + 1 <= max) && (j + 1 <= max))
if (m[i + 1, j + 1] == -1) { x++; }
return x;
}
public void button_1() { pole(0, 0, 0); } //при натисканні на ділянку викликаємо функцію з параметрами ігрового обєкта
public void button_2() { pole(0, 1, 1); }
public void button_3() { pole(0, 2, 2); }
public void button_4() { pole(0, 3, 3); }
public void button_5() { pole(0, 4, 4); }
public void button_6() { pole(0, 5, 5); }
public void button_7() { pole(0, 6, 6); }
public void button_8() { pole(0, 7, 7); }
public void button_9() { pole(0, 8, 8); }
public void button_10() { pole(0, 9,9 ); }
public void button_11() { pole(1, 0, 10); }
public void button_12() { pole(1, 1, 11); }
public void button_13() { pole(1, 2, 12); }
public void button_14() { pole(1, 3, 13); }
public void button_15() { pole(1, 4, 14); }
public void button_16() { pole(1, 5, 15); }
public void button_17() { pole(1, 6, 16); }
public void button_18() { pole(1, 7, 17); }
public void button_19() { pole(1, 8, 18); }
public void button_20() { pole(1, 9, 19); }
public void button_21() { pole(2, 0, 20); }
public void button_22() { pole(2, 1, 21); }
public void button_23() { pole(2, 2, 22); }
public void button_24() { pole(2, 3, 23); }
public void button_25() { pole(2, 4, 24); }
public void button_26() { pole(2, 5, 25); }
public void button_27() { pole(2, 6, 26); }
public void button_28() { pole(2, 7, 27); }
public void button_29() { pole(2, 8, 28); }
public void button_30() { pole(2, 9, 29); }
public void button_31() { pole(3, 0, 30); }
public void button_32() { pole(3, 1, 31); }
public void button_33() { pole(3, 2, 32); }
public void button_34() { pole(3, 3, 33); }
public void button_35() { pole(3, 4, 34); }
public void button_36() { pole(3, 5, 35); }
public void button_37() { pole(3, 6, 36); }
public void button_38() { pole(3, 7, 37); }
public void button_39() { pole(3, 8, 38); }
public void button_40() { pole(3, 9, 39); }
public void button_41() { pole(4, 0, 40); }
public void button_42() { pole(4, 1, 41); }
public void button_43() { pole(4, 2, 42); }
public void button_44() { pole(4, 3, 43); }
public void button_45() { pole(4, 4, 44); }
public void button_46() { pole(4, 5, 45); }
public void button_47() { pole(4, 6, 46); }
public void button_48() { pole(4, 7, 47); }
public void button_49() { pole(4, 8, 48); }
public void button_50() { pole(4, 9, 49); }
public void button_51() { pole(5, 0, 50); }
public void button_52() { pole(5, 1, 51); }
public void button_53() { pole(5, 2, 52); }
public void button_54() { pole(5, 3, 53); }
public void button_55() { pole(5, 4, 54); }
public void button_56() { pole(5, 5, 55); }
public void button_57() { pole(5, 6, 56); }
public void button_58() { pole(5, 7, 57); }
public void button_59() { pole(5, 8, 58); }
public void button_60() { pole(5, 9, 59); }
public void button_61() { pole(6, 0, 60); }
public void button_62() { pole(6, 1, 61); }
public void button_63() { pole(6, 2, 62); }
public void button_64() { pole(6, 3, 63); }
public void button_65() { pole(6, 4, 64); }
public void button_66() { pole(6, 5, 65); }
public void button_67() { pole(6, 6, 66); }
public void button_68() { pole(6, 7, 67); }
public void button_69() { pole(6, 8, 68); }
public void button_70() { pole(6, 9, 69); }
public void button_71() { pole(7, 0, 70); }
public void button_72() { pole(7, 1, 71); }
public void button_73() { pole(7, 2, 72); }
public void button_74() { pole(7, 3, 73); }
public void button_75() { pole(7, 4, 74); }
public void button_76() { pole(7, 5, 75); }
public void button_77() { pole(7, 6, 76); }
public void button_78() { pole(7, 7, 77); }
public void button_79() { pole(7, 8, 78); }
public void button_80() { pole(7, 9, 79); }
public void button_81() { pole(8, 0, 80); }
public void button_82() { pole(8, 1, 81); }
public void button_83() { pole(8, 2, 82); }
public void button_84() { pole(8, 3, 83); }
public void button_85() { pole(8, 4, 84); }
public void button_86() { pole(8, 5, 85); }
public void button_87() { pole(8, 6, 86); }
public void button_88() { pole(8, 7, 87); }
public void button_89() { pole(8, 8, 88); }
public void button_90() { pole(8, 9, 89); }
public void button_91() { pole(9, 0, 90); }
public void button_92() { pole(9, 1, 91); }
public void button_93() { pole(9, 2, 92); }
public void button_94() { pole(9, 3, 93); }
public void button_95() { pole(9, 4, 94); }
public void button_96() { pole(9, 5, 95); }
public void button_97() { pole(9, 6, 96); }
public void button_98() { pole(9, 7, 97); }
public void button_99() { pole(9, 8, 98); }
public void button_100() { pole(9, 9, 99); }
public void restart() // при натисканні на кнопку
{
Start(); // викликаємо функцію Start()
}
public void menu() // при натисканні на кнопку
{
SceneManager.LoadScene("MainWindow"); // Запуск сцени MainWindow
}
public void resume() // при натисканні на кнопку
{
Time.timeScale = 1; // відновлюємо ігровий час
paus.SetActive(false); // приховуємо вікно паузи
}
public void pouse() // при натисканні на кнопку
{
Time.timeScale = 0; // зупиняємо ігровий час
paus.SetActive(true); // викликаємо вікно паузи
}
void Viner(int i) // при натисканні на кнопку
{
if (i == 0) VI.SetActive(true); // якщо усі поля окрім бомб були відкриті викликаємо вікно перемоги
}
void pole(int i, int j, int x)
{
if (m[i, j] == -1)
{
knop[x].GetComponentInChildren<Text>().text = "Boom!"; // якщо стоїть бомба показуємо це
GO.SetActive(true); // викликаємо вікно програшу
Handheld.Vibrate();
} // викликаємо вібрацію
else
{
knop[x].GetComponentInChildren<Text>().text = (bombs(i, j).ToString()); // якщо бомба не стоїть показуємо кількість бомб навколо
--v; Viner(v);
} // віднімаємо вільне від бомб поле, перевіряємо чи такі ще є
}
}