Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.22 KB | None | 0 0
  1. //MAINBORD
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Drawing;
  5.  
  6.  
  7.  
  8. namespace Assignment2
  9. {
  10.     public class Bord : Form
  11.     {
  12.         public Bord() : base()
  13.         {
  14.             Text = "Snake";
  15.             Width = 800;
  16.             Height = 600;
  17.             BackColor = Color.White;
  18.         }
  19.     }
  20.    
  21. }
  22.  
  23. //CIRCLE
  24. using System;
  25. namespace Assignment2
  26. {
  27.     public class Circle
  28.     {
  29.         public int X { get; set; }
  30.         public int Y { get; set; }
  31.  
  32.         public Circle()
  33.         {
  34.             Y = 0;
  35.             X = 0;
  36.         }
  37.     }
  38. }
  39.  
  40. //MAINCLASS
  41. using System;
  42. using System.Windows.Forms;
  43.  
  44. namespace Assignment2
  45. {
  46.     static class MainClass
  47.     {
  48.         public static void Main(string[] args)
  49.         {
  50.             Engine2 engine = new Engine2();
  51.             engine.Run();
  52.         }
  53.     }
  54. }
  55.  
  56. //ENGINE
  57. using System;
  58. using System.Drawing;
  59. using System.Collections.Generic;
  60. using System.Windows.Forms;
  61. namespace Assignment2
  62. {
  63.     public class Engine2
  64.     {
  65.         public static Bord MainBord { get; set; }
  66.         private Timer Timer;
  67.         public player player1;
  68.         //private ISet<player> Players = new HashSet<player>()
  69.         //{
  70.           //   new player(20,20)
  71.         //};
  72.  
  73.         public Engine2()
  74.         {
  75.  
  76.             new Settings();
  77.  
  78.             MainBord = new Bord();
  79.             player1 = new player();
  80.  
  81.             Timer = new Timer();
  82.         }
  83.         public void Run()
  84.         {
  85.             MainBord.Paint += updateScreen;
  86.             Timer.Interval = 1000 / 25;
  87.             Timer.Tick += Updater;
  88.             Timer.Start();
  89.  
  90.             Circle head = new Circle { X = 5, Y = 10 };
  91.             player1.Snake.Add(head);
  92.             Application.Run(MainBord);
  93.  
  94.  
  95.             //StartGame();
  96.  
  97.         }
  98.  
  99.         private void Updater(object sender, EventArgs e)
  100.         {
  101.  
  102.             Directions directions = choseDirection();
  103.  
  104.  
  105.             if (Settings.GameOver == true)
  106.             {
  107.                 if(directions == Directions.esc)
  108.                 {
  109.                     StartGame();
  110.                 }
  111.             }
  112.             else
  113.             {
  114.  
  115.                 if (directions == Directions.up && Settings.direction != Directions.down)
  116.                 {
  117.                     Settings.direction = Directions.up;
  118.                 }
  119.                 else if (directions == Directions.down && Settings.direction != Directions.up)
  120.                 {
  121.                     Settings.direction = Directions.down;
  122.                 }
  123.                 else if (directions == Directions.left && Settings.direction != Directions.right)
  124.                 {
  125.                     Settings.direction = Directions.left;
  126.                 }
  127.                 else if(directions == Directions.right && Settings.direction != Directions.left)
  128.                 {
  129.                     Settings.direction = Directions.right;
  130.                 }
  131.  
  132.                 player1.MovePlayer();
  133.             }
  134.             MainBord.Refresh();
  135.         }
  136.  
  137.         public void updateScreen(object obj, PaintEventArgs args)
  138.         {
  139.  
  140.             player1.updateScreen(args.Graphics);
  141.         }
  142.  
  143.         private void StartGame()
  144.         {
  145.             player1.Snake.Clear();
  146.             Circle head = new Circle() { X = 5, Y = 10 };
  147.             player1.Snake.Add(head);
  148.  
  149.             Application.Run(MainBord);
  150.         }
  151.  
  152.         public Directions choseDirection()
  153.         {
  154.             var key = Console.ReadKey(true);
  155.  
  156.             switch (key.Key)
  157.             {
  158.  
  159.                 case ConsoleKey.UpArrow: //Direction.Up
  160.                     return Directions.up;
  161.  
  162.                 case ConsoleKey.DownArrow://Direction.Down:
  163.                     return Directions.down;
  164.  
  165.                 case ConsoleKey.LeftArrow:
  166.                     return Directions.left;
  167.  
  168.                 case ConsoleKey.RightArrow:
  169.                     return Directions.right;
  170.  
  171.                 default:
  172.                     return Directions.esc;
  173.             }
  174.  
  175.  
  176.         }
  177.  
  178.  
  179.     }
  180. }
  181.  
  182. //PLAYER
  183. using System;
  184. using System.Collections.Generic;
  185. using System.Drawing;
  186. namespace Assignment2
  187. {
  188.  
  189.     public class player
  190.     {
  191.         //public enum Direction { up, down, left, right };
  192.  
  193.         public List<Circle> Snake;
  194.  
  195.         public Brush SnakeColor;
  196.         public int Width;
  197.         public int Height;
  198.         //public Direction direction;
  199.  
  200.         public player()
  201.         {
  202.             Width = Settings.Width;
  203.             Height = Settings.Height;
  204.             //direction = Settings.direction;
  205.             SnakeColor = Brushes.Green;
  206.  
  207.             Snake = new List<Circle>();
  208.         }
  209.  
  210.         public void MovePlayer( )
  211.         {
  212.             var key = Console.ReadKey(true);
  213.             //loop for snake head and parts
  214.             for (int i = Snake.Count -1 ; i >= 0; i--)
  215.             {
  216.                 // if snake head is active
  217.                 if (i == 0)
  218.                 {
  219.                     //move body along with head
  220.                     switch (Settings.direction/*Settings.direction*/)
  221.                     {
  222.                         case Directions.up :
  223.                             Snake[i].Y--;
  224.                             break;
  225.  
  226.                         case Directions.down:
  227.                             Snake[i].Y++;
  228.                             break;
  229.  
  230.                         case Directions.left:
  231.                             Snake[i].X--;
  232.                             break;
  233.  
  234.                         case Directions.right:
  235.                             Snake[i].X++;
  236.                             break;
  237.                     }
  238.  
  239.                    /* int maxXpos = Engine2.MainBord.Width;
  240.                     int maxYpos = Engine2.MainBord.Height;
  241.  
  242.  
  243.                     // if snake outruns the gameboard
  244.                     if (Snake[i].X < 0 || Snake[i].Y < 0 || Snake[i].X > maxXpos || Snake[i].Y > maxYpos)
  245.                     {
  246.                         die();
  247.                     }
  248.  
  249.                     // if snake runs into itself
  250.                     for (int j = 1; j < Snake.Count; j++)
  251.                     {
  252.                         if (Snake[i].X == Snake[j].X && Snake[i].Y == Snake[j].Y)
  253.                         {
  254.                             die();
  255.                         }
  256.  
  257.                     }
  258.  
  259.                     // later will check collision with food
  260.                     if (Snake[i].X == 10000 && Snake[i].Y == 10000)
  261.                     {
  262.                         die();
  263.                         //eat();
  264.                     }*/
  265.                 }
  266.  
  267.                 //if no collision continue
  268.                 else
  269.                 {
  270.                     Snake[i].X = Snake[i - 1].X;
  271.                     Snake[i].Y = Snake[i - 1].Y;
  272.                 }
  273.                
  274.             }
  275.         }
  276.  
  277.         public void updateScreen(Graphics e)
  278.         {
  279.            
  280.  
  281.             if (Settings.GameOver == false)
  282.             {
  283.                 for (int i = 0; i < Snake.Count; i++)
  284.                 {
  285.                     if (i == 0)
  286.                     {
  287.                         SnakeColor = Brushes.Black;
  288.                     }
  289.                     else
  290.                     {
  291.                         SnakeColor = Brushes.Blue;
  292.                     }
  293.  
  294.                     e.FillEllipse(SnakeColor, Snake[i].X,Snake[i].Y,Width,Height);
  295.  
  296.                 }
  297.             }
  298.  
  299.         }
  300.  
  301.         public void die()
  302.         {
  303.             Settings.GameOver = true;
  304.         }
  305.  
  306.     }
  307. }
  308.  
  309. //SETTINGS
  310. using System;
  311. namespace Assignment2
  312. {
  313.     public enum Directions { up, down, left, right, esc };
  314.  
  315.     public class Settings
  316.     {
  317.         public static bool GameOver { get; set; }
  318.         public static Directions direction { get; set; }
  319.         public static int Width { get; set; }
  320.         public static int Height { get; set; }
  321.         public static int Score { get; set; }
  322.         public static int Speed { get; set; }
  323.  
  324.  
  325.  
  326.         public Settings()
  327.         {
  328.             GameOver = false;
  329.             direction = Directions.down;
  330.             Width = 20;
  331.             Height = 20;
  332.             Score = 0;
  333.             Speed = 20;
  334.         }
  335.     }
  336. }
  337.  
  338. //INPUT
  339.  
  340. using System;
  341. using System.Collections;
  342. using System.Windows.Forms;
  343. using System.Collections.Generic;
  344. namespace Assignment2
  345. {
  346.     public class Input
  347.     {
  348.         public static Hashtable kTable = new Hashtable();
  349.  
  350.         public static bool KeyPressed(Keys key)
  351.         {
  352.             if (kTable[key] == null)
  353.             {
  354.                 return false;
  355.             }
  356.             return (bool)kTable[key];
  357.         }
  358.  
  359.         public static void State(Keys key, bool state)
  360.         {
  361.             kTable[key] = state;
  362.         }
  363.  
  364.         public static Directions ChoseDirection()
  365.         {
  366.             var key = Console.ReadKey(true);
  367.  
  368.             switch (key.Key)
  369.             {
  370.  
  371.                 case ConsoleKey.UpArrow: //Direction.Up
  372.                     return Directions.up;
  373.  
  374.                 case ConsoleKey.DownArrow://Direction.Down:
  375.                     return Directions.down;
  376.  
  377.                 case ConsoleKey.LeftArrow:
  378.                     return Directions.left;
  379.  
  380.                 case ConsoleKey.RightArrow:
  381.                     return Directions.right;
  382.  
  383.                 default:
  384.                     return Directions.esc;
  385.             }
  386.         }
  387.     }
  388. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement