Anyway, At the moment I'm attempting to create a basic Pacman Clone for practice. I plan on using this to help me understand programming more and get a stronger grasp on programming for games, as well as strengthening my knowledge of Finite State Machines. At the moment I've only just started creating the basics, and So far I've managed to get an image on the screen, and Now I'm trying to get that image to move about by pressing the arrow keys (UP, DOWN, LEFT, RIGHT). I've tried to use KeyPress, but it won't work (It tells me that a '(' is expected, even if I add One and then click away)
So, Does anyone have some advice on how to do this? I'm using XNA 4.0, conding in Microsoft Visual C# in Visual Studio 2010 Professional.
Here's the Source Code:
[spoiler]
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 Pacman
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
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>
//This is to create a renderable texture, specifically a 2d texture.
Texture2D PacmanTestSprite; //Same as C++ a ";" is needed to show the end of a line. Within the code, Texture2D is the identifier,
//And "PacmanTestSprite" is the actual name of the texture that I will be using. Since, for now, I
//Only need to have a basic sprite to move about, it will not be animated until later.
//*****************************************************************************************************
Vector2 spriteSpeed = Vector2.One; //This will come into use later, when I use this variable to move the sprite around the screen.
//*****************************************************************************************************
Vector2 spritePosition = Vector2.Zero; //This sets the co-ordinates for the sprite to be drawn at. In this case, it draws it at the origin,
//Or (0,0) I plan on changing this once I find out how large my window is.
//*****************************************************************************************************
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
PacmanTestSprite = Content.Load<Texture2D>("PacmanTest"); //This is telling the program that the 2D texture I created earlier,
//PacmanTestSprite, is to be loaded as the image in the Content folder named
//pacmantest.
//******************************************************************************************************
// 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)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
int MaxX = graphics.GraphicsDevice.Viewport.Width - PacmanTestSprite.Width;
int MaxY = graphics.GraphicsDevice.Viewport.Height - PacmanTestSprite.Height;
//int MinX = 0;
//int MinY = 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);
// TODO: Add your drawing code here
//Draw the sprite
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(PacmanTestSprite, spritePosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
[/spoiler]