Public Class Game1
Inherits Microsoft.Xna.Framework.Game
Private WithEvents graphics As GraphicsDeviceManager
Private WithEvents spriteBatch As SpriteBatch
Private background As Texture2D
Private ghost As Texture2D
Private ghost_location As Vector2
Private gradientball As Texture2D
Private display As RenderTarget2D = Nothing
Private screamsound As SoundEffect
Private rnd As New Random
Private blend_min As BlendState = New BlendState With _
{.AlphaBlendFunction = BlendFunction.Min, .AlphaSourceBlend = Blend.One, .AlphaDestinationBlend = Blend.One, _
.ColorBlendFunction = BlendFunction.Min, .ColorSourceBlend = Blend.One, .ColorDestinationBlend = Blend.One}
Public Sub New()
graphics = New GraphicsDeviceManager(Me)
Content.RootDirectory = "Content"
End Sub
Protected Overrides Sub Initialize()
IsMouseVisible = True
Window.AllowUserResizing = True
AddHandler Window.ClientSizeChanged, AddressOf picknewlocation
MyBase.Initialize()
End Sub
Protected Overrides Sub LoadContent()
spriteBatch = New SpriteBatch(GraphicsDevice)
background = Content.Load(Of Texture2D)("Penguins")
ghost = Content.Load(Of Texture2D)("LittleGhost")
screamsound = Content.Load(Of SoundEffect)("Scream")
generate_gradientball(100, Color.Black, Color.White)
picknewlocation()
MyBase.LoadContent()
End Sub
Protected Overrides Sub UnloadContent()
display.Dispose()
gradientball.Dispose()
MyBase.UnloadContent()
End Sub
Protected Overrides Sub Update(ByVal gameTime As GameTime)
If Keyboard.GetState.IsKeyDown(Keys.Escape) Then Me.Exit()
Dim u As Vector2 = New Vector2(Mouse.GetState.X, Mouse.GetState.Y)
Dim v As Vector2 = New Vector2(ghost_location.X + ghost.Width \ 2, ghost_location.Y + ghost.Height \ 2)
If Vector2.Distance(u, v) < 8 Then
screamsound.Play()
picknewlocation()
End If
MyBase.Update(gameTime)
End Sub
Protected Overrides Sub Draw(ByVal gameTime As GameTime)
GraphicsDevice.Clear(Color.Black)
spriteBatch.Begin()
spriteBatch.Draw(gradientball, New Vector2(Mouse.GetState.X - gradientball.Width \ 2, _
Mouse.GetState.Y - gradientball.Height \ 2), Color.White)
spriteBatch.End()
spriteBatch.Begin(Nothing, blend_min)
spriteBatch.Draw(display, Vector2.Zero, Color.White)
spriteBatch.End()
MyBase.Draw(gameTime)
End Sub
Private Sub picknewlocation()
If display IsNot Nothing Then display.Dispose()
display = New RenderTarget2D(GraphicsDevice, background.Width, background.Height)
ghost_location = New Vector2(rnd.Next(0, background.Width - ghost.Width), rnd.Next(0, background.Height - ghost.Height))
GraphicsDevice.SetRenderTarget(display)
spriteBatch.Begin()
spriteBatch.Draw(background, Vector2.Zero, Color.White)
spriteBatch.Draw(ghost, ghost_location, Color.White)
spriteBatch.End()
GraphicsDevice.SetRenderTarget(Nothing)
End Sub
Private Sub generate_gradientball(radius As Integer, c1 As Color, c2 As Color)
Dim diameter As Integer = 2 * radius + 1
Dim rval(diameter * diameter - 1) As Color
Dim rsquared As Single = radius * radius
Dim k As Integer = 0
Dim l, d As Single
For i As Integer = 0 To diameter - 1
d = (i - radius) * (i - radius)
For j As Integer = 0 To diameter - 1
l = (d + (j - radius) * (j - radius)) / rsquared
rval(k) = Color.Lerp(c2, c1, l)
k += 1
Next
Next
gradientball = New Texture2D(GraphicsDevice, diameter, diameter)
gradientball.SetData(rval)
End Sub
End Class