your first game - xna game studio in 2d
TRANSCRIPT
-
7/24/2019 Your First Game - XNA Game Studio in 2D
1/5
Your First Game XNA Game Studio in 2D
Follow these steps to create a simple spritebased game by using XNA Game Studio. This sample code works on all platforms, including Windows Phone.
Note
A sprite is a simple 2D graphic such as a bitmap that is displayed on the screen using a call to SpriteBatch.Draw.
Step 1: Install Your Software
Step 2: Create a New Project
Step 3: View the Code
Step 4: Add a Sprite
Step 5: Make the Sprite Move and Bounce
Step 6: Explore!
The Complete SampleThe code in this tutorial illustrates the technique described in the text. A complete code sample for this tutorial is available for you to download, including full source code and any
additional supporting files required by the sample.
Download MyFirstGame_Tutorial_Sample.zip.
Step 1: Install Your SoftwareBefore you begin, make sure that you have installed all the necessary software, including a supported version of Microsoft Visual Studio tools and XNA Game Studio.
Step 2: Create a New Project
1. From the Startmenu, click All Programs, click the XNA Game Studio 4.0 folder, and then click your supported version of Microsoft Visual Studio tools.
2. When the Start Page appears, click the Filemenu, and then click New Project.
A dialog box appears with a tree list on the left pane, marked Project Types.
3. Select the XNA Game Studio XNA Game Studio 4.0 tree node underneath the Visual C#node.
A set of available projects appears in the right pane.
4. In the right pane of the dialog box that appears, click Windows Game 4.0, and then type a title for your project such as "MyFirstGame" in the Namebox.
5. Type a path where you'd like to save your project in the Locationbox, and then click OK.
After creating a new project, you'll be presented with the code view of your game.
Step 3: View the CodeSome of the hard work has already been done for you. If you build and run your game now, the GraphicsDeviceManagerwill set up your screen size and render a blank screen. Your game
will run and update all by itself. It's up to you to insert your own code to make the game more interesting.
Much of the code to start and run your game has already been written for you. You can insert your own code now.
The Initializemethod is where you can initialize any assets that do not require a GraphicsDeviceto be initialized.
The LoadContentmethod is where you load any necessary game assets such as models and textures.
The UnloadContentmethod is where any game assets can be released. Generally, no extra code is required here, as assets will be released automatically when they are no longer
needed.
The Updateloop is the best place to update your game logic: move objects around, take player input, decide the outcome of collisions between objects, and so on.
The Drawloop is the best place to render all of your objects and backgrounds on the screen.
Step 4: Add a SpriteThe next step is to add a graphic that can be drawn on the screen. Use a small graphics file, such as a small .bmp or .jpg file. Be creativeyou can even make your own. You can even skip
ahead a bit and make a sprite that "hides" parts that should not be seen such as edges or corners so that it looks even better.
Once you have a graphic picked out on your computer, follow these steps.
1. Make sure you can see the Solution Explorer for your project on the r ight side of the window. If you cannot see it, click the Viewmenu, and then click Solution Explorer.
When it appears, you will see files associated with your project in a tree structure. Inside the tree, you will see a node named Content.
2. Rightclick the Contentnode, click Add, click Existing Item, and then browse to your graphic.
If you can't see any files, make sure you change the Files of typeselection box to read Texture Files.
3. Click the graphic file, and then click Add.
An entry for the graphic file will appear in Solution Explorer.
4. Click the entry for the graphic in the Solution Explorer. If you do not see the entry, ensure the Contentnode is expanded by clicking the small plus sign + to the left of the node,
and then click on the entry that appears underneath the Contentnode.
When you add a graphic file, it is added automatically to the XNA Framework Content Pipeline. This allows you to quickly and easily load the graphic into your game.
https://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.initialize.aspxhttp://-/?-http://-/?-http://-/?-http://-/?-https://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.draw.aspxhttps://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.update.aspxhttps://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.unloadcontent.aspxhttps://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.loadcontent.aspxhttps://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.graphicsdevice.aspxhttps://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.initialize.aspxhttps://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphicsdevicemanager.aspxhttp://go.microsoft.com/fwlink/?LinkId=198877http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-https://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.spritebatch.draw.aspx -
7/24/2019 Your First Game - XNA Game Studio in 2D
2/5
In the Propertieswindow below Solution Explorer, look for the "Asset Name" property. Note the name; you'll use it in your code to load the graphic so it can be displayed in your
game.
5. If the Propertieswindow is not visible, press F4, or click the Viewmenu, and then click Properties Window.
Now, you must write code that loads and displays the sprite on the screen.
6. Back in the Code view of your game, find the LoadContentmethod, and add the following lines in and above the method so it looks similar to this:
The Contentproperty of the parent Gameclass offers the ContentManagerclass through which your game assets can be loaded.
Make sure the call to Content.Loadis using the "Asset Name" you saw in the Properties window in the previous step. This code will load and prepare your graphic to be drawn, and
will reload your graphic if the graphics device is reset such as in the case of the game window being resized.
7. Now, add code to the Drawloop so it looks like this:
This code draws the sprite on the screen each frame.
Notice the parameter passed by the Beginmethod, BlendState.AlphaBlend. This parameter tells the Drawmethod to use the alpha channel of the source color to create a
transparency effect so that the destination color appears through the source color.
8. Build and run your game.
The sprite appears.
Now, it's time to give it some motion.
Step 5: Make the Sprite Move and Bounce
Change the lines of code in the Updatemethod to read this way:
// This is a texture we can render.
Texture2D myTexture;
// Set the coordinates to draw the sprite at.
Vector2 spritePosition = Vector2.Zero;
// Store some information about the sprite's motion.
Vector2 spriteSpeed = new Vector2(50.0f, 50.0f);
protectedoverridevoid LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
myTexture = Content.Load("mytexture");
}
protectedoverridevoid Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
// Draw the sprite.
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(myTexture, spritePosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
void UpdateSprite(GameTime gameTime)
{
// Move the sprite by speed, scaled by elapsed time.
spritePosition +=
spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
int MaxX =
graphics.GraphicsDevice.Viewport.Width myTexture.Width;
int MinX = 0;
int MaxY =
graphics.GraphicsDevice.Viewport.Height myTexture.Height;
int MinY = 0;
// Check for bounce.
if (spritePosition.X > MaxX)
{ spriteSpeed.X *= 1;
spritePosition.X = MaxX;
}
elseif (spritePosition.X < MinX)
{
spriteSpeed.X *= 1;
C#
C#
C#
https://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.update.aspxhttps://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.draw.aspxhttps://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.blendstate.aspxhttps://msdn.microsoft.com/en-us/library/ff433698.aspxhttps://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.draw.aspxhttps://msdn.microsoft.com/en-us/library/bb197848.aspxhttps://msdn.microsoft.com/en-us/library/microsoft.xna.framework.content.contentmanager.aspxhttps://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.aspxhttps://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.content.aspxhttps://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.loadcontent.aspx -
7/24/2019 Your First Game - XNA Game Studio in 2D
3/5
Community Additions
This adds a little bit of logic that will move the sprite around each frame and cause the sprite to change direction if it hits the edges of the game window.
Build and run your game.
The sprite moves across the screen and changes direction when it encounters the edges of the game window.
Step 6: Explore!From here, you can do just about anything.
Here are some more ideas to extend this sample:
Add a second sprite, and use BoundingBoxobjects to detect collisions.Use Keyboard, Mouse, or GamePadto make the sprite respond to movements of an input device. See Overview of User Input and Input Devices.
Create some audio events so that the sprite makes sounds as it moves. See Adding a Sound File XACT
and Playing Sounds from an XACT Project.
Get more ideas and resources at App Hub.
See AlsoSoftware Install Requirements
Getting Started with XNA Game Studio Development
Missing code in instruction step 5
This code:
// Move the sprite around.
UpdateSprite
gameTime
;
Needs to be added just above:
base.Update
gameTime
;
}
And the code listed in step 5 should be added here.
StachowskiK12/15/2014
Code in VB.net
Dim mytexture As Texture2D
Dim spriteposition As Vector2 = Vector2.Zero
Dim spritespeed As Vector2 = New Vector2
50.0F, 50.0F
Protected Overrides Sub LoadContent
spriteBatch = New SpriteBatch
GraphicsDevice
mytexture = Content.Load
Of Texture2D
"mytexture"
End Sub
Protected Overrides Sub Update
ByVal gameTime As GameTime
If GamePad.GetState
PlayerIndex.One
.Buttons.Back = ButtonState.Pressed Then
Me.Exit
End If
spriteposition += spritespeed * gameTime.ElapsedGameTime.TotalSeconds
spritePosition.X = MinX;
}
if (spritePosition.Y > MaxY)
{
spriteSpeed.Y *= 1;
spritePosition.Y = MaxY;
}
elseif (spritePosition.Y < MinY)
{
spriteSpeed.Y *= 1;
spritePosition.Y = MinY;
}
}
https://social.msdn.microsoft.com/profile/stachowskik/https://msdn.microsoft.com/en-us/library/bb203894.aspxhttps://msdn.microsoft.com/en-us/library/bb203916.aspxhttp://void%280%29/http://go.microsoft.com/fwlink/?LinkID=215642https://msdn.microsoft.com/en-us/library/dd231915.aspxhttps://msdn.microsoft.com/en-us/library/dd940199.aspxhttps://msdn.microsoft.com/en-us/library/bb203903.aspxhttps://msdn.microsoft.com/en-us/library/microsoft.xna.framework.input.gamepad.aspxhttps://msdn.microsoft.com/en-us/library/microsoft.xna.framework.input.mouse.aspxhttps://msdn.microsoft.com/en-us/library/microsoft.xna.framework.input.keyboard.aspxhttps://msdn.microsoft.com/en-us/library/microsoft.xna.framework.boundingbox.aspxhttps://social.msdn.microsoft.com/profile/stachowskik/ -
7/24/2019 Your First Game - XNA Game Studio in 2D
4/5
Dim MaxX As Integer = graphics.GraphicsDevice.Viewport.Width mytexture.Width
Dim MinX As Integer = 0
Dim MaxY As Integer = graphics.GraphicsDevice.Viewport.Height mytexture.Height
Dim miny As Integer = 0
If spriteposition.X = MaxX Then
spritespeed.X *= 1
spriteposition.X = MaxX
ElseIf spriteposition.X < MinX Then
spritespeed.X *= 1
spriteposition.X = MinX
End If
If spriteposition.Y > MaxY Then
spritespeed.Y *= 1
spriteposition.Y = MaxY
ElseIf spriteposition.Y < miny Then
spritespeed.Y *= 1
spriteposition.Y = miny
End If
MyBase.Update
gameTime
End Sub
bye from Bilbao
josederio
12/30/2012
Missing an important instruction step!!
The "motion" part of this tutorial is incomplete. Following these instructions exactly as written produces a sprite that doesn't move.
got it working by calling UpdateSprite
gameTime
at the end of the Draw method. Not sure if this was the correct thing to do, but like I said, it works now.
No, this is not correct. You should call UpdateSpritegameTime inside the Update method look for the Add your update logic here comment.
Petr Kadlec
10/18/2012
ERROR
Sir,
am using xna3.0 using visual studio 2008 in win XP.
while debugging a game in my pc i got an error at"GraphicsDevice.Clear
Color.Navy
;".
t shows the error as "This property requires a graphics device
service in the game service container."
What is the reason for this and send me the remedy for this error.
Umasankar3259
8/16/2012
"Content" node
in my version of visual studio the "Content" node is actually called "Content Content
". So if my project was called "WindowsGame3" the folder would be called
"WindowsGame3Content Content
".
chenjesu
2/2/2012
2015 Microsoft
https://social.msdn.microsoft.com/profile/chenjesu/https://social.msdn.microsoft.com/profile/umasankar3259/https://social.msdn.microsoft.com/profile/petr%20kadlec/https://social.msdn.microsoft.com/profile/josederio/https://social.msdn.microsoft.com/profile/chenjesu/https://social.msdn.microsoft.com/profile/umasankar3259/https://social.msdn.microsoft.com/profile/petr%20kadlec/https://social.msdn.microsoft.com/profile/josederio/ -
7/24/2019 Your First Game - XNA Game Studio in 2D
5/5