NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

public class Game1 : Microsoft.Xna.Framework.Game
{
#region In-game
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
#region In-game
Vector2 healthPos = new Vector2(20, 50);
Vector2 scorePos = new Vector2(20, 10);
int score = 0;
SpriteFont gameFont;
KeyboardState ks;
KeyboardState prevKs;
Texture2D backgroundTex;
Vector2 background1Pos;
Vector2 background2Pos;
float scrollSpeed = 3.0f;
Vector2 startPos;
Texture2D startTex;
Vector2 highScorePos;
Texture2D highScoreTex;
MouseState ms;
MouseState prevMs;
Point mousePos;
#endregion
#region Player
Texture2D playerTex;
Vector2 playerPos;
Texture2D playeraccTex;
Vector2 playerSpeed = new Vector2();
bool press;
float playerRotation;
const float playerMaxspeed = 4;
Texture2D shotTex;
List<Vector2> shotPos = new List<Vector2>();
List<Vector2> shotSpeed = new List<Vector2>();
const int shotDelay = 200;
int shotTime;
int life = 10;
int highScore;
SpriteFont Font2;
Vector2 FontPos;
Texture2D hsTex;
float time = 0;

#endregion
#region Enemy
List<Vector2> enemyPositions;
Texture2D enemyTexture;
Random randomizer;
float rotation;
Vector2 speed;
#endregion

enum GameState
{
Start,
GameOn,
Pause,
GameOver,
HighScore
}
GameState gameState;
#endregion

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferHeight = 600;
IsMouseVisible = true;
}

/// <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 = GameState.Start;
Vector2 healthPos = new Vector2(20, 50);
life = 10;
Vector2 scorePos = new Vector2(20, 10);
score = 0;
enemyPositions = new List<Vector2>();
randomizer = new Random();
playerPos = new Vector2(20, 300);
shotPos = new List<Vector2>();
powerUpPos = new Vector2(1600, 900);
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
gameFont = Content.Load<SpriteFont>("SpriteFont1");
backgroundTex = Content.Load<Texture2D>("parallax_009");
background1Pos = Vector2.Zero;
background2Pos = new Vector2(backgroundTex.Width, 0);
playerTex = Content.Load<Texture2D>("normal");
playeraccTex = Content.Load<Texture2D>("normal med boost");
enemyTexture = Content.Load<Texture2D>("enemy");
shotTex = Content.Load<Texture2D>("shot 1");
startScreenTex = Content.Load<Texture2D>("Start_Screen");
startTex = Content.Load<Texture2D>("Start");
highScoreTex = Content.Load<Texture2D>("highscore");
powerUpTex = Content.Load<Texture2D>("Star1");
hsTex = Content.Load<Texture2D>("HighscoreTable");
Font2 = Content.Load<SpriteFont>("SpriteFont2");
FontPos = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2, graphics.GraphicsDevice.Viewport.Height / 2);
}

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all 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)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here
prevKs = ks;
ks = Keyboard.GetState();
switch (gameState)
{
#region Startscreen
case GameState.Start:
if (ks.IsKeyDown(Keys.Enter))
{
gameState = GameState.GameOn;
}
prevMs = ms;
ms = Mouse.GetState();
mousePos = new Point(ms.X, ms.Y);
startButtonRectangle = new Rectangle((int)startPos.X + 365, (int)startPos.Y + 273, 458 - 365, 300 - 273);
if (startButtonRectangle.Contains(mousePos))
{
if (ms.LeftButton == ButtonState.Pressed && prevMs.LeftButton == ButtonState.Released)
{
gameState = GameState.GameOn;
}
}
highScoreButtonRectangle = new Rectangle((int)startPos.X + 365, (int)startPos.Y + 332, 458 - 365, 360 - 332);
if (highScoreButtonRectangle.Contains(mousePos))
{
if (ms.LeftButton == ButtonState.Pressed && prevMs.LeftButton == ButtonState.Released)
{
gameState = GameState.HighScore;
}
}
break;
#endregion

#region Game on
case GameState.GameOn:
#region enemy
// skapar nya fiender om listan är tom
if (enemyPositions.Count < 8)
{
enemyPositions.Add(new Vector2(randomizer.Next(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Width + 1000), randomizer.Next(0, GraphicsDevice.Viewport.Height - enemyTexture.Height)));
}
for (int i = 0; i < enemyPositions.Count; i++)
{
if (enemyPositions[i].X < 0 - enemyTexture.Width)
{
enemyPositions.RemoveAt(i);

}
rotation = (float)(Math.Atan2(playerPos.Y - enemyPositions[i].Y, playerPos.X - enemyPositions[i].X));
speed = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation));
enemyPositions[i] += speed * 5 * (i * 0.8f);
}
#endregion

#region player
if (ks.IsKeyDown(Keys.P) && prevKs.IsKeyUp(Keys.P))
{
gameState = GameState.Pause;
}
if (ks.IsKeyDown(Keys.Left))
{
playerPos.Y -= 10;
time = 0;
}
if (ks.IsKeyDown(Keys.Right))
{
playerPos.Y += 10;
time = 0;
}
//hur skeppet förflyttar sig med acceleration m.m
if (ks.IsKeyDown(Keys.Up))
{
playerPos.X += 5;
time = 0;
press = true;
}
else
{
press = false;
if (playerPos.X >= 20)
{
playerPos.X -= 3;
}
}
playerPos += playerSpeed;

shotTime -= gameTime.ElapsedGameTime.Milliseconds;
if (shotTime < 0)
{
shotTime = 0;
}
if (ks.IsKeyDown(Keys.Space) && shotTime == 0)
{
shotTime = shotDelay;
shotPos.Add(new Vector2(playerPos.X, playerPos.Y));
shotSpeed.Add(new Vector2(11 * (float)Math.Cos(playerRotation), 11 * (float)Math.Sin(playerRotation)));
}
{
for (int i = 0; i < shotPos.Count; i++)
{
shotPos[i] += shotSpeed[i];
if (shotPos[i].X < -100 || shotPos[i].X > graphics.GraphicsDevice.Viewport.Width + 100 || shotPos[i].Y < -100 || shotPos[i].Y > graphics.GraphicsDevice.Viewport.Height + 100)
{
shotPos.RemoveAt(i);
shotSpeed.RemoveAt(i);
continue;
}
}
}
time += gameTime.ElapsedGameTime.Milliseconds;
if (time > 3000)
{
life -= 1;
time = 0;
}

#endregion

#region rectangles

playerPos.X = MathHelper.Clamp(playerPos.X, 0, GraphicsDevice.Viewport.Width - playerTex.Width);
playerPos.Y = MathHelper.Clamp(playerPos.Y, 0, GraphicsDevice.Viewport.Height - playerTex.Height);
//kollar om karaktären träffar meteorerna
Rectangle playerRectangle = new Rectangle((int)playerPos.X, (int)playerPos.Y, playerTex.Width, playerTex.Height);
Rectangle powerUpRectangle = new Rectangle((int)powerUpPos.X, (int)powerUpPos.Y, powerUpTex.Width, powerUpTex.Height);
if (playerRectangle.Intersects(powerUpRectangle))
{
powerUpPos = new Vector2(1600, 900);
powerUp = true;
}
for (int j = 0; j < enemyPositions.Count; j++)
{
Rectangle enemyRectangle = new Rectangle((int)enemyPositions[j].X, (int)enemyPositions[j].Y, enemyTexture.Width, enemyTexture.Height);
if (enemyRectangle.Intersects(playerRectangle))
{
enemyPositions.RemoveAt(j);
life--;
break;
}
//om livet är slut, stängs spelet ner eller om poäng är - 10

if (life <= 0)
{
gameState = GameState.GameOver;
}
for (int i = 0; i < shotPos.Count; i++)
{
Rectangle shotRectangle = new Rectangle((int)shotPos[i].X, (int)shotPos[i].Y, shotTex.Width, shotTex.Height);
if (enemyRectangle.Intersects(shotRectangle))
{
enemyPositions.RemoveAt(j);
shotPos.RemoveAt(i);
shotSpeed.RemoveAt(i);
score += 10;
}
}
}
#endregion

#region background
background1Pos.X -= scrollSpeed;
background2Pos.X -= scrollSpeed;
if (background1Pos.X < -backgroundTex.Width)
{
background1Pos.X = background2Pos.X + backgroundTex.Width;
}
if (background2Pos.X < -backgroundTex.Width)
{
background2Pos.X = background1Pos.X + backgroundTex.Width;
}

#endregion

if (life <= 0)
{
StreamReader sr = new StreamReader("highscore.txt");
highScore = int.Parse(sr.ReadLine());
sr.Close();
if (score > highScore)
{
StreamWriter sw = new StreamWriter("highscore.txt");
sw.WriteLine("" + score);
sw.Close();
}
}
break;
#endregion

#region Paus
case GameState.Pause:
if (ks.IsKeyDown(Keys.P) && prevKs.IsKeyUp(Keys.P))
{
gameState = GameState.GameOn;
}
break;
#endregion

#region gameover
case GameState.GameOver:
if (ks.IsKeyDown(Keys.S))
{
gameState = GameState.Start;
Initialize();
}

break;
#endregion

#region Highscore
case GameState.HighScore:
{
if (ks.IsKeyDown(Keys.Space))
{
gameState = GameState.Start;
}
}
break;
#endregion
}
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
spriteBatch.Begin();
spriteBatch.Draw(backgroundTex, background1Pos, Color.White);
spriteBatch.Draw(backgroundTex, background2Pos, null, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);

spriteBatch.Draw(playerTex , playerPos, null, Color.White, (float)Math.PI / 2 + playerRotation, new Vector2(playerTex.Width / 2, playerTex.Height / 2), 1.0f, SpriteEffects.None, 1.0f);
if (press)
{
spriteBatch.Draw(playeraccTex, playerPos, null, Color.White, playerRotation + (float)Math.PI / 2, new Vector2(playerTex.Width / 2, playerTex.Height / 2), 1.0f, SpriteEffects.None, 0);
}
switch (gameState)
{
case GameState.Start:
break;
case GameState.GameOn:
for (int i = 0; i < shotPos.Count; i++)
{
spriteBatch.Draw(shotTex, shotPos[i], null, Color.White, 0, new Vector2(shotTex.Width / 2, shotTex.Height / 2), 1.0f, SpriteEffects.None, 0);
}
for (int i = 0; i < enemyPositions.Count; i++)
{
spriteBatch.Draw(enemyTexture, enemyPositions[i], Color.White);
}
spriteBatch.DrawString(gameFont, "Score: " + score, scorePos, Color.Cyan);
spriteBatch.DrawString(gameFont, "Lives: " + life, healthPos, Color.Cyan);
break;
case GameState.Pause:
spriteBatch.DrawString(gameFont, "Space = shot", new Vector2(200, 290), Color.Yellow);
break;
case GameState.GameOver:
spriteBatch.DrawString(gameFont, "GameOver, Press S to go to Start", new Vector2(100, 100), Color.Yellow);
spriteBatch.DrawString(gameFont, "Your Score: " + score, scorePos, Color.Cyan);
break;
case GameState.HighScore:
spriteBatch.Draw(hsTex, new Vector2(0, 0), Color.White);
spriteBatch.DrawString(Font2, "Highscore: " + highScore, new Vector2(300 , GraphicsDevice.Viewport.Height / 2), Color.White);
spriteBatch.DrawString(gameFont, "Press Space to go back to Start", new Vector2(230, 200), Color.Yellow);
break;
}

spriteBatch.End();

base.Draw(gameTime);
}

public double shotRotation { get; set; }
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.