Advertisement
sindrijo

Pre-Alpha My First Game

Jan 12th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace BallGameWin
  13. {
  14.     /// <summary>
  15.     /// This is the main type for your game
  16.     /// </summary>
  17.     public class BallGame : Microsoft.Xna.Framework.Game
  18.     {
  19.         //Gjort disse public for senere bruk.
  20.         public GraphicsDeviceManager Graphics;
  21.         public SpriteBatch SpriteBatch;
  22.         SpriteFont MyFont;
  23.  
  24.         Texture2D Texture = null;
  25.         Vector2 Position = new Vector2(400, 240); //Skjermen er 800x480, så dette blir på midten
  26.         Vector2 HalfTexture = Vector2.Zero; //Tilsvarer "new Vector2(0,
  27.         Vector2 textPos1 = new Vector2(760, 30);
  28.         Vector2 textPos2 = new Vector2(30, 30);
  29.        
  30.  
  31.         Random Rand = new Random();
  32.  
  33.         int dir = 1;
  34.  
  35.         public BallGame()
  36.         {
  37.             Graphics = new GraphicsDeviceManager(this);
  38.             Content.RootDirectory = "Content";
  39.  
  40.             Graphics.PreferredBackBufferWidth = 800;
  41.             Graphics.PreferredBackBufferHeight = 480;
  42.         }
  43.  
  44.         /// <summary>
  45.         /// Allows the game to perform any initialization it needs to before starting to run.
  46.         /// This is where it can query for any required services and load any non-graphic
  47.         /// related content.  Calling base.Initialize will enumerate through any components
  48.         /// and initialize them as well.
  49.         /// </summary>
  50.         protected override void Initialize()
  51.         {
  52.             // TODO: Add your initialization logic here
  53.  
  54.             base.Initialize();
  55.         }
  56.  
  57.         /// <summary>
  58.         /// LoadContent will be called once per game and is the place to load
  59.         /// all of your content.
  60.         /// </summary>
  61.         protected override void LoadContent()
  62.         {
  63.             // Create a new SpriteBatch, which can be used to draw textures.
  64.             SpriteBatch = new SpriteBatch(GraphicsDevice);
  65.  
  66.  
  67.             // TODO: use Content to load your game content here
  68.             MyFont = Content.Load<SpriteFont>("Font");
  69.             Texture = Content.Load<Texture2D>("Ball");
  70.             HalfTexture = Texture.Size() * 0.5f;
  71.            
  72.         }
  73.  
  74.         /// <summary>
  75.         /// UnloadContent will be called once per game and is the place to unload
  76.         /// all content.
  77.         /// </summary>
  78.         protected override void UnloadContent()
  79.         {
  80.             // TODO: Unload any non ContentManager content here
  81.         }
  82.  
  83.         float Velocity = 0.01f;
  84.         float VelocityM = 0.01f;
  85.         float VelocityMax = 50f;
  86.         float Acceleration = 0.9f;
  87.         float spd = 1;
  88.         int sTime = 0;
  89.         int msTime = 0;
  90.         int msTick = 10;
  91.         int counter = 0;
  92.         Vector2 Movement = Vector2.Zero;
  93.         Vector2 Rotation = new Vector2(0, 0);
  94.         int rotRadius = 5;
  95.         String TimeScore = "";
  96.         String HighScore = "";
  97.         int curTime;
  98.         int curScore = 0;
  99.         int hiScore = 0;
  100.  
  101.         /// <summary>
  102.         /// Allows the game to run logic such as updating the world,
  103.         /// checking for collisions, gathering input, and playing audio.
  104.         /// </summary>
  105.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  106.         protected override void Update(GameTime gameTime)
  107.         {
  108.             // Stopwatch...
  109.             msTime = gameTime.TotalGameTime.Milliseconds;
  110.  
  111.             sTime = gameTime.TotalGameTime.Seconds;
  112.  
  113.             if (sTime > curTime)
  114.             {
  115.                 curScore++;
  116.  
  117.                 if (curScore > hiScore)
  118.                 {
  119.                     hiScore = curScore;
  120.                 }
  121.  
  122.             }
  123.  
  124.             curTime = sTime;
  125.  
  126.             TimeScore = curScore.ToString();
  127.             HighScore = hiScore.ToString();
  128.  
  129.             int Rn = Rand.Next(2);
  130.             if (sTime*7 % 3 == 0)
  131.             {
  132.                 spd += 0.1f*dir;
  133.                 spd /= (float) Math.Tan(spd);
  134.                 counter++;
  135.  
  136.                 if (spd > 10000)
  137.                 {
  138.                     spd = 1;
  139.                 }
  140.  
  141.                 if (Rn == 1)
  142.                 {
  143.                     dir = 1;
  144.                 }
  145.                 else
  146.                 {
  147.                     dir = -1;
  148.                 }
  149.  
  150.             }
  151.  
  152.             // Allows the game to exit
  153.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  154.                 this.Exit();
  155.  
  156.             if (Keyboard.GetState().IsKeyDown(Keys.Escape))
  157.                 this.Exit();
  158.  
  159.             // Boundary Enforcer
  160.  
  161.             if (Position.X > 775 ||
  162.                 Position.X < 25 ||
  163.                 Position.Y > 455 ||
  164.                 Position.Y < 25
  165.                 )
  166.             {
  167.                 Movement *= 0;
  168.                 Velocity = 0;
  169.                 spd = 1;
  170.                 curScore = 0;
  171.             }
  172.  
  173.  
  174.  
  175.  
  176.             // Basic Controls
  177.             if (Keyboard.GetState().IsKeyDown(Keys.Left))
  178.             {
  179.                 Movement += new Vector2(-Velocity, 0);
  180.                 if (Velocity < VelocityMax & msTime % msTick == 0)
  181.                 {
  182.                     Velocity += Acceleration;
  183.                 }
  184.             }
  185.  
  186.             if (Keyboard.GetState().IsKeyDown(Keys.Right))
  187.             {
  188.                 Movement += new Vector2(Velocity, 0);
  189.  
  190.                 if (Velocity < VelocityMax & msTime % msTick == 0)
  191.                 {
  192.                     Velocity += Acceleration;
  193.                 }
  194.             }
  195.  
  196.             if (Keyboard.GetState().IsKeyDown(Keys.Up))
  197.             {
  198.                 Movement += new Vector2(0, -Velocity);
  199.  
  200.                 if (Velocity < VelocityMax & msTime % msTick == 0)
  201.                 {
  202.                     Velocity += Acceleration;
  203.                 }
  204.             }
  205.  
  206.             if (Keyboard.GetState().IsKeyDown(Keys.Down))
  207.             {
  208.                 Movement += new Vector2(0, Velocity);
  209.                 if (Velocity < VelocityMax & msTime % msTick == 0)
  210.                 {
  211.                     Velocity += Acceleration;
  212.                 }
  213.             }
  214.  
  215.             // Velocity Reset
  216.             if (Keyboard.GetState().IsKeyUp(Keys.Up) &
  217.                 Keyboard.GetState().IsKeyUp(Keys.Down) &
  218.                 Keyboard.GetState().IsKeyUp(Keys.Left) &
  219.                 Keyboard.GetState().IsKeyUp(Keys.Right))
  220.             {
  221.                 Velocity = VelocityM;
  222.  
  223.             }
  224.  
  225.            
  226.             Rotation.X = rotRadius * (float)Math.Cos(counter*spd) + (float)Math.Cos(Velocity)*Velocity*dir;
  227.             Rotation.Y = rotRadius * (float)Math.Sin(counter*spd) + (float)Math.Sin(Velocity)*Velocity;
  228.  
  229.  
  230.             // Position Reset
  231.           //  if (Keyboard.GetState().IsKeyDown(Keys.Space))
  232.        //     {
  233.                 Movement += Rotation;
  234.        //     }
  235.  
  236.  
  237.  
  238.            
  239.  
  240.  
  241.             // TODO: Add your update logic here
  242.             Position = Mouse.GetState().Position();
  243.  
  244.             Position =  new Vector2(400,240) + Movement;
  245.  
  246.             base.Update(gameTime);
  247.  
  248.         }
  249.  
  250.         /// <summary>
  251.         /// This is called when the game should draw itself.
  252.         /// </summary>
  253.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  254.         protected override void Draw(GameTime gameTime)
  255.         {
  256.             GraphicsDevice.Clear(Color.Black);
  257.  
  258.             // TODO: Add your drawing code here
  259.  
  260.             SpriteBatch.Begin();
  261.             SpriteBatch.Draw(Texture, Position - HalfTexture, Color.White);
  262.  
  263.             // Draw Hello World
  264.             string textOutput1 = TimeScore;
  265.             string textOutput2 = HighScore;
  266.  
  267.             // Find the center of the string
  268.             Vector2 FontOrigin1 = MyFont.MeasureString(textOutput1) / 2;
  269.             Vector2 FontOrigin2 = MyFont.MeasureString(textOutput2) / 2;
  270.             // Draw the string
  271.             SpriteBatch.DrawString(MyFont, textOutput1, textPos1, Color.LightGreen, 0, FontOrigin1, 1.0f, SpriteEffects.None, 0.5f);
  272.             SpriteBatch.DrawString(MyFont, textOutput2, textPos2, Color.LightGreen, 0, FontOrigin2, 1.0f, SpriteEffects.None, 0.5f);
  273.             SpriteBatch.End();
  274.  
  275.  
  276.            
  277.  
  278.             base.Draw(gameTime);
  279.         }
  280.     }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement