public void Move(Game game) { // calculate the next x and y coordinate by using the formula : xNew = xOld + cos(angle) // yNew = yOld + sin(angle) // Look at trigonometrical functions and their definition int x = (int)Math.Round(Math.Cos(ToRad(Direction))) + X; int y = (int)Math.Round(Math.Sin(ToRad(Direction))) + Y; // Will be the Robutton leave the game field? If yes, it shall reflect int borderCollision = game.IsOnTheField(x, y); if (borderCollision != Game.ON_THE_FIELD) { ReflectFromBorder(borderCollision); // need to recalculate the Coordinates X = (int)Math.Round(Math.Cos(ToRad(Direction))) + X; Y = (int)Math.Round(Math.Sin(ToRad(Direction))) + Y; return; } // Will the Robutton collide with some other robutton or coin Unit possibleCollision = game.IsThereAnUnit(x, y); if (possibleCollision != null) { // The unit is another Robutton if (possibleCollision is Robutton) { Robutton otherRobutton = (Robutton)possibleCollision; RotateAfterContact(); otherRobutton.RotateAfterContact(); } // The unit is a coin else { if (Coin == null) { // pick up the coin Coin = possibleCollision; game.Coins.Remove(Coin); } else { // drop the coin in front of the found coin Coin.X = this.X; Coin.Y = this.Y; game.Coins.Add(Coin); Coin = null; // rotate up to 180 degree Direction = Direction + 180; } } // need to recalculate the Coordinates X = (int)Math.Round(Math.Cos(ToRad(Direction))) + X; Y = (int)Math.Round(Math.Sin(ToRad(Direction))) + Y; return; } // if everything is ok, the robutton moves to the calculated coordinates X = x; Y = y; }
private void button1_Click(object sender, EventArgs e) { game = new Game((int)numericUpDown1.Value, (int)numericUpDown2.Value); timer = new GameLoop(game, splitContainer1.Panel1); timer.Interval = 1000 / 30; timer.Enabled = true; button2.Enabled = true; }
public GameLoop(Game game, SplitterPanel table) { this.game = game; this.table = table; this.Elapsed += new ElapsedEventHandler(OnTimedEvent); }