public override void Initialize() { // Set random X coordinate for the first Asteroid int x = random.Next(Asteroid.WIDTH, Game.GraphicsDevice.Viewport.Width - Asteroid.WIDTH); // Y coordinate: over the top of the game screen int y = -Asteroid.HEIGHT; // Add the first Asteroid parent.AddComponent(new Asteroid(Game, new Vector2(x, y))); base.Initialize(); }
/// <summary> /// Create a new bullet at regular time interval /// </summary> /// <param name="gameTime">Game Time</param> private void CreateNewBullet(GameTime gameTime) { createBulletTimer += gameTime.ElapsedGameTime.TotalSeconds; if (createBulletTimer > BULLET_DUARTION) { // Starting X position is a middle of a Alien Spacecraft float x = position.X + WIDTH / 2 - Bullet.WIDTH / 2; float y = position.Y + HEIGHT; parent.AddComponent(new Bullet(Game, new Vector2(x, y))); createBulletTimer = 0; } }
public override void Update(GameTime gameTime) { // Set current score text (string) to display it scoreText = $"Score : {GetScore()} points"; // Set position for display score (top, center horizontally) scorePosition.X = Game.GraphicsDevice.Viewport.Width / 2 - scoreFont.MeasureString(scoreText).X / 2; scorePosition.Y = scoreFont.MeasureString(scoreText).Y; if (((GetScore() >= 100 && GetScore() < 150) || (GetScore() >= 1000 && GetScore() < 1050) ) && !Game.Components.OfType <Bonus>().Any()) { // Create a Bonus on the top Bonus bonus = new Bonus(Game, new Vector2(50, 10)); parent.AddComponent(bonus); } base.Update(gameTime); }
/// <summary> /// Move Spacecraft by user key inputs (Up/Down/Left/Right keys) /// Fire missiles (Space key) /// </summary> /// <param name="gameTime"></param> private void UpdateUserInput(GameTime gameTime) { KeyboardState ks = Keyboard.GetState(); if (ks.IsKeyDown(Keys.Right)) { state = PlayerState.Right; Position.X += SPEED; } else if (ks.IsKeyDown(Keys.Left)) { state = PlayerState.Left; Position.X -= SPEED; } else { state = PlayerState.Idle; currentFrame = 0; } if (ks.IsKeyDown(Keys.Up)) { state = PlayerState.Idle; Position.Y -= SPEED; currentFrame = 0; } else if (ks.IsKeyDown(Keys.Down)) { state = PlayerState.Idle; Position.Y += SPEED; currentFrame = 0; } // Space Key: Shoot Missile if (ks.IsKeyDown(Keys.Space)) { // Check Shooting Time Inverval shootingTimer += gameTime.ElapsedGameTime.TotalSeconds; if (shootingTimer >= MISSILE_INTERVAL) { if (Missile.missileLevel == 1) { // Create a Missile on the top of the player Missile missile = new Missile(Game, new Vector2(Position.X + WIDTH / 2, Position.Y)); shootingTimer = 0; parent.AddComponent(missile); } else if (Missile.missileLevel == 2) { // Create a Missile on the top of the player Missile missile1 = new Missile(Game, new Vector2(Position.X, Position.Y)); Missile missile2 = new Missile(Game, new Vector2(Position.X + WIDTH, Position.Y)); shootingTimer = 0; parent.AddComponent(missile1); parent.AddComponent(missile2); } } } // Check Boundary, a player cannot go out of the screen int screenWidth = Game.GraphicsDevice.Viewport.Width; int screenHeight = Game.GraphicsDevice.Viewport.Height; Position.X = MathHelper.Clamp(Position.X, 0, screenWidth - WIDTH); Position.Y = MathHelper.Clamp(Position.Y, 0, screenHeight - HEIGHT); }