Results 1 to 12 of 12

Thread: Short list of examples (exclusively 2D) as an introduction to XNA

Threaded View

  1. #10

    Thread Starter
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Short list of examples (exclusively 2D) as an introduction to XNA

    Example 7: A complete game using just 100 lines of code and relying on nothing but what you've learned.

    I thought that I would end this series of examples with a little funny game with rich potential for development. The game basically consists of an invisible background (make the window full-screen). Your mouse-pointer is a torch, that you can use to light up the image - find the ghost and make him scream! Once you have found him, he will move to a new location. There are plenty of oppotunities to add score to this game - and difficulty making the torch smaller or the ghost more invisible. Or adding bonus ghosts. This is a cash-cow handed to you .
    To use it simply add my little ghost and my scream to the Content project - both are linked here Name:  LittleGhost.png
Views: 1130
Size:  1.9 KB {OK so there's no method of uploading sound - but find and download a very short scream (3-5 secs tops) and add it as Scream(.wav)}. As background the trusty old Penguins.jpg is used. Enjoy and thanks for reading.

    vb.net Code:
    1. Public Class Game1
    2.     Inherits Microsoft.Xna.Framework.Game
    3.  
    4.     Private WithEvents graphics As GraphicsDeviceManager
    5.     Private WithEvents spriteBatch As SpriteBatch
    6.  
    7.     Private background As Texture2D
    8.     Private ghost As Texture2D
    9.     Private ghost_location As Vector2
    10.     Private gradientball As Texture2D
    11.     Private display As RenderTarget2D = Nothing
    12.     Private screamsound As SoundEffect
    13.     Private rnd As New Random
    14.     Private blend_min As BlendState = New BlendState With _
    15.         {.AlphaBlendFunction = BlendFunction.Min, .AlphaSourceBlend = Blend.One, .AlphaDestinationBlend = Blend.One, _
    16.          .ColorBlendFunction = BlendFunction.Min, .ColorSourceBlend = Blend.One, .ColorDestinationBlend = Blend.One}
    17.  
    18.     Public Sub New()
    19.         graphics = New GraphicsDeviceManager(Me)
    20.         Content.RootDirectory = "Content"
    21.     End Sub
    22.  
    23.     Protected Overrides Sub Initialize()
    24.         IsMouseVisible = True
    25.         Window.AllowUserResizing = True
    26.         AddHandler Window.ClientSizeChanged, AddressOf picknewlocation
    27.         MyBase.Initialize()
    28.     End Sub
    29.  
    30.     Protected Overrides Sub LoadContent()
    31.         spriteBatch = New SpriteBatch(GraphicsDevice)
    32.         background = Content.Load(Of Texture2D)("Penguins")
    33.         ghost = Content.Load(Of Texture2D)("LittleGhost")
    34.         screamsound = Content.Load(Of SoundEffect)("Scream")
    35.         generate_gradientball(100, Color.Black, Color.White)
    36.         picknewlocation()
    37.         MyBase.LoadContent()
    38.     End Sub
    39.  
    40.     Protected Overrides Sub UnloadContent()
    41.         display.Dispose()
    42.         gradientball.Dispose()
    43.         MyBase.UnloadContent()
    44.     End Sub
    45.  
    46.     Protected Overrides Sub Update(ByVal gameTime As GameTime)
    47.         If Keyboard.GetState.IsKeyDown(Keys.Escape) Then Me.Exit()
    48.         Dim u As Vector2 = New Vector2(Mouse.GetState.X, Mouse.GetState.Y)
    49.         Dim v As Vector2 = New Vector2(ghost_location.X + ghost.Width \ 2, ghost_location.Y + ghost.Height \ 2)
    50.         If Vector2.Distance(u, v) < 8 Then
    51.             screamsound.Play()
    52.             picknewlocation()
    53.         End If
    54.         MyBase.Update(gameTime)
    55.     End Sub
    56.  
    57.     Protected Overrides Sub Draw(ByVal gameTime As GameTime)
    58.         GraphicsDevice.Clear(Color.Black)
    59.         spriteBatch.Begin()
    60.         spriteBatch.Draw(gradientball, New Vector2(Mouse.GetState.X - gradientball.Width \ 2, _
    61.                                                    Mouse.GetState.Y - gradientball.Height \ 2), Color.White)
    62.         spriteBatch.End()
    63.         spriteBatch.Begin(Nothing, blend_min)
    64.         spriteBatch.Draw(display, Vector2.Zero, Color.White)
    65.         spriteBatch.End()
    66.         MyBase.Draw(gameTime)
    67.     End Sub
    68.  
    69.     Private Sub picknewlocation()
    70.         If display IsNot Nothing Then display.Dispose()
    71.  
    72.         display = New RenderTarget2D(GraphicsDevice, background.Width, background.Height)
    73.         ghost_location = New Vector2(rnd.Next(0, background.Width - ghost.Width), rnd.Next(0, background.Height - ghost.Height))
    74.         GraphicsDevice.SetRenderTarget(display)
    75.         spriteBatch.Begin()
    76.         spriteBatch.Draw(background, Vector2.Zero, Color.White)
    77.         spriteBatch.Draw(ghost, ghost_location, Color.White)
    78.         spriteBatch.End()
    79.         GraphicsDevice.SetRenderTarget(Nothing)
    80.     End Sub
    81.  
    82.     Private Sub generate_gradientball(radius As Integer, c1 As Color, c2 As Color)
    83.         Dim diameter As Integer = 2 * radius + 1
    84.         Dim rval(diameter * diameter - 1) As Color
    85.         Dim rsquared As Single = radius * radius
    86.         Dim k As Integer = 0
    87.         Dim l, d As Single
    88.         For i As Integer = 0 To diameter - 1
    89.             d = (i - radius) * (i - radius)
    90.             For j As Integer = 0 To diameter - 1
    91.  
    92.                 l = (d + (j - radius) * (j - radius)) / rsquared
    93.                 rval(k) = Color.Lerp(c2, c1, l)
    94.                 k += 1
    95.             Next
    96.         Next
    97.         gradientball = New Texture2D(GraphicsDevice, diameter, diameter)
    98.         gradientball.SetData(rval)
    99.     End Sub
    100. End Class

    Ok so the 'nothing but what you have learned'-comment was a bit rash. We actually hadn't covered sound yet, but since it is as simple as scratching you own arse (pardon my italian), I thought I'd leave it to you to determine how that <SoundEffect>.Play works.

    Regards Tom
    Last edited by ThomasJohnsen; Nov 18th, 2012 at 01:36 PM.
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width