Monday, May 14, 2018

Simple snake game, c# console application


using System;
using System.Collections;
using System.Threading;


public class Snake {
 public static void Main() {
  
  ArrayList posx = new ArrayList();
  ArrayList posy = new ArrayList();
  
  int dx, dy, foodx, foody, skor, i, j
   , enemyx, enemyy, enemyTimer, batasAtas
   , delaySpeed;
  String foodChar = "f";
  String enemyChar = "e";
  
  delaySpeed = 100;

  Random r = new Random();
  
  for (i=0; i<6; i++) {
   posx.Add(35+i);
   posy.Add(13);
  }
  
  bool selesai = false;
  
  foodx = 10;
  foody = 10;
  enemyx = 70;
  enemyy = 20;
  enemyTimer = 150;
  
  dx = -1;
  dy = 0;
  skor = 0;
  batasAtas = 1;
  
  ConsoleKeyInfo key = new ConsoleKeyInfo();
  
  while(!selesai) {
   Console.Clear();
   
   Console.SetCursorPosition(1,0);
   Console.Write("Score : " + skor);

   Console.SetCursorPosition(60,0);
   Console.Write("Speed : " + (100-delaySpeed));
   
   // cetak
   for (i=0; i0; i--) {
    if ((int)posx[0] == (int)posx[i] && (int)posy[0] == (int)posy[i]) {
     // tabrak badan sendiri
     selesai = true;
    }
    posx[i] = posx[i-1];
    posy[i] = posy[i-1];
   }
   posx[0] = (int)posx[0] + dx;
   posy[0] = (int)posy[0] + dy;
   
   if ((int)posx[0] < 1) posx[0] = Console.BufferWidth-1;
   else if ((int)posx[0] > Console.BufferWidth-2) posx[0] = 1;
   
   if ((int)posy[0] < batasAtas) posy[0] = Console.BufferHeight-1;
   else if ((int)posy[0] > Console.BufferHeight-2) posy[0] = batasAtas;
   
   if ((int)posx[0] == foodx && (int)posy[0] == foody) {
    skor++;
    Console.Beep();
    //     Always 5, 6, 7, 8 or 9.
    // Console.WriteLine(r.Next(5, 10));  
    foodx = r.Next(1, Console.BufferWidth-2);
    foody = r.Next(2, Console.BufferHeight-2);
    posx.Add((int)posx[posx.Count-1]-dx);
    posy.Add((int)posy[posy.Count-1]-dy);
    
    if (skor % 10 == 0) {
     delaySpeed -= 10;
     if (delaySpeed <= 0) delaySpeed = 0;
     skor++;
    }
    Console.Beep();
   }
   
   

   if ((int)posx[0] == enemyx && (int)posy[0] == enemyy) {
    selesai = true;
   }
   enemyTimer--;
   if (enemyTimer <= 0) {
    enemyx = r.Next(1, 80);
    enemyy = r.Next(2, 23);
    enemyTimer = 150;
   }
   
   Console.SetCursorPosition(1, Console.BufferHeight-1);
   if (Console.KeyAvailable) key = Console.ReadKey();
   if (key.Key == ConsoleKey.Escape) selesai = true;
   
   if (key.Key == ConsoleKey.UpArrow) {
    if (dy != 1) {
     dy = -1;
     dx = 0;
    }
   }
   else if (key.Key == ConsoleKey.DownArrow) {
    if (dy != -1) {
     dy = 1;
     dx = 0;
    }
   }    
   else if (key.Key == ConsoleKey.LeftArrow) {
    if (dx != 1) {
     dy = 0;
     dx = -1;
    }
   }    
   else if (key.Key == ConsoleKey.RightArrow) {
    if (dx != -1) {
     dy = 0;
     dx = 1;
    }
   }    
   else if (key.KeyChar == ' ') {
    // spasi untuk pause
    Console.SetCursorPosition(35, 0);
    Console.Write("PAUSED");    
    key = new ConsoleKeyInfo();
    do {
     key = Console.ReadKey();
     Console.Write("\b \b");
    } while (key.KeyChar != ' ');
    key = new ConsoleKeyInfo();
   }
   
   Thread.Sleep(delaySpeed);
  }
  
  for (i=10; i<20; i++) {
   for (j=15; j<55; j++) {
    Console.SetCursorPosition(j, i);
    if (i==10 || i == 19 || j == 15 || j == 54)
     Console.Write("*");
    else 
     Console.Write(" ");
   }
  }
  Console.SetCursorPosition(30, 12);
  Console.Write("GAME OVER");

  Console.SetCursorPosition(17, 14);
  Console.Write("YOUR SCORE : " + skor);
  
  Console.SetCursorPosition(16, 20);
  Console.Write("f = food");
  Console.SetCursorPosition(16, 21);
  Console.Write("e = enemy");
  
  Console.SetCursorPosition(1, Console.BufferHeight-1);
  Console.ReadKey();
 }
}  // end of code

if you use windows 7, you can compile by typing :
c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc file_name.cs