A few weeks in development, but it is finally here! The first complete game tutorial for XNA. The game covered here is Paddles, a Pong clone. I brought this tutorial from 110 pages to around 55 (thats what took so long :P) Im also a perfectionist, wanted to make sure it was very good quality before I released it. Videos will be uploaded in the next few days. HD ones are paying members only (premium members or XNA subscribers).
Video Completion: 100%
Here are the download links:
Comments
seriously wtf!
I mean, thanks for putting tutorials online! It's well described and everything, but for a noob like me it's not possible to finish this tutorial with all this mistakes in the .pdf! Not to mention that the source-code provided by you differs A LOT from the .pdf!
There are some little mistakes, like missing _ in the variablenames or like 48 and 53
Page 48:
public void Update()
{
if ((position.Y <= 0) || (position.Y >= 800))
velocity.Y *= -1;
if (position.X <= 0)
{
//Computer gets a point
Reset();
}
else if (position.X >= 600)
{
//Player gets a point
Reset();
}
position += velocity;
boundary = new Rectangle((int)position.X, (int)position.Y, 10, 10);
}
Page 53:
public void Update()
{
if ((position.Y <= 0) || (position.Y >= 600))
velocity.Y *= -1;
if (position.X <= 0)
{
//Computer gets a point
Reset();
}
else if (position.X >= 800)
{
//Player gets a point
Reset();
}
position += velocity;
boundary = new Rectangle((int)position.X, (int)position.Y, 10, 10);
}
but there are also some major mistakes and I read carefully the whole tutorial over and over again and I coudn't figure out what to do!
Page 29:
ScreenManager.cs
protected override void LoadContent()
{
....
spriteFont = Game1.content.Load("basic");
....
}
and
Page 51:
GameScreen.cs
public void LoadContent()
{
pixel = Game1.content.Load("singlePixel");
}
Game1.content is marked because Game1. only provides "Equals" and "ReferenceEquals"!
I tried to take your source code for help, but there is not even a LoadContent() in both ScreenManager.cs and GameScreen.cs!
So, what should I do? Do you need more information?
It is not made for XNA 2.0+
This tutorial was made for XNA 1.0 Refresh and that is all. XNA 2.0+ removed/changed some stuff that makes some of it now work. I honestly do not recommend this tutorial anymore. I would remove it, but there are still people out there using it for 1.0. Check out the Basic Training then the Advanced Techniques videos.
Basic training then!
oh ok, thanks for the fast answer!
well I'm gone try out the basic training then!
didn't want to be offensive, just had a bad day!
I understand
Oh believe me I understand. I would update the paddles tutorial but it is really not worth it. The amount of time it would take would be very long. The next Complete Games will be out in a week or two and you have the Basic Training and Advanced Techniques videos to watch.
worked on xna 3.0
I managed to complete the tutorial on XNA 3.0 following the pdf. Sure, there were some mistakes, but I feel it's useful to learn how to debug a program.
Correction in pdf
Hi Whiplash, thanks for the great tutorial. I'm still half way through the tutorial.
I updated the following codes to make it work.
Text.Draw(SpriteBatch spriteBatch) method on page 29:
public void Draw(SpriteBatch spriteBatch)
{
//Draw the string
spriteBatch.DrawString(ScreenManager.spriteFont, this.text, this.position, this.aColor);
}
Text.Update() method on page 39:
public void Update()
{
if (this.active && this.aColor == this.bColor)
this.aColor = this.sColor;
else if (!this.active && this.aColor == this.sColor)
this.aColor = this.bColor;
}
The links
The links to the videos seams to be down....
Fixed
Fixed
Help!
How do I make it where I can press Escape in the game and it will take me back to the main menu.
sorry, i did'nt read the whole text above ...
--- erased my comment. sry.
how to make work on xbox with gamepad controller?
Whiplash,
Your tutorial is really good. I got the source code up and I'm trying to get gamepad controller to work - in place of the keyboard. Is there a tutorial for this available, or source code? I'm familiar with C++ but this is my first go around with C#.
Any help would be great.
Thanks,
Ernest
Gamepad Tutorial
Go here - http://www.phstudios.com/tutorials/xna/AdvancedTechniques/media/
Tutorial number 06 is Gamepad Movement. The sample can be found here - http://www.phstudios.com/?q=node/108
Thanks
Thanks for a great tutorial. This was the first thing I have done in C# and with my C++ knowledge I could understand most of what was going on. There are a couple of minor mistakes in the document, but by the end I think most(if not all) are sorted out. Thanks again!
Awww
Links are down....
Fixing them
Thanks, I will fix them tonight (links were moved to a new server).
It's really great that there
It's really great that there are people like you creating extensive but not too extensive tutorials for creating games :-). Paddles isn't hard to understand I think but your tutorial can really help to code the thing faster. Please don't stop to develop cool games and game design tutorials!
Bye, Stefan Lyrics
Issues with text highlighting
Hi, I've been following the PDF tutorial and I'm having some trouble with the black/yellow menu selection highlighting. I was debugging a little, and the selection variable is increasing and decreasing when you press Up and Down, but there is no actual highlighting going on. Your document has some major issues with Text.cs, mainly the fact that early in the tutorial you declare Colors aColor, bColor and sColor, but never just "color", and sColor is never used. That confused me for a bit so I replaced it, but "sColor" was assigned to Yellow and the drawString is calling that, which makes everything yellow upon creation. Setting it to aColor makes it black, but there's never any Yellow coloring.
Anyway, your Text.cs in the source code download is quite a bit ahead of where I actually am so I can't make comparisons with it.
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PaddlesTutorial.GameScreens;
namespace PaddlesTutorial {
class Text {
string text;
Vector2 position;
public Vector2 Position {
get { return position; }
set { position = value; }
}
Color aColor, bColor, color;
bool active = false;
public bool Active {
get { return active; }
set { active = value; }
}
public Vector2 Size {
get { return ScreenManager.spriteFont.MeasureString(this.text); }
}
public Text(string text, Vector2 position) {
this.text = text;
this.position = position;
this.aColor = this.bColor = Color.Black;
this.color = Color.Yellow;
}
public void Update() {
if(this.active && this.color == this.bColor)
this.color = this.aColor;
else if(!this.active && this.color == this.aColor)
this.color = this.bColor;
}
public void Draw(SpriteBatch spriteBatch) {
spriteBatch.DrawString(ScreenManager.spriteFont, this.text, this.position, this.aColor);
}
}
}
Here's the MenuScreen class if it makes any difference.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace PaddlesTutorial.GameScreens {
public enum MenuState {
Main,
Help
}
class MenuScreen {
List menuEntries, helpText;
MenuState mState = MenuState.Main;
int selection;
public MenuScreen() {
selection = 0;
#region Menu Items
menuEntries = new List();
menuEntries.Add(new Text("Play Game", new Vector2(30, 80)));
menuEntries.Add(new Text("Help", new Vector2(30, 5.0f)));
menuEntries.Add(new Text("Quit", new Vector2(30, 5.0f)));
#endregion
#region Help Items
helpText = new List();
helpText.Add(new Text("Help", new Vector2(30, 80)));
helpText.Add(new Text("Up Arrow - Move Up", new Vector2(30, 5.0f)));
helpText.Add(new Text("Down Arrow - Move Down", new Vector2(30, 5.0f)));
helpText.Add(new Text("Escape (In Main Menu) - Quits the game", new Vector2(30, 5.0f)));
helpText.Add(new Text("< Press Backspace to go back to the Main Menu", new Vector2(30, 5.0f)));
#endregion
}
public void UpdateTextPositioning() {
#region Menu Items
for(int i = 1; i < menuEntries.Count; i++)
menuEntries[i].Position += new Vector2(0, menuEntries[i - 1]
.Position.Y + menuEntries[i - 1].Size.Y);
#endregion
#region Help Items
for(int i = 1; i < helpText.Count; i++)
helpText[i].Position += new Vector2(0, helpText[i - 1]
.Position.Y + helpText[i - 1].Size.Y);
#endregion
}
public void Update(GameTime gameTime) {
#region Main
if(mState == MenuState.Main) {
if(ScreenManager.keyboard.Down) {
if(selection < menuEntries.Count - 1)
selection++;
else
selection = 0;
} else if(ScreenManager.keyboard.Up) {
if(selection > 0)
selection--;
else
selection = menuEntries.Count - 1;
} else if(ScreenManager.keyboard.PauseOrQuit)
ScreenManager.isExiting = true;
for(int i = 0; i < menuEntries.Count; i++) {
if(i == selection) {
if(!menuEntries[i].Active)
menuEntries[i].Active = true;
} else {
if(menuEntries[i].Active)
menuEntries[i].Active = false;
}
menuEntries[i].Update();
}
}
#endregion
#region Help
// help area
#endregion
}
public void Draw(SpriteBatch spriteBatch) {
#region Main
if(mState == MenuState.Main) {
foreach(Text t in menuEntries) {
t.Draw(spriteBatch);
}
}
#endregion
#region help
else {
foreach(Text t in helpText) {
t.Draw(spriteBatch);
}
}
#endregion
}
}
}
Nobody else said anything about that issue
If you keep going, does it fix itself? Nobody else said anything about that issue to me since I fixed something a long time ago. It you still have problems, let me know.
My fault
Hi, sorry, I managed to fix the problem - it was just a misplaced if/else bracket. Later, I had a similar problem with loading a computer object but once I found the problem, everything worked. Great tutorial, by the way.
Another Problem lol
I marked the errors within the code...I am using the pdf tutorial as my guide here, and I dont know why I am getting errors
Thanks for the help
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Pong_Evolved.Game_Screens;
namespace Pong_Evolved.Game_Screens
{
public enum MenuState
{
Main,
Help
}
class MenuScreen
{
List menuEntries, helpText;
MenuState menuState = MenuState.Main;
int selection;
public MenuScreen()
{
selection = 0;
#region Menu Items
menuEntries = new List();
menuEntries.Add(new Text("Play Game", new Vector2(30, 80)));
menuEntries.Add(new Text("Help", new Vector2(30, menuEntries[0].Position.Y + 5.0f)));
menuEntries.Add(new Text("Quit", new Vector2(30, menuEntries[1].Position.Y + 5.0f)));
#endregion
#region Help Items
helpText = new List();
helpText.Add(new Text("Help", new Vector2(30, 80)));
helpText.Add(new Text("Up Arrow - Move Up", new Vector2(30, helpText[0].Position.Y + 5.0f)));
helpText.Add(new Text("Down Arrow - Move Down", new Vector2(30, helpText[1].Position.Y + 5.0f)));
helpText.Add(new Text("Escape (In Main Menu) - Quits the game", new Vector2(30, helpText[2].Position.Y + 5.0f)));
helpText.Add(new Text("< Press Backspace to go back to the Main Menu", new Vector2(30, helpText[3].Position.Y + 5.0f)));
#endregion
}
public void Update(GameTime gameTime)
{
#region main
#endregion
#region help
#endregion
}
public void Draw(SpriteBatch spriteBatch)
{
#region main
if (menuState == MenuState.Main)
{
foreach (Text t in menuEntries) t.Draw(spriteBatch);
}
#endregion
#region help
else
{
foreach (Text t in helpText) t.Draw(spriteBatch);
}
#endregion
menuScreen.Draw(spriteBatch); <--error here "The name 'menuScreen' does not exist in the current context C:\...."
}
}
}
However i declared this in ScreenManager.cs as menuScreen....also:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Pong_Evolved.Game_Screens;
namespace Pong_Evolved.Game_Screens
{
public enum GameState
{
Menu,
Play
}
class ScreenManager: DrawableGameComponent
{
public static GameState gameState = GameState.Menu;
public static Keyboard keyboard;
public static SpriteFont spriteFont;
public static bool isExisting = false;
MenuScreen menuScreen;
GameScreen gameScreen;
SpriteBatch spriteBatch;
public ScreenManager(Game game)
: base(game)
{
gameScreen = new GameScreen();
menuScreen = new MenuScreen();
keyboard = new Keyboard();
}
protected override void LoadContent()
{
spriteFont = Game1.content.Load("Text"); <--error "An object reference is required for the non-static field, method, or property 'Pong_Evolved.Game1.content' C:\..."
spriteBatch = new SpriteBatch(GraphicsDevice);
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
menuScreen.Draw(spriteBatch);
base.Draw(gameTime);
spriteBatch.End();
}
}
}
Remove that line
Had my last test today so I was studying.
menuScreen.Draw(spriteBatch); <--error here "The name 'menuScreen' does not exist in the current context C:\...."
You can simply remove that line, as it will do nothing since you are already inside the MenuScreen. Try that and see if it helps!
Paddles Screen Manager
I keep getting an error on the keyboard = new Keyboard();
Here is the code:
namespace PongClone.GameScreens
{
public enum Gamestate
{
Menu,
Play
}
class ScreenManager: DrawableGameComponent
{
public static Keyboard keyboard;
public static Gamestate gameState = Gamestate.Menu;
public static SpriteFont spriteFont;
public static bool isExiting = false;
MenuScreen screen_Menu;
GameScreen screen_Game;
SpriteBatch spriteBatch;
public ScreenManager(Game game)
: base(game)
{
screen_Game = new GameScreen();
screen_Menu = new MenuScreen();
keyboard = new Keyboard();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
}
}
}
I can't seem to find the problem.
Thanks for the tuts they are great! I have already learned a ton!
Knox
Change the above coding
First off, what does (or did if its fixed) the error say?
Inside the constructor, you have:
keyboard = new Keyboard();
Delete that line, and change this line:
public static Keyboard keyboard;
to this:
public static Keyboard keyboard = new Keyboard();
See if that helps any. If not, let me know what the error says.
Awesome
This was a really sweet tutorial, and everything works, but could you tell me how i could make it so you can go back from the game to the main menu by pressing escaped i tried to look over some stuff but i don't get how i could do it.
Changes
Here are a few changes you will need to make to get this to work:
Inside the Keyboard class, modify the Quit attribute to look like this:
public bool Quit
{
get
{
return IsNewKeyPress(Keys.Escape);
}
}
Inside the GameScreen class, add the following in the Update method:
if (ScreenManager.keyboard.Quit)
ScreenManager.gameState = GameState.Menu;
This will act as a pause screen now.
great
The tutorial is great. Thanks for sharing. -- 3dmultiplayergames.net
Help
I haven't quite finished the tutorial yet, but I keep getting a NullReference error on the ScreenManager.cs page.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace Pong.GameScreens
{
public enum GameState
{
Menu,
Play
}
class ScreenManager:DrawableGameComponent
{
public static GameState gameState = GameState.Menu;
public static Keyboard keyboard;
public static SpriteFont spriteFont;
public static bool isExiting = false;
MenuScreens screen_Menu;
GameScreens screen_Game;
SpriteBatch spriteBatch;
public ScreenManager(Game game)
: base(game)
{
screen_Game = new GameScreens();
screen_Menu = new MenuScreens();
keyboard = new Keyboard();
}
protected override void LoadContent()
{
spriteFont = Game1.content.Load(@"Content\basic");
base.LoadContent();
}
public override void Draw(GameTime gameTime)
{
spriteBatch.Begin(); <----- (right there)
screen_Menu.Draw(spriteBatch);
base.Draw(gameTime);
spriteBatch.End();
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
}
also, if i delete the begin and end thing i get it here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Pong.GameScreens;
namespace Pong
{
class Text
{
string text;
Vector2 position;
public Vector2 Position
{
get { return position; }
set { position = value; }
}
Color aColor, bColor, color;
bool active = false;
public bool Active
{
get { return Active; }
set { Active = value; }
}
public Text(string text, Vector2 position)
{
this.text = text;
this.position = position;
this.aColor = this.bColor = Color.Black;
this.color = Color.Yellow;
}
public void Update()
{
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.DrawString(ScreenManager.spriteFont, this.text, this.position, this.color); <------ (right there)
}
public Vector2 Size
{
get
{
return ScreenManager.spriteFont.MeasureString(this.text);
}
}
}
}
but once again, that is only if i delete the begin end part. PLEASE HELP! Awesome tutorial so far though.
Initialized
Make sure everything is Initialized before you reach that line of code.
Most importantly, make sure ScreenManager has the spriteFont initialized. NullPointers refer to a null object. So one of those in that line is set to null.
Fixed it, (but got another problem...)
I fixed the null problem, but now i am getting a stack overflow error on my Text.cs page, here it is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Pong.GameScreens;
namespace Pong
{
class Text
{
string text;
Vector2 position;
public Vector2 Position
{
get { return position; } <------(says the stack overflow error is right there)
set { position = value; }
}
Color aColor, bColor, color;
bool active = false;
public bool Active
{
get { return Active; }
set { Active = value; }
}
public Text(string text, Vector2 position)
{
this.text = text;
this.position = position;
this.aColor = this.bColor = Color.Black;
this.color = Color.Yellow;
}
public void Update()
{
if
(this.active && this.aColor == this.bColor)
this.color = this.aColor;
else if
(!this.active && this.color == this.aColor)
this.aColor = this.bColor;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.DrawString(ScreenManager.spriteFont, this.text, this.position, this.aColor);
}
public Vector2 Size
{
get
{
return ScreenManager.spriteFont.MeasureString(this.text);
}
}
}
}
don't like to be a bother, but i guess i just don't pay enough attention, lol, thanks!
Breakpoint
The code looks correct. It shouldnt cause a stack overflow. Add a breakpoint at the line (click just to the left of the long vertical lines with the [-] to add a breakpoint). Press F5 to run in debug mode, then keep pressing F11 to see what is going on.
Everything figured out.
I got it all figured out and my pong game is up and running. I even added in a cool background by going to Game1 and at the bottom i added a method called DrawBackground() i declared my background as a texture2d, then i loaded it in the LoadContent() method of my Game1.cs in the DrawBackground() method i put this: Rectangle screenrectangle = new Rectangle(0, 0, 800, 600);
spriteBatch.Draw(Background, screenrectangle, Color.White);
works just fine and you have a cool background. but there is one thing that i want from you...
could you put on a link for an audio file for the boing sound. i tried myself and it always says that it cant find it. i know it is there and working but it gives me an error saying it cant find it, something to help would be greatly appreciated. Great Tutorial really taught me a lot!
Which file are you using?
My sample never had any audio, are you trying to add it yourself?
Yes
Yeah, i made the sound myself on audacity and i tried to add it, (i know how) but it says it can't find it. and it is most definitely there.
Solution Explorer
Sorry for the late response, I had a bunch of presentations and tests to do. Did you add it to your solution explorer in the content area? The error says it cannot locate the file, or cannot load it in the program?
If it loads into your solution explorer fine, do the following (if you havent done so):
Field at the top of the game.
SoundEffect bounce;
Load the sound effect inside the LoadContent method:
bounce = Content.Load("fileName");
where "fileName" is the name of the sound (asset name).
When you want to play the effect, simply do:
bounce.Play();
If you cannot load it into your solution, try either saving it again (if you have the sound project still), or save it to a different location. This "should" fix the issue. If you have any other errors, let me know!
Where
I hade an xact file, but i tried it your way instead and i want to do the boing sound whenever it hits the paddle or a wall. so i tried to do it inside the GameScreens.cs place and it won't let me say Content.Load. I'm not sure if i should do it somewhere else, or change back to an xact file or what. the reason i am doing it in GameScreens is because i play the sound right after i check collisions in the update method. so... yeah. is that the right place to do it? or am i just not doing it right?
Game1.cs
You need to do this inside Game1.cs where you have access to the content manager. I would make the SoundEffect a public static then. Basically do the same thing with the SoundEffect as we did for the single pixel and the spritefont object.
...
I initialized everything, but you are right, spriteBatch in the ScreenManager.cs is set to null... how do I undo that?
I love this game
Hey, I really like this game. Thanks for all the effort.
Peter
http://www.dealsvista.com
Done with some touches!
Hey Whiplash,
Thanks for all your prompt responses!
I just finished the tutorial about an hour ago and decided to start fully understanding what was going on. I think the first thing I wanted to try and understand was the GameScreens idea... it was actually what I really needed as my last game didn't do that... it started out rendering the game and finished with it.... no title, menu, anything :P so I decided to try to understand the game by making a new IntroScreen class... so basically what it does is display a logo when the game starts and waits for enter to be pressed, then it goes to the MenuScreen as per your demo... i also edited the ball and paddle to have separate custom sprites, and changed the ball to 20x20 and the paddle to 20x80. This, is for me to learn more about XNA and your method of programming. Even though I have completed the tutorial i stil don't 100% get all of it... probably 85%+... I plan to fiddle with it as much as I can so I know what it is all about. When I feel it's a bit more funky, I will post a YouTube video of it for other to see what can be done with your tutorial.
I had a few questions and comments:
- I noticed that the ball only travels in 45 degree angles( i think) ? such as NE, NW, SE, SW... is there anyway to make it so that these angles are 'random'...
for example if the ball hits my paddle coming SW, it can bounce off either SE, or NE... and the angle can be slighty different or random every time... obviously as u had suggested, this cannot be a straight line (or could it lol)...
i was also thinking of implementing an idea that bounce of the paddle can be turned in to a user controller SHOT ... for example.. if the user is holding down the Y button, then the ball will always bounce north(e/w), if it is the A buttom, the ball will always bounce south (e/w).... and if the B button is held then perhaps the ball is hit straight (but faster), or something like that I don't know... I bet I could implement this... but I am a total newb when it comes to angles. When I made my own Pong clone is C++/Allegro I made it ust a regular 45 degree bouncer... 4 diagonal directions basically. Other clones I saw had angles based on a sense of randomness... and not based on the whole 45 degree thing. Could you perhaps give me advice on this math ? If you can help me with this, perhaps I could "add" to your tutorial with this idea of a "user SHOT" that would add more control and chaos to a regular pong game... and enhance the learning experience.
- Also... if you do have time, please take a look at my Platformer Scroller that I coded in C++ and Allegro:
http://www.youtube.com/watch?v=WlX25SDE5ps&feature=channel_page
It is currently being worked on on the side.... my main focus now is learning XNA, C# and proper programming practice so that I can learn to re-create my platformer and make it even better on XNA... and then move on to a 3D platformer. Would you ever consider doing a basic tile-scrolling or object-based platformer? If you do I would love to be involved as I do have some experience doing that, however, using your methods of the GameScreen and better understanding of C# and OOP, it can turn out to be a much more flexible engine, and a greater learning experience.
Anyway, sorry If I'm a bother, but all this is so much fun, and there is nothing better than learning to program games and helping others do so as well,
Cheers to u and ur efforts man!!!
Keep up the GREAT work.
Congratulations!
Congratulations on completing the first tutorial!
It is a great thing that you are building on top of it and wanting to learn more about it. That will help you out in the long run. Nice idea to add an intro screen. What 15% are you not sure about?
As for your degree and math request, I will write something up for you this weekend. Want me to cover trigonometry as well?
Will check that youtube link out after work (2 hours). As for the 2d scroller, I can do that for Game 4. I will go ahead and post the upcoming game tutorial lists now so you can see them. Just look at the front page for the latest blog entries.
You are no bother! I love helping people out, which is why I made this site.
Thanks :)
2D Scrolling Tutorial, Math and questions about scrolling
Hey Wiplash,
Please cover all you feel is necessary. I am so 'out of it' since I attended high school.
My other interests in terms of Maths/Angles and such is coding the following:
i have a Player on screen who basically shoots in the direction in which the cursor (either controller with the mouse or the Right thumbstick) is placed... how do i calculate these trajectors... basically i have player.x, player.y and cursor.x and cursor.y.... how do i get the projectile from player to cursor... and such... I am imagining a 2d scrolling multiplayer deathmatch game - imagine the possibilities :) However, before I even think of this, I need to know my angles, as Player won't just be shooting left or right, but at any angle in which he aims at... creating for a very flexible game! I think this relates a lot to my request for an angled pong ball movement tutorial... and has to do with trig as well... so yeh... whatever you can help with will be mostly appreciated and I know it will help a lot of others who are making 2D games.
As for the 2d scrolling tutorial, I am really glad you are attempting this... please cover tile scrolling in this, as the concept of having a level, and ONLY displaying the appropriate part of it (as in around the player) totally boggles me (for now at least). I managed to do it in c++/allegro as u can see from my 2d scroller on youtube, but I don't know how to implement that in XNA. I think the most important thing in a 2d scroller is the ability to load custom maps, to be able to actually scroll through a level that is bigger than the screen, to be able to load a custom tileset and 'tile' the map according to the level/map file, and also collision, and basic gravity/jumping. The rest will fall in to place after this.
A quick question...
In allegro I used a buffer bitmap to draw my entire level in to... basically drawing all my sprites/bitmaps on to my buffer bitmap, then I would draw the appropriate chunk of that buffer to the screen based on my camera location (which would basically centre itself around the player). How would I implement this in XNA? I have already made a basic tile display 'game' where u can move a person around (no gravity, just --/++x and y). but the tiles array is such that it only fits the screen, and I want to be able to implement the concept of scrolling through the level. The whole level is 'alive' in the memory, but only what the camera covers is displayed on screen. Once way I thought this would be possible is to make a constantly updated Texture2D buffer... everything draws on and is drawn on the screen at the end based on the 'camera' position.... blah.... this is confusing me. I guess I am trying to duplicate what I did in C++ and it is intimidating me slightly, as i'm trying to learn C#, advanced OOP topics (such as inheritance and polymorphism), XNA and also keep a flexible structure to my program! :) I guess I just gotta be patient :)
Thanks again man,
Cheers.
MATH :P
Hey man,
I was wondering if you got a chance to do any of those math tutorials? Just checking, no pressure or annoyance intended :)
Hope things are well with you :)
I am currently working on a Space shooter using your ScreenManager code from PaddlesTutorial, and some ideas i got from the XNA Creators Club 2D Demo... I plan to soon read your 2nd tutorial and update my shooter based on some of the stuff you have implemented in it such as Pause sceens and ships actually turning...
I also have a question about Random numbers...
for example... i have an EnemyManager class that basically stores Lists of enemies and updates them with a foreach loop... and.... say I have 3 EnemyFighter objects on screen. In each of them, there is a random choice of a direction to make, SouthW, South, SouthE... funny thing is.. they make exactly the same move ? Shouldn't it be random for each ship? I mean each instance of the EnemyFighter object asks for a random number and then makes it's movement update. Also... the fighters fire randomly.... if the random number is 5 (from Random.Next(1,20)) then fire... this behavior works quite well, but all the ships on the screen do exactly the same thing lol.... it kinda looks cool sometimes, but it's also ridiculous... as I want each ship to 'do it's own thing'... this is baffling me! Would you know 'what the deal is' ? lol...
Cheers
Sorry
This week is really crazy for me, I have three tests (sucks). I will have the tutorials finished by the end of the weekend though.
The random numbers should work, try to limit the chances a bit more, (1,100) or so. If you really want to go all out, you can create an AI system that will look at your ships movement, its own ships direction and check to see if the bullet will have a chance to hit you or not. If the chances are low that it will hit, have the ship move closer instead of firing.
I did the following to randomly generate the position, BUT they would all move to your ship.
Random r = new Random();
Enemy e = new Enemy(p);
e.Position = new Vector2(r.Next(0, viewport.Width), r.Next(-50, 0 - (e.Texture.Height / 2)));
where p is the player object.
Randomizing on the computer is not true randomizing, there is a small chance you can get the same number and even right after the other. But I have had no problems with my above code.
Other Tutorials
I also wanted to ask about the other tutorials? I only see Pong here but I here talk of a space shooter and a 3rd one? Is that available for download(the tutorial) or it still i nthe works? Cheers.
2nd Tutorial
Hey,
The second tutorial is here -> http://www.phstudios.com/?q=node/49
The third will be out in a bit (I am doing the XNA Basics first due to requests)
GameScreen object created in Game1.cs
Hey there,
Firstly,
Thanks for taking the time to write this awesome tutorial... not just to show us how to make a pong clone, but also taking the time to teach us proper structure and practice. The thing I look for most in a tutorial is for someone to show proper organization and structure, so we can take what we got in that and apply it to pretty much anything... i would rather make pong and breakout and learn my programming skills first, than jump nose first in to a huge fancy project... i think knowing those things will help in writing a fancy project. Please don't ever change the way you are doing that. You are educating people a lot more by being focused on your methods and program organization reusability...
Anyway... I was on Page 30/60 on the PDF. It is telling me to add a:
ScreenManager screenManager; to Game1.cs....
I just realized that you have to add ( i think ) this to Game1.cs... otherwise it can't see GameScreen.cs
using PaddlesTutorial.GameScreens;
If I don't use this using statememt then I get the error: Error 1 The type or namespace name 'ScreenManager' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\Neil\My Documents\Visual Studio 2008\Projects\PaddlesTutorial\PaddlesTutorial\Game1.cs 32 33 PaddlesTutorial
Am I right by adding this using statement? Or have I messed up elsehwere? I don't think so as I've been following as carefully as I can. Any help on this wuld be greatly appreciated,
Thanks & keep up the great work.
Namespaces
Thanks for the compliments :) The next game is way better, hopefully you check it out soon.
As for your problem, you only need that if the namespaces are different. You can keep it like you have it, it is fine that way.
namespaces again...
hey,
sorry to go on about this again but i just really want to get it straight
but the GameScreen object is in
namespace PaddlesTutorial.GameScreens
so technically you must put the using statement in Game1.cs right?
But I guess what your saying is, change the namespaces in the following files:
GameScreen, MenuScreen and ScreenManager to
namespace PaddlesTutorial INSTEAD of namespace PaddlesTutorial.GameScreens
?
I would just like to verify that for the sake of understanding this concept,
thanks again for the response :)
Yes, you are correct
If you do not want the using statements, you need to make sure the namespaces in all files are the same.
hi
hi