One article for today : lets build on what have seen so far and lets have a bouncing ball with a background and some sound when the ball hit the walls.
As always we load contents (textures and sound), we update our variables (position and direction), and we finally draw.
By now, you probably got the idea 🙂
The project files : xna_demo_4
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.Media
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)
'sound
Private mysound As SoundEffect
'background
Private backgroundTexture As Texture2D
Public Sub New()
graphics = New GraphicsDeviceManager(Me)
'graphics.ToggleFullScreen()
End Sub
Protected Overrides Sub Initialize()
'TODO: Add your initialization logic here'
MyBase.Initialize()
End Sub
Protected Overrides Sub LoadContent()
' TODO: use this.Content to load your game content here'
MyBase.LoadContent()
' Create a new SpriteBatch, which can be used to draw textures.'
spriteBatch = New SpriteBatch(GraphicsDevice)
'Load the texture'
mTexture = Content.Load(Of Texture2D)("bille")
'load sound
mysound = Content.Load(Of SoundEffect)("sleep")
'load background
backgroundTexture = Content.Load(Of Texture2D)("background2")
End Sub
Protected Overrides Sub UnloadContent()
MyBase.UnloadContent()
'TODO: Unload any non ContentManager content here'
End Sub
Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
'TODO: Add your update logic here'
'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)
'TODO: Add your drawing code here'
Dim mainFrame As New Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height)
'Draw the sprite'
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend)
spriteBatch.Draw(backgroundTexture, mainFrame, Color.White)
spriteBatch.Draw(mTexture, spritePos, Color.White)
spriteBatch.End()
'TODO: Add your drawing code here'
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
mysound.Play()
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
mysound.Play()
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
mysound.Play()
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 mysound.Play() End If End Sub End Class