public void MainScreen_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { //if focused on screen or enough time has passed if (myWatch.ElapsedMilliseconds > 4000 || focus == true) { //stop watch myWatch.Stop(); Form f = this.FindForm(); //if key is pressed switch (e.KeyCode) { case Keys.Escape: //close program Application.Exit(); break; case Keys.N: //go to sps screen SinglePlayerScreen sps = new SinglePlayerScreen { Location = new Point(f.Location.X, f.Location.Y) }; f.Controls.Remove(this); sps.Size = f.Size; f.Controls.Add(sps); sps.Focus(); break; case Keys.B: //go to hs screen HighScoreScreen hs = new HighScoreScreen(); hs.Location = new Point((f.Width - hs.Width) / 2, (f.Height - hs.Height) / 2); f.Controls.Remove(this); hs.Size = f.Size; f.Controls.Add(hs); hs.Focus(); break; case Keys.M: //go to ins screen InstructionScreen ins = new InstructionScreen(); ins.Location = new Point((f.Width - ins.Width) / 2, (f.Height - ins.Height) / 2); f.Controls.Remove(this); ins.Size = f.Size; f.Controls.Add(ins); ins.Focus(); break; case Keys.Space: //go to game screen GameScreen gs = new GameScreen { Location = new Point(f.Location.X, f.Location.Y) }; f.Controls.Remove(this); gs.Size = f.Size; f.Controls.Add(gs); gs.Focus(); break; } } }
private void gameLoop_Tick(object sender, EventArgs e) { //each timer tick //check if can upgrade attribute if (p1.boost != p1.lvl) { boost = 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 for collision between player and bots p1.Collision(p2); p1.Collision(p3); p1.Collision(p4); 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); } //check to see if opponent bullet collides with player if (b.shooter != 1) { b.Collision(p1); } else { //if bot dies give player xp if (b.Collision(p2) != 0) { p1.xp += 50 * p2.lvl; } if (b.Collision(p3) != 0) { p1.xp += 50 * p3.lvl; } if (b.Collision(p4) != 0) { p1.xp += 50 * p4.lvl; } } //if bullet hits wall or runs out of health, 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 player 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); } } //bots move toward player else { if (p.y > p1.y + 10) { p.Move("up", this); } else if (p.y < p1.y - 10) { p.Move("down", this); } else if (p.x > p1.x + 10) { p.Move("left", this); } else { p.Move("right", 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 (p1.xp >= 7500) { p1.lvl = 20; } else if (p1.xp >= 6500) { lvlUpgrade = 7500 - p1.xp; p1.lvl = 19; } else if (p1.xp >= 5550) { lvlUpgrade = 6500 - p1.xp; p1.lvl = 18; } else if (p1.xp >= 4650) { lvlUpgrade = 5550 - p1.xp; p1.lvl = 17; } else if (p1.xp >= 3800) { lvlUpgrade = 4650 - p1.xp; p1.lvl = 16; } else if (p1.xp >= 3000) { lvlUpgrade = 3800 - p1.xp; p1.lvl = 15; } else if (p1.xp >= 2250) { lvlUpgrade = 3000 - p1.xp; p1.lvl = 14; } else if (p1.xp >= 1700) { lvlUpgrade = 2250 - p1.xp; p1.lvl = 13; } else if (p1.xp >= 1300) { lvlUpgrade = 1700 - p1.xp; p1.lvl = 12; } else if (p1.xp >= 1000) { lvlUpgrade = 1300 - p1.xp; p1.lvl = 11; } else if (p1.xp >= 750) { lvlUpgrade = 1000 - p1.xp; p1.lvl = 10; } else if (p1.xp >= 550) { lvlUpgrade = 750 - p1.xp; p1.lvl = 9; } else if (p1.xp >= 400) { lvlUpgrade = 550 - p1.xp; p1.lvl = 8; } else if (p1.xp >= 280) { lvlUpgrade = 400 - p1.xp; p1.lvl = 7; } else if (p1.xp >= 180) { lvlUpgrade = 280 - p1.xp; p1.lvl = 6; } else if (p1.xp >= 110) { lvlUpgrade = 180 - p1.xp; p1.lvl = 5; } else if (p1.xp >= 60) { lvlUpgrade = 110 - p1.xp; p1.lvl = 4; } else if (p1.xp >= 30) { lvlUpgrade = 60 - p1.xp; p1.lvl = 3; } else if (p1.xp >= 10) { lvlUpgrade = 30 - p1.xp; p1.lvl = 2; } else { lvlUpgrade = 10 - p1.xp; p1.lvl = 1; } foreach (Triangle t in triangleList) { //triangle moves towards player if (t.x - p1.x > 13) { t.Move("left"); } else if (t.x - p1.x < 7) { t.Move("right"); } else if (t.y - p1.y > 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, p1.playerNumber); 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, p1.playerNumber); 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, p1.playerNumber); bulletList.Add(b); } else { Bullet b = new Bullet(p1.x + 10, p1.y - 15, 20, p1.bulletHealth, p1.bulletDamage, p1.bulletSpeed, p1.direction, p1.playerNumber); bulletList.Add(b); } } } //if reloaded bots shoot in direction of movement 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, p2.playerNumber); bulletList.Add(b); } else if (p2.direction == "right") { Bullet b = new Bullet(p2.x + p2.size - 5, p2.y + 10, 20, p2.bulletHealth, p2.bulletDamage, p2.bulletSpeed, p2.direction, p2.playerNumber); bulletList.Add(b); } else if (p2.direction == "down") { Bullet b = new Bullet(p2.x + 10, p2.y + p2.size - 5, 20, p2.bulletHealth, p2.bulletDamage, p2.bulletSpeed, p2.direction, p2.playerNumber); bulletList.Add(b); } else { Bullet b = new Bullet(p2.x + 10, p2.y - 15, 20, p2.bulletHealth, p2.bulletDamage, p2.bulletSpeed, p2.direction, p2.playerNumber); bulletList.Add(b); } } if (p3CoolDown <= 0) { p3CoolDown = p3.reload; if (p3.direction == "left") { Bullet b = new Bullet(p3.x - 15, p3.y + 10, 20, p3.bulletHealth, p3.bulletDamage, p3.bulletSpeed, p3.direction, p3.playerNumber); bulletList.Add(b); } else if (p3.direction == "right") { Bullet b = new Bullet(p3.x + p3.size - 5, p3.y + 10, 20, p3.bulletHealth, p3.bulletDamage, p3.bulletSpeed, p3.direction, p3.playerNumber); bulletList.Add(b); } else if (p3.direction == "down") { Bullet b = new Bullet(p3.x + 10, p3.y + p3.size - 5, 20, p3.bulletHealth, p3.bulletDamage, p3.bulletSpeed, p3.direction, p3.playerNumber); bulletList.Add(b); } else { Bullet b = new Bullet(p3.x + 10, p3.y - 15, 20, p3.bulletHealth, p3.bulletDamage, p3.bulletSpeed, p3.direction, p3.playerNumber); bulletList.Add(b); } } if (p4CoolDown <= 0) { p4CoolDown = p4.reload; if (p4.direction == "left") { Bullet b = new Bullet(p4.x - 15, p4.y + 10, 20, p4.bulletHealth, p4.bulletDamage, p4.bulletSpeed, p4.direction, p4.playerNumber); bulletList.Add(b); } else if (p4.direction == "right") { Bullet b = new Bullet(p4.x + p4.size - 5, p4.y + 10, 20, p4.bulletHealth, p4.bulletDamage, p4.bulletSpeed, p4.direction, p4.playerNumber); bulletList.Add(b); } else if (p1.direction == "down") { Bullet b = new Bullet(p4.x + 10, p4.y + p4.size - 5, 20, p4.bulletHealth, p4.bulletDamage, p4.bulletSpeed, p4.direction, p4.playerNumber); bulletList.Add(b); } else { Bullet b = new Bullet(p4.x + 10, p4.y - 15, 20, p4.bulletHealth, p4.bulletDamage, p4.bulletSpeed, p4.direction, p4.playerNumber); bulletList.Add(b); } } //if run out of health if (p1.hp <= 0) { Form z = FindForm(); if (z != null) { //stop game gameLoop.Enabled = false; //play sound youLose.PlaySync(); //switch to high score screen HighScoreScreen hss = new HighScoreScreen(); hss.Location = new Point((z.Width - hss.Width) / 2, (z.Height - hss.Height) / 2); z.Controls.Remove(this); hss.Size = z.Size; z.Controls.Add(hss); hss.Focus(); //transfer high score hss.Results(p1.xp); } } //if bots die, they respawn with an improved attribute if (p2.hp <= 0) { p1.xp += p2.lvl * 50; p2.BotRespawn(randGen.Next(0, 8)); p2.x = 1520; p2.y = 40; } if (p3.hp <= 0) { p1.xp += p3.lvl * 50; p3.x = 40; p3.y = 820; p3.BotRespawn(randGen.Next(0, 8)); } if (p4.hp <= 0) { p1.xp += p4.lvl * 50; p4.x = 1520; p4.y = 820; p4.BotRespawn(randGen.Next(0, 8)); } //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"; level1Label.Text = "lvl " + Convert.ToString(p1.lvl); if (p1.lvl != 20) { xp1Label.Text = Convert.ToString(lvlUpgrade) + " xp to lvl " + Convert.ToString(p1.lvl + 1); } else { xp1Label.Text = "Max Level: " + Convert.ToString(p1.xp) + " xp"; } //if upgrade possible, show upgrade labels if (boost == 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 (keyCoolDown < 0) { //swaps between attributes to upgrade if (mDown == true) { if (attribute < 7) { attribute++; keyCoolDown = 5; BoldLabel(); } } if (bDown == true) { if (attribute > 0) { attribute--; keyCoolDown = 5; BoldLabel(); } } //selects upgrade if (nDown == true) { keyCoolDown = 5; LevelUp(); //upgrade labels dissapear boost = 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; } } } //bots do not have xp p2.xp = p3.xp = p4.xp = 0; //counters count p1CoolDown--; p2CoolDown--; p3CoolDown--; p4CoolDown--; keyCoolDown--; healthRegenCounter++; //clear object removal lists deadSquareList.Clear(); deadTriangleList.Clear(); deadHexagonList.Clear(); deadBulletList.Clear(); Refresh(); }