{"id":584,"date":"2013-11-05T22:24:57","date_gmt":"2013-11-05T21:24:57","guid":{"rendered":"http:\/\/labalec.fr\/erwan\/?p=584"},"modified":"2013-11-16T14:39:33","modified_gmt":"2013-11-16T13:39:33","slug":"vb-net-and-xna-article-5-move-a-texture-add-a-background-play-some-sound","status":"publish","type":"post","link":"https:\/\/labalec.fr\/erwan\/?p=584","title":{"rendered":"VB.Net and XNA : Article 6 (move a texture, add a background, play some sound)"},"content":{"rendered":"<p>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.<\/p>\n<p>As always we <strong>load contents<\/strong> (textures and sound), we <strong>update <\/strong>our variables (position and direction), and we finally <strong>draw<\/strong>.<br \/>\nBy now, you probably got the idea \ud83d\ude42<\/p>\n<p>The project files : <a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna_demo_4.zip\">xna_demo_4<\/a><\/p>\n<p><code><br \/>\nImports Microsoft.Xna.Framework<br \/>\nImports Microsoft.Xna.Framework.Graphics<br \/>\nImports Microsoft.Xna.Framework.Audio<br \/>\nImports Microsoft.Xna.Framework.Content<br \/>\nImports Microsoft.Xna.Framework.Media<br \/>\nImports Microsoft.Xna.Framework.Input<\/p>\n<p>Public Class Game<br \/>\n    Inherits Microsoft.Xna.Framework.Game<br \/>\n    'Fields in our game graphic manager etc'<br \/>\n    Dim graphics As GraphicsDeviceManager<br \/>\n    Dim spriteBatch As SpriteBatch<br \/>\n    'Texture that we will render'<br \/>\n    Private mTexture As Texture2D<br \/>\n    'Set the coordinates to draw the sprite at'<br \/>\n    Private spritePos As Vector2 = Vector2.Zero<br \/>\n    'X and Y speed of the sprite'<br \/>\n    Private XSpeed As Single = 80<br \/>\n    Private YSpeed As Single = 120<br \/>\n    'Vector2 used for the speed of the sprite'<br \/>\n    Private spriteSpeed As New Vector2(XSpeed, YSpeed)<br \/>\n    'sound<br \/>\n    Private mysound As SoundEffect<br \/>\n    'background<br \/>\n    Private backgroundTexture As Texture2D<\/p>\n<p>    Public Sub New()<br \/>\n        graphics = New GraphicsDeviceManager(Me)<br \/>\n        'graphics.ToggleFullScreen()<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub Initialize()<br \/>\n        'TODO: Add your initialization logic here'<br \/>\n        MyBase.Initialize()<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub LoadContent()<br \/>\n        ' TODO: use this.Content to load your game content here'<br \/>\n        MyBase.LoadContent()<br \/>\n        ' Create a new SpriteBatch, which can be used to draw textures.'<br \/>\n        spriteBatch = New SpriteBatch(GraphicsDevice)<br \/>\n        'Load the texture'<br \/>\n        mTexture = Content.Load(Of Texture2D)(\"bille\")<br \/>\n        'load sound<br \/>\n        mysound = Content.Load(Of SoundEffect)(\"sleep\")<br \/>\n        'load background<br \/>\n        backgroundTexture = Content.Load(Of Texture2D)(\"background2\")<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub UnloadContent()<br \/>\n        MyBase.UnloadContent()<br \/>\n        'TODO: Unload any non ContentManager content here'<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)<br \/>\n        'TODO: Add your update logic here'<br \/>\n        'The method that will update our sprite position'<br \/>\n        UpdateSprite(gameTime)<br \/>\n        '<br \/>\n        MyBase.Update(gameTime)<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)<br \/>\n        'GraphicsDevice.Clear(Color.CornflowerBlue)<br \/>\n        'TODO: Add your drawing code here'<br \/>\n        Dim mainFrame As New Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height)<br \/>\n        'Draw the sprite'<br \/>\n        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend)<br \/>\n        spriteBatch.Draw(backgroundTexture, mainFrame, Color.White)<br \/>\n        spriteBatch.Draw(mTexture, spritePos, Color.White)<br \/>\n        spriteBatch.End()<br \/>\n        'TODO: Add your drawing code here'<br \/>\n        MyBase.Draw(gameTime)<br \/>\n    End Sub<\/p>\n<p>    Private Sub UpdateSprite(ByVal gameTime As GameTime)<br \/>\n        'The function move the sprite and check if its on the limits of the screen so it can bounce back'<br \/>\n        Dim intMaxX As Integer<br \/>\n        Dim intMinX As Integer = 0<br \/>\n        Dim intMaxY As Integer<br \/>\n        Dim intMinY As Integer = 0<br \/>\n        'move the sprite by speed scaled by elapsed time'<br \/>\n        spritePos += spriteSpeed * CSng(gameTime.ElapsedGameTime.TotalSeconds)<br \/>\n        'Get the max. X and Y coordinates'<br \/>\n        intMaxX = graphics.GraphicsDevice.Viewport.Width - mTexture.Width<br \/>\n        intMaxY = graphics.GraphicsDevice.Viewport.Height - mTexture.Height<br \/>\n        'Check the sprite if its at some of the edges of the screen and then reverce the speed of the sprite'<br \/>\n        If spritePos.X > intMaxX Then<br \/>\n            'Check if the sprite is at the maximum X coordinates of the screen if so reverse the speed'<br \/>\n            spriteSpeed.X *= -1<br \/>\n            spritePos.X = intMaxX<br \/>\n            mysound.Play()<br \/>\n        ElseIf spritePos.X < intMinX Then\n            'Check if the sprite is at the minimum X coordinates of the scree and reverse the speed'\n            spriteSpeed.X *= -1\n            spritePos.X = intMinX\n            mysound.Play()\n        End If\n\n        If spritePos.Y > intMaxY Then<br \/>\n            'Check if the sprite is at the maximum Y coordinates of the screen if so reverse the speed'<br \/>\n            spriteSpeed.Y *= -1<br \/>\n            spritePos.Y = intMaxY<br \/>\n            mysound.Play()<\/p>\n<p>        ElseIf spritePos.Y < intMinY Then\n            'Check if the sprite is at the minimum Y coordinates of the scree and reverse the speed'\n            spriteSpeed.Y *= -1\n            spritePos.Y = intMinY\n            mysound.Play()\n        End If\n    End Sub\nEnd Class\n<\/code><\/p>\n<div style=\"width: 695px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-584-1\" width=\"695\" height=\"417\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna06.mp4?_=1\" \/><a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna06.mp4\">https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna06.mp4<\/a><\/video><\/div>\n","protected":false},"excerpt":{"rendered":"<p>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 <a href='https:\/\/labalec.fr\/erwan\/?p=584' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34,35],"tags":[],"class_list":["post-584","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-xna","category-34-id","category-35-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"_links":{"self":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=584"}],"version-history":[{"count":3,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/584\/revisions"}],"predecessor-version":[{"id":608,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/584\/revisions\/608"}],"wp:attachment":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}