private void GameScreen_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { //if key is pressed boolean is true switch (e.KeyCode) { case Keys.Left: leftArrowDown = true; break; case Keys.Right: rightArrowDown = true; break; case Keys.Up: upArrowDown = true; break; case Keys.Down: downArrowDown = true; break; case Keys.N: nDown = true; break; case Keys.B: bDown = true; break; case Keys.M: mDown = true; break; case Keys.Space: spaceDown = true; break; case Keys.Escape: //return to main screen MainScreen ms = new MainScreen(); Form f = this.FindForm(); ms.Location = new Point((f.Width - ms.Width) / 2, (f.Height - ms.Height) / 2); f.Controls.Remove(this); ms.Size = f.Size; f.Controls.Add(ms); ms.Focus(); break; } }
private void InstructionScreen_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { //if key pressed switch (e.KeyCode) { case Keys.Escape: //close program Application.Exit(); break; case Keys.Space: //go to MainScreen MainScreen ms = new MainScreen(); Form f = this.FindForm(); ms.Location = new Point((f.Width - ms.Width) / 2, (f.Height - ms.Height) / 2); f.Controls.Remove(this); ms.Size = f.Size; f.Controls.Add(ms); ms.Focus(); break; } }
private void HighScoreScreen_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { //when key is pressed switch (e.KeyCode) { case Keys.W: //so that at Z returns to A if (upDown == 26) { upDown = 1; } else { upDown++; } Initials(); break; case Keys.D: if (leftRight == 3) { leftRight = 1; } else { leftRight++; } Initials(); break; case Keys.A: if (leftRight == 1) { leftRight = 3; } else { leftRight--; } Initials(); break; case Keys.S: if (upDown == 1) { upDown = 26; } else { upDown--; } Initials(); break; case Keys.Escape: //close program Application.Exit(); break; case Keys.Space: //if focused on screen or enough time has passed if (myWatch.ElapsedMilliseconds > 2000 || focus == true) { //stop watch myWatch.Stop(); try { //if saving high score //add new high score object (your initials, your score) scores.Remove(hs); hs = new HighScore(yourName1.Text + yourName2.Text + yourName3.Text, Convert.ToInt16(yourScore.Text)); scores.Add(hs); scores = scores.OrderByDescending(x => x.score).ToList(); List <string> tempList = new List <string>(); // Add all info from each HighScore object to temp list foreach (HighScore hs in scores) { tempList.Add(hs.name); tempList.Add(Convert.ToString(hs.score)); } //upload updated scores to text file File.WriteAllLines("Score.txt", tempList); } catch { //if viewing high scores } //return to main screen MainScreen ms = new MainScreen(); Form f = this.FindForm(); ms.Location = new Point((f.Width - ms.Width) / 2, (f.Height - ms.Height) / 2); f.Controls.Remove(this); ms.Size = f.Size; f.Controls.Add(ms); ms.Focus(); } break; } }
private void gameLoop_Tick(object sender, EventArgs e) { //each timer tick //if player reaches winning XP if (p1.xp >= 8000) { Form f = this.FindForm(); if (f != null) { //stop game gameLoop.Enabled = false; //play sound gameOver.Play(); //2s pause this.Refresh(); Thread.Sleep(2000); //go to main screen f.Controls.Remove(this); MainScreen ms = new MainScreen(); ms.Location = new Point((f.Width - ms.Width) / 2, (f.Height - ms.Height) / 2); ms.Size = f.Size; f.Controls.Add(ms); ms.Focus(); //display winner ms.Winner(1); return; } } if (p2.xp >= 8000) { Form f = this.FindForm(); if (f != null) { gameLoop.Enabled = false; gameOver.Play(); this.Refresh(); Thread.Sleep(2000); MainScreen ms = new MainScreen(); ms.Location = new Point((f.Width - ms.Width) / 2, (f.Height - ms.Height) / 2); f.Controls.Remove(this); ms.Size = f.Size; f.Controls.Add(ms); ms.Focus(); ms.Winner(2); } } //check if can upgrade attribute if (p1.boost != p1.lvl) { boost1 = true; } if (p2.boost != p2.lvl) { boost2 = true; } //shape generation (same as OnStart) addShape = true; if (randGen.Next(1, squareGenerationSpeed) < 7) { shapeX = randGen.Next(1, this.Width - squareSize - 1); shapeY = randGen.Next(1, this.Height - squareSize - 1); newSquare = new Rectangle(shapeX, shapeY, squareSize, squareSize); foreach (Square s in squareList) { r = new Rectangle(s.x - 1, s.y - 1, s.size + 1, s.size + 1); if (r.IntersectsWith(newSquare)) { addShape = false; } } foreach (Hexagon h in hexagonList) { r = new Rectangle(h.x - 1, h.y - 1, h.size + 1, h.size + 1); if (r.IntersectsWith(newSquare)) { addShape = false; } } if (addShape == true) { Square f = new Square(shapeX, shapeY, squareSize, squareHp, squareDamage); squareList.Add(f); } } else if (randGen.Next(1, squareGenerationSpeed) == 11) { shapeX = randGen.Next(1, this.Width - hexagonSize - 1); shapeY = randGen.Next(1, this.Height - hexagonSize - 1); newSquare = new Rectangle(shapeX, shapeY, hexagonSize, hexagonSize); foreach (Square s in squareList) { r = new Rectangle(s.x - 1, s.y - 1, s.size + 1, s.size + 1); if (r.IntersectsWith(newSquare)) { addShape = false; } } foreach (Hexagon h in hexagonList) { r = new Rectangle(h.x - 1, h.y - 1, h.size + 1, h.size + 1); if (r.IntersectsWith(newSquare)) { addShape = false; } } if (addShape == true) { Hexagon f = new Hexagon(shapeX, shapeY, hexagonSize, hexagonHp, hexagonDamage); hexagonList.Add(f); //increase shape generation speed if (squareGenerationSpeed > 100) { squareGenerationSpeed -= 3; } } } else if (randGen.Next(1, squareGenerationSpeed) == 12) { shapeX = randGen.Next(1, this.Width - triangleSize - 1); shapeY = randGen.Next(1, this.Height - triangleSize - 1); newSquare = new Rectangle(shapeX, shapeY, triangleSize, triangleSize); foreach (Square s in squareList) { r = new Rectangle(s.x - 1, s.y - 1, s.size + 1, s.size + 1); if (r.IntersectsWith(newSquare)) { addShape = false; } } foreach (Hexagon h in hexagonList) { r = new Rectangle(h.x - 1, h.y - 1, h.size + 1, h.size + 1); if (r.IntersectsWith(newSquare)) { addShape = false; } } if (addShape == true) { Triangle t = new Triangle(shapeX, shapeY, triangleSize, triangleHp, triangleDamage, 3, "none"); triangleList.Add(t); } } //check if players collide p1.Collision(p2); foreach (Bullet b in bulletList) { //bullet move method b.Move(b.direction); //square collision method foreach (Square s in squareList) { b.SquareCollision(s, p1, p2); } //hexagon collision method foreach (Hexagon h in hexagonList) { b.HexagonCollision(h, p1, p2); } //triangle collision method foreach (Triangle t in triangleList) { b.TriangleCollision(t); } //give xp to shooter if kills opponent if (b.shooter == 2) { p2.xp += b.Collision(p1); } else { p1.xp += b.Collision(p2); } //if bullet runs out of health or hits wall it gets removed if (b.hp <= 0 || b.x < 0 || b.x > this.Width - b.size || b.y < 0 || b.y > this.Height - b.size) { deadBulletList.Add(b); } } foreach (Bot p in botList) { //move method for players if (p.playerNumber == 1) { if (leftArrowDown) { p.Move("left", this); } if (rightArrowDown) { p.Move("right", this); } if (upArrowDown) { p.Move("up", this); } if (downArrowDown) { p.Move("down", this); } } else { if (aArrowDown) { p.Move("left", this); } if (dArrowDown) { p.Move("right", this); } if (wArrowDown) { p.Move("up", this); } if (sArrowDown) { p.Move("down", this); } } //shape collision methods foreach (Square s in squareList) { p.SquareCollision(s); } foreach (Hexagon h in hexagonList) { p.HexagonCollision(h); } foreach (Triangle t in triangleList) { p.TriangleCollision(t); } //determine player lvl using xp if (p.xp >= 7500) { p.lvl = 20; } else if (p.xp >= 6500) { lvlUpgrade = 7500 - p.xp; p.lvl = 19; } else if (p.xp >= 5550) { lvlUpgrade = 6500 - p.xp; p.lvl = 18; } else if (p.xp >= 4650) { lvlUpgrade = 5550 - p.xp; p.lvl = 17; } else if (p.xp >= 3800) { lvlUpgrade = 4650 - p.xp; p.lvl = 16; } else if (p.xp >= 3000) { lvlUpgrade = 3800 - p.xp; p.lvl = 15; } else if (p.xp >= 2250) { lvlUpgrade = 3000 - p.xp; p.lvl = 14; } else if (p.xp >= 1700) { lvlUpgrade = 2250 - p.xp; p.lvl = 13; } else if (p.xp >= 1300) { lvlUpgrade = 1700 - p.xp; p.lvl = 12; } else if (p.xp >= 1000) { lvlUpgrade = 1300 - p.xp; p.lvl = 11; } else if (p.xp >= 750) { lvlUpgrade = 1000 - p.xp; p.lvl = 10; } else if (p.xp >= 550) { lvlUpgrade = 750 - p.xp; p.lvl = 9; } else if (p.xp >= 400) { lvlUpgrade = 550 - p.xp; p.lvl = 8; } else if (p.xp >= 280) { lvlUpgrade = 400 - p.xp; p.lvl = 7; } else if (p.xp >= 180) { lvlUpgrade = 280 - p.xp; p.lvl = 6; } else if (p.xp >= 110) { lvlUpgrade = 180 - p.xp; p.lvl = 5; } else if (p.xp >= 60) { lvlUpgrade = 110 - p.xp; p.lvl = 4; } else if (p.xp >= 30) { lvlUpgrade = 60 - p.xp; p.lvl = 3; } else if (p.xp >= 10) { lvlUpgrade = 30 - p.xp; p.lvl = 2; } else { lvlUpgrade = 10 - p.xp; p.lvl = 1; } if (p.playerNumber == 1) { lvl1Upgrade = lvlUpgrade; } else { lvl2Upgrade = lvlUpgrade; } } foreach (Triangle t in triangleList) { //Manhattan distance between triangle and players d1x = (t.x - p1.x); d1y = (t.y - p1.y); d1t = Math.Abs(d1x) + Math.Abs(d1y); d2x = (t.x - p2.x); d2y = (t.y - p2.y); d2t = Math.Abs(d2x) + Math.Abs(d2y); //follows closest player if (d2t > d1t) { if (d1x > 13) { t.Move("left"); } else if (d1x < 7) { t.Move("right"); } else if (d1y > 10) { t.Move("up"); } else { t.Move("down"); } } else { if (d2x > 13) { t.Move("left"); } else if (d2x < 7) { t.Move("right"); } else if (d2y > 10) { t.Move("up"); } else { t.Move("down"); } } //shape collision methods foreach (Square s in squareList) { t.SquareCollision(s); } foreach (Hexagon h in hexagonList) { t.HexagonCollision(h); } //if runs out of health, it gets removed if (t.hp <= 0) { deadTriangleList.Add(t); } } foreach (Square s in squareList) { if (s.hp <= 0) { deadSquareList.Add(s); } } foreach (Hexagon h in hexagonList) { if (h.hp <= 0) { deadHexagonList.Add(h); } } //remove dead objects foreach (Square s in deadSquareList) { squareList.Remove(s); } foreach (Hexagon h in deadHexagonList) { hexagonList.Remove(h); } foreach (Triangle t in deadTriangleList) { triangleList.Remove(t); } foreach (Bullet b in deadBulletList) { bulletList.Remove(b); } //if shooting and has reloaded if (spaceDown == true) { if (p1CoolDown <= 0) { //reset shoot cooldown p1CoolDown = p1.reload; //create bullet object in direction of movement if (p1.direction == "left") { Bullet b = new Bullet(p1.x - 15, p1.y + 10, 20, p1.bulletHealth, p1.bulletDamage, p1.bulletSpeed, p1.direction, 1); bulletList.Add(b); } else if (p1.direction == "right") { Bullet b = new Bullet(p1.x + p1.size - 5, p1.y + 10, 20, p1.bulletHealth, p1.bulletDamage, p1.bulletSpeed, p1.direction, 1); bulletList.Add(b); } else if (p1.direction == "down") { Bullet b = new Bullet(p1.x + 10, p1.y + p1.size - 5, 20, p1.bulletHealth, p1.bulletDamage, p1.bulletSpeed, p1.direction, 1); bulletList.Add(b); } else if (p1.direction == "up") { Bullet b = new Bullet(p1.x + 10, p1.y - 15, 20, p1.bulletHealth, p1.bulletDamage, p1.bulletSpeed, p1.direction, 1); bulletList.Add(b); } } } if (zDown == true) { if (p2CoolDown <= 0) { p2CoolDown = p2.reload; if (p2.direction == "left") { Bullet b = new Bullet(p2.x - 15, p2.y + 10, 20, p2.bulletHealth, p2.bulletDamage, p2.bulletSpeed, p2.direction, 2); bulletList.Add(b); } else if (p2.direction == "right") { Bullet b = new Bullet(p2.x + 35, p2.y + 10, 20, p2.bulletHealth, p2.bulletDamage, p2.bulletSpeed, p2.direction, 2); bulletList.Add(b); } else if (p2.direction == "down") { Bullet b = new Bullet(p2.x + 10, p2.y + 35, 20, p2.bulletHealth, p2.bulletDamage, p2.bulletSpeed, p2.direction, 2); bulletList.Add(b); } else if (p2.direction == "up") { Bullet b = new Bullet(p2.x + 10, p2.y - 15, 20, p2.bulletHealth, p2.bulletDamage, p2.bulletSpeed, p2.direction, 2); bulletList.Add(b); } } } //if a player dies if (p1.hp <= 0) { //respawn method p1.Respawn(); //reset upgrades healthLabel.ForeColor = healthRegenLabel.ForeColor = bodyDamageLabel.ForeColor = bulletSpeedLabel.ForeColor = bulletHealthLabel.ForeColor = bulletDamageLabel.ForeColor = reloadLabel.ForeColor = speedLabel.ForeColor = Color.White; } if (p2.hp <= 0) { p2.Respawn(); healthTwoLabel.ForeColor = healthRegenTwoLabel.ForeColor = bodyDamageTwoLabel.ForeColor = bulletSpeedTwoLabel.ForeColor = bulletHealthTwoLabel.ForeColor = bulletDamageTwoLabel.ForeColor = reloadTwoLabel.ForeColor = speedTwoLabel.ForeColor = Color.White; } //if time for health regen if (healthRegenCounter == 100) { foreach (Bot p in botList) { //if not at max health if (p.hp < p.maxHealth) { //will not surpass max health if (p.hp > p.maxHealth - p.healthRegen) { p.hp = p.maxHealth; } else { //increase health by health regen attribute p.hp += p.healthRegen; } } } //reset counter healthRegenCounter = 0; } //update labels health1Label.Text = Convert.ToString(p1.hp) + " hp"; health2Label.Text = Convert.ToString(p2.hp) + " hp"; level1Label.Text = "lvl " + Convert.ToString(p1.lvl); level2Label.Text = "lvl " + Convert.ToString(p2.lvl); if (p1.lvl != 20) { xp1Label.Text = Convert.ToString(lvl1Upgrade) + " xp to lvl " + Convert.ToString(p1.lvl + 1); } else { xp1Label.Text = "Max Level: " + Convert.ToString(p1.xp) + " xp"; } if (p2.lvl != 20) { xp2Label.Text = Convert.ToString(lvl2Upgrade) + " xp to lvl " + Convert.ToString(p2.lvl + 1); } else { xp2Label.Text = "Max Level: " + Convert.ToString(p1.xp) + " xp"; } //if upgrade possible, show upgrade labels if (boost1 == true) { healthLabel.Visible = true; healthRegenLabel.Visible = true; bodyDamageLabel.Visible = true; bulletSpeedLabel.Visible = true; bulletHealthLabel.Visible = true; bulletDamageLabel.Visible = true; reloadLabel.Visible = true; speedLabel.Visible = true; if (keyCoolDown1 < 0) { //swaps between attributes to upgrade if (mDown == true) { if (attribute1 < 7) { attribute1++; keyCoolDown1 = 5; Bold1Label(); } } if (bDown == true) { if (attribute1 > 0) { attribute1--; keyCoolDown1 = 5; Bold1Label(); } } //selects upgrade if (nDown == true) { keyCoolDown1 = 5; LevelUp(p1, attribute1); //upgrade labels dissapear boost1 = false; healthLabel.Visible = false; healthRegenLabel.Visible = false; bodyDamageLabel.Visible = false; bulletSpeedLabel.Visible = false; bulletHealthLabel.Visible = false; bulletDamageLabel.Visible = false; reloadLabel.Visible = false; speedLabel.Visible = false; } } } if (boost2 == true) { healthTwoLabel.Visible = true; healthRegenTwoLabel.Visible = true; bodyDamageTwoLabel.Visible = true; bulletSpeedTwoLabel.Visible = true; bulletHealthTwoLabel.Visible = true; bulletDamageTwoLabel.Visible = true; reloadTwoLabel.Visible = true; speedTwoLabel.Visible = true; if (keyCoolDown2 < 0) { if (xDown == true) { if (attribute2 < 7) { attribute2++; keyCoolDown2 = 5; Bold2Label(); } } if (cDown == true) { if (attribute2 > 0) { attribute2--; keyCoolDown2 = 5; Bold2Label(); } } if (vDown == true) { keyCoolDown2 = 5; LevelUp(p2, attribute2); boost2 = false; healthTwoLabel.Visible = false; healthRegenTwoLabel.Visible = false; bodyDamageTwoLabel.Visible = false; bulletSpeedTwoLabel.Visible = false; bulletHealthTwoLabel.Visible = false; bulletDamageTwoLabel.Visible = false; reloadTwoLabel.Visible = false; speedTwoLabel.Visible = false; } } } //counters count p1CoolDown--; p2CoolDown--; keyCoolDown1--; keyCoolDown2--; healthRegenCounter++; //clear object removal lists deadSquareList.Clear(); deadTriangleList.Clear(); deadHexagonList.Clear(); deadBulletList.Clear(); Refresh(); }