In previous articles, we have seen what is needed to code with XNA and also the basics of an XNA class.
Lets now have a look at the small XNA app below : a bouncing ball.
-We declare some global variables
-we load our content (here : a sprite = a bleu ball) in the loadcontent xna method
-we call our updatesprite method in the update xna method which will take care of the moving position
-we draw our sprite in the draw xna method using the update spritepos
Attached to this post is the complete source code and compiled executable : xna_demo_1
Enjoy !
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Input
Public Class Game
Inherits Microsoft.Xna.Framework.Game
'Fields in our game graphic manager etc'
Dim graphics As GraphicsDeviceManager
Dim spriteBatch As SpriteBatch
'
'Texture that we will render'
Private mTexture As Texture2D
'Set the coordinates to draw the sprite at'
Private spritePos As Vector2 = Vector2.Zero
'X and Y speed of the sprite'
Private XSpeed As Single = 80
Private YSpeed As Single = 120
'Vector2 used for the speed of the sprite'
Private spriteSpeed As New Vector2(XSpeed, YSpeed)
Public Sub New()
graphics = New GraphicsDeviceManager(Me)
End Sub
Protected Overrides Sub Initialize()
MyBase.Initialize()
End Sub
Protected Overrides Sub LoadContent()
MyBase.LoadContent()
' Create a new SpriteBatch, which can be used to draw textures.'
spriteBatch = New SpriteBatch(GraphicsDevice)
'
'Load the texture'
'We are using Stream , not a content (xnb format)
Dim textureStream As System.IO.Stream = New System.IO.StreamReader(Application.StartupPath + "\bille_bleu1.png").BaseStream
'Loading the texture'
mTexture = Texture2D.FromStream(GraphicsDevice, textureStream)
End Sub
Protected Overrides Sub UnloadContent()
MyBase.UnloadContent()
End Sub
Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
'Allows the game to exit'
Dim newState As KeyboardState = Keyboard.GetState
If newState.IsKeyDown(Keys.Q) Then Application.Exit()
'The method that will update our sprite position'
UpdateSprite(gameTime)
MyBase.Update(gameTime)
End Sub
Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
GraphicsDevice.Clear(Color.CornflowerBlue)
'Draw the sprite'
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend)
spriteBatch.Draw(mTexture, spritePos, Color.White)
spriteBatch.End()
MyBase.Draw(gameTime)
End Sub
Private Sub UpdateSprite(ByVal gameTime As GameTime)
'The function move the sprite and check if its on the limits of the screen so it can bounce back'
Dim intMaxX As Integer
Dim intMinX As Integer = 0
Dim intMaxY As Integer
Dim intMinY As Integer = 0
'move the sprite by speed scaled by elapsed time'
spritePos += spriteSpeed * CSng(gameTime.ElapsedGameTime.TotalSeconds)
'Get the max. X and Y coordinates'
intMaxX = graphics.GraphicsDevice.Viewport.Width - mTexture.Width
intMaxY = graphics.GraphicsDevice.Viewport.Height - mTexture.Height
'Check the sprite if its at some of the edges of the screen and then reverce the speed of the sprite'
If spritePos.X > intMaxX Then
'Check if the sprite is at the maximum X coordinates of the screen if so reverse the speed'
spriteSpeed.X *= -1
spritePos.X = intMaxX
ElseIf spritePos.X < intMinX Then
'Check if the sprite is at the minimum X coordinates of the scree and reverse the speed'
spriteSpeed.X *= -1
spritePos.X = intMinX
End If
If spritePos.Y > intMaxY Then
'Check if the sprite is at the maximum Y coordinates of the screen if so reverse the speed'
spriteSpeed.Y *= -1
spritePos.Y = intMaxY
ElseIf spritePos.Y < intMinY Then 'Check if the sprite is at the minimum Y coordinates of the scree and reverse the speed' spriteSpeed.Y *= -1 spritePos.Y = intMinY End If End Sub End Class