Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- namespace BallGameWin
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class BallGame : Microsoft.Xna.Framework.Game
- {
- //Gjort disse public for senere bruk.
- public GraphicsDeviceManager Graphics;
- public SpriteBatch SpriteBatch;
- SpriteFont MyFont;
- Texture2D Texture = null;
- Vector2 Position = new Vector2(400, 240); //Skjermen er 800x480, så dette blir på midten
- Vector2 HalfTexture = Vector2.Zero; //Tilsvarer "new Vector2(0,
- Vector2 textPos1 = new Vector2(760, 30);
- Vector2 textPos2 = new Vector2(30, 30);
- Random Rand = new Random();
- int dir = 1;
- public BallGame()
- {
- Graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- Graphics.PreferredBackBufferWidth = 800;
- Graphics.PreferredBackBufferHeight = 480;
- }
- /// <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
- 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 Content to load your game content here
- MyFont = Content.Load<SpriteFont>("Font");
- Texture = Content.Load<Texture2D>("Ball");
- HalfTexture = Texture.Size() * 0.5f;
- }
- /// <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
- }
- float Velocity = 0.01f;
- float VelocityM = 0.01f;
- float VelocityMax = 50f;
- float Acceleration = 0.9f;
- float spd = 1;
- int sTime = 0;
- int msTime = 0;
- int msTick = 10;
- int counter = 0;
- Vector2 Movement = Vector2.Zero;
- Vector2 Rotation = new Vector2(0, 0);
- int rotRadius = 5;
- String TimeScore = "";
- String HighScore = "";
- int curTime;
- int curScore = 0;
- int hiScore = 0;
- /// <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)
- {
- // Stopwatch...
- msTime = gameTime.TotalGameTime.Milliseconds;
- sTime = gameTime.TotalGameTime.Seconds;
- if (sTime > curTime)
- {
- curScore++;
- if (curScore > hiScore)
- {
- hiScore = curScore;
- }
- }
- curTime = sTime;
- TimeScore = curScore.ToString();
- HighScore = hiScore.ToString();
- int Rn = Rand.Next(2);
- if (sTime*7 % 3 == 0)
- {
- spd += 0.1f*dir;
- spd /= (float) Math.Tan(spd);
- counter++;
- if (spd > 10000)
- {
- spd = 1;
- }
- if (Rn == 1)
- {
- dir = 1;
- }
- else
- {
- dir = -1;
- }
- }
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- if (Keyboard.GetState().IsKeyDown(Keys.Escape))
- this.Exit();
- // Boundary Enforcer
- if (Position.X > 775 ||
- Position.X < 25 ||
- Position.Y > 455 ||
- Position.Y < 25
- )
- {
- Movement *= 0;
- Velocity = 0;
- spd = 1;
- curScore = 0;
- }
- // Basic Controls
- if (Keyboard.GetState().IsKeyDown(Keys.Left))
- {
- Movement += new Vector2(-Velocity, 0);
- if (Velocity < VelocityMax & msTime % msTick == 0)
- {
- Velocity += Acceleration;
- }
- }
- if (Keyboard.GetState().IsKeyDown(Keys.Right))
- {
- Movement += new Vector2(Velocity, 0);
- if (Velocity < VelocityMax & msTime % msTick == 0)
- {
- Velocity += Acceleration;
- }
- }
- if (Keyboard.GetState().IsKeyDown(Keys.Up))
- {
- Movement += new Vector2(0, -Velocity);
- if (Velocity < VelocityMax & msTime % msTick == 0)
- {
- Velocity += Acceleration;
- }
- }
- if (Keyboard.GetState().IsKeyDown(Keys.Down))
- {
- Movement += new Vector2(0, Velocity);
- if (Velocity < VelocityMax & msTime % msTick == 0)
- {
- Velocity += Acceleration;
- }
- }
- // Velocity Reset
- if (Keyboard.GetState().IsKeyUp(Keys.Up) &
- Keyboard.GetState().IsKeyUp(Keys.Down) &
- Keyboard.GetState().IsKeyUp(Keys.Left) &
- Keyboard.GetState().IsKeyUp(Keys.Right))
- {
- Velocity = VelocityM;
- }
- Rotation.X = rotRadius * (float)Math.Cos(counter*spd) + (float)Math.Cos(Velocity)*Velocity*dir;
- Rotation.Y = rotRadius * (float)Math.Sin(counter*spd) + (float)Math.Sin(Velocity)*Velocity;
- // Position Reset
- // if (Keyboard.GetState().IsKeyDown(Keys.Space))
- // {
- Movement += Rotation;
- // }
- // TODO: Add your update logic here
- Position = Mouse.GetState().Position();
- Position = new Vector2(400,240) + Movement;
- 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.Black);
- // TODO: Add your drawing code here
- SpriteBatch.Begin();
- SpriteBatch.Draw(Texture, Position - HalfTexture, Color.White);
- // Draw Hello World
- string textOutput1 = TimeScore;
- string textOutput2 = HighScore;
- // Find the center of the string
- Vector2 FontOrigin1 = MyFont.MeasureString(textOutput1) / 2;
- Vector2 FontOrigin2 = MyFont.MeasureString(textOutput2) / 2;
- // Draw the string
- SpriteBatch.DrawString(MyFont, textOutput1, textPos1, Color.LightGreen, 0, FontOrigin1, 1.0f, SpriteEffects.None, 0.5f);
- SpriteBatch.DrawString(MyFont, textOutput2, textPos2, Color.LightGreen, 0, FontOrigin2, 1.0f, SpriteEffects.None, 0.5f);
- SpriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement