Jump to content
  • Advertisement
Sign in to follow this  
pat_stevenson

XNA game studio - complete begginer overwhelmed - rotation

This topic is 2931 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Everyone.

I'm Pat, I'm from Scotland, and I am studying a degree entitled "Computer Games Technology" which aims to turn me into a competent game programmer within 4 years.

I'm just into my 4th week at university and unlike most of my peers, I have no experience of programming until I started the course. Right now in my first trimester I have two classes, one is called "Introduction to game development" and the other is called "Introduction to Programming". In Intro to programming I am learning the C++ language, at a slow pace and it is sinking in quite well. On the other hand, intro to games development I am learning the C# language and using XNA game studio. Now in this class its not so important to learn the C# language because the aims of this class is just to show whats involved in creating a game. We basically have to cobble together a 2d game and learn about the process.

Now here is where I start having problems. Because this class is so rushed I am having a hard time coming to terms with all the concepts within XNA game studio, and C# in general. So far I've been able to display a sprite on the screen, get the sprite moving across the screen, get user input from the keyboard(just the basics moving it up,down, left, right) and creating a class for the sprite. What I've now been asked to do is make the sprite rotate when the up key and down key are pressed. This is probably really easy but as I've said we're rushing through this course without getting the basics cemented and I'm losing track of what all the code I've written actually does. So can anyone help me with this, and maybe explain to me where I've went wrong and what I need to add? If you want to have a look at the notes I'm working from I can also PM you a link.

Game1.cs


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 SpaceShooter
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1: Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D starfield;
Sprite spaceship;
Vector2 position;
float rotation = 0;
Texture2D image;
Vector2 origin;





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

/// <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 this.Content to load your game content here
starfield = Content.Load<Texture2D>("Starfield");
spaceship = new Sprite(Content.Load<Texture2D>("Spaceship"),
new Vector2(graphics.PreferredBackBufferWidth / 2,
graphics.PreferredBackBufferHeight / 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();

Vector2 move = Vector2.Zero;

if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
move.X--;
}

if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
move.X++;
}

if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
move.Y--;
}

if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
move.Y++;
}

spaceship.MoveBy(move);



spaceship.MoveBy(move);

// TODO: Add your update logic here
spaceship.MoveBy(new Vector2(0, 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);
spriteBatch.Begin();
spriteBatch.Draw(starfield, new Vector2(0, 0), Color.White);
spaceship.Draw(spriteBatch);
spriteBatch.End();



// TODO: Add your drawing code here


base.Draw(gameTime);
}
}
}





Sprite.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 SpaceShooter
{



class Sprite
{
Texture2D image;
Vector2 position;
Vector2 origin;
float rotation;
float rotate = 0;

public Sprite(Texture2D image, Vector2 position)
{
this.image = image;
this.position = new Vector2(position.X, position.Y);

origin = new Vector2(image.Width / 2, image.Height / 2);
}

public void Draw(SpriteBatch spriteBatch)

{
spriteBatch.Draw(image,
position,
null,
Color.White,
rotation,
origin,
1,
SpriteEffects.None,
0);

}

public void MoveBy(Vector2 move)
{
position += move;
}

public void SetPosition(Vector2 position)
{
this.position.X = position.X;
this.position.Y = position.Y;
}

public Vector2 GetPosition()
{
return new Vector2(position.X, position.Y);
}

public void RotateBy(float rotatation)
{
rotation += rotate;
}

public void SetRotation(float rotate)
{

}

public void GetRotation()
{

}

}
}






i know really should be asking this stuff in class but when there are 40 people to one lecturer then it can be difficult getting help.

Thank you
Pat.

[Edited by - pat_stevenson on October 20, 2010 11:02:45 AM]

Share this post


Link to post
Share on other sites
Advertisement
Just call the Sprite.RotateBy() function where you handle the Up/Down keys.


if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
spaceShip.RotateBy(1.0f);
}


Share this post


Link to post
Share on other sites
Just a tip for you, instead of having methods like "GetRotation()" and "SetRotation(float value)", you should use properties. It accomplishes the same thing but makes for much cleaner looking and readable code, and can give you some added advantages. You could replace that, for instance, with this:


private float rotation;
public float Rotation{
get{return rotation;}
set{rotation = value;}
}



Now, anytime the name "Rotation" (the name of the PROPERTY) is used, it's just like calling those methods indirectly. For instance, the statement "float x = sprite.Rotation;" will call the "get" part. And "sprite.Rotation = 0f;" would call the "set" part. It works just like a regular variable, but the property is actually retrieving and modifying the private field. Hope that makes sense and helps you write better code.

Share this post


Link to post
Share on other sites
Hi Pat

I think we are reading from the same notes (Dr John Sutherland's?) I'm at Paisley...

I'm finding the C# notes almost incomprehensible to follow too, and have only just moved on to the rotation bit. WAs seriuosly thinking of jacking it in because of this, but what I'm currently doing is largely ignoring the class notes adn working my way through Rob Miles : Introduction to Programming Through Game Development Using Microsoft XNA Game Studio (academic edition)(free download) - and that explains most things far more clearly, plus there is samnple code to look at when your code doesn't work.

I don't remember where I downloaded it from, but if you mail me I cam email it
to you.

Rosy



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!