Pages

Tampilkan postingan dengan label Pemrograman (C#). Tampilkan semua postingan
Tampilkan postingan dengan label Pemrograman (C#). Tampilkan semua postingan

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;

                }
            }
        }
    }
}

Kamis, 27 Maret 2014

How to make A Simple game with Console Application On C# (Suit China)


This is example How to make A Simple game with Console Application On C#?
Nah ini contoh kodingan  Sederhana untuk membuat game suit china . tau kan suit china ? yang isinya gunting,kertas dan batu . well, langsung aja di check Syntaxnya .  Cekidot !


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

namespace PROJECT
{
    class Program
    {

        public static void PlayervsComputer()
        {
            Console.Clear();
            string playerChoice;
            string computerChoice;
            string[] RPS = { "B", "K", "G" };
            Random ranNumberGenerator = new Random();


            int LivesCount = 3;
            int Menang = 0;
            Console.WriteLine("Masukkan Nama Anda : ");
            string Nama = Console.ReadLine();
            do
            {

                Console.Clear();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.SetCursorPosition(0, 0);
                Console.WriteLine("Kesempatan : {0}", LivesCount);
                Console.SetCursorPosition(62, 0);
                Console.WriteLine("Kemenangan : {0}", Menang);
                Console.ResetColor();
                Console.SetCursorPosition(0, 3);
                for (int i = 0; i < 80; i++)
                {
                    Console.Write("=");
                }
                Console.WriteLine("");
                Console.WriteLine();

                Console.Write("Pilih (B) Untuk Batu , (K) untuk Kertas, (G) Untuk Gunting : ");
                playerChoice = Console.ReadLine().ToUpper();

                switch (playerChoice)
                {
                    case "B":
                    case "K":
                    case "G":
                        computerChoice = RPS[ranNumberGenerator.Next(RPS.Length)];
                        for (int i = 0; i < 5; i++)
                        {
                            Console.Write(".");
                            Thread.Sleep(200);
                        }
                        Console.WriteLine();
                        Console.WriteLine("Pilihan Computer: " + computerChoice);
                        if (computerChoice == playerChoice)
                            {
                                Console.WriteLine("Pilihan anda dan Computer sama");
                                Console.WriteLine("Seri !");

                            }
                            else
                            {
                                switch (computerChoice)
                                {
                                    case "B":
                                        if (playerChoice == "K")
                                        {
                                            Console.WriteLine("Paper wraps Rock!");
                                            Console.WriteLine("Selamat {0} !!", Nama + " Anda Menang");
                                            Menang++;

                                        }
                                        else
                                        {
                                            Console.WriteLine("Rock smashes Scissors!");
                                            Console.WriteLine("Maaf, anda kalah !");
                                            Console.Beep();
                                            LivesCount--;
                                        }
                                        break;

                                    case "K":
                                        if (playerChoice == "B")
                                        {
                                            Console.WriteLine("Paper wraps Rock!");
                                            Console.WriteLine("Maaf, Anda Kalah!");
                                            Console.Beep();
                                            LivesCount--;
                                        }
                                        else
                                        {
                                            Console.WriteLine("Scissors cut Paper!");
                                            Console.WriteLine("Selamat {0} !!", Nama + " Anda Menang");
                                            Menang++;
                                        }
                                        break;

                                    case "G":
                                        if (playerChoice == "K")
                                        {
                                            Console.WriteLine("Scissors cut Paper!");
                                            Console.WriteLine("Maaf, Anda Kalah!");
                                            Console.Beep();
                                            LivesCount--;
                                        }
                                        else
                                        {
                                            Console.WriteLine("Rock smashes Scissors!");
                                            Console.WriteLine(" Selamat {0} !!", Nama + " Anda Menang");
                                            Menang++;
                                        }
                                        break;
                                }
                            }
                        break;

                    default:
                        Console.WriteLine("Pilihan Anda Tidak Terdapat dalam Menu !!");
                        break;
                }
                Console.ReadLine();
            } while (LivesCount != 0);
            Console.SetCursorPosition(32, 0);
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Game Over !");
            Console.ResetColor();
            Console.ReadLine();
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Yellow;
            for(int i = 0 ; i < 80 ; i++)
            {
            Console.Write("=");
            Thread.Sleep(10);
            }
            Console.ResetColor();
            Console.BackgroundColor = ConsoleColor.Yellow;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.WriteLine("Hello {0} !!", Nama);
            Console.WriteLine("Anda sudah bermain dengan sangat baik !");
            Console.WriteLine("Score anda adalah : {0}", Menang);
            Console.ResetColor();
            Console.ResetColor();
            Console.ForegroundColor = ConsoleColor.Yellow;
            for (int i = 0; i < 80; i++)
            {
                Console.Write("=");
                Thread.Sleep(10);
            }
            Console.ReadLine();
        }
       
            
        public static void PlayervsPlayer()
        {
           
            Console.WriteLine("Masukkan Nama untuk pemain Pertama : ");
            string Name1 = Console.ReadLine();
            Console.WriteLine("Masukkan Nama untuk pemain Kedua : ");
            string Name2 = Console.ReadLine();
            Console.Clear();     
           
             int LivesCount1 = 3;
            int LivesCount2 = 3;
            int Player1Menang = 0;
            int Player2Menang = 0;
           
            do
            {

                Console.Clear();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.SetCursorPosition(0, 0);

                Console.WriteLine("Kesempatan Player 1 : {0}", LivesCount1);
                Console.SetCursorPosition(0, 1);
                Console.WriteLine("Kemenangan : {0}", Player1Menang);
                Console.WriteLine();
                Console.SetCursorPosition(50, 0);
                Console.WriteLine("Kesempatan Player 2 : {0}", LivesCount2);
                Console.SetCursorPosition(50, 1);
                Console.WriteLine("Kemengan Player 2 : {0}", Player2Menang);
                Console.WriteLine();
                Console.WriteLine("{0}", Name1 + " Mulai");
                Console.Write("Pilih B, K, G : ");
                string player1Choice = Console.ReadLine().ToUpper();
                Console.Clear();
                Console.SetCursorPosition(0, 0);
                Console.SetCursorPosition(0, 0);
                Console.WriteLine("Kesempatan Player 1 : {0}", LivesCount1);
                Console.SetCursorPosition(0, 1);
                Console.WriteLine("Kemenangan : {0}", Player1Menang);
                Console.WriteLine();
                Console.SetCursorPosition(50, 0);
                Console.WriteLine("Kesempatan Player 2 : {0}", LivesCount2);
                Console.SetCursorPosition(50, 1);
                Console.WriteLine("Kemengan Player 2 : {0}", Player2Menang);
                Console.WriteLine();
                               
                Console.WriteLine("{0}", Name2 + " Mulai");
                Console.Write("Pilih B, K, G : ");
                string Player2Choice = Console.ReadLine().ToUpper();

                switch (player1Choice)
                {
                    case "B":
                    case "K":
                    case "G":
                        Console.WriteLine("Pilihan Player 1 : " + player1Choice);
                        Console.WriteLine("Pilihan Player 2 : " + Player2Choice);

                        if (player1Choice == Player2Choice)
                        {
                            Console.WriteLine("Pilihan {0}",Name1 + "dan {1}",Name2 + " sama");
                            Console.WriteLine("Seri !");

                        }
                        else
                        {
                            switch (Player2Choice)
                            {
                                case "B":
                                    if (player1Choice == "K")
                                    {
                                        Console.WriteLine("Paper wraps Rock!");
                                        Console.WriteLine("Selamat {0} !!",Name1 + "Anda Menang");
                                        Player1Menang++;
                                        LivesCount2--;
                                    }
                                    else
                                    {
                                        Console.WriteLine("Rock smashes Scissors!");
                                        Console.WriteLine("Selamat {0} ", Name2 + " anda Menang");
                                        Player2Menang++;
                                        LivesCount1--;
                                    }
                                    break;

                                case "K":
                                    if (player1Choice == "B")
                                    {
                                        Console.WriteLine("Paper wraps Rock!");
                                        Console.WriteLine("Selamat {0} ", Name2 + " anda Menang");
                                        Player2Menang++;
                                        LivesCount1--;
                                    }
                                    else
                                    {
                                        Console.WriteLine("Scissors cut Paper!");
                                        Console.WriteLine("Selamat {0} !!", Name1 + "Anda Menang");
                                        Player1Menang++;
                                        LivesCount2--;
                                    }
                                    break;

                                case "G":
                                    if (player1Choice == "K")
                                    {
                                        Console.WriteLine("Scissors cut Paper!");
                                        Console.WriteLine("Selamat {0} ", Name2 + " anda Menang");
                                        Player2Menang++;
                                        LivesCount1--;
                                    }
                                    else
                                    {
                                        Console.WriteLine("Rock smashes Scissors!");
                                        Console.WriteLine("Selamat {0} !!", Name1 + "Anda Menang");
                                        Player1Menang++;
                                            LivesCount2--;
                                    }
                                    break;
                            }
                        }
                        break;

                    default:
                        Console.WriteLine("Pilihan Anda Tidak Terdapat dalam Menu !!");
                        break;
                }
                Console.ReadLine();
            } while((LivesCount1 !=0)||(LivesCount2 !=0));
        }
   
        static void PeraturanPermainan()
        {
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("<=================================================>");
            Console.ResetColor();
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.BackgroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("         0 0 0     0   0  0      00000             ");
            Console.WriteLine("         0    0    0   0  0      0                 ");
            Console.WriteLine("         0 0 0     0   0  0      0000              ");
            Console.WriteLine("         0    0    0   0  0      0                 ");
            Console.WriteLine("         0     0   00000  00000  00000             ");
            Console.ResetColor();
            Console.ResetColor();
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("<=================================================>");
            Console.ResetColor();
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.BackgroundColor = ConsoleColor.White;
            Console.WriteLine("Game ini sangat simple dan mudah untuk dimengerti");
            Console.WriteLine("Anda Cukup Memilih Gunting (G), Kertas (K) Atau Batu (B)");
            Console.WriteLine("Disini Terdapat Dua Pilihan");
            Console.WriteLine("Jika Anda meimilih Player vs Computer, Maka Anda Akan Menghadapi Computer");
            Console.WriteLine("Kesempatan Anda Untuk Menang hanya 3 kali");
            Console.WriteLine("Gunakan feeling untuk menang agar anda mampu menjadi pemilik Nilai Tertinggi!!");
            Console.WriteLine("Jika Anda Memilih Player vs Player, Maka Anda melakukan Duel Melawan Teman Anda");
            Console.WriteLine("Anda Juga Bisa Live Chat lho dengan teman Anda");
            Console.WriteLine("Yang lebih dulu menang 3 kali kesempatan Maka dialah Pemenangnya");
            Console.WriteLine("Keep Calm and having fun with this game !!!!");
            Console.ResetColor();
            Console.ResetColor();
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            Console.WriteLine("\nPress Any Key to Continue");
            Console.ResetColor();
            Console.ResetColor();
            Console.SetCursorPosition(32, 20);
            Console.WriteLine("--Terima Kasih--");

            Console.ReadKey();

        }

        static void TentangGame()
        {
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine("Flashback ke massa kecil, game ini cukup populer dengan nama suit china");
            Console.WriteLine("game ini terdiri dari gunting, kertas dan batu");
            Console.WriteLine("andaikan anda kalah maka akan ada hukuman yang terjadi");
            Console.WriteLine("nah, game ini hadir dengan konteks yang berbeda");
            Console.WriteLine("dimana Anda bisa memecahkan rekor untuk menang");
            Console.WriteLine("Game Ini Terlahir Karena adanya Tugas Project di @CCIT-FTUI ");
            Console.WriteLine("Credits : - Anjar Puspitasari");
            Console.WriteLine("          - Falah Achmad Bagusti");
            Console.WriteLine("          - Muhammad Fahmi Rukmana");
            Console.WriteLine("Ingin Tahu bagaimana Cara membuat game ini ?");
            Console.WriteLine("Kunjungi, www.Falahachmadbagusti.blogspot.com");
            for (int i = 0; i < 43; i++)
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("-");
                Thread.Sleep(20);
                Console.ResetColor();
            }
            Console.WriteLine("\n\t\tThank you !\n");
            for (int i = 0; i < 43; i++)
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("-");
                Thread.Sleep(20);
                Console.ResetColor();
            }
            Console.ReadKey();

        }

       

        public static void Main()
        {
            for (int i = 0; i < 80; i++)
            {
                Console.Write("=");
                Thread.Sleep(10);
            }
            Console.BackgroundColor = ConsoleColor.Blue;
            Console.WriteLine("Selamat datang di <>  ");
            Console.WriteLine("--> GAME TEMPOE DOLOE <--");
            Console.WriteLine("");
            Console.ResetColor();
            for (int i = 0; i < 80; i++)
            {
                Console.Write("=");
                Thread.Sleep(10);
            }
                Console.WriteLine("1. Player VS Computer");
            Console.WriteLine("2. Player VS Player");
            Console.WriteLine("3. Peraturan Permainan ");
            Console.WriteLine("4. Nilai Tertinggi");
            Console.WriteLine("5. Tentang Game");
            Console.WriteLine("6. Keluar");

            Console.Write("Pilihan Anda: ");
            int Choice = Convert.ToInt32(Console.ReadLine());
            switch (Choice)
            {
                case 1:
                    PlayervsComputer();
                    Console.Clear();
                    Main();
                    break;
                case 2:
                    PlayervsPlayer();
                    Console.Clear();
                    Main();
                    break;
                case 3 :
                    PeraturanPermainan();
                    Console.Clear();
                    Main();
                    break;
                case 5 :
                    TentangGame();
                    Console.Clear();
                    Main();
                    break;
                case 6:
                    Console.WriteLine("Terima Kasih telah mencoba game ini. jangan bosan-bosan ya !!");
                    break;
                default:
                    Console.WriteLine("Maaf, Pilihan anda tidak terdapat pada menu");
                    Console.ReadKey();
                    Main();
                    break;
            }
        }
    }
}