×

Free code, який допоможе вам зробити простеньку гру

Цей код допоможе зробити вам простеньку гру. Але не думайте, що це повністю завершений код — його можна допрацювати. Успіхів!!

using System;

using System.Collections.Generic;

class Program

{

static void Main(string[] args)

{

// Створити гравця та ворогів

Player player = new Player();

List<Enemy> enemies = new List<Enemy>();

for (int i = 0; i < 10; i++)

{

enemies.Add(new Enemy());

}

// Створити локації та розмістити в них гравця та ворогів

Location startLocation = new Location("Початкова локація");

Location middleLocation = new Location("Середня локація");

Location endLocation = new Location("Кінцева локація");

startLocation.PlaceObject(player, 50, 100);

middleLocation.PlaceObject(enemies[0], 150, 150);

middleLocation.PlaceObject(enemies[1], 200, 100);

middleLocation.PlaceObject(enemies[2], 300, 150);

middleLocation.PlaceObject(enemies[3], 400, 100);

middleLocation.PlaceObject(enemies[4], 500, 150);

middleLocation.PlaceObject(enemies[5], 600, 100);

middleLocation.PlaceObject(enemies[6], 700, 150);

middleLocation.PlaceObject(enemies[7], 800, 100);

middleLocation.PlaceObject(enemies[8], 900, 150);

middleLocation.PlaceObject(enemies[9], 1000, 100);

// Запустити гру

Game game = new Game(startLocation, middleLocation, endLocation);

game.Play();

}

}

class Game

{

private Location currentLocation;

private Location startLocation;

private Location middleLocation;

private Location endLocation;

public Game(Location startLocation, Location middleLocation, Location endLocation)

{

this.startLocation = startLocation;

this.middleLocation = middleLocation;

this.endLocation = endLocation;

this.currentLocation = startLocation;

}

public void Play()

{

Console.WriteLine("Гра почалась!");

while (true)

{

Console.WriteLine("Знаходження в локації: " + currentLocation.Name);

// Перевірити, чи гравець мертвий

if (currentLocation.Objects.Contains(player))

{

if (player.Health <= 0)

{

Console.WriteLine("Ви програли! Гру закінчено.");

return;

}

}

// Перевірити, чи гравець досяг кінцевої локації

if (currentLocation == endLocation)

{

Console.WriteLine("Вітаємо, в грі") }

// Перевірити, чи гравець біля боса

if (currentLocation.Objects.Contains(boss))

{

Console.WriteLine("БОС: Якщо хочеш пройти далі, тобі потрібно зі мною битись!");

while (true)

{

Console.WriteLine("БОС здоров’я: " + boss.Health);

Console.WriteLine("Гравець здоров’я: " + player.Health);

Console.WriteLine("Виберіть дію: 1 — атакувати, 2 — втікти“);

string input = Console.ReadLine();

if (input == “1”)

{

boss.Health -= player.Attack;

Console.WriteLine("Гравець завдав " + player.Attack + " урону босу!");

if (boss.Health <= 0)

{

Console.WriteLine("Гравець переміг боса! Вітаємо!");

currentLocation.Objects.Remove(boss);

currentLocation = endLocation;

break;

}

player.Health -= boss.Attack;

Console.WriteLine("БОС завдав " + boss.Attack + " урону гравцеві!");

if (player.Health <= 0)

{

Console.WriteLine("Ви програли! Гру закінчено.“);

return;

}

}

else if (input == “2”)

{

Console.WriteLine("Гравець втік з бою!");

break;

}

}

}

// Вивести можливі переміщення та дозволити гравцеві обрати локацію

Console.WriteLine("Доступні локації:");

if (currentLocation == startLocation)

{

Console.WriteLine("1 — Середня локація");

}

else if (currentLocation == middleLocation)

{

Console.WriteLine("1 — Початкова локація");

Console.WriteLine("2 — Кінцева локація");

}

else if (currentLocation == endLocation)

{

Console.WriteLine("1 — Середня локація“);

}

string input2 = Console.ReadLine();

if (input2 == “1”)

{

if (currentLocation == startLocation)

{

currentLocation = middleLocation;

}

else if (currentLocation == middleLocation)

{

currentLocation = startLocation;

}

else if (currentLocation == endLocation)

{

currentLocation = middleLocation;

}

}

else if (input2 == “2” && currentLocation == middleLocation)

{

currentLocation = endLocation;

}

// Пересувати ворогів

foreach (Enemy enemy in enemies)

{

if (currentLocation.Objects.Contains(enemy))

{

enemy.Move(currentLocation);

if (currentLocation.Objects.Contains(player))

{

player.Health -= enemy.Attack;

Console.WriteLine("Ворог завдав " + enemy.Attack + " урону гравцеві!");

if (player.Health <= 0)

{



Підписуйтеся на Telegram-канал @gamedev_dou, щоб не пропустити найважливіші статті і новини про геймдев

👍ПодобаєтьсяСподобалось0
До обраногоВ обраному0
LinkedIn
Дозволені теги: blockquote, a, pre, code, ul, ol, li, b, i, del.
Ctrl + Enter
Дозволені теги: blockquote, a, pre, code, ul, ol, li, b, i, del.
Ctrl + Enter

ох жесть. хоча може для реферату на урок інформатики в 9 класі комусь і зійде.

Я також так писав, на 1-2 курсі

Підписатись на коментарі