public override void Update(GameTime gameTime) { prevKeyState = keyState; keyState = Keyboard.GetState(); timer += 1; explostionTimer += 1; explosionAnimation.animationTick(gameTime); mineExplostionAnimation.animationTick(gameTime); missileSmoke.Update(gameTime); exLimSound.Update(gameTime); previousMouseState = currentMouseState; currentMouseState = Mouse.GetState(); mouse_x = currentMouseState.X; mouse_y = currentMouseState.Y; // Sets up mine if (currentMouseState.LeftButton == ButtonState.Pressed) { minePos.X = mouse_x; minePos.Y = mouse_y; drawMines = true; mine.active = true; mineCount = 1; } // Controls player movements if (keyState.IsKeyDown(Keys.Left)) { if (player.getPosX() > Dir.leftBoundary) { player.setPosX(player.getPosX() - 4); // Moves player 3 units left player.setFlip(SpriteEffects.FlipHorizontally); } } if (keyState.IsKeyDown(Keys.Right)) { if (player.getPosX() < (Dir.rightBoundary - (player.getWidth() - 5))) { player.setPosX(player.getPosX() + 4); // Moves player 3 units right player.setFlip(SpriteEffects.None); } } // Moves enemy sprite enemy1.setPosX(enemy1.getPosX() + Dir.rnd.Next(1, 3)); enemy2.setPosX(enemy2.getPosX() - Dir.rnd.Next(1, 3)); // If enemy runs off screen, reset position to random x postion if (enemy1.getPosX() > Dir.rightBoundary + 60) { enemy1.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); } if (enemy2.getPosX() < Dir.leftBoundary - (enemy2.getWidth() + 50)) { enemy2.setPos(Dir.rnd.Next(Dir.rightBoundary, Dir.rightBoundary + 200), Dir.rnd.Next(200, 700)); } // Shoot missile if (keyState.IsKeyDown(Keys.Space) && prevKeyState.IsKeyUp(Keys.Space)) { missile.setPos(player.getPosX() + player.getWidth() / 2, player.getPosY() - missile.getHeight()); missile.active = true; drawSmoke(); drawMissile = true; if (missileCount > 0) { missileCount--; } } if (missileCount == 0) { missileCount = 5; } if (missile.getPosY() < 185 + missile.getHeight()) { missile.setPos(player.getPos()); drawMissile = false; missileSmoke.deActivate(); } if (timer % 361 == 360) { anchor.setPos(enemy1.getPosX() + enemy1.getWidth() / 2, enemy1.getPosY() + enemy1.getHeight() / 2); anchorDraw = true; } if (anchor.getPosY() > Dir.bottomBoundary + anchor.getHeight()) { anchor.active = false; anchorDraw = false; } // Collision Control enemy1Collision = missile.collision(enemy1); enemy2Collision = missile.collision(enemy2); if (enemy1Collision) { enemy1.hitPoints = enemy1.hitPoints - 10; currXMissile = enemy1.getPosX() + enemy1.getWidth() / 2; currYMissile = enemy1.getPosY() - enemy1.getHeight() / 2; if (enemy1.hitPoints <= 0) { collision1 = true; enemy1.hitPoints = 10; Dir.enemy1Count++; missileSmoke.deActivate(); } getSetMissile(); } if (enemy2Collision) { missileSmoke.deActivate(); enemy2.hitPoints = enemy2.hitPoints - 10; drawMissile = false; if (enemy2.hitPoints <= 0) { currXMissile = enemy2.getPosX() + enemy2.getWidth() / 2; currYMissile = enemy2.getPosY() - enemy2.getHeight() / 2; collision2 = true; Dir.enemy2Count++; enemy2.hitPoints = 20; } getSetMissile(); } // Gain 10 hp if missile hits the anchor preserve bool lifeGain = missile.collision(life); if (lifeGain == true) { if (player.hitPoints < 50) { player.hitPoints = player.hitPoints + 10; } lifeGain = false; drawMissile = false; life.active = false; life.setPos(new Vector2(Dir.rnd.Next(250, 800), Dir.rnd.Next(400, 600))); missile.active = false; missile.setPos(player.getPosX(), player.getPosY()); missileSmoke.deActivate(); } bool anchorCollision = anchor.collision(player); if (anchorCollision) { anchorDraw = false; anchor.setActiveAndVisible(false); anchor.setPos(new Vector2(-100, -100)); player.hitPoints = player.hitPoints - 20; } mineCollission = mine.collision(enemy1); if (mineCollission) { mine.active = false; drawMines = false; mineCurrPos = mine.getPos(); mine.setPos(new Vector2(-100, 0)); enemy1.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); mineCollisionToggle = true; score += 150; mineCount = 0; Dir.enemy1Count += 1; exLimSound.playSoundIfOk(); } mineCollission2 = mine.collision(enemy2); if (mineCollission2) { mine.active = false; drawMines = false; mineCurrPos = mine.getPos(); mine.setPos(new Vector2(-100, 0)); enemy2.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); mineCollisionToggle2 = true; score += 200; mineCount = 0; Dir.enemy2Count += 1; exLimSound.playSoundIfOk(); } // Toggle BB if (keyState.IsKeyDown(Keys.B) && prevKeyState.IsKeyUp(Keys.B)) { showBB = !showBB; } // Pause if (keyState.IsKeyDown(Keys.P) && !prevKeyState.IsKeyDown(Keys.P)) { gameStateManager.pushLevel(3); } // Help State if (keyState.IsKeyDown(Keys.F1) && !prevKeyState.IsKeyDown(Keys.F1)) { gameStateManager.pushLevel(5); } if (player.hitPoints < 0) { player.hitPoints = 0; } // End State if (score >= 1000 || player.hitPoints <= 0) { if (timer % 181 == 180) { gameStateManager.setLevel(4); } } }
public override void Draw(GameTime gameTime) { if (score >= 1000) { status = "THE END!"; } else if (player.hitPoints == 0) { status = "YOU DEAD!"; } else { status = "Missile Left: " + missileCount; } spriteBatch.Begin(); backgroundLevel2.Draw(spriteBatch); spriteBatch.DrawString(scoreFont, "Score: " + score + "/1000", new Vector2(20, 20), Color.Black); // Draw Score on top left spriteBatch.DrawString(scoreFont, status, new Vector2(1100, 20), Color.Black); // Draw missile count spriteBatch.Draw(texMouse, new Rectangle((int)mouse_x, (int)mouse_y, 16, 16), Color.White); // draw the mouse here enemy1.Draw(spriteBatch); enemy2.Draw(spriteBatch); player.Draw(spriteBatch); missileSmoke.Draw(spriteBatch); if (timer > 300) { life.setActiveAndVisible(true); life.draw(spriteBatch); } // If certain conditions are met, then draw these weapons if (drawMissile == true && missileCount != 0) { missile.draw(spriteBatch); missile.setPosY(missile.getPosY() - 3); missileSmoke.sysPos = new Vector2(missile.getPosX() + missile.getWidth() / 2, missile.getPosY() + missile.getHeight()); } if (anchorDraw == true) { anchor.setActiveAndVisible(true); anchor.draw(spriteBatch); anchor.setPosY(anchor.getPosY() + 4); } if (drawMines && mineCount == 1) { mine.setPos(minePos.X, minePos.Y); mine.draw(spriteBatch); } // If there is a collision, draw explosion animation if (collision1 == true) { drawMissile = false; drawExplosion(currXMissile, currYMissile); enemy1.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); if (explostionTimer == 100) { collision1 = false; score += 50; explostionTimer = 0; } } if (collision2 == true) { drawMissile = false; drawExplosion(currXMissile, currYMissile); enemy2.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); if (explostionTimer == 80) { score += 100; collision2 = false; explostionTimer = 0; missileSmoke.deActivate(); } } if (mineCollisionToggle == true) { mineCollission = false; mineExplostionAnimation.setPos(mineCurrPos.X - mine.getWidth() / 2, mineCurrPos.Y - mine.getHeight() / 2); mineExplostionAnimation.draw(spriteBatch); if (explostionTimer == 100) { mineExplostionAnimation.makeInactive = true; enemy1.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); mineCollisionToggle = false; explostionTimer = 0; } } if (mineCollisionToggle2 == true) { mineCollission2 = false; mineExplostionAnimation.setPos(mineCurrPos.X - mine.getWidth() / 2, mineCurrPos.Y - mine.getHeight() / 2); mineExplostionAnimation.draw(spriteBatch); if (explostionTimer == 100) { mineExplostionAnimation.makeInactive = true; enemy2.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); mineCollisionToggle2 = false; explostionTimer = 0; } } // Draw BB if (showBB) { player.drawInfo(spriteBatch, Color.White, Color.Yellow); enemy1.drawInfo(spriteBatch, Color.White, Color.Yellow); enemy2.drawInfo(spriteBatch, Color.White, Color.Yellow);; missile.drawInfo(spriteBatch, Color.White, Color.Yellow); life.drawInfo(spriteBatch, Color.White, Color.Yellow); mine.drawInfo(spriteBatch, Color.White, Color.Yellow); anchor.drawInfo(spriteBatch, Color.White, Color.Yellow); //weaponList.drawInfo(spriteBatch, Color.White, Color.Yellow); } spriteBatch.End(); }
public override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(graphicsDevice); weaponList = new SpriteList(20); playerXLocation = Dir.rnd.Next(0, 1000); playerYLocation = 880; enemyXLocation = -20; enemyYLocation = Dir.rnd.Next(213, 600); enemy2XLocation = Dir.rnd.Next(Dir.rightBoundary - 300, Dir.rightBoundary); anchorXLocation = Dir.rnd.Next(Dir.leftBoundary + 100, Dir.rightBoundary - 100); anchorYLocation = 175; texBackgroundLevel2 = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Sea04.png"); texPlayer = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\ship-md.png"); texEnemy1 = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Merchant1.png"); texEnemy2 = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Merchant2.png"); texMissile = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Torpedo5up.png"); texMissileSmoke = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Particle1.png"); texExplostionAnimation = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\explodeWater1024x64.png"); texMineExplostion = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\explode1.png"); texMouse = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Mouse.png"); texAnchor = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\anchor-th.png"); texLife = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\life-preserver-th.png"); texMine = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\water_mine.png"); scoreFont = Content.Load <SpriteFont>("display"); helpFont = Content.Load <SpriteFont>("helpText"); explostionSound = Content.Load <SoundEffect>("bazooka_fire"); exLimSound = new LimitSound(explostionSound, 3); backgroundLevel2 = new ImageBackground(texBackgroundLevel2, Color.White, graphicsDevice); player = new Sprite3(true, texPlayer, playerXLocation, playerYLocation); enemy1 = new Sprite3(true, texEnemy1, enemyXLocation, enemyYLocation); enemy2 = new Sprite3(true, texEnemy2, enemy2XLocation, enemyYLocation); explosionAnimation = new Sprite3(true, texExplostionAnimation, 0, playerYLocation); mineExplostionAnimation = new Sprite3(true, texMineExplostion, 0, playerYLocation); mine = new Sprite3(true, texMine, 0, 0); mine.setHSoffset(new Vector2(296, 313)); weaponList.addSpriteReuse(mine); life = new Sprite3(true, texLife, Dir.rnd.Next(Dir.leftBoundary + 200, Dir.rightBoundary - 200), Dir.rnd.Next(300, 600)); life.setActiveAndVisible(false); for (int i = 1; i < 3; i++) { anchor = new Sprite3(true, texAnchor, anchorXLocation, anchorYLocation); anchor.setWidthHeight(50, 50); weaponList.addSpriteReuse(anchor); } for (int m = 1; m < 6; m++) { missile = new Sprite3(true, texMissile, playerXLocation, playerYLocation); missile.setWidthHeight(missile.getWidth() / 2, missile.getHeight() / 2); weaponList.addSpriteReuse(missile); } player.setColor(Color.Red); // Adjusting sprite image sizes to suit my taste player.setWidthHeight(210, 64); enemy1.setWidthHeight(256, 64); enemy2.setWidthHeight(enemy1.getWidth() / 2, 64); life.setWidthHeight(48, 48); mine.setWidthHeight(40, 45); // animation for torpedo explosion explosionAnimation.setWidthHeightOfTex(1024, 64); explosionAnimation.setXframes(16); explosionAnimation.setYframes(1); explosionAnimation.setWidthHeight(80, 80); for (int moveX = 0; moveX <= 15; moveX++) { anim[moveX].X = moveX; anim[moveX].Y = 0; } explosionAnimation.setAnimationSequence(anim, 0, 15, 3); explosionAnimation.setAnimFinished(0); explosionAnimation.animationStart(); // animation for mine explostion mineExplostionAnimation.setWidthHeightOfTex(1024, 64); mineExplostionAnimation.setXframes(16); mineExplostionAnimation.setYframes(1); mineExplostionAnimation.setWidthHeight(80, 80); for (int moveX = 0; moveX <= 15; moveX++) { mineAnim[moveX].X = moveX; mineAnim[moveX].Y = 0; } mineExplostionAnimation.setAnimationSequence(mineAnim, 0, 15, 4); mineExplostionAnimation.setAnimFinished(0); mineExplostionAnimation.animationStart(); HealthBarAttached h1 = new HealthBarAttached(Color.Aquamarine, Color.Green, Color.Red, 9, true); h1.offset = new Vector2(0, -1); // one pixel above the bounding box h1.gapOfbar = 2; enemy1.hitPoints = 10; enemy1.maxHitPoints = 10; enemy1.attachedRenderable = h1; HealthBarAttached h2 = new HealthBarAttached(Color.Aquamarine, Color.Green, Color.Red, 9, true); h2.offset = new Vector2(0, -1); // one pixel above the bounding box h2.gapOfbar = 2; enemy2.hitPoints = 20; enemy2.maxHitPoints = 20; enemy2.attachedRenderable = h2; HealthBarAttached playerHP = new HealthBarAttached(Color.Aquamarine, Color.Green, Color.Red, 9, true); playerHP.offset = new Vector2(0, -1); // one pixel above the bounding box playerHP.gapOfbar = 2; player.hitPoints = 50; player.maxHitPoints = 50; player.attachedRenderable = playerHP; drawSmoke(); }