Guest User

Untitled

a guest
Jan 7th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5.  
  6. namespace WindowsFormsApplication1
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.         // animated gif: http://i.imgur.com/LjsmASl.gif
  11.         // Resources: http://imgur.com/a/aupvO
  12.  
  13.         /// <summary>
  14.         /// Square types. Depending on square type you can perform some actions
  15.         /// </summary>
  16.         private enum squareTypes
  17.         {
  18.             cyan = 0,
  19.             black=1,
  20.             startPosition=2                
  21.         }
  22.  
  23.         /// <summary>
  24.         /// Square class
  25.         /// </summary>
  26.         private class Square
  27.         {
  28.             /// <summary>
  29.             /// Unique identifier for this square
  30.             /// </summary>
  31.             public int ID { get; set; }          
  32.             public squareTypes type { get; set; }
  33.             /// <summary>
  34.             /// X,Y location (pixels) in our board game
  35.             /// </summary>
  36.             public Point location { get; set; }
  37.             /// <summary>
  38.             /// ID for left square. Null if no left square
  39.             /// </summary>
  40.             public int? left { get; set; }
  41.             /// <summary>
  42.             /// ID for right square. Null if no right square
  43.             /// </summary>
  44.             public int? right { get; set; }
  45.             /// <summary>
  46.             /// ID for top square. Null if no top square
  47.             /// </summary>
  48.             public int? top { get; set; }
  49.             /// <summary>
  50.             /// ID for bottom square. Null if no bottom square
  51.             /// </summary>
  52.             public int? bottom { get; set; }
  53.         }
  54.  
  55.         /// <summary>
  56.         /// Our board game
  57.         /// </summary>
  58.         private List<Square> boardGame;
  59.  
  60.         /// <summary>
  61.         /// Player class
  62.         /// </summary>
  63.         private class Player
  64.         {
  65.             /// <summary>
  66.             /// name for the player (also can be use as Player unique ID)
  67.             /// </summary>
  68.             public string name { get; set; }
  69.             /// <summary>
  70.             /// In which square is currently the player
  71.             /// </summary>
  72.             public Square currentSquare { get; set; }
  73.         }
  74.  
  75.         /// <summary>
  76.         /// Our four players
  77.         /// </summary>
  78.         private Player playerRed, playerGreen, playerYellow, playerBlue;
  79.  
  80.         /// <summary>
  81.         /// Enumeration for available moves allowed
  82.         /// </summary>
  83.         private enum Moves {Up,Down,Left,Right}
  84.  
  85.         /// <summary>
  86.         /// User Pressed W,A,S or D key
  87.         /// </summary>
  88.         private bool readyToMove;
  89.  
  90.         /// <summary>
  91.         /// Direction player is moving
  92.         /// </summary>
  93.         private Moves MovingTo;
  94.  
  95.         /// <summary>
  96.         /// Stuff for checking some key has been released (we use KeyUp event for this stuff)
  97.         /// </summary>
  98.         private bool W_release = true, A_release = true, S_release = true, D_release = true;
  99.  
  100.         /// <summary>
  101.         /// Game Loop
  102.         /// </summary>
  103.         private Timer TimerGameLoop;
  104.  
  105.         /// <summary>
  106.         /// For Blink current player move
  107.         /// </summary>
  108.         private Timer BlinkMove;
  109.  
  110.         /// <summary>
  111.         /// Who is moving now?
  112.         /// </summary>
  113.         private Player CurrentPlayerMove;
  114.  
  115.         /// <summary>
  116.         /// Constructor. Entry Point
  117.         /// </summary>
  118.         public Form1()
  119.         {
  120.             InitializeComponent();
  121.  
  122.             this.Text = "HF Game prototype";
  123.             this.BackgroundImageLayout = ImageLayout.None;
  124.             this.BackgroundImage = Properties.Resources.HFBoardGame;
  125.             lblShowWrongMove.Text = string.Empty;
  126.  
  127.             InitialzeBoardGame();
  128.  
  129.             this.KeyDown += Form1_KeyDown;
  130.             this.KeyUp += Form1_KeyUp;
  131.  
  132.             TimerGameLoop = new Timer();
  133.             TimerGameLoop.Tick += TimerGameLoop_Tick;
  134.             TimerGameLoop.Interval = 100;
  135.             TimerGameLoop.Enabled = true;
  136.  
  137.             BlinkMove = new Timer();
  138.             BlinkMove.Tick += BlinkMove_Tick;
  139.             BlinkMove.Interval = 500;
  140.             BlinkMove.Enabled = true;
  141.         }
  142.  
  143.         /// <summary>
  144.         /// This method initializes our board game. You should use a "level" file instead hardcoding this, but hope  you get the idea
  145.         /// </summary>
  146.         private void InitialzeBoardGame()
  147.         {
  148.             boardGame = new List<Square>();            
  149.  
  150.             // first row (0 to 10)
  151.             boardGame.Add(new Square() { ID = 0, type = squareTypes.cyan, location = new Point(16, 21), right = 1, bottom = 11 });
  152.             boardGame.Add(new Square() { ID = 1, type = squareTypes.black, location = new Point(83, 21), left = 0, right = 2 });
  153.             boardGame.Add(new Square() { ID = 2, type = squareTypes.cyan, location = new Point(150, 21), left = 1, right = 3, bottom = 13 });
  154.             boardGame.Add(new Square() { ID = 3, type = squareTypes.black, location = new Point(217, 21), left = 2, right = 4 });
  155.             boardGame.Add(new Square() { ID = 4, type = squareTypes.black, location = new Point(284, 21), left = 3, right = 5 });
  156.             boardGame.Add(new Square() { ID = 5, type = squareTypes.cyan, location = new Point(351, 21), left = 4, right = 6, bottom = 16 });
  157.             boardGame.Add(new Square() { ID = 6, type = squareTypes.black, location = new Point(418, 21), left = 5, right = 7 });
  158.             boardGame.Add(new Square() { ID = 7, type = squareTypes.black, location = new Point(485, 21), left = 6, right = 8 });
  159.             boardGame.Add(new Square() { ID = 8, type = squareTypes.cyan, location = new Point(552, 21), left = 7, right = 9, bottom = 19 });
  160.             boardGame.Add(new Square() { ID = 9, type = squareTypes.black, location = new Point(619, 21), left = 8, right = 10 });
  161.             boardGame.Add(new Square() { ID = 10, type = squareTypes.cyan, location = new Point(686, 21), left = 9, bottom = 21 });
  162.  
  163.             // second row (11 to 21)
  164.             boardGame.Add(new Square() { ID = 11, type = squareTypes.black, location = new Point(16, 89), right = 12,top = 0 });
  165.             boardGame.Add(new Square() { ID = 12, type = squareTypes.black, location = new Point(83, 89), left = 11, right = 13 });
  166.             boardGame.Add(new Square() { ID = 13, type = squareTypes.black, location = new Point(150, 89), left = 12, right = 14, top = 2 });
  167.             boardGame.Add(new Square() { ID = 14, type = squareTypes.black, location = new Point(217, 89), left = 13, right = 15 });
  168.             boardGame.Add(new Square() { ID = 15, type = squareTypes.black, location = new Point(284, 89), left = 14, right = 16 });
  169.             boardGame.Add(new Square() { ID = 16, type = squareTypes.black, location = new Point(351, 89), left = 15, right = 17, top = 5 });
  170.             boardGame.Add(new Square() { ID = 17, type = squareTypes.black, location = new Point(418, 89), left = 16, right = 18 });
  171.             boardGame.Add(new Square() { ID = 18, type = squareTypes.black, location = new Point(485, 89), left = 17, right = 19 });
  172.             boardGame.Add(new Square() { ID = 19, type = squareTypes.black, location = new Point(552, 89), left = 18, right = 20, top = 8 });
  173.             boardGame.Add(new Square() { ID = 20, type = squareTypes.black, location = new Point(619, 89), left = 19, right = 21 });
  174.             boardGame.Add(new Square() { ID = 21, type = squareTypes.black, location = new Point(686, 89), left = 20 , top = 10 });
  175.  
  176.             // player start positions
  177.             boardGame.Add(new Square() { ID = 22, type = squareTypes.startPosition, location = new Point(83, 157), top = 12 });
  178.             boardGame.Add(new Square() { ID = 23, type = squareTypes.startPosition, location = new Point(217, 157), top = 14 });
  179.             boardGame.Add(new Square() { ID = 24, type = squareTypes.startPosition, location = new Point(485, 157), top = 18 });
  180.             boardGame.Add(new Square() { ID = 25, type = squareTypes.startPosition, location = new Point(619, 157), top = 20 });
  181.  
  182.             // Initialize Players positions
  183.             playerRed = new Player() { currentSquare = boardGame.Find(x => x.ID == 22), name="red" };
  184.             playerGreen = new Player() { currentSquare = boardGame.Find(x => x.ID == 23), name ="green" };
  185.             playerYellow = new Player() { currentSquare = boardGame.Find(x => x.ID == 24), name ="yellow" };
  186.             playerBlue = new Player() { currentSquare = boardGame.Find(x => x.ID == 25), name = "blue" };
  187.  
  188.             // Initialize Players picturebox            
  189.             pictureBoxRed.Image = Properties.Resources.HFplayerRed;
  190.             pictureBoxRed.SizeMode = PictureBoxSizeMode.AutoSize;
  191.             pictureBoxRed.BackColor = Color.Transparent;
  192.             pictureBoxGreen.Image = Properties.Resources.HFplayerGreen;
  193.             pictureBoxGreen.SizeMode = PictureBoxSizeMode.AutoSize;
  194.             pictureBoxGreen.BackColor = Color.Transparent;
  195.             pictureBoxYellow.Image = Properties.Resources.HFplayerYellow;
  196.             pictureBoxYellow.SizeMode = PictureBoxSizeMode.AutoSize;
  197.             pictureBoxYellow.BackColor = Color.Transparent;
  198.             pictureBoxBlue.Image = Properties.Resources.HFplayerBlue;
  199.             pictureBoxBlue.SizeMode = PictureBoxSizeMode.AutoSize;
  200.             pictureBoxBlue.BackColor = Color.Transparent;
  201.  
  202.             CurrentPlayerMove = playerRed;
  203.         }
  204.  
  205.         /// <summary>
  206.         /// 'Render' players in our board game
  207.         /// </summary>
  208.         private void RenderPlayers()
  209.         {
  210.             pictureBoxRed.Location = playerRed.currentSquare.location;
  211.             pictureBoxGreen.Location = playerGreen.currentSquare.location;
  212.             pictureBoxYellow.Location = playerYellow.currentSquare.location;
  213.             pictureBoxBlue.Location = playerBlue.currentSquare.location;
  214.  
  215.             // using the blinkFlag the current player turn will be 'blink'
  216.             switch (CurrentPlayerMove.name)
  217.             {
  218.                 case "red":
  219.                     pictureBoxRed.Visible = blinkFlag;
  220.                     pictureBoxGreen.Visible = true;
  221.                     pictureBoxYellow.Visible = true;
  222.                     pictureBoxBlue.Visible = true;
  223.                     break;
  224.                 case "green":
  225.                     pictureBoxRed.Visible = true;
  226.                     pictureBoxGreen.Visible = blinkFlag;
  227.                     pictureBoxYellow.Visible = true;
  228.                     pictureBoxBlue.Visible = true;
  229.                     break;
  230.                 case "yellow":
  231.                     pictureBoxRed.Visible = true;
  232.                     pictureBoxGreen.Visible = true;
  233.                     pictureBoxYellow.Visible = blinkFlag;
  234.                     pictureBoxBlue.Visible = true;
  235.                     break;
  236.                 case "blue":
  237.                     pictureBoxRed.Visible = true;
  238.                     pictureBoxGreen.Visible = true;
  239.                     pictureBoxYellow.Visible = true;
  240.                     pictureBoxBlue.Visible = blinkFlag;
  241.                     break;
  242.             }
  243.         }
  244.  
  245.         /// <summary>
  246.         /// Moving a player. If the movement is allowed this function returns true
  247.         /// </summary>
  248.         /// <param name="player">Player we are moving</param>
  249.         /// <param name="movingTo">Direction to move</param>
  250.         /// <returns></returns>
  251.         private bool MovePlayer(Player player, Moves movingTo)
  252.         {
  253.  
  254.             switch (movingTo)
  255.             {
  256.                 case Moves.Up:
  257.                     if (player.currentSquare.top != null)
  258.                     {
  259.                         player.currentSquare = boardGame.Find(x => x.ID == player.currentSquare.top);
  260.                         return true;
  261.                     }
  262.                     break;
  263.                 case Moves.Left:
  264.  
  265.                     if (player.currentSquare.left != null)
  266.                     {
  267.                         player.currentSquare = boardGame.Find(x => x.ID == player.currentSquare.left);
  268.                         return true;
  269.                     }
  270.                     break;
  271.  
  272.                 case Moves.Down:
  273.  
  274.                     if (player.currentSquare.bottom != null)
  275.                     {
  276.                         player.currentSquare = boardGame.Find(x => x.ID == player.currentSquare.bottom);
  277.                         return true;
  278.                     }
  279.                     break;
  280.  
  281.                 case Moves.Right:
  282.                     if (player.currentSquare.right != null)
  283.                     {
  284.                         player.currentSquare = boardGame.Find(x => x.ID == player.currentSquare.right);
  285.                         return true;
  286.                     }
  287.                     break;
  288.             }
  289.  
  290.             return false;
  291.         }
  292.  
  293.  
  294.         private bool blinkFlag;
  295.         private void BlinkMove_Tick(object sender, EventArgs e)
  296.         {
  297.             blinkFlag = !blinkFlag;            
  298.         }
  299.  
  300.         // 'Game Loop'
  301.         private void TimerGameLoop_Tick(object sender, EventArgs e)
  302.         {
  303.             if (readyToMove)
  304.             {
  305.                 var can = MovePlayer(CurrentPlayerMove, MovingTo);
  306.                 if (can)
  307.                 {
  308.                     switch (CurrentPlayerMove.name)
  309.                     {
  310.                         case "red":
  311.                             CurrentPlayerMove = playerGreen;
  312.                             break;
  313.                         case "green":
  314.                             CurrentPlayerMove = playerYellow;
  315.                             break;
  316.                         case "yellow":
  317.                             CurrentPlayerMove = playerBlue;
  318.                             break;
  319.                         case "blue":
  320.                             CurrentPlayerMove = playerRed;
  321.                             break;
  322.                     }
  323.                     readyToMove = false;
  324.                     lblShowWrongMove.Text = string.Empty;
  325.                 }
  326.                 else
  327.                     lblShowWrongMove.Text = $"You can not move to {MovingTo}! Try again.";
  328.             }
  329.  
  330.             RenderPlayers();
  331.         }
  332.  
  333.         // Keyboard stuff (KeyUp & KeyDown)
  334.         private void Form1_KeyUp(object sender, KeyEventArgs e)
  335.         {
  336.             switch (e.KeyCode)
  337.             {
  338.                 case Keys.W:
  339.                     W_release = true;
  340.                     break;
  341.                 case Keys.A:
  342.                     A_release = true;
  343.                     break;
  344.                 case Keys.S:
  345.                     S_release = true;
  346.                     break;
  347.                 case Keys.D:
  348.                     D_release = true;
  349.                     break;
  350.             }
  351.         }
  352.  
  353.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  354.         {
  355.             if (W_release && A_release && S_release && D_release)
  356.             {
  357.                 readyToMove = true;
  358.                 switch (e.KeyCode)
  359.                 {
  360.                     case Keys.W:
  361.                         W_release = false;
  362.                         MovingTo = Moves.Up;
  363.                         break;
  364.                     case Keys.A:
  365.                         A_release = false;
  366.                         MovingTo = Moves.Left;
  367.                         break;
  368.                     case Keys.S:
  369.                         S_release = false;
  370.                         MovingTo = Moves.Down;
  371.                         break;
  372.                     case Keys.D:
  373.                         D_release = false;
  374.                         MovingTo = Moves.Right;
  375.                         break;
  376.                 }
  377.             }
  378.         }
  379.     }
  380. }
Advertisement
Add Comment
Please, Sign In to add comment