internal void CaptureKey(object sender, KeyEventArgs e) { ModelInterface m = Engine.GetInstance()._model; int key = e.KeyValue; if (key == Engine.GetInstance()._configuration.Keys.GetKey("left")) { Vector2D newPos = m.GetShipPosition() - m.GetShipSpeed(); m.SetShipPosition((newPos.X > 0) ? newPos : new Vector2D(0, newPos.Y)); } else if (key == Engine.GetInstance()._configuration.Keys.GetKey("right")) { Vector2D newPos = m.GetShipPosition() + m.GetShipSpeed(); int maxX = (int)m.GetMapDimensions().X; int width = (int)m.GetShipDimensions().X; m.SetShipPosition((newPos.X < maxX - width) ? newPos : new Vector2D(maxX - width, newPos.Y)); } else if (key == Engine.GetInstance()._configuration.Keys.GetKey("esc")) { m.SetExited(true); } }
private static void OnTimedEvent(object source, ElapsedEventArgs e) { ModelInterface m = Engine.GetInstance()._model; int length = m.GetBallCount(); int length2 = m.GetBrickCount(); Vector2D msize = m.GetMapDimensions(); Vector2D shipPosition = m.GetShipPosition(); Vector2D fallSpeed = (length2 > 0) ? m.GetBrickSpeed(0) : null; Random r = new Random(); double diff; Vector2D bposition, newPosition, newSpeed, bspeed; int j, health; bool chocked; // S'il n'y a plus de briques, on passe au niveau suivant if (length2 <= 0) { m.SetLevel(m.GetLevel() + 1); Engine.GetInstance().Restart(); } // Pour chaque balle (car le moteur gère plusieurs balles même si on l'a limité pour la démonstration) for (int i = 0; i < length; i++) { bspeed = m.GetBallSpeed(i); j = 0; chocked = false; while (j < length2 && !chocked) { if (Tools.Utils.Intersects(m.GetBallBoundingBox(i), m.GetBrickBoundingBox(j))) { health = m.GetBrickHealth(j) - m.GetBallDamage(i); m.SetBrickHealth(j, health); if (health <= 0) { // Il faut corriger l'index car la brique va être supprimée dans le setbrickhealth j--; length2--; } // Le rebond est géré dans une classe utilitaire, ici on inverse X et Y m.SetBallSpeed(i, Tools.Utils.ChocResult(bspeed)); chocked = true; } j++; } bposition = m.GetBallPosition(i); newPosition = bposition + bspeed; newSpeed = bspeed; // On surveille que la balle ne soit pas sur le point de sortir de l'écran et on corrige par des rebonds sinon if (newPosition.X < 0) { newPosition.invertX(); newSpeed.invertX(); } else if (newPosition.X >= msize.X - 1) { newPosition.X = 2 * (msize.X - 1) - newPosition.X; newSpeed.invertX(); } else if (newPosition.Y >= msize.Y - 1) { newPosition.Y = 2 * (msize.Y - 1) - newPosition.Y; newSpeed.invertY(); } // La balle peut aussi rebondir sur le ship, nous le testons, si c'est le cas elle rebondit avec un petit aléa (diff) else if (Tools.Utils.Intersects(m.GetBallBoundingBox(i), m.GetShipBoundingBox())) { newPosition.Y = 2 * (shipPosition.Y + m.GetShipDimensions().Y) - newPosition.Y; newSpeed.invertY(); diff = r.NextDouble() / 2 * newSpeed.X * ((r.Next(2) < 1) ? -1 : 1); newSpeed.X += diff; } m.SetBallPosition(i, newPosition); m.SetBallSpeed(i, newSpeed); // Si on a perdu la balle, on perd la partie if (newPosition.Y < 0) { m.SetLost(true); Engine.GetInstance().Stop(); } } // On va accélérer les briques en mode HighSpeedMode et aussi les déplacer for (j = 0; j < length2; j++) { newSpeed = fallSpeed + Engine.GetInstance()._configuration.GameMode.StepSpeed; newPosition = m.GetBrickPosition(j) + m.GetBrickSpeed(j); m.SetBrickPosition(j, newPosition); m.SetBrickSpeed(j, newSpeed); if (newPosition.Y < -1) { j--; length2--; } } // Enfin si le ship touche une brique, il la détruit mais est endommagé, s'il est détruit, on perd for (j = 0; j < length2; j++) { if (Tools.Utils.Intersects(m.GetShipBoundingBox(), m.GetBrickBoundingBox(j))) { m.SetShipHealth(m.GetShipHealth() - m.GetBrickHealth(j)); if (m.GetShipHealth() <= 0) { m.SetLost(true); Engine.GetInstance().Stop(); } m.SetBrickHealth(j, 0); j--; length2--; } } }
public void Refresh(Observable m) { ModelInterface model = (ModelInterface)m; if (model.IsStopped() && model.IsLost()) { this.Controls.Clear(); List <Score> scores = model.GetScores(); int i = 0; Label title = new System.Windows.Forms.Label(); this.SuspendLayout(); // // title // title.AutoSize = true; title.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; title.Font = new System.Drawing.Font("Microsoft Sans Serif", 25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); title.ForeColor = System.Drawing.SystemColors.ActiveCaption; title.Location = new System.Drawing.Point(90, 38); title.Name = "title"; title.Size = new System.Drawing.Size(200, 41); title.TabIndex = 0; title.Text = "High scores"; this.Controls.Add(title); foreach (Score sc in scores) { Control label = new Label(); label.Text = i + 1 + " :"; label.BackColor = Color.Black; label.ForeColor = Color.White; label.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); label.Size = new System.Drawing.Size(30, 30); label.Location = new System.Drawing.Point(60, i * 60 + 120); this.Controls.Add(label); Control name = new Label(); name.BackColor = Color.Black; name.ForeColor = Color.White; name.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); name.Size = new System.Drawing.Size(80, 30); name.Location = new System.Drawing.Point(120, i * 60 + 120); this.Controls.Add(name); name.Text = sc.Player.Pseudo; Control score = new Label(); score.Text = sc.Value.ToString(); score.BackColor = Color.Black; score.ForeColor = Color.White; score.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); score.Size = new System.Drawing.Size(50, 30); score.Location = new System.Drawing.Point(210, i * 60 + 120); this.Controls.Add(score); i++; } } else if (model.IsStopped()) { this.Controls.Clear(); } else if (model.IsEnded()) { this.Close(); } this._grid = model.GetMapDimensions(); }