Pages

Selasa, 01 April 2014

Project Console game : Galaxy Impact

Sebenernya sih konsep game ini simple, kita tinggal ngerandom simbolnya aja.
so, come on we checked a game !

cekidot !!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;

namespace ConsoleApplication12
{
    class Program
    {
        struct unit
        {
            public int x;
            public int y;
            public string Player;
            public char Symbol;
            public ConsoleColor color;

        }

        static void PrintAtPosition(int x, int y, string player, ConsoleColor color)
        {
            Console.SetCursorPosition(x, y);
            Console.ForegroundColor = color;
            Console.Write(player);

        }

        static void PrintAtPosition(int x, int y, char Symbol, ConsoleColor color)
        {
            Console.SetCursorPosition(x, y);
            Console.ForegroundColor = color;
            Console.Write(Symbol);
        }
        static void PrintStringAtPosition(int x, int y, string text, ConsoleColor color)
        {
            Console.SetCursorPosition(x, y);
            Console.ForegroundColor = color;
            Console.Write(text);
        }
        static string today = DateTime.Now.ToString("dd-MM-yyyy");

        static void WriteScore()
        {
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Black;
            Console.BackgroundColor = ConsoleColor.White;
            for (int i = 0; i < 80; i++)
            {
                Console.Write("x");
                Thread.Sleep(20);
            }
            Console.ResetColor();
            Console.ResetColor();
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("\t\t\t\tGOOD JOB!!");
            Console.WriteLine();
            Console.WriteLine("\t\tthanks, cause you want try to save our earth from meteor");
            Console.WriteLine();
            Console.WriteLine("\t\t\tHave A nice Day !!");
            Console.ForegroundColor = ConsoleColor.Black;
            Console.BackgroundColor = ConsoleColor.White;
            for (int i = 0; i < 80; i++)
            {
                Console.Write("x");
                Thread.Sleep(20);
            }


            Console.WriteLine();
            FileStream Fs = new FileStream("c:/score.txt", FileMode.Append, FileAccess.Write);
            StreamWriter w = new StreamWriter(Fs);
            Console.ResetColor();
            Console.ResetColor();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(30, 10);
            Console.WriteLine("Enter Your Name : ");
            Console.ForegroundColor = ConsoleColor.White;
            Console.SetCursorPosition(30, 11);
            string Name = Console.ReadLine();

            w.Write("Name : {0}", Name);
            w.Write(" | ");
            w.Write("Date : {0}", today);
            Console.WriteLine();
            w.Flush();
            w.Close();
            Fs.Close();

            Console.WriteLine("your name has been entered !");
        Replay:
            Console.WriteLine("Do you want to play again ? press (Y) or (N)");
            string playagain = Console.ReadLine().ToLower();
            switch (playagain)
            {
                case "y":
                    play();
                    break;
                case "n":
                    Console.Clear();
                    Main();
                    break;
                default:
                    Console.WriteLine("Wrong choice ! please repeat again !! ");
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.BackgroundColor = ConsoleColor.White;
                    Console.Write("Advice : ");
                    Console.ForegroundColor = ConsoleColor.Black;
                    Console.Write("You can only choice 1 or 2");
                    Console.ResetColor();
                    Console.ResetColor();
                    Console.Clear();
                    goto Replay;
            }
        }


        public static void play()
        {
            Random RandGen = new Random();
            List<unit> Meteor = new List<unit>();
            int livesCount = 3;
            int score = 0;
            int time;
            char[] symbolList = { '*', 'o', '.' };
            int speed = 0;

            unit ship = new unit();
            ship.x = 79;
            ship.y = (Console.WindowHeight / 2);
            ship.Player = "[]";


            while (true)
            {
                bool hitted = false;
                {
                    unit newinmeteor = new unit();
                    newinmeteor.x = RandGen.Next(0, Console.WindowLeft / 2);
                    newinmeteor.y = Console.WindowTop / 2;
                    newinmeteor.color = ConsoleColor.Blue;
                    newinmeteor.Symbol = symbolList[RandGen.Next(0, 2)];
                    Meteor.Add(newinmeteor);

                    if (Console.KeyAvailable)
                    {
                        ConsoleKeyInfo keyPressed = Console.ReadKey(true);
                        while (Console.KeyAvailable) { Console.ReadKey(true); }
                        if (keyPressed.Key == ConsoleKey.UpArrow)
                        {
                            if (ship.y > 0)
                            {
                                ship.y--;
                            }
                        }
                        if (keyPressed.Key == ConsoleKey.DownArrow)
                        {
                            if (ship.y < Console.WindowHeight / 2)
                            {
                                ship.y++;
                            }
                        }
                        if (keyPressed.Key == ConsoleKey.Enter)
                        {
                            Console.WriteLine("i");
                        }
                        if (keyPressed.Key == ConsoleKey.P)
                        {
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.Write("\nEnter The time who you Want to pause (on second) : ");
                            int Pause = Convert.ToInt32(Console.ReadLine());
                            Thread.Sleep(Pause * 1000);
                        }
                        List<unit> newList = new List<unit>();
                        for (int i = 0; i < 1000; i++)
                        {
                            unit KindMeteor = Meteor[i];
                            unit MeteorMoved = new unit();
                            MeteorMoved.x = KindMeteor.x + 1;
                            MeteorMoved.y = KindMeteor.y;
                            MeteorMoved.color = KindMeteor.color;
                            MeteorMoved.Symbol = KindMeteor.Symbol;

                            if (MeteorMoved.Symbol != '+' && MeteorMoved.x == ship.x && MeteorMoved.y == ship.y)
                            {
                                hitted = true;
                                livesCount--;
                                Console.Beep();
                                speed = 0;

                                if (livesCount <= 3 && livesCount > 0)
                                {
                                    PrintAtPosition(44, 2, "Try Again ", ConsoleColor.Red);
                                    Console.ReadKey();

                                }

                                else
                                {
                                    while (false) ;
                                    Console.SetCursorPosition(45, 2);
                                    Console.ForegroundColor = ConsoleColor.Red;
                                    Console.WriteLine("Game over !!");
                                    WriteScore();
                                    Console.ReadKey();
                                    break;

                                }
                                break;
                            }

                            if (MeteorMoved.y > Console.WindowHeight)
                            {
                                newList.Add(MeteorMoved);
                            }
                            else
                            {
                                score++;
                            }
                            Meteor = newList;
                            Console.Clear();
                            if (hitted)
                            {

                                PrintAtPosition(ship.x, ship.y, 'X', ConsoleColor.Red);
                                Meteor.Clear();
                            }
                            else
                            {
                                PrintAtPosition(ship.x, ship.y, ship.Symbol, ship.color);
                            }
                            foreach (unit Meteors in Meteor)
                            {
                                PrintAtPosition(Meteors.x, Meteors.y, Meteors.Symbol, Meteors.color);
                            }


                            for (int a = 5; a < 25; a++)
                            {
                                PrintAtPosition(0, a, '|', ConsoleColor.Gray);
                            }
                            PrintStringAtPosition(30, 0, "Today: " + today, ConsoleColor.Cyan);
                            PrintStringAtPosition(10, 2, "Lives: " + livesCount, ConsoleColor.Green);
                            PrintStringAtPosition(22, 2, "Score: " + score, ConsoleColor.Green);
                            PrintStringAtPosition(33, 2, "Speed: " + speed, ConsoleColor.Green);


                            if (speed < 210)
                            {
                                speed++;
                            }
                            Thread.Sleep(250 - speed);
                        }

                    }
                }

            }
        }
        class MainInterface
        {

            static void Main()
            {
                //Main menu

                for (int i = 0; i < 79; i++)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("=");
                    Thread.Sleep(13);
                    Console.ResetColor();
                }
                Console.WriteLine();
                Console.BackgroundColor = ConsoleColor.Red;
                Console.WriteLine("\t\t\tWelcome On Galaxy Impact game !                       ");
                Console.WriteLine("\t\t\tKeep Away From The meteor !!!                         ");
                Console.ResetColor();

                for (int i = 0; i < 79; i++)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("=");
                    Thread.Sleep(13);

                }
                Console.WriteLine();
                Console.Write("\t\t\t|");
                Console.WriteLine("1. Play");
                Console.Write("\t\t\t|");
                Console.WriteLine("2. Rule Of Game");
                Console.Write("\t\t\t|");
                Console.WriteLine("3. View Score");
                Console.Write("\t\t\t|");
                Console.WriteLine("4. Delete Score");
                Console.Write("\t\t\t|");
                Console.WriteLine("5. About");
                Console.Write("\t\t\t|");
                Console.WriteLine("6. Exit");
                Console.Write("\n\t\t\t");
                Console.Write("  Your Choice : ");
                Console.ForegroundColor = ConsoleColor.Gray;
                string Choice = Console.ReadLine();
                Console.ResetColor();
                int P = Int32.Parse(Choice);

                switch (P)
                {
                    case 1:
                        play();
                        break;

                }
            }
        }
    }
}

Tidak ada komentar:

Posting Komentar