using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using SharpDX.Direct3D9; using System.Windows.Forms.Automation; namespace Space_invaders_2 { public class Game1 : Game { private GraphicsDeviceManager _graphics; private SpriteBatch _spriteBatch; Player player = new Player(); private Texture2D invaderTexture; private Texture2D _title; private Enemy[,] enemies; // 2D array för enemies private int rows = 3; private int cols = 5; private int _score = 0; private Texture2D _liveText; private Screen screen; private int playerLives = 3; private float playerInvincibleTimer = 0f; // Timer för invincibility private float invincibilityDuration = 2f; // antal sekunder för invincibility public Game1() { _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; _graphics.PreferredBackBufferWidth = 600; _graphics.PreferredBackBufferHeight = 800; // geight på console skräm } protected override void Initialize() { screen = new Screen(); Window.Title = "Space Invaders - SCORE:" + _score; //score i titel listan base.Initialize(); } protected override void LoadContent() { _spriteBatch = new SpriteBatch(GraphicsDevice); player.Load(Content); _title = Content.Load("title"); invaderTexture = Content.Load("Invader"); _liveText = Content.Load("lives"); screen.Load(Content); Array(); } protected override void Update(GameTime gameTime) { screen.Update(gameTime); bool allEnemiesDestroyed = true; //normalisera invincibility timer så att tiden går ner if (playerInvincibleTimer > 0) { playerInvincibleTimer -= (float)gameTime.ElapsedGameTime.TotalSeconds; } if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); player.Update(gameTime, Content); // Update each enemy for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { Enemy enemy = enemies[row, col]; if (enemy != null) { enemy.Update(); allEnemiesDestroyed = false; } } } // checha kollision mellan enemy och bullet (foor loop som går genom hela loppen) for (int i = player.Bullets.Count - 1; i >= 0; i--) { Bullet bullet = player.Bullets[i]; for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { Enemy enemy = enemies[row, col]; //collision för enemy och bullet if (enemy != null && enemy.CheckCollision(bullet)) { // Remove bullet after colliding with an enemy arry player.Bullets.RemoveAt(i); // remove enemy after geting hit by a bullet boom enemies[row, col] = null; _score++; Window.Title = "Space Invaders - SCORE:" + _score; break; } } } } for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { Enemy enemy = enemies[row, col]; //collision med spelare och enemy if (enemy != null && enemy.CheckCollisionPlayer(player)) { //logik för collision med enemy och spelare playerLives--; } } } // spelet tar slut när liv = 0 eller när antal fiender = 0 och kollar även så att det inte blir null if (allEnemiesDestroyed || (enemies[0, 0] != null && enemies[0, 0].Lives <= 0)) { screen.SetGameOver(); } //if (playerLives <= 0) //{ // screen.SetGameOver(); // End the game when player has no lives left //} //if (playerInvincibleTimer <= 0) // { // // Reduce lives and start invincibility // enemy.Lives--; // Assuming you reduce life here for testing // playerInvincibleTimer = invincibilityDuration; // Reset the timer // } base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Gray); _spriteBatch.Begin(); // Only draw non-null enemies for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { if (enemies[row, col] != null) { enemies[row, col].Draw(_spriteBatch); } } } _spriteBatch.Draw(_title, new Vector2(0, 30), Color.White); player.Draw(_spriteBatch); screen.Draw(_spriteBatch); _spriteBatch.End(); base.Draw(gameTime); } // metod för enemy array void Array() { enemies = new Enemy[rows, cols]; float totalWidth = (cols - 1) * 120; float startX = (_graphics.PreferredBackBufferWidth - totalWidth) / 2; // Center invaders textture i mitten av skärmen for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { //justera start positionen för aenemy rrayn int y = row * 50 + 80; int x = (int)(startX + col * 100); enemies[row, col] = new Enemy(new Vector2(x, y), invaderTexture, _liveText); } } } } }