public void OnKeyUp(object sender, KeyEventArgs e) { ModelGame mg = (ModelGame)model; if (e.KeyCode == Keys.W) { pressedUp = false; } if (e.KeyCode == Keys.S) { pressedDown = false; } if (e.KeyCode == Keys.A) { pressedLeft = false; } if (e.KeyCode == Keys.D) { pressedRight = false; } if (e.KeyCode == Keys.Space) { pressedSpeed = false; mg.player.Speed = 5; } }
public void OnKeyDownWASD(object sender, KeyEventArgs e) { ModelGame mg = (ModelGame)model; if (e.KeyCode == Keys.W) { pressedUp = true; } if (e.KeyCode == Keys.S) { pressedDown = true; } if (e.KeyCode == Keys.A) { pressedLeft = true; mg.player.ImageURL = "../PlayerLeft.png"; } if (e.KeyCode == Keys.D) { pressedRight = true; mg.player.ImageURL = "../Player.png"; } if (e.KeyCode == Keys.Space) { pressedSpeed = true; } }
public void OnPaintEvent(object sender, PaintEventArgs pe) { Graphics g = pe.Graphics; ModelGame mg = (ModelGame)model; // Teken player g.DrawImage(Image.FromFile(mg.player.ImageURL), mg.player.Location.X, mg.player.Location.Y, mg.player.Width, mg.player.Height); // Teken andere gameobjects foreach (GameObject gameObject in mg.GameObjects) { if (gameObject is Obstacle) { g.DrawImage(Image.FromFile(gameObject.ImageURL), gameObject.Location.X, gameObject.Location.Y, gameObject.Width, gameObject.Height); } if (gameObject is Explosion) { g.DrawImage(Image.FromFile(gameObject.ImageURL), gameObject.Location.X, gameObject.Location.Y, gameObject.Width, gameObject.Height); { } } } }
private void GameLoop(object sender, EventArgs e) { ProcessUserInput(); ProcessObstacles(); counter++; ModelGame mg = (ModelGame)model; mg.graphicsPanel.Invalidate(); }
private void ProcessUserInput() { ModelGame mg = (ModelGame)model; if (mg.player.SpeedCooldown > 0) { mg.player.SpeedCooldown--; } if (pressedSpeed && (mg.player.SpeedCooldown == 0)) { mg.player.Speed = mg.player.OriginalSpeed * 2; mg.player.SpeedDuration++; } if (mg.player.SpeedDuration > 50) { mg.player.SpeedDuration = 0; mg.player.Speed = mg.player.OriginalSpeed; mg.player.SpeedCooldown = 200; } if (pressedDown && mg.player.Location.Y <= (mg.graphicsPanel.Size.Height + mg.graphicsPanel.Location.Y) - mg.player.Height) { mg.player.Location = new Point(mg.player.Location.X, mg.player.Location.Y + mg.player.Speed); } if (pressedUp && mg.player.Location.Y >= mg.graphicsPanel.Location.Y) { mg.player.Location = new Point(mg.player.Location.X, mg.player.Location.Y - mg.player.Speed); } if (pressedLeft && mg.player.Location.X >= mg.graphicsPanel.Location.X) { mg.player.Location = new Point(mg.player.Location.X - mg.player.Speed, mg.player.Location.Y); } if (pressedRight && mg.player.Location.X <= (mg.graphicsPanel.Size.Width + mg.graphicsPanel.Location.X) - mg.player.Width) { mg.player.Location = new Point(mg.player.Location.X + mg.player.Speed, mg.player.Location.Y); } }
private void countFix(ModelGame mg) { score++; mg.score.Text = score.ToString(); }
private void ProcessObstacles() { ModelGame mg = (ModelGame)model; // We moeten een 2e array maken om door heen te loopen // Er is kans dat we de array door lopen en ook tegelijkertijd een explosie toevoegen // We voegen dan als het ware iets toe en lezen tegelijk, dit mag niet List <GameObject> safeListArray = new List <GameObject>(mg.GameObjects); // Loop door alle obstacles objecten en roep methode aan foreach (GameObject gameObject in safeListArray) { if (gameObject is MovingExplodingObstacle) { MovingExplodingObstacle gameObstacle = (MovingExplodingObstacle)gameObject; gameObstacle.ChasePlayer(mg.player); if (gameObstacle.CollidesWith(mg.player)) { mg.player.Location = new Point(0, 0); mg.InitializeField(); mg.GameObjects.Add(new Explosion(gameObstacle.Location, 80, 80)); } } if (gameObject is SlowingObstacle) { SlowingObstacle gameObstacle = (SlowingObstacle)gameObject; gameObstacle.ChasePlayer(mg.player); if (gameObstacle.CollidesWith(mg.player)) { mg.player.Speed = mg.player.OriginalSpeed / 2; } else { mg.player.Speed = mg.player.OriginalSpeed; } } if (gameObject is ExplodingObstacle) { ExplodingObstacle gameObstacle = (ExplodingObstacle)gameObject; if (gameObstacle.CollidesWith(mg.player)) { mg.player.Location = new Point(0, 0); mg.InitializeField(); mg.GameObjects.Add(new Explosion(gameObstacle.Location, 80, 80)); } } if (gameObject is StaticObstacle) { StaticObstacle gameObstacle = (StaticObstacle)gameObject; if (gameObstacle.CollidesWith(mg.player)) { if (pressedUp) { mg.player.Location = new Point(mg.player.Location.X, mg.player.Location.Y + mg.player.Speed); } if (pressedDown) { mg.player.Location = new Point(mg.player.Location.X, mg.player.Location.Y - mg.player.Speed); } if (pressedLeft) { mg.player.Location = new Point(mg.player.Location.X + mg.player.Speed, mg.player.Location.Y); } if (pressedRight) { mg.player.Location = new Point(mg.player.Location.X - mg.player.Speed, mg.player.Location.Y); } } } // Check of we de explosie kunnen verwijderen if (gameObject is Explosion) { Explosion explosion = (Explosion)gameObject; DateTime nowDateTime = DateTime.Now; DateTime explosionDateTime = explosion.TimeStamp; TimeSpan difference = nowDateTime - explosionDateTime; // Verschil is 3 seconden, dus het bestaat al voor 3 seconden, verwijderen maar! if (difference.TotalSeconds > 3) { mg.GameObjects.Remove(gameObject); } } } }