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;
- namespace Game14
- {
- /// <summary>
- /// This is the main type for your game.
- /// </summary>
- public class Game1 : Game
- {
- GraphicsDeviceManager graphics;
- Texture2D ramGFX;
- Rectangle ramposition;
- Rectangle ramposition2;
- SpriteBatch spriteBatch;
- Texture2D spelareGFX;
- Rectangle spelarposition;
- Texture2D enemyGFX;
- Rectangle enemyposition;
- Texture2D bollGFX;
- Rectangle bollposition;
- Texture2D bakgrundGFX;
- Rectangle bakgrundposition;
- KeyboardState tangentbord;
- Vector2 bollriktning;
- SpriteFont score;
- Vector2 scorepos;
- Vector2 scorepos2;
- float bollhastighet;
- int spelarepoäng1;
- int spelarepoäng2;
- int ingame = 1;
- int huvudmeny = 2;
- string ingamez = "ingame";
- string menu = "huvudmeny";
- public string gamestate;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- public void Restart()
- {
- bollriktning.X = 1;
- bollriktning.Y = 1;
- bollhastighet = 1;
- bollposition = new Rectangle(400 - bollGFX.Width / 2, 191, bollGFX.Width, bollGFX.Height);
- }
- /// <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
- gamestate = "meny";
- 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);
- // TODO: use this.Content to load your game content here
- ramGFX = Content.Load<Texture2D>("ram2");
- spelareGFX = Content.Load<Texture2D>("legendfixed");
- enemyGFX = Content.Load<Texture2D>("legendfixed");
- bakgrundGFX = Content.Load<Texture2D>("petfixed");
- bollGFX = Content.Load<Texture2D>("heartstone");
- score = Content.Load<SpriteFont>("scorefont");
- scorepos = new Vector2(50, 50);
- scorepos2 = new Vector2(713, 50);
- spelarposition = new Rectangle(8, 191, spelareGFX.Width, spelareGFX.Height);
- enemyposition = new Rectangle(762, 191, enemyGFX.Width, enemyGFX.Height);
- bollposition = new Rectangle(400 - bollGFX.Width / 2, 191, bollGFX.Width, bollGFX.Height);
- bakgrundposition = new Rectangle(0, 0, 800, 480);
- ramposition = new Rectangle(8, 4, 786, 20);
- ramposition2 = new Rectangle(8, 456, 786, 20);
- bollriktning.Y = bollriktning.Y = 1;
- bollriktning.X = bollriktning.X = 1;
- }
- /// <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)
- {
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
- Exit();
- // TODO: Add your update logic here
- tangentbord = Keyboard.GetState();
- if (tangentbord.IsKeyDown(Keys.W))
- spelarposition.Y = spelarposition.Y - 4;
- if (tangentbord.IsKeyDown(Keys.S))
- spelarposition.Y = spelarposition.Y + 4;
- if (tangentbord.IsKeyDown(Keys.Up))
- enemyposition.Y = enemyposition.Y - 4;
- if (tangentbord.IsKeyDown(Keys.Down))
- enemyposition.Y = enemyposition.Y + 4;
- bollhastighet = bollhastighet + 0.01f;
- bollposition.X = bollposition.X + (int)(bollriktning.X * bollhastighet);
- bollposition.Y = bollposition.Y + (int)(bollriktning.Y * bollhastighet);
- if (spelarposition.Intersects(bollposition) == true)
- bollriktning.X = 1;
- if (enemyposition.Intersects(bollposition) == true)
- bollriktning.X = -1;
- if (spelarposition.Y < 24)
- spelarposition.Y = 24;
- if (spelarposition.Y > 359)
- spelarposition.Y = 359;
- if (enemyposition.Y < 24)
- enemyposition.Y = 24;
- if (enemyposition.Y > 359)
- enemyposition.Y = 359;
- if (bollposition.Y < 24)
- bollriktning.Y = 1;
- if (bollposition.Y > 456 - bollGFX.Height)
- bollriktning.Y = -1;
- if (bollposition.X < 0)
- {
- spelarepoäng2 = spelarepoäng2 + 1;
- Restart();
- }
- if (bollposition.X > 800)
- {
- spelarepoäng1 = spelarepoäng1 + 1;
- Restart();
- }
- if (spelarepoäng1 == 3 || spelarepoäng2 == 3)
- {
- Restart();
- spelarepoäng1 = 0;
- spelarepoäng2 = 0;
- }
- 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.CornflowerBlue);
- // TODO: Add your drawing code here
- string poäng1 = spelarepoäng1.ToString();
- string poäng2 = spelarepoäng2.ToString();
- spriteBatch.Begin();
- spriteBatch.Draw(bakgrundGFX, bakgrundposition, Color.White);
- spriteBatch.Draw(ramGFX, ramposition, Color.White);
- spriteBatch.Draw(ramGFX, ramposition2, Color.White);
- spriteBatch.Draw(spelareGFX, spelarposition, Color.White);
- spriteBatch.Draw(enemyGFX, enemyposition, Color.White);
- spriteBatch.Draw(bollGFX, bollposition, Color.White);
- spriteBatch.DrawString(score, (poäng1), scorepos, Color.White);
- spriteBatch.DrawString(score, (poäng2), scorepos2, Color.White);
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement