private void gameTimer_Tick(object sender, EventArgs e) { if (leftArrowDown) { if (p1X > 100) { p1X -= 5; if (p1Width > 0) { p1Width *= -1; p1X -= p1Width / 2; } } else { trueX--; foreach (Ground b in floorTiles) { b.x++; } foreach (Platform b in platforms) { b.x++; } } } if (rightArrowDown) { if (p1X < this.Width - 200) { p1X += 5; if (p1Width < 0) { p1Width *= -1; p1X -= p1Width / 2; } } else { trueX++; foreach (Ground b in floorTiles) { b.x--; } foreach (Platform b in platforms) { b.x--; } } } if (upArrowDown) { if (grounded) { jump = true; p1YStored = p1Y; } if (p1Y + 150 > p1YStored && jump) { if (p1YStored - p1Y > 90) { p1Y -= 8; } else if (p1YStored - p1Y > 50) { p1Y -= 12; } else { p1Y -= 16; } } else if (p1Y + 150 <= p1YStored) { jump = false; } } if (count > 200 && this.BackColor != Color.LightCyan) { this.BackColor = Color.LightCyan; } if (floorTiles.Count < 50) { Ground newG = new Ground(gX, gY, gW, gH); gX += gW; floorTiles.Add(newG); } if (trueX == 0 && platforms.Count < 5) { Platform newP = new Platform(pX, pY, pW, pH, platforms.Count); pX += gW; platforms.Add(newP); } //Remove boxes from left but probaby wont use so going back is possible. //int index = floorTiles.FindIndex(b => b.x < -30); //if (index >= 0) //{ // //TODO - remove box if it has gone of screen // floorTiles.RemoveAt(index); //} p1Bot = new Rectangle(p1X, p1Y + p1Height, 30, 1); p1Top = new Rectangle(p1X, p1Y, 30, 1); foreach (Ground b in floorTiles) { Rectangle coFloor = new Rectangle(b.x, b.y, b.width, b.height); if (p1Bot.IntersectsWith(coFloor)) { grounded = true; break; } else { grounded = false; } } foreach (Platform p in platforms) { Rectangle coPlat = new Rectangle(p.x, p.y, p.width, p.height); //delete block on hit (needs fix) //if (p1Top.IntersectsWith(coPlat)) //{ // platforms.RemoveAt(0); // break; //} if (p1Bot.IntersectsWith(coPlat)) { grounded = true; break; } } if (!grounded) { if (p1YStored - p1Y > 90) { p1Y += 4; } else if (p1YStored - p1Y > 50) { p1Y += 6; } else { p1Y += 8; } } if (p1Y > this.Height) { lives--; dead(); } count++; Refresh(); }
//Everything that happens every tick of the game will run here (game engine) private void gameTimer_Tick(object sender, EventArgs e) { //Code to move the player and object respectively based on user input #region Movement if (leftArrowDown) { if (left) { if (p1X > 100) { p1X -= 5; if (p1Width > 0) { p1Width *= -1; p1X -= p1Width / 2; } } else { trueX -= 5; backX++; foreach (Goonba g in goons) { g.RX += 5; g.LX += 5; } foreach (Ground b in floorTiles) { b.x += 5; } foreach (Platform b in platforms) { b.x += 5; } foreach (Koopa b in koopas) { b.x += 5; } } } } if (rightArrowDown) { if (right) { if (p1X < this.Width - 200) { p1X += 5; if (p1Width < 0) { p1Width *= -1; p1X -= p1Width / 2; } } else { trueX += 5; backX--; foreach (Goonba g in goons) { g.RX -= 5; g.LX -= 5; } foreach (Ground b in floorTiles) { b.x -= 5; } foreach (Platform b in platforms) { b.x -= 5; } foreach (Koopa b in koopas) { b.x -= 5; } } } } if (upArrowDown) { if (grounded) { jump = true; p1YStored = p1Y; } if (p1Y + 150 > p1YStored && jump) { if (p1YStored - p1Y > 90) { p1Y -= 8; } else if (p1YStored - p1Y > 50) { p1Y -= 12; } else { p1Y -= 16; } } else if (p1Y + 150 <= p1YStored) { jump = false; } } left = right = true; #endregion //Invinvibility Frames if (iStore > 0) { iStore--; } //Coins to Life if (coins >= 100) { if (coins - oldCoins >= 100) { lives++; oldCoins = coins; } } //Change color at start if (count > 200 && this.BackColor != Color.DeepSkyBlue) { this.BackColor = Color.DeepSkyBlue; } //Creates the floor before the games begins #region Floor generation while (floorTiles.Count < 50) { Ground newG = new Ground(gX, gY, gW, gH); gX += gW; floorTiles.Add(newG); } while (floorTiles.Count < 80) { Ground newG = new Ground(gX + 200, gY, gW, gH); gX += gW; floorTiles.Add(newG); } while (floorTiles.Count < 130) { Ground newG = new Ground(gX + 400, gY, gW, gH); gX += gW; floorTiles.Add(newG); } #endregion //Creates platforms before the games begin also uses a random to genereate some mystery blocks #region Platform generation while (pSpawned < 5) { if (mysteryChance.Next(0, 5) == 1) { isMyst = true; } else { isMyst = false; } Platform newP = new Platform(pX + (gW * pSpawned), pY, pW, pH, isMyst); platforms.Add(newP); pSpawned++; } while (pSpawned < 7) { if (mysteryChance.Next(0, 5) == 1) { isMyst = true; } else { isMyst = false; } Platform newP = new Platform(pX + (gW * pSpawned) + 200, pY, pW, pH, isMyst); platforms.Add(newP); pSpawned++; } while (pSpawned < 11) { if (mysteryChance.Next(0, 5) == 1) { isMyst = true; } else { isMyst = false; } Platform newP = new Platform(pX + (gW * pSpawned) + 600, pY, pW, pH, isMyst); platforms.Add(newP); pSpawned++; } //Note: ground platforms, no randoms. while (pSpawned < 16) { isMyst = false; Platform newP = new Platform(pX + (gW * pSpawned) + 820, this.Height - 60, pW, pH, isMyst); platforms.Add(newP); pSpawned++; } while (pSpawned < 20) { isMyst = false; Platform newP = new Platform(pX + (gW * pSpawned) + 700, this.Height - 90, pW, pH, isMyst); platforms.Add(newP); pSpawned++; } while (pSpawned < 25) { isMyst = false; Platform newP = new Platform(pX + (gW * pSpawned) + 900, this.Height - 60, pW, pH, isMyst); platforms.Add(newP); pSpawned++; } while (pSpawned < 29) { isMyst = false; Platform newP = new Platform(pX + (gW * pSpawned) + 750, this.Height - 90, pW, pH, isMyst); platforms.Add(newP); pSpawned++; } while (pSpawned < 36) { isMyst = false; Platform newP = new Platform(pX + (gW * pSpawned) + 2420, this.Height - 60, pW, pH, isMyst); platforms.Add(newP); pSpawned++; } while (pSpawned < 42) { isMyst = false; Platform newP = new Platform(pX + (gW * pSpawned) + 2240, this.Height - 90, pW, pH, isMyst); platforms.Add(newP); pSpawned++; } while (pSpawned < 47) { isMyst = false; Platform newP = new Platform(pX + (gW * pSpawned) + 2090, this.Height - 120, pW, pH, isMyst); platforms.Add(newP); pSpawned++; } while (pSpawned < 51) { isMyst = false; Platform newP = new Platform(pX + (gW * pSpawned) + 1970, this.Height - 150, pW, pH, isMyst); platforms.Add(newP); pSpawned++; } while (pSpawned < 54) { isMyst = false; Platform newP = new Platform(pX + (gW * pSpawned) + 1880, this.Height - 180, pW, pH, isMyst); platforms.Add(newP); pSpawned++; } while (pSpawned < 56) { isMyst = false; Platform newP = new Platform(pX + (gW * pSpawned) + 1820, this.Height - 210, pW, pH, isMyst); platforms.Add(newP); pSpawned++; } while (pSpawned < 57) { isMyst = false; Platform newP = new Platform(pX + (gW * pSpawned) + 1790, this.Height - 240, pW, pH, isMyst); platforms.Add(newP); pSpawned++; } while (pSpawned < 62) { if (mysteryChance.Next(0, 5) == 1) { isMyst = true; } else { isMyst = false; } Platform newP = new Platform(pX + (gW * pSpawned), pY, pW, pH, isMyst); platforms.Add(newP); pSpawned++; } while (pSpawned < 66) { if (mysteryChance.Next(0, 5) == 1) { isMyst = true; } else { isMyst = false; } Platform newP = new Platform(pX + (gW * pSpawned) + 200, pY - 60, pW, pH, isMyst); platforms.Add(newP); pSpawned++; } while (pSpawned < 71) { if (mysteryChance.Next(0, 5) == 1) { isMyst = true; } else { isMyst = false; } Platform newP = new Platform(pX + (gW * pSpawned) + 800, pY - 60, pW, pH, isMyst); platforms.Add(newP); pSpawned++; } while (pSpawned < 75) { if (mysteryChance.Next(0, 5) == 1) { isMyst = true; } else { isMyst = false; } Platform newP = new Platform(pX + (gW * pSpawned) + 980, pY, pW, pH, isMyst); platforms.Add(newP); pSpawned++; } #endregion //Spawns the specified enemy at specified loaction uses the spawned int so dead enemies dont come back #region Enemy Spawner if (baSpawned < 1) { Goonba newG = new Goonba(baX, baY, baWidth, baHeight, 400, 200, baDead); goons.Add(newG); baSpawned++; } if (baSpawned < 2) { baX = 650; Goonba newG = new Goonba(baX, baY, baWidth, baHeight, 800, 630, baDead); goons.Add(newG); baSpawned++; } if (baSpawned < 3) { baX = 1050; Goonba newG = new Goonba(baX, baY, baWidth, baHeight, 1200, 1030, baDead); goons.Add(newG); baSpawned++; } if (baSpawned < 4) { baX = 2050; Goonba newG = new Goonba(baX, baY, baWidth, baHeight, 2200, 2030, baDead); goons.Add(newG); baSpawned++; } if (baSpawned < 5) { baX = 2950; Goonba newG = new Goonba(baX, baY, baWidth, baHeight, 3000, 2930, baDead); goons.Add(newG); baSpawned++; } if (baSpawned < 6) { baX = 3150; Goonba newG = new Goonba(baX, baY, baWidth, baHeight, 3200, 3130, baDead); goons.Add(newG); baSpawned++; } if (koSpawned < 1) { koSpawned++; Koopa newK = new Koopa(koX, koY, koWidth, koHeight, 1, false); koopas.Add(newK); } if (koSpawned < 2) { koSpawned++; Koopa newK = new Koopa(koX + 1600, koY, koWidth, koHeight, 1, false); koopas.Add(newK); } if (koSpawned < 3) { koSpawned++; Koopa newK = new Koopa(koX + 2000, koY, koWidth, koHeight, 1, false); koopas.Add(newK); } #endregion //Collisions with player and other objects #region Collisions p1Bot = new Rectangle(p1X + (p1Width / 2), p1Y + p1Height, 15, 1); p1Top = new Rectangle(p1X + (p1Width / 2), p1Y, 15, 1); p1L = new Rectangle(p1X + (p1Width / 3), p1Y + (p1Height / 2) - 20, 1, 50); p1R = new Rectangle(p1X + p1Width - (p1Width / 3), p1Y + (p1Height / 2) - 20, 1, 50); foreach (Ground b in floorTiles) { Rectangle coFloor = new Rectangle(b.x, b.y, b.width, b.height); if (p1Bot.IntersectsWith(coFloor)) { grounded = true; break; } else { grounded = false; } } foreach (Platform p in platforms) { //Note: Consider adding block break if time. Rectangle coPlat = new Rectangle(p.x, p.y, p.width, p.height); if (p1Bot.IntersectsWith(coPlat)) { grounded = true; foreach (Platform b in platforms) { Rectangle coPlat2 = new Rectangle(b.x, b.y, b.width, b.height); if (p1R.IntersectsWith(coPlat2) || p1L.IntersectsWith(coPlat2)) { if (p1Width > 0) { right = false; } else { left = false; } break; } } break; } if (p1Top.IntersectsWith(coPlat)) { jump = false; if (p.isMyst && p.state != 2) { p.state = 2; p.randItem(mysteryChance.Next(0, 10)); coins += p.coins; if (p.item == "star") { pState = 2; } if (p.item == "life") { lives++; } } break; } if (p1R.IntersectsWith(coPlat) || p1L.IntersectsWith(coPlat)) { if (p1Width > 0) { right = false; } else { left = false; } break; } } if (!grounded) { if (p1YStored - p1Y > 90) { p1Y += 4; } else if (p1YStored - p1Y > 50) { p1Y += 6; } else { p1Y += 8; } } if (p1Y > this.Height) { dead(); } foreach (Goonba g in goons) { Rectangle goon = new Rectangle(g.x, g.y, g.width, g.height); if (baDir) { if (g.x > g.RX) { baDir = !baDir; } else { g.moveGoon(g, baDir); } } else { if (g.x < g.LX) { baDir = !baDir; } else { g.moveGoon(g, baDir); } } if (p1Bot.IntersectsWith(goon)) { bounce(); g.dead = true; } if (p1L.IntersectsWith(goon) || p1R.IntersectsWith(goon) || p1Top.IntersectsWith(goon)) { dead(); } } foreach (Koopa k in koopas) { Rectangle koop = new Rectangle(); k.attack(); if (k.facingR) { if (k.state == 2) { koop = new Rectangle(k.x, k.y + (k.height / 2), k.width, k.height / 2); } else { koop = new Rectangle(k.x, k.y, k.width, k.height); } } else { if (k.state == 2) { koop = new Rectangle(k.x - k.width, k.y + (k.height / 2), k.width, k.height / 2); } else { koop = new Rectangle(k.x - k.width, k.y, k.width, k.height); } } if (p1Bot.IntersectsWith(koop)) { if (k.state == 1) { k.state = 2; bounce(); break; } else { k.dead = true; bounce(); break; } } if (p1L.IntersectsWith(koop) || p1R.IntersectsWith(koop) || p1Top.IntersectsWith(koop) && k.state == 1) { dead(); } } Bullet.shoot(); foreach (Bullet b in Koopa.bullets) { Rectangle bull = new Rectangle(b.x, b.y, b.width, b.height); if (p1Bot.IntersectsWith(bull)) { b.dead = true; bounce(); } if (p1L.IntersectsWith(bull) || p1R.IntersectsWith(bull) || p1Top.IntersectsWith(bull)) { dead(); } } #endregion //If an enemy is dead, find and remove it from the list #region kill/break int index = goons.FindIndex(g => g.dead == true); if (index >= 0) { goons.RemoveAt(index); } int index2 = Koopa.bullets.FindIndex(g => g.dead == true); if (index2 >= 0) { Koopa.bullets.RemoveAt(index2); } int index3 = koopas.FindIndex(k => k.dead == true); if (index3 >= 0) { koopas.RemoveAt(index3); } #endregion //If touching the location of the flag, win. if (p1X >= floorTiles[0].x + 3800 && p1X <= floorTiles[0].x + 3805 && p1Y >= this.Height - 250) { win(); } //Add one to the counter used for timing logic count++; //Refresh the gamescreen Refresh(); }