Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using System; // ez
- namespace SpaceShooter
- {
- /// <summary>
- /// This is the main type for your game.
- /// </summary>
- public class Game1 : Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch SpriteBatch;
- Texture2D Ship;
- Vector2 ShipPos;
- Vector2 ShipPosAi;
- bool ShipColor = false;
- Random rng = new Random();
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- /// <summary>
- /// Allows the game to perform any initialization it needs to before starting to run.
- /// This is where it can query for any required services and load any non-graphic
- /// related content. Calling base.Initialize will enumerate through any components
- /// and initialize them as well.
- /// </summary>
- protected override void Initialize()
- {
- // TODO: Add your initialization logic here
- ShipPos = new Vector2(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 2);
- ShipPosAi = new Vector2(Window.ClientBounds.Width, Window.ClientBounds.Height);
- base.Initialize();
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- SpriteBatch = new SpriteBatch(GraphicsDevice);
- Ship = Content.Load<Texture2D>("images/ship");
- // TODO: use this.Content to load your game content here
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// game-specific content.
- /// </summary>
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- /// <summary>
- /// Allows the game to run logic such as updating the world,
- /// checking for collisions, gathering input, and playing audio.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Update(GameTime gameTime)
- {
- // Spelare
- KeyboardState KeyboardState = Keyboard.GetState();
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
- Exit();
- if (KeyboardState.IsKeyDown(Keys.W) && !(ShipPos.Y <= 0))
- {
- ShipPos.Y -= 3f;
- }
- if (KeyboardState.IsKeyDown(Keys.S) && !(ShipPos.Y >= Window.ClientBounds.Height - (int)Ship.Height))
- {
- ShipPos.Y += 3f;
- }
- if (KeyboardState.IsKeyDown(Keys.A) && !(ShipPos.X <= 0))
- {
- ShipPos.X -= 3f;
- }
- if (KeyboardState.IsKeyDown(Keys.D) && !(ShipPos.X >= Window.ClientBounds.Width - (int)Ship.Width))
- {
- ShipPos.X += 3f;
- }
- if (KeyboardState.IsKeyDown(Keys.E))
- {
- if (ShipColor == false)
- {
- ShipColor = true;
- }
- else if (ShipColor == true)
- {
- ShipColor = false;
- }
- // AI
- int dir = rng.Next(1,5);
- if (dir == 1)
- {
- ShipPosAi.Y -= 2f;
- }
- if (dir == 2)
- {
- ShipPosAi.Y += 2f;
- }
- if (dir == 3)
- {
- ShipPosAi.X -= 2f;
- }
- if (dir == 4)
- {
- ShipPosAi.Y += 2f;
- }
- }
- // TODO: Add your update logic here
- base.Update(gameTime);
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.HotPink);
- SpriteBatch.Begin();
- SpriteBatch.Draw(Ship, ShipPosAi, Color.White);
- if (ShipColor == false)
- {
- SpriteBatch.Draw(Ship, ShipPos, Color.Green);
- }
- else
- {
- SpriteBatch.Draw(Ship, ShipPos, Color.Red);
- }
- SpriteBatch.End();
- // TODO: Add your drawing code here
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement