public void Update(float timeStamp) { if (!Started) { return; } _gameTime.TotalTime = timeStamp; UpdateBombs(); UpdateAliens(); UpdateShots(); if (_aliens.All(_ => _.Destroyed)) { _gameSpeed += 0.75; _aliens.Clear(); for (int i = 0; i < 11; i++) { _aliens.Add(new Alien(AlienType.Squid, new Point((i * 60 + 120), 100), i, 0)); _aliens.Add(new Alien(AlienType.Crab, new Point((i * 60 + 120), 145), i, 1)); _aliens.Add(new Alien(AlienType.Crab, new Point((i * 60 + 120), 190), i, 2)); _aliens.Add(new Alien(AlienType.Octopus, new Point((i * 60 + 120), 235), i, 3)); _aliens.Add(new Alien(AlienType.Octopus, new Point((i * 60 + 120), 270), i, 4)); } } if (_motherShip == null && _random.Next(100) > 90) { _motherShip = new MotherShip(new Point(_width, 80)); } }
public void Update(float timeStamp) { if (!Started) { return; } _gameTime.TotalTime = timeStamp; UpdateBombs(); UpdateAliens(); UpdateShots(); if (_aliens.All(_ => _.Destroyed)) { Won = true; } if (_motherShip == null && _random.Next(100) > 90) { _motherShip = new MotherShip(new Point(_width, 80)); } }
private void UpdateAliens() { if (_gameTime.TotalTime - _lastUpdate > 250) { _lastUpdate = _gameTime.TotalTime; if (_aliens.Any(_ => (_.CurrentPosition.X + 80) >= (_width - 10)) && _currentMovement == Direction.Right) { _aliens.ForEach(_ => _.MoveDown()); _currentMovement = Direction.Left; } else if (_aliens.Any(_ => _.CurrentPosition.X <= 15) && _currentMovement == Direction.Left) { _aliens.ForEach(_ => _.MoveDown()); _currentMovement = Direction.Right; } else { _aliens.ForEach(_ => _.MoveHorizontal(_currentMovement)); } _aliens.Where(_ => _.HasBeenHit).ToList().ForEach(_ => _.Destroyed = true); if (_aliens.Any(_ => !_.Destroyed && _player.Collision(_)) || Aliens.Any(_ => !_.Destroyed && _.CurrentPosition.Y > 500)) { _lives = 0; GameOver = true; } if (_motherShip?.Destroyed ?? false) { _motherShip = null; } if (_motherShip?.HasBeenHit ?? false) { _motherShip.Destroyed = true; } } }
private async Task RenderGameFrame() { if (_lostALife) { var text = $"You where hit, lost a life!"; var length = await _context.MeasureTextAsync(text); await _context.SetFillStyleAsync("red"); await _context.FillTextAsync(text, _width / 2 - (length.Width / 2), 55); await _context.SetFillStyleAsync("white"); } foreach (var a in Aliens) { if (a.Destroyed) { continue; } await _context.DrawImageAsync(_spriteSheet, a.Sprite.TopLeft.X, a.Sprite.TopLeft.Y, a.Sprite.Size.Width, a.Sprite.Size.Height, a.CurrentPosition.X, a.CurrentPosition.Y, a.Sprite.RenderSize.Width, a.Sprite.RenderSize.Height); } var ship = Player.Sprite; await _context.DrawImageAsync(_spriteSheet, ship.TopLeft.X, ship.TopLeft.Y, ship.Size.Width, ship.Size.Height, Player.CurrentPosition.X, Player.CurrentPosition.Y, ship.RenderSize.Width, ship.RenderSize.Height); foreach (var s in _shots.Where(_ => !_.Remove)) { if (s.CurrentPosition.Y <= 0) { s.Remove = true; continue; } await _context.DrawImageAsync(_spriteSheet, s.Sprite.TopLeft.X, s.Sprite.TopLeft.Y, s.Sprite.Size.Width, s.Sprite.Size.Height, s.CurrentPosition.X, s.CurrentPosition.Y, s.Sprite.RenderSize.Width, s.Sprite.RenderSize.Height); } foreach (var b in _bombs.Where(_ => !_.Remove)) { if (b.CurrentPosition.Y >= _height) { b.Remove = true; continue; } var x = b.Sprite.Size; var y = b.Sprite.RenderSize; await _context.DrawImageAsync(_spriteSheet, b.Sprite.TopLeft.X, b.Sprite.TopLeft.Y, b.Sprite.Size.Width, b.Sprite.Size.Height, b.CurrentPosition.X, b.CurrentPosition.Y, b.Sprite.RenderSize.Width, b.Sprite.RenderSize.Height); } if (_motherShip != null) { if (!_motherShip.HasBeenHit) { _motherShip.Move(); } if (_motherShip.CurrentPosition.X < 0) { _motherShip = null; } else { await _context.DrawImageAsync(_spriteSheet, _motherShip.Sprite.TopLeft.X, _motherShip.Sprite.TopLeft.Y, _motherShip.Sprite.Size.Width, _motherShip.Sprite.Size.Height, _motherShip.CurrentPosition.X, _motherShip.CurrentPosition.Y, _motherShip.Sprite.RenderSize.Width, _motherShip.Sprite.RenderSize.Height); //TODO: Remove hardcoded sizes } } }