private void ImageBox_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Point mpos = this.MousePos; int w2 = this.ImgWidth / 2; //pour savoir quelle grille il faut chercker //======== si aucune tile est en deplacement if (this.ActualGameStade == GameStade.sNothingHappening) { if (mpos.X < w2) { oVirtualGrid.CasePos CaseUnderM = this.GridRep.GetCaseUnderPoint(mpos.X, mpos.Y); if (CaseUnderM.Exist && CaseUnderM.x <= 2 && CaseUnderM.y <= 2) { oTile.TilePos tp = new oTile.TilePos(CaseUnderM.x, CaseUnderM.y, oTile.GridParent.gRep); //check s'il y a une tile a cette case if (this.IsAnyTileAtPos(tp)) { oTile TheClickedTile = this.GetTileAtPos(tp); TheClickedTile.SavePos(); TheClickedTile.IsMoving = true; //ce changement de coordonner sert a degager la tile des grille au cas ou le joueur la remet a la meme place TheClickedTile.pos.x = -1; TheClickedTile.pos.y = -1; this.ActualGameStade = GameStade.sTileMoving; } } } if (mpos.X > w2) { oVirtualGrid.CasePos CaseUnderM = this.GridStart.GetCaseUnderPoint(mpos.X, mpos.Y); if (CaseUnderM.Exist && CaseUnderM.x <= 2 && CaseUnderM.y <= 2) { oTile.TilePos tp = new oTile.TilePos(CaseUnderM.x, CaseUnderM.y, oTile.GridParent.gStart); //check s'il y a une tile a cette case if (this.IsAnyTileAtPos(tp)) { oTile TheClickedTile = this.GetTileAtPos(tp); TheClickedTile.SavePos(); TheClickedTile.IsMoving = true; //ce changement de coordonner sert a degager la tile des grille au cas ou le joueur la remet a la meme place TheClickedTile.pos.x = -1; TheClickedTile.pos.y = -1; this.ActualGameStade = GameStade.sTileMoving; } } } } // /!\ /!\ /!\ IL EST TRES IMPORTANT QUE CE SOIT UN ELSE car lorsqu'une case est clicker pour commencer un deplacement, this.ActualGameStade devien sTileMoving et il ne faut pas que le code de relachement de la tile s'execute immediatement apres else //========= si une tile est en deplacement { oTile TheMovingTile = this.GetTheMovingTile(); if (mpos.X < w2) { oVirtualGrid.CasePos cp = this.GridRep.GetCaseUnderPoint(mpos.X, mpos.Y); if (cp.Exist && cp.x <= 2 && cp.y <= 2) { oTile.TilePos tp = new oTile.TilePos(cp.x, cp.y, oTile.GridParent.gRep); //check s'il y a deja une tile en dessous bool IsAnyTile = this.IsAnyTileAtPos(tp); oTile TheTileUnder = this.GetTileAtPos(tp); if (!IsAnyTile) { TheMovingTile.pos.x = tp.x; TheMovingTile.pos.y = tp.y; TheMovingTile.pos.parent = tp.parent; TheMovingTile.IsMoving = false; this.ActualGameStade = GameStade.sNothingHappening; this.EasyResetGameIfOk(); } else { //oTile.TilePos temppos = TheMovingTile.pos; TheMovingTile.pos = TheTileUnder.pos; TheTileUnder.pos = TheMovingTile.savedpos; TheMovingTile.IsMoving = false; this.ActualGameStade = GameStade.sNothingHappening; this.EasyResetGameIfOk(); } } } if (mpos.X > w2) { oVirtualGrid.CasePos cp = this.GridStart.GetCaseUnderPoint(mpos.X, mpos.Y); if (cp.Exist && cp.x <= 2 && cp.y <= 2) { oTile.TilePos tp = new oTile.TilePos(cp.x, cp.y, oTile.GridParent.gStart); //check s'il y a deja une tile en dessous bool IsAnyTile = this.IsAnyTileAtPos(tp); oTile TheTileUnder = this.GetTileAtPos(tp); if (!IsAnyTile) { TheMovingTile.pos.x = tp.x; TheMovingTile.pos.y = tp.y; TheMovingTile.pos.parent = tp.parent; TheMovingTile.IsMoving = false; this.ActualGameStade = GameStade.sNothingHappening; this.EasyResetGameIfOk(); } else { TheMovingTile.pos = TheTileUnder.pos; TheTileUnder.pos = TheMovingTile.savedpos; TheMovingTile.IsMoving = false; this.ActualGameStade = GameStade.sNothingHappening; this.EasyResetGameIfOk(); } } } } } this.Refresh(); ////aucune tile ne doit etre en deplacement car les coordonner negative des tile en movement vont generer des erreur //if (this.ActualGameStade != GameStade.sTileMoving) //{ // this.EasyResetGameIfOk(); //} }
public void Refresh() { Bitmap img = new Bitmap(this.ImgWidth, this.ImgHeight); Graphics g = Graphics.FromImage(img); g.Clear(Color.Gray); //dessine les grille for (int y = 0; y <= 2; y++) { for (int x = 0; x <= 2; x++) { Rectangle ActualCaseRep = this.GridRep.GetCasePosition(x, y); Rectangle ActualCaseStart = this.GridStart.GetCasePosition(x, y); g.FillRectangle(Brushes.Gainsboro, ActualCaseRep); g.FillRectangle(Brushes.Gainsboro, ActualCaseStart); } } //lorsqu'une tile est en mouvement, il aparait dans la case sous la tile un petit rectangle foncer. il doit etre dessiner avant toute les tile pour qu'il soit dessous if (this.ActualGameStade == GameStade.sTileMoving) { Point mpos = this.MousePos; int w2 = this.ImgWidth / 2; if (mpos.X < w2) { oVirtualGrid.CasePos cp = this.GridRep.GetCaseUnderPoint(mpos.X, mpos.Y); if (cp.Exist && cp.x <= 2 && cp.y <= 2) { Rectangle TheZone = this.GridRep.GetCasePosition(cp.x, cp.y); TheZone.Width -= 1; TheZone.Height -= 1; TheZone.Inflate(-5, -5); g.DrawRectangle(Pens.DimGray, TheZone); TheZone.Inflate(-1, -1); g.DrawRectangle(Pens.DimGray, TheZone); } } if (mpos.X > w2) { oVirtualGrid.CasePos cp = this.GridStart.GetCaseUnderPoint(mpos.X, mpos.Y); if (cp.Exist && cp.x <= 2 && cp.y <= 2) { Rectangle TheZone = this.GridStart.GetCasePosition(cp.x, cp.y); TheZone.Width -= 1; TheZone.Height -= 1; TheZone.Inflate(-5, -5); g.DrawRectangle(Pens.DimGray, TheZone); TheZone.Inflate(-1, -1); g.DrawRectangle(Pens.DimGray, TheZone); } } } //dessine les tuile //cette gestion de la tile en movement est obligatoire pour qu'elle soit dessiner par dessus toute les autre oTile TheMovingTile = null; bool IsAMovingTile = false; foreach (oTile ActualTile in this.AllTile) { if (!ActualTile.IsMoving) { if (ActualTile.pos.parent == oTile.GridParent.gRep) { Rectangle ActualTilePos = this.GridRep.GetCasePosition(ActualTile.pos.x, ActualTile.pos.y); g.DrawImage(ActualTile.ActualImage, ActualTilePos.Location); } if (ActualTile.pos.parent == oTile.GridParent.gStart) { Rectangle ActualTilePos = this.GridStart.GetCasePosition(ActualTile.pos.x, ActualTile.pos.y); g.DrawImage(ActualTile.ActualImage, ActualTilePos.Location); } } else { TheMovingTile = ActualTile; IsAMovingTile = true; } } if (IsAMovingTile) { Point mpos = this.MousePos; int demiwidth = oTile.TileWidth / 2; Point tilepos = new Point(mpos.X - demiwidth, mpos.Y - demiwidth); g.DrawImage(TheMovingTile.ActualImage, tilepos); } g.Dispose(); if (this.ImageBox.Image != null) { this.ImageBox.Image.Dispose(); } this.ImageBox.Image = img; this.ImageBox.Width = this.ImgWidth; this.ImageBox.Height = this.ImgHeight; GC.Collect(); }