private void timer1_Tick(object sender, EventArgs e) { GameConstants gc = new GameConstants(); // Get current direction, Move tank switch (this.tank1.Direction) { case 0: // Move tank up this.greentank.Image = this.greenimage0; if (this.tank2.Vertical >= 0) { this.tank1.Vertical -= gc.TankSpeed; Point pointa = new Point(this.tank1.Horizontal, this.tank1.Vertical); this.greentank.Location = pointa; } else // Recoil { this.tank1.Direction = 2; this.tank1.Vertical += gc.TankSpeed; Point point3 = new Point(this.tank1.Horizontal, this.tank1.Vertical); this.redtank.Location = point3; } break; case 1: // Move tank right this.greentank.Image = this.greenimage1; if (this.tank1.Horizontal <= gc.GameWidth) { this.tank1.Horizontal += gc.TankSpeed; Point pointb = new Point(this.tank1.Horizontal, this.tank1.Vertical); this.greentank.Location = pointb; } else // Recoil { this.tank1.Direction = 3; this.tank1.Horizontal -= gc.TankSpeed; Point point3 = new Point(this.tank1.Horizontal, this.tank1.Vertical); this.redtank.Location = point3; } break; case 2: // Move tank down this.greentank.Image = this.greenimage2; if (this.tank1.Vertical <= gc.GameHeight) { this.tank1.Vertical += gc.TankSpeed; Point pointc = new Point(this.tank1.Horizontal, this.tank1.Vertical); this.greentank.Location = pointc; } else // Recoil { this.tank1.Direction = 0; this.tank1.Vertical -= gc.TankSpeed; Point point3 = new Point(this.tank1.Horizontal, this.tank1.Vertical); this.redtank.Location = point3; } break; case 3: // Move tank left this.greentank.Image = this.greenimage3; if (this.tank1.Horizontal >= 0) { this.tank1.Horizontal -= gc.TankSpeed; Point pointd = new Point(this.tank1.Horizontal, this.tank1.Vertical); this.greentank.Location = pointd; } else // Recoil { this.tank1.Direction = 1; this.tank1.Horizontal += gc.TankSpeed; Point point3 = new Point(this.tank1.Horizontal, this.tank1.Vertical); this.redtank.Location = point3; } break; } switch (this.tank2.Direction) { case 0: // Move tank up this.redtank.Image = this.redimage0; if (this.tank2.Vertical >= 0) { this.tank2.Vertical -= gc.TankSpeed; Point point1 = new Point(this.tank2.Horizontal, this.tank2.Vertical); this.redtank.Location = point1; } else // Recoil { this.tank2.Direction = 2; this.tank2.Vertical += gc.TankSpeed; Point point3 = new Point(this.tank2.Horizontal, this.tank2.Vertical); this.redtank.Location = point3; } break; case 1: // Move tank right this.redtank.Image = this.redimage1; if (this.tank2.Horizontal <= gc.GameWidth) { this.tank2.Horizontal += gc.TankSpeed; Point point2 = new Point(this.tank2.Horizontal, this.tank2.Vertical); this.redtank.Location = point2; } else // Recoil { this.tank2.Direction = 3; this.tank2.Horizontal -= gc.TankSpeed; Point point3 = new Point(this.tank2.Horizontal, this.tank2.Vertical); this.redtank.Location = point3; } break; case 2: // Move tank down this.redtank.Image = this.redimage2; if (this.tank2.Vertical <= gc.GameHeight) { this.tank2.Vertical += gc.TankSpeed; Point point3 = new Point(this.tank2.Horizontal, this.tank2.Vertical); this.redtank.Location = point3; } else // Recoil { this.tank2.Direction = 0; this.tank2.Vertical -= gc.TankSpeed; Point point3 = new Point(tank2.Horizontal, tank2.Vertical); this.redtank.Location = point3; } break; case 3: // Move tank left this.redtank.Image = this.redimage3; if (this.tank2.Horizontal >= 0) { this.tank2.Horizontal -= gc.TankSpeed; Point point4 = new Point(this.tank2.Horizontal, this.tank2.Vertical); this.redtank.Location = point4; } else // Recoil { this.tank2.Direction = 1; this.tank2.Horizontal += gc.TankSpeed; Point point3 = new Point(this.tank2.Horizontal, this.tank2.Vertical); this.redtank.Location = point3; } break; } // Get current direction, Move bullet switch (this.bull1.Direction) { case 0: if (this.bull1.Vertical >= 0) { this.bull1.Vertical -= gc.BulletSpeed; Point point = new Point(this.bull1.Horizontal, this.bull1.Vertical); this.bullet1.Location = point; } else { this.bull1.Vertical = -100; this.bull1.Horizontal = -100; this.bull1.Direction = 0; Point point = new Point(this.bull1.Horizontal, this.bull1.Vertical); this.bullet1.Location = point; this.tank1.CanFire = true; } break; case 1: if (this.bull1.Horizontal <= gc.GameWidth) { this.bull1.Horizontal += gc.BulletSpeed; Point point = new Point(this.bull1.Horizontal, this.bull1.Vertical); this.bullet1.Location = point; } else { this.bull1.Vertical = -100; this.bull1.Horizontal = -100; this.bull1.Direction = 0; Point point = new Point(this.bull1.Horizontal, this.bull1.Vertical); this.bullet1.Location = point; this.tank1.CanFire = true; } break; case 2: if (this.bull1.Vertical <= gc.GameHeight) { this.bull1.Vertical += gc.BulletSpeed; Point point = new Point(this.bull1.Horizontal, this.bull1.Vertical); this.bullet1.Location = point; } else { this.bull1.Vertical = -100; this.bull1.Horizontal = -100; this.bull1.Direction = 0; Point point = new Point(this.bull1.Horizontal, this.bull1.Vertical); this.bullet1.Location = point; this.tank1.CanFire = true; } break; case 3: if (this.bull1.Horizontal >= 0) { this.bull1.Horizontal -= gc.BulletSpeed; Point point = new Point(this.bull1.Horizontal, this.bull1.Vertical); this.bullet1.Location = point; } else { this.bull1.Vertical = -100; this.bull1.Horizontal = -100; this.bull1.Direction = 0; Point point = new Point(this.bull1.Horizontal, this.bull1.Vertical); this.bullet1.Location = point; this.tank1.CanFire = true; } break; } switch (this.bull2.Direction) { case 0: if (this.bull2.Vertical >= 0) { this.bull2.Vertical -= gc.BulletSpeed; Point point = new Point(this.bull2.Horizontal, this.bull2.Vertical); this.bullet2.Location = point; } else { this.bull2.Vertical = -100; this.bull2.Horizontal = -100; this.bull2.Direction = 0; Point point = new Point(this.bull2.Horizontal, this.bull2.Vertical); this.bullet2.Location = point; this.tank2.CanFire = true; } break; case 1: if (this.bull2.Horizontal <= gc.GameWidth) { this.bull2.Horizontal += gc.BulletSpeed; Point point = new Point(this.bull2.Horizontal, this.bull2.Vertical); this.bullet2.Location = point; } else { this.bull2.Vertical = -100; this.bull2.Horizontal = -100; this.bull2.Direction = 0; Point point = new Point(this.bull2.Horizontal, this.bull2.Vertical); this.bullet2.Location = point; this.tank2.CanFire = true; } break; case 2: if (this.bull2.Vertical <= gc.GameHeight) { this.bull2.Vertical += gc.BulletSpeed; Point point = new Point(this.bull2.Horizontal, this.bull2.Vertical); this.bullet2.Location = point; } else { this.bull2.Vertical = -100; this.bull2.Horizontal = -100; this.bull2.Direction = 0; Point point = new Point(this.bull2.Horizontal, this.bull2.Vertical); this.bullet2.Location = point; this.tank2.CanFire = true; } break; case 3: if (this.bull2.Horizontal >= 0) { this.bull2.Horizontal -= gc.BulletSpeed; Point point = new Point(this.bull2.Horizontal, this.bull2.Vertical); this.bullet2.Location = point; } else { this.bull2.Vertical = -100; this.bull2.Horizontal = -100; this.bull2.Direction = 0; Point point = new Point(this.bull2.Horizontal, this.bull2.Vertical); this.bullet2.Location = point; this.tank2.CanFire = true; } break; } Refresh(); }
private void Form2_KeyDown(object sender, KeyEventArgs e) { GameConstants gc = new GameConstants(); switch (e.KeyData) { case Keys.W: // Tank faces up this.tank1.Direction = 0; this.greentank.Image = this.greenimage0; break; case Keys.D: // Tank faces right this.tank1.Direction = 1; this.greentank.Image = this.greenimage1; break; case Keys.S: // Tank faces down this.tank1.Direction = 2; this.greentank.Image = this.greenimage2; break; case Keys.A: // Tank faces left this.tank1.Direction = 3; this.greentank.Image = this.greenimage3; break; case Keys.F: // Bullet appears if (this.tank1.Direction == 0 && this.tank1.CanFire == true) { Point point1 = new Point( this.tank1.Horizontal + (gc.BulletWidth / 2), this.tank1.Vertical - gc.BulletHeight); this.bullet1.Location = point1; this.bull1.Vertical = this.tank1.Vertical - gc.BulletHeight; this.bull1.Horizontal = this.tank1.Horizontal + (gc.BulletWidth / 2); this.bull1.Direction = 0; this.tank1.CanFire = false; } else if (this.tank1.Direction == 1 && this.tank1.CanFire == true) { Point point2 = new Point(this.tank1.Horizontal + gc.ImageWidth, this.tank1.Vertical + (gc.BulletWidth / 2)); this.bullet1.Location = point2; this.bull1.Vertical = (this.tank1.Vertical + (gc.BulletHeight / 2)); this.bull1.Horizontal = (this.tank1.Horizontal + gc.ImageWidth); this.bull1.Direction = 1; this.tank1.CanFire = false; } else if (this.tank1.Direction == 2 && this.tank1.CanFire == true) { Point point3 = new Point( this.tank1.Horizontal + (gc.BulletWidth / 2), this.tank1.Vertical + gc.ImageHeight); this.bullet1.Location = point3; this.bull1.Vertical = (this.tank1.Vertical + gc.ImageHeight); this.bull1.Horizontal = (this.tank1.Horizontal + (gc.BulletWidth / 2)); this.bull1.Direction = 2; this.tank1.CanFire = false; } else if (this.tank1.Direction == 3 && this.tank1.CanFire == true) { Point point4 = new Point(this.tank1.Horizontal - gc.BulletWidth, this.tank1.Vertical + (gc.BulletWidth / 2)); this.bullet1.Location = point4; this.bull1.Vertical = (this.tank1.Vertical + (gc.BulletWidth / 2)); this.bull1.Horizontal = (this.tank1.Horizontal - gc.BulletWidth); this.bull1.Direction = 3; this.tank1.CanFire = false; } break; case Keys.I: // Tank faces up this.tank2.Direction = 0; this.redtank.Image = this.redimage0; break; case Keys.L: // Tank faces right this.tank2.Direction = 1; this.redtank.Image = this.redimage1; break; case Keys.K: // Tank faces down this.tank2.Direction = 2; this.redtank.Image = this.redimage2; break; case Keys.J: // Tank faces left this.tank2.Direction = 3; this.redtank.Image = this.redimage3; break; case Keys.H: // Bullet appears if (this.tank2.Direction == 0 && this.tank2.CanFire == true) { Point point1 = new Point( this.tank2.Horizontal + (gc.BulletWidth / 2), this.tank2.Vertical - gc.BulletHeight); this.bullet2.Location = point1; this.bull2.Vertical = this.tank2.Vertical - gc.BulletHeight; this.bull2.Horizontal = this.tank2.Horizontal + (gc.BulletWidth / 2); this.bull2.Direction = 0; this.tank2.CanFire = false; } else if (this.tank2.Direction == 1 && this.tank2.CanFire == true) { Point point2 = new Point(this.tank2.Horizontal + gc.ImageWidth, this.tank2.Vertical + (gc.BulletHeight / 2)); this.bullet2.Location = point2; this.bull2.Vertical = (this.tank2.Vertical + (gc.BulletHeight / 2)); this.bull2.Horizontal = (this.tank2.Horizontal + gc.ImageWidth); this.bull2.Direction = 1; this.tank2.CanFire = false; } else if (this.tank2.Direction == 2 && this.tank2.CanFire == true) { Point point3 = new Point(this.tank2.Horizontal + (gc.BulletWidth / 2), this.tank2.Vertical + gc.ImageHeight); this.bullet2.Location = point3; this.bull2.Vertical = (this.tank2.Vertical + gc.ImageHeight); this.bull2.Horizontal = (this.tank2.Horizontal + (gc.BulletWidth / 2)); this.bull2.Direction = 2; this.tank2.CanFire = false; } else if (tank2.Direction == 3 && tank2.CanFire == true) { Point point4 = new Point( this.tank2.Horizontal - gc.BulletWidth, this.tank2.Vertical + (gc.BulletWidth / 2)); this.bullet2.Location = point4; this.bull2.Vertical = (this.tank2.Vertical + (gc.BulletWidth / 2)); this.bull2.Horizontal = (this.tank2.Horizontal - gc.BulletWidth); this.bull2.Direction = 3; this.tank2.CanFire = false; } break; } }