{"id":550,"date":"2013-11-02T12:53:40","date_gmt":"2013-11-02T11:53:40","guid":{"rendered":"http:\/\/labalec.fr\/erwan\/?p=550"},"modified":"2013-11-16T14:06:23","modified_gmt":"2013-11-16T13:06:23","slug":"vb-net-and-xna-article-2","status":"publish","type":"post","link":"https:\/\/labalec.fr\/erwan\/?p=550","title":{"rendered":"VB.Net and XNA : Article 2 (moving texture)"},"content":{"rendered":"<p>In previous articles, we have seen <a href=\"https:\/\/labalec.fr\/erwan\/?p=541\" target=\"_blank\">what is needed to code with XNA<\/a> and also <a href=\"https:\/\/labalec.fr\/erwan\/?p=544\" target=\"_blank\">the basics of an XNA class<\/a>.<\/p>\n<p>Lets now have a look at the small XNA app below : a bouncing ball.<\/p>\n<p>-We declare some global variables<br \/>\n-we load our content (here : a sprite = a bleu ball) in the <strong>loadcontent<\/strong> xna method<br \/>\n-we call our updatesprite method in the <strong>update<\/strong> xna method which will take care of the moving position<br \/>\n-we draw our sprite in the <strong>draw<\/strong> xna method using the update spritepos<\/p>\n<p>Attached to this post is the complete source code and compiled executable : <a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna_demo_1.zip\">xna_demo_1<\/a><\/p>\n<p>Enjoy !<\/p>\n<p><code><br \/>\nImports Microsoft.Xna.Framework<br \/>\nImports Microsoft.Xna.Framework.Graphics<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    '<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)<\/p>\n<p>    Public Sub New()<br \/>\n        graphics = New GraphicsDeviceManager(Me)<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub Initialize()<br \/>\n        MyBase.Initialize()<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub LoadContent()<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        '<br \/>\n        'Load the texture'<br \/>\n        'We are using Stream , not a content (xnb format)<br \/>\n        Dim textureStream As System.IO.Stream = New System.IO.StreamReader(Application.StartupPath + \"\\bille_bleu1.png\").BaseStream<br \/>\n        'Loading the texture'<br \/>\n        mTexture = Texture2D.FromStream(GraphicsDevice, textureStream)<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub UnloadContent()<br \/>\n        MyBase.UnloadContent()<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)<br \/>\n        'Allows the game to exit'<br \/>\n        Dim newState As KeyboardState = Keyboard.GetState<br \/>\n        If newState.IsKeyDown(Keys.Q) Then Application.Exit()<br \/>\n        'The method that will update our sprite position'<br \/>\n        UpdateSprite(gameTime)<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        'Draw the sprite'<br \/>\n        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend)<br \/>\n        spriteBatch.Draw(mTexture, spritePos, Color.White)<br \/>\n        spriteBatch.End()<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<\/p>\n<p>        '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<\/p>\n<p>        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        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<\/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        End If\n\n    End Sub\nEnd Class\n\n<\/code><\/p>\n<div style=\"width: 695px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-550-1\" width=\"695\" height=\"417\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna02.mp4?_=1\" \/><a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna02.mp4\">https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna02.mp4<\/a><\/video><\/div>\n","protected":false},"excerpt":{"rendered":"<p>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 <a href='https:\/\/labalec.fr\/erwan\/?p=550' 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-550","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\/550","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=550"}],"version-history":[{"count":7,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/550\/revisions"}],"predecessor-version":[{"id":600,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/550\/revisions\/600"}],"wp:attachment":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}