public bool collision(GameSprite target) { if (target == null) return false; Rectangle sourceRectangle = new Rectangle( (int)position.X, (int)position.Y, image.Width, image.Height); Rectangle targetRectangle = new Rectangle( (int)target.position.X, (int)target.position.Y, target.image.Width, target.image.Height); return (sourceRectangle.Intersects(targetRectangle)); }
private void UpdateGameplay(GameTime gameTime) { if (currentKeys.IsKeyDown(Keys.P) && oldKeys.IsKeyUp(Keys.P)) { currentGameState = GameState.pause; MediaPlayer.Play(PauseMusic); MediaPlayer.Volume = 1; } player.position.X += GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X * 4; if (Keyboard.GetState().IsKeyDown(Keys.Left)) player.velocity.X -= 1; if (Keyboard.GetState().IsKeyDown(Keys.Right)) player.velocity.X += 1; if (Keyboard.GetState().IsKeyDown(Keys.Up)) player.velocity.Y -= 1; else if (player.position.Y < screenHeight / 2) player.velocity.Y += 0.2f; if (Keyboard.GetState().IsKeyDown(Keys.Down)) player.velocity.Y += 1; player.velocity *= 0.95f; player.position += player.velocity; if (player.position.X < 0) player.position.X = 0; if (player.position.X > screenWidth - player.image.Width) player.position.X = screenWidth - player.image.Width; if (player.position.Y < 0) player.position.Y = 0; if (player.position.Y > screenHeight - player.image.Height) player.position.Y = screenHeight - player.image.Height; gunCooldown -= gameTime.ElapsedGameTime.Milliseconds; if (Keyboard.GetState().IsKeyDown(Keys.Space) && gunCooldown <= 0) { bullets.Add(new GameSprite(bulletPic, player.position + new Vector2(player.image.Width / 2, 0))); bullets[bullets.Count() - 1].velocity.Y = -15; shootSound.Play(); gunCooldown = MAXGUNCOOLDOWN; } for (int bulletNum = 0; bulletNum < bullets.Count(); bulletNum++) { bullets[bulletNum].position += bullets[bulletNum].velocity; } invulnerableTime -= gameTime.ElapsedGameTime.Milliseconds; for (int bulletNum = 0; bulletNum < enemyBullets.Count(); bulletNum++) { enemyBullets[bulletNum].position += enemyBullets[bulletNum].velocity; if (enemyBullets[bulletNum].collision(player)) { enemyBullets.RemoveAt(bulletNum); bulletNum--; if(invulnerableTime<=0) PlayerDeath(); } } difficulty += gameTime.ElapsedGameTime.Milliseconds * difficultyIncreaseRate; float spawnChance = 10; if (rng.Next(0, 1000) < (difficulty / 100 * spawnChance)) { enemies.Add(new GameSprite(baddiePic, new Vector2(rng.Next(0, screenWidth - baddiePic.Width), -100))); } float bulletSpawnChance = 5; foreach (GameSprite enemy in enemies) { if (rng.Next(0, 1000) < (difficulty / 100 * bulletSpawnChance)) { GameSprite newBullet = new GameSprite(bulletPic, enemy.position); newBullet.velocity = player.position - enemy.position; newBullet.velocity.Normalize(); newBullet.velocity *= (6 * difficulty / 100); enemyBullets.Add(newBullet); } } for (int enemyNum = 0; enemyNum < enemies.Count(); enemyNum++) { enemies[enemyNum].velocity.Y = (3 * difficulty / 100); if (enemies[enemyNum].position.X + enemies[enemyNum].image.Width/2 < player.position.X + player.image.Width/2-4) enemies[enemyNum].velocity.X = 2; else if (enemies[enemyNum].position.X + enemies[enemyNum].image.Width / 2 > player.position.X + player.image.Width / 2+4) enemies[enemyNum].velocity.X = -2; else enemies[enemyNum].velocity.X = 0; enemies[enemyNum].position += enemies[enemyNum].velocity; if (enemies[enemyNum].position.Y > screenHeight) { enemies.RemoveAt(enemyNum); break; } } for (int enemyNum = 0; enemyNum < enemies.Count(); enemyNum++) { if (enemies[enemyNum].collision(player)) { enemies.RemoveAt(enemyNum); enemyNum--; if(invulnerableTime<=0) PlayerDeath(); } } for (int enemyNum = 0; enemyNum < enemies.Count(); enemyNum++) { for (int bulletNum = 0; bulletNum < bullets.Count(); bulletNum++) { if (enemies[enemyNum].collision(bullets[bulletNum])) { GameSprite newExplosion = new GameSprite(explosionImage, enemies[enemyNum].position); newExplosion.animationframes = 6; newExplosion.animationTimeMax = 1000; explosions.Add(newExplosion); enemies.RemoveAt(enemyNum); enemyNum--; bullets.RemoveAt(bulletNum); bulletNum--; baddieHitSound.Play(); score += 10; pointsUntilNextBonusLife -= 10; if (pointsUntilNextBonusLife <= 0) { lives++; pointsUntilNextBonusLife += POINTSPERLIFE; } break; } } } if (score > highscore) { highscore = score; } if (lives <= 0) { currentGameState = GameState.gameOver; string outputFile = "highscores.txt"; StreamWriter outputFileStream = new StreamWriter(outputFile); outputFileStream.WriteLine(highscore); outputFileStream.Close(); } }
protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); baddiePic = Content.Load<Texture2D>("badguys"); Texture2D playerimage = Content.Load<Texture2D>("player"); player = new GameSprite(playerimage, new Vector2(screenWidth / 2 - playerimage.Width / 2, screenHeight - playerimage.Height)); gameFont = Content.Load<SpriteFont>("War Hero"); hitSound = Content.Load<SoundEffect>("asplode"); BGmusic = Content.Load<Song>("POL-battle-march-long"); PauseMusic = Content.Load<Song>("PauseMusic"); MediaPlayer.IsRepeating = true; MediaPlayer.Play(BGmusic); MediaPlayer.Volume = 0.2f; shootSound = Content.Load<SoundEffect>("shoot"); bulletPic = Content.Load<Texture2D>("bullet"); baddieHitSound = Content.Load<SoundEffect>("asplode2"); explosionImage = Content.Load<Texture2D>("badguyexplosion"); pauseImage = Content.Load<Texture2D>("pauseImage"); }