different way of learning
The best way to learn is to do. C++ and SFML didn't click for me until I started writing my own code. I rigorously copied down the examples. The key here is to change the examples. Experiment with them. Look at, say, an images functions and try them all out. As you read constantly think about how you can apply this knowledge and you'll have it in no time. The only way to learn is to do.
The only way I'm learning good programming habits is to code. I'm slowly figuring out where to apply the single responsibility principle, how encapsulation should be implemented, good design patterns / principles, etc. The only way to actually understand a subject is to rigorously practice in the subject.
The only way I'm learning good programming habits is to code. I'm slowly figuring out where to apply the single responsibility principle, how encapsulation should be implemented, good design patterns / principles, etc. The only way to actually understand a subject is to rigorously practice in the subject.
alright i made this from scratch, but the paddles dont move. anyone know whats wrong?
[source lang="csharp"] protected override void Initialize()
{
// TODO: Add your initialization logic here
KeyboardState state = Keyboard.GetState();
if (state.IsKeyDown(Keys.Up))
{
paddle1Pos.Y -= 3;
}
if (state.IsKeyDown(Keys.Down))
{
paddle1Pos.Y += 3;
}
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
ball = Content.Load<Texture2D>("ball");
paddle = Content.Load<Texture2D>("paddle");
}
/// <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)
{
int minY = 0;
int minX = 0;
int maxY = 600 - ball.Height;
int maxX = 1000 - ball.Width;
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
ballPos += ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
if (ballPos.X > maxX)
{
ballPos.X = maxX;
ballSpeed.X *= -1;
}
if (ballPos.X < minX)
{
ballSpeed.X *= -1;
ballPos.X = minX;
}
if (ballPos.Y < minY)
{
ballPos.Y = minY;
ballSpeed.Y *= -1;
}
if (ballPos.Y > maxY)
{
ballPos.Y = maxY;
ballSpeed.Y *= -1;
}
Rectangle paddle1Rect = new Rectangle((int)paddle1Pos.X, (int)paddle1Pos.Y, paddle.Width, paddle.Height);
Rectangle ballRect = new Rectangle((int)ballPos.X, (int)ballPos.Y, ball.Width, ball.Height);
if (ballRect.Intersects(paddle1Rect))
{
ballSpeed *= -1;
}
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.White);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(paddle, paddle1Pos, Color.White);
spriteBatch.Draw(ball, ballPos, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
[/source]
[source lang="csharp"] protected override void Initialize()
{
// TODO: Add your initialization logic here
KeyboardState state = Keyboard.GetState();
if (state.IsKeyDown(Keys.Up))
{
paddle1Pos.Y -= 3;
}
if (state.IsKeyDown(Keys.Down))
{
paddle1Pos.Y += 3;
}
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
ball = Content.Load<Texture2D>("ball");
paddle = Content.Load<Texture2D>("paddle");
}
/// <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)
{
int minY = 0;
int minX = 0;
int maxY = 600 - ball.Height;
int maxX = 1000 - ball.Width;
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
ballPos += ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
if (ballPos.X > maxX)
{
ballPos.X = maxX;
ballSpeed.X *= -1;
}
if (ballPos.X < minX)
{
ballSpeed.X *= -1;
ballPos.X = minX;
}
if (ballPos.Y < minY)
{
ballPos.Y = minY;
ballSpeed.Y *= -1;
}
if (ballPos.Y > maxY)
{
ballPos.Y = maxY;
ballSpeed.Y *= -1;
}
Rectangle paddle1Rect = new Rectangle((int)paddle1Pos.X, (int)paddle1Pos.Y, paddle.Width, paddle.Height);
Rectangle ballRect = new Rectangle((int)ballPos.X, (int)ballPos.Y, ball.Width, ball.Height);
if (ballRect.Intersects(paddle1Rect))
{
ballSpeed *= -1;
}
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.White);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(paddle, paddle1Pos, Color.White);
spriteBatch.Draw(ball, ballPos, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
[/source]
You put your keyboard polling within Initialize. Initialize is not called every frame, you would want that in Update.
If I had to guess, I would say it is because the code for moving the paddles is located inside Initialize(), which is only called once (I would hope, otherwise it needs a new name).
[EDIT]
I idled for a while before replying.
[/EDIT]
L. Spiro
[EDIT]
I idled for a while before replying.
[/EDIT]
L. Spiro
Another chance to post my famous list. Hold, please.
EDIT: Here we go.
The reason for this list is because you know how these games work. The rules and how all the parts are supposed to work. So it's easy to go down the list and learn new skills as you complete each game. For a really good tutorial on 2D game development, google for Lazy Foo SDL
EDIT: Here we go.
- Pong
- Breakout
- Space Invaders (credit to GearSlayer360)
- Missile Command
- Asteroids
- Tetris
- Pac-Man
- Tic-Tac-Toe (credit to 3Ddreamer)
- ????
- Make Money
The reason for this list is because you know how these games work. The rules and how all the parts are supposed to work. So it's easy to go down the list and learn new skills as you complete each game. For a really good tutorial on 2D game development, google for Lazy Foo SDL
breakout is easy enough. i made that in java and C++. i tried snake after that but failed space invaders would be a challenge because you have the shields that protect you from enemy bullets, and a whole array of enemies that move together. and lets not forget the actual process of shooting a bullet which i never understood
and lets not forget the actual process of shooting a bullet which i never understood
I don't consider this a programing or api knowledge problem. I see this as an imagination problem. It's just like a puzzle, but harder one. You have certain elements( types like float, int, in xna case also Vectors, containers like lists, you can create your own classes and structers), only thing is to think of how to combine this pieces of puzzle into whole.
As I always suggest, piece of paper is your best friend. Draw your problem (in case of shooting you got a plane/vehicle and a bullet). Write what defines a plane and what defines a bullet(position, velocity etc.). Draw some vectors (for instance, from plane in shoting direction). Search for relations, experiment. If you won't try you won't accomplish a thing
The most importan thing of all is to never give up
I learned basics of xna from http://rbwhitaker.wikidot.com/xna-tutorials , maybe it will help you too ;)
I managed to make a pong game but with no ai. Where do i go from there?
But the comment above initialise would have when the game1.cs file is generated. Have a look at the comments above the functions in the main game class after creating a new XNA game from the wizards. They say when and how often they are executed also the //TODO comments kind mention what you should do in them.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement