Nov 242013

Quick and dirty : buttons for XNA with mouse over and mouse click.

3 rectangles, 3 textures, mouse inputs…

The source code : XNA_demo_13 .

The video to illustrate.

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
Dim graphics As GraphicsDeviceManager
Dim SpriteBatch As SpriteBatch

Private easy As Texture2D
Private medium As Texture2D
Private hard As Texture2D

Dim color_a As Color
Dim color_b As Color
Dim color_c As Color

Dim rec_a As Rectangle
Dim rec_b As Rectangle
Dim rec_c As Rectangle

Public Sub New()
graphics = New GraphicsDeviceManager(Me)
Window.Title = "Test"
Me.IsMouseVisible = True
End Sub

Protected Overrides Sub Initialize()
End Sub
Protected Overrides Sub LoadContent()
SpriteBatch = New SpriteBatch(GraphicsDevice)
easy = Content.Load(Of Texture2D)("easy")
medium = Content.Load(Of Texture2D)("medium")
hard = Content.Load(Of Texture2D)("hard")

rec_a = New Rectangle(40, 40, easy.Width, easy.Height)
rec_b = New Rectangle(40, 140, medium.Width, medium.Height)
rec_c = New Rectangle(40, 240, hard.Width, hard.Height)

End Sub
Protected Overrides Sub UnloadContent()
End Sub
Private Sub update_mouse()
Dim mouse_state As MouseState = Mouse.GetState()
Dim x As Integer = mouse_state.X
Dim y As Integer = mouse_state.Y

If rec_a.Contains(x, y) Then color_a = Color.Red Else color_a = Color.White
If rec_b.Contains(x, y) Then color_b = Color.Red Else color_b = Color.White
If rec_c.Contains(x, y) Then color_c = Color.Red Else color_c = Color.White

If rec_a.Contains(x, y) And mouse_state.LeftButton = ButtonState.Pressed Then color_a = Color.Yellow
If rec_b.Contains(x, y) And mouse_state.LeftButton = ButtonState.Pressed Then color_b = Color.Yellow
If rec_c.Contains(x, y) And mouse_state.LeftButton = ButtonState.Pressed Then color_c = Color.Yellow
End Sub

Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
End Sub

Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend)
SpriteBatch.Draw(easy, rec_a, color_a)
SpriteBatch.Draw(medium, rec_b, color_b)
SpriteBatch.Draw(hard, rec_c, color_c)
End Sub

End Class

 Posted by at 14 h 39 min

Poster un Commentaire

Me notifier des