Click here to Skip to main content
15,896,606 members
Articles / Mobile Apps

Zune HD Level Program Written in XNA

Rate me:
Please Sign up or sign in to vote.
4.99/5 (58 votes)
25 Sep 2009CPOL19 min read 121.6K   615   39  
A level program written for the Zune using the XNA framework.
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace AccelerometerTest
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteFont mainFont;
        SpriteBatch spriteBatch;
        AccelerometerState accelState;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            // Frame rate is 30 fps by default for Zune.
            TargetElapsedTime = TimeSpan.FromSeconds(1 / 30.0);
            AccelerometerCapabilities AccelCaps = Accelerometer.GetCapabilities();
            if ((AccelCaps.IsConnected) && (AccelCaps.HasXAxis) && (AccelCaps.HasYAxis) && (AccelCaps.HasZAxis))
            {

            }
            else
            {
                this.Exit();
            }


        }

        /// <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);
            mainFont = Content.Load<SpriteFont>("Zegoe");

            // TODO: use this.Content to load your game content here
        }

        /// <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)
        {

            accelState =  Accelerometer.GetState();
            if (accelState.IsConnected)
            {
                
            }
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            TouchCollection tc = TouchPanel.GetState();
            if (tc.Count > 0)
            {
                this.Exit();
            }
            // 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.Black);
            Vector2 XPosition = new Vector2(20, 10);
            Vector2 YPosition = new Vector2(20, 60);
            Vector2 ZPosition = new Vector2(20, 110);
            Vector2 LogoPosition = new Vector2(20,250);
            Vector2 RotationPosition = new Vector2(0.5f, 0.5f);

            spriteBatch.Begin();

            spriteBatch.DrawString(mainFont, "J2i.net", LogoPosition, Color.White, -270, RotationPosition, 2.5f, SpriteEffects.None, 0);
            spriteBatch.DrawString(mainFont, String.Format("X:{0:0.00}",accelState.Acceleration.X), XPosition, Color.White);
            spriteBatch.DrawString(mainFont, String.Format("Y:{0:0.00}",accelState.Acceleration.Y), YPosition, Color.White);
            spriteBatch.DrawString(mainFont, String.Format("Z:{0:0.00}",accelState.Acceleration.Z), ZPosition, Color.White);

            spriteBatch.End();

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering. I've largely developed solutions that are based on a mix of Microsoft technologies with open source technologies mixed in. I've got an interest in astronomy and you'll see that interest overflow into some of my code project articles from time to time.



Twitter:@j2inet

Instagram: j2inet


Comments and Discussions