Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- // animated gif: http://i.imgur.com/LjsmASl.gif
- // Resources: http://imgur.com/a/aupvO
- /// <summary>
- /// Square types. Depending on square type you can perform some actions
- /// </summary>
- private enum squareTypes
- {
- cyan = 0,
- black=1,
- startPosition=2
- }
- /// <summary>
- /// Square class
- /// </summary>
- private class Square
- {
- /// <summary>
- /// Unique identifier for this square
- /// </summary>
- public int ID { get; set; }
- public squareTypes type { get; set; }
- /// <summary>
- /// X,Y location (pixels) in our board game
- /// </summary>
- public Point location { get; set; }
- /// <summary>
- /// ID for left square. Null if no left square
- /// </summary>
- public int? left { get; set; }
- /// <summary>
- /// ID for right square. Null if no right square
- /// </summary>
- public int? right { get; set; }
- /// <summary>
- /// ID for top square. Null if no top square
- /// </summary>
- public int? top { get; set; }
- /// <summary>
- /// ID for bottom square. Null if no bottom square
- /// </summary>
- public int? bottom { get; set; }
- }
- /// <summary>
- /// Our board game
- /// </summary>
- private List<Square> boardGame;
- /// <summary>
- /// Player class
- /// </summary>
- private class Player
- {
- /// <summary>
- /// name for the player (also can be use as Player unique ID)
- /// </summary>
- public string name { get; set; }
- /// <summary>
- /// In which square is currently the player
- /// </summary>
- public Square currentSquare { get; set; }
- }
- /// <summary>
- /// Our four players
- /// </summary>
- private Player playerRed, playerGreen, playerYellow, playerBlue;
- /// <summary>
- /// Enumeration for available moves allowed
- /// </summary>
- private enum Moves {Up,Down,Left,Right}
- /// <summary>
- /// User Pressed W,A,S or D key
- /// </summary>
- private bool readyToMove;
- /// <summary>
- /// Direction player is moving
- /// </summary>
- private Moves MovingTo;
- /// <summary>
- /// Stuff for checking some key has been released (we use KeyUp event for this stuff)
- /// </summary>
- private bool W_release = true, A_release = true, S_release = true, D_release = true;
- /// <summary>
- /// Game Loop
- /// </summary>
- private Timer TimerGameLoop;
- /// <summary>
- /// For Blink current player move
- /// </summary>
- private Timer BlinkMove;
- /// <summary>
- /// Who is moving now?
- /// </summary>
- private Player CurrentPlayerMove;
- /// <summary>
- /// Constructor. Entry Point
- /// </summary>
- public Form1()
- {
- InitializeComponent();
- this.Text = "HF Game prototype";
- this.BackgroundImageLayout = ImageLayout.None;
- this.BackgroundImage = Properties.Resources.HFBoardGame;
- lblShowWrongMove.Text = string.Empty;
- InitialzeBoardGame();
- this.KeyDown += Form1_KeyDown;
- this.KeyUp += Form1_KeyUp;
- TimerGameLoop = new Timer();
- TimerGameLoop.Tick += TimerGameLoop_Tick;
- TimerGameLoop.Interval = 100;
- TimerGameLoop.Enabled = true;
- BlinkMove = new Timer();
- BlinkMove.Tick += BlinkMove_Tick;
- BlinkMove.Interval = 500;
- BlinkMove.Enabled = true;
- }
- /// <summary>
- /// This method initializes our board game. You should use a "level" file instead hardcoding this, but hope you get the idea
- /// </summary>
- private void InitialzeBoardGame()
- {
- boardGame = new List<Square>();
- // first row (0 to 10)
- boardGame.Add(new Square() { ID = 0, type = squareTypes.cyan, location = new Point(16, 21), right = 1, bottom = 11 });
- boardGame.Add(new Square() { ID = 1, type = squareTypes.black, location = new Point(83, 21), left = 0, right = 2 });
- boardGame.Add(new Square() { ID = 2, type = squareTypes.cyan, location = new Point(150, 21), left = 1, right = 3, bottom = 13 });
- boardGame.Add(new Square() { ID = 3, type = squareTypes.black, location = new Point(217, 21), left = 2, right = 4 });
- boardGame.Add(new Square() { ID = 4, type = squareTypes.black, location = new Point(284, 21), left = 3, right = 5 });
- boardGame.Add(new Square() { ID = 5, type = squareTypes.cyan, location = new Point(351, 21), left = 4, right = 6, bottom = 16 });
- boardGame.Add(new Square() { ID = 6, type = squareTypes.black, location = new Point(418, 21), left = 5, right = 7 });
- boardGame.Add(new Square() { ID = 7, type = squareTypes.black, location = new Point(485, 21), left = 6, right = 8 });
- boardGame.Add(new Square() { ID = 8, type = squareTypes.cyan, location = new Point(552, 21), left = 7, right = 9, bottom = 19 });
- boardGame.Add(new Square() { ID = 9, type = squareTypes.black, location = new Point(619, 21), left = 8, right = 10 });
- boardGame.Add(new Square() { ID = 10, type = squareTypes.cyan, location = new Point(686, 21), left = 9, bottom = 21 });
- // second row (11 to 21)
- boardGame.Add(new Square() { ID = 11, type = squareTypes.black, location = new Point(16, 89), right = 12,top = 0 });
- boardGame.Add(new Square() { ID = 12, type = squareTypes.black, location = new Point(83, 89), left = 11, right = 13 });
- boardGame.Add(new Square() { ID = 13, type = squareTypes.black, location = new Point(150, 89), left = 12, right = 14, top = 2 });
- boardGame.Add(new Square() { ID = 14, type = squareTypes.black, location = new Point(217, 89), left = 13, right = 15 });
- boardGame.Add(new Square() { ID = 15, type = squareTypes.black, location = new Point(284, 89), left = 14, right = 16 });
- boardGame.Add(new Square() { ID = 16, type = squareTypes.black, location = new Point(351, 89), left = 15, right = 17, top = 5 });
- boardGame.Add(new Square() { ID = 17, type = squareTypes.black, location = new Point(418, 89), left = 16, right = 18 });
- boardGame.Add(new Square() { ID = 18, type = squareTypes.black, location = new Point(485, 89), left = 17, right = 19 });
- boardGame.Add(new Square() { ID = 19, type = squareTypes.black, location = new Point(552, 89), left = 18, right = 20, top = 8 });
- boardGame.Add(new Square() { ID = 20, type = squareTypes.black, location = new Point(619, 89), left = 19, right = 21 });
- boardGame.Add(new Square() { ID = 21, type = squareTypes.black, location = new Point(686, 89), left = 20 , top = 10 });
- // player start positions
- boardGame.Add(new Square() { ID = 22, type = squareTypes.startPosition, location = new Point(83, 157), top = 12 });
- boardGame.Add(new Square() { ID = 23, type = squareTypes.startPosition, location = new Point(217, 157), top = 14 });
- boardGame.Add(new Square() { ID = 24, type = squareTypes.startPosition, location = new Point(485, 157), top = 18 });
- boardGame.Add(new Square() { ID = 25, type = squareTypes.startPosition, location = new Point(619, 157), top = 20 });
- // Initialize Players positions
- playerRed = new Player() { currentSquare = boardGame.Find(x => x.ID == 22), name="red" };
- playerGreen = new Player() { currentSquare = boardGame.Find(x => x.ID == 23), name ="green" };
- playerYellow = new Player() { currentSquare = boardGame.Find(x => x.ID == 24), name ="yellow" };
- playerBlue = new Player() { currentSquare = boardGame.Find(x => x.ID == 25), name = "blue" };
- // Initialize Players picturebox
- pictureBoxRed.Image = Properties.Resources.HFplayerRed;
- pictureBoxRed.SizeMode = PictureBoxSizeMode.AutoSize;
- pictureBoxRed.BackColor = Color.Transparent;
- pictureBoxGreen.Image = Properties.Resources.HFplayerGreen;
- pictureBoxGreen.SizeMode = PictureBoxSizeMode.AutoSize;
- pictureBoxGreen.BackColor = Color.Transparent;
- pictureBoxYellow.Image = Properties.Resources.HFplayerYellow;
- pictureBoxYellow.SizeMode = PictureBoxSizeMode.AutoSize;
- pictureBoxYellow.BackColor = Color.Transparent;
- pictureBoxBlue.Image = Properties.Resources.HFplayerBlue;
- pictureBoxBlue.SizeMode = PictureBoxSizeMode.AutoSize;
- pictureBoxBlue.BackColor = Color.Transparent;
- CurrentPlayerMove = playerRed;
- }
- /// <summary>
- /// 'Render' players in our board game
- /// </summary>
- private void RenderPlayers()
- {
- pictureBoxRed.Location = playerRed.currentSquare.location;
- pictureBoxGreen.Location = playerGreen.currentSquare.location;
- pictureBoxYellow.Location = playerYellow.currentSquare.location;
- pictureBoxBlue.Location = playerBlue.currentSquare.location;
- // using the blinkFlag the current player turn will be 'blink'
- switch (CurrentPlayerMove.name)
- {
- case "red":
- pictureBoxRed.Visible = blinkFlag;
- pictureBoxGreen.Visible = true;
- pictureBoxYellow.Visible = true;
- pictureBoxBlue.Visible = true;
- break;
- case "green":
- pictureBoxRed.Visible = true;
- pictureBoxGreen.Visible = blinkFlag;
- pictureBoxYellow.Visible = true;
- pictureBoxBlue.Visible = true;
- break;
- case "yellow":
- pictureBoxRed.Visible = true;
- pictureBoxGreen.Visible = true;
- pictureBoxYellow.Visible = blinkFlag;
- pictureBoxBlue.Visible = true;
- break;
- case "blue":
- pictureBoxRed.Visible = true;
- pictureBoxGreen.Visible = true;
- pictureBoxYellow.Visible = true;
- pictureBoxBlue.Visible = blinkFlag;
- break;
- }
- }
- /// <summary>
- /// Moving a player. If the movement is allowed this function returns true
- /// </summary>
- /// <param name="player">Player we are moving</param>
- /// <param name="movingTo">Direction to move</param>
- /// <returns></returns>
- private bool MovePlayer(Player player, Moves movingTo)
- {
- switch (movingTo)
- {
- case Moves.Up:
- if (player.currentSquare.top != null)
- {
- player.currentSquare = boardGame.Find(x => x.ID == player.currentSquare.top);
- return true;
- }
- break;
- case Moves.Left:
- if (player.currentSquare.left != null)
- {
- player.currentSquare = boardGame.Find(x => x.ID == player.currentSquare.left);
- return true;
- }
- break;
- case Moves.Down:
- if (player.currentSquare.bottom != null)
- {
- player.currentSquare = boardGame.Find(x => x.ID == player.currentSquare.bottom);
- return true;
- }
- break;
- case Moves.Right:
- if (player.currentSquare.right != null)
- {
- player.currentSquare = boardGame.Find(x => x.ID == player.currentSquare.right);
- return true;
- }
- break;
- }
- return false;
- }
- private bool blinkFlag;
- private void BlinkMove_Tick(object sender, EventArgs e)
- {
- blinkFlag = !blinkFlag;
- }
- // 'Game Loop'
- private void TimerGameLoop_Tick(object sender, EventArgs e)
- {
- if (readyToMove)
- {
- var can = MovePlayer(CurrentPlayerMove, MovingTo);
- if (can)
- {
- switch (CurrentPlayerMove.name)
- {
- case "red":
- CurrentPlayerMove = playerGreen;
- break;
- case "green":
- CurrentPlayerMove = playerYellow;
- break;
- case "yellow":
- CurrentPlayerMove = playerBlue;
- break;
- case "blue":
- CurrentPlayerMove = playerRed;
- break;
- }
- readyToMove = false;
- lblShowWrongMove.Text = string.Empty;
- }
- else
- lblShowWrongMove.Text = $"You can not move to {MovingTo}! Try again.";
- }
- RenderPlayers();
- }
- // Keyboard stuff (KeyUp & KeyDown)
- private void Form1_KeyUp(object sender, KeyEventArgs e)
- {
- switch (e.KeyCode)
- {
- case Keys.W:
- W_release = true;
- break;
- case Keys.A:
- A_release = true;
- break;
- case Keys.S:
- S_release = true;
- break;
- case Keys.D:
- D_release = true;
- break;
- }
- }
- private void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- if (W_release && A_release && S_release && D_release)
- {
- readyToMove = true;
- switch (e.KeyCode)
- {
- case Keys.W:
- W_release = false;
- MovingTo = Moves.Up;
- break;
- case Keys.A:
- A_release = false;
- MovingTo = Moves.Left;
- break;
- case Keys.S:
- S_release = false;
- MovingTo = Moves.Down;
- break;
- case Keys.D:
- D_release = false;
- MovingTo = Moves.Right;
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment