Tag: 2D game

  • Writing a game using TurboSprite (2/2)

    Continued from here.

    Now I want to enable shooting. When the user presses the Control key, I create a bullet, sets its properties and adds it to the engine. This code is added to the KeyDown event. I have to modify the KeyUp event, because in the previous version, it unconditionally made the ship stop. Now when more keys are involved, I only want the ship to stop if it is the up or down buttons are released.

    This is the new version of the KeyDown event:

       Private Sub Form1_KeyDown(ByVal _
    sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
          If e.KeyData = Keys.A Then
    
             'If user presses "a", move up!
             Me.PlayerMover.DestY = 0
             Me.PlayerMover.SpeedY = -2
    
          ElseIf e.KeyData = Keys.Z Then
    
             'If user presses "z", move down!
             Me.PlayerMover.DestY = SpriteSurface1.Height
             Me.PlayerMover.SpeedY = 2
    
          End If
    
          If (e.KeyData And Keys.ControlKey) = Keys.ControlKey Then
    
             'If user presses Control, fire!
             Dim Bullet As New SCG.TurboSprite.PolygonSprite(2, 0, -2, 1, -2, -1)
             Bullet.Position = New Point(Player.Position.X + 15, Player.Position.Y + 9)
             Bullet.Color = Color.Cyan
             SpriteEngineDestination1.AddSprite(Bullet)
             Dim BulletMover As SCG.TurboSprite.DestinationMover = _
    SpriteEngineDestination1.GetMover(Bullet)
             BulletMover.Destination = New Point(SpriteSurface1.Width + 5, _
    Bullet.Height)
             BulletMover.SpeedX = 4
    
          End If
       End Sub

    This is the new version of the KeyUp event:

       Private Sub Form1_KeyUp(ByVal sender As _
    Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    
          'Stop moving, but only if "a" or "z" is released.
          If e.KeyData = Keys.A Or e.KeyCode = Keys.Z Then
             Me.PlayerMover.SpeedY = 0
          End If
    
       End Sub

    Finally, when a bullet reaches its destination at the right end of the screen, I want it deleted. Check the SpriteReachedDestination event in the final code listing below.

    When this is all working, I want to add an enemy and use the collision detection to see if it was killed. I did not subclass the PolygonSprite, and this is really something you should do to be able to tag the sprites. The “ugly hack” of this post, must be that I use color to determine if the sprite is a bullet, or the enemy. This is the complete code:

    Public Class Form1
    
       'A reference to the player (created in the Shown event) and the mover.
       Private Player As SCG.TurboSprite.PolygonSprite
       Private PlayerMover As SCG.TurboSprite.DestinationMover
    
       Private Sub Form1_Shown(ByVal sender _
    As Object, ByVal e As System.EventArgs) Handles Me.Shown
    
          'Set the desired number of frames per second and activate the surface.
          SpriteSurface1.DesiredFPS = 40
          SpriteSurface1.Active = True
    
          'Create the player and save the reference.
          Me.Player = New SCG.TurboSprite.PolygonSprite(-20, -10, 20, 10, -20, 10)
    
          'Set player original position.
          Me.Player.Position = New Point(100, 100)
    
          'Set player color.
          Me.Player.Color = Color.Yellow
    
          'Add the player to the sprite engine.
          SpriteEngineDestination1.AddSprite(Me.Player)
    
          'Save a reference to the mover.
          Me.PlayerMover = SpriteEngineDestination1.GetMover(Me.Player)
    
          'Now, add an ENEMY!
          Dim Enemy As New SCG.TurboSprite.PolygonSprite(-30, 20, 0, -20, 30, 20)
          Enemy.Position = New Point(SpriteSurface1.Width, SpriteSurface1.Height)
          Enemy.Color = Color.Red
          SpriteEngineDestination1.AddSprite(Enemy)
          Dim EnenyMover As SCG.TurboSprite.DestinationMover = _
    SpriteEngineDestination1.GetMover(Enemy)
          EnenyMover.Speed = 1
          EnenyMover.Destination = New Point(-50, 100)
    
       End Sub
    
       Private Sub Form1_KeyDown(ByVal sender As _
    Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
          If e.KeyData = Keys.A Then
    
             'If user presses "a", move up!
             Me.PlayerMover.DestY = 0
             Me.PlayerMover.SpeedY = -2
    
          ElseIf e.KeyData = Keys.Z Then
    
             'If user presses "z", move down!
             Me.PlayerMover.DestY = SpriteSurface1.Height
             Me.PlayerMover.SpeedY = 2
    
          End If
    
          If (e.KeyData And Keys.ControlKey) = Keys.ControlKey Then
    
             'If user presses Control, fire!
             Dim Bullet As New SCG.TurboSprite.PolygonSprite(2, 0, -2, 1, -2, -1)
             Bullet.Position = New Point(Player.Position.X + 15, Player.Position.Y + 9)
             Bullet.Color = Color.Cyan
             SpriteEngineDestination1.AddSprite(Bullet)
             Dim BulletMover As SCG.TurboSprite.DestinationMover = _
    SpriteEngineDestination1.GetMover(Bullet)
             BulletMover.Destination = New Point(SpriteSurface1.Width + 5, _
    Bullet.Height)
             BulletMover.SpeedX = 4
    
          End If
       End Sub
    
       Private Sub Form1_KeyUp(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    
          'Stop moving, but only if "a" or "z" is released.
          If e.KeyData = Keys.A Or e.KeyCode = Keys.Z Then
             Me.PlayerMover.SpeedY = 0
          End If
    
       End Sub
    
       Private Sub SpriteEngineDestination1_SpriteReachedDestination(ByVal sender As _
    Object, ByVal e As SCG.TurboSprite.SpriteEventArgs) Handles SpriteEngineDestination1.SpriteReachedDestination
          'You really should subclass the bullet to be able to
          'tag it as a bullet. I use color. Hack!!!
          Dim P As SCG.TurboSprite.PolygonSprite = CType(e.Sprite, SCG.TurboSprite.PolygonSprite)
          If P.Color = Color.Cyan Then
             P.Kill()
             SpriteEngineDestination1.RemoveSprite(P)
          End If
       End Sub
    
       Private Sub SpriteSurface1_SpriteCollision(ByVal sender As _
    Object, ByVal e As SCG.TurboSprite.SpriteCollisionEventArgs) Handles SpriteSurface1.SpriteCollision
          'Did the player shoot the enemy? Is one a bullet and one the enemy?
          'Again, I use color. Hack!!!
          Dim FirstSprite As SCG.TurboSprite.PolygonSprite = CType(e.Sprite1, _
    SCG.TurboSprite.PolygonSprite)
          Dim SecondSprite As SCG.TurboSprite.PolygonSprite = CType(e.Sprite2, _
    SCG.TurboSprite.PolygonSprite)
          If FirstSprite.Color = Color.Cyan Then
             'We have a bullet.
             If SecondSprite.Color = Color.Red Then
                'And the enemy.
                SecondSprite.Kill()
                SpriteEngineDestination1.RemoveSprite(SecondSprite)
             End If
          ElseIf FirstSprite.Color = Color.Red Then
             'We have the enemy.
             If SecondSprite.Color = Color.Cyan Then
                'And a bullet.
                FirstSprite.Kill()
                SpriteEngineDestination1.RemoveSprite(FirstSprite)
             End If
          End If
       End Sub
    
    End Class
    

    Again, subclass and add your own properties! Do not use color to determine what sprite you are dealing with. This is the result:

    Thank you, TurboSprite!

  • Writing a game using TurboSprite (1/2)

    There are two things to take away from this post. The ease of TurboSprite and the power and speed of GDI+. Of course, you are still supposed to write games using hardware acceleration, but you can do decent desktop games without DirectX, XNA or OpenGL.

    In this first part, I will create a player (a spaceship) that can respond to keyboard control. In the second part, the player will be able to shoot and kill enemies.

    To start off, I create a project, I add a sprite surface and an sprite engine destination. I connect them to each other by setting the Surface property of the engine. Also, I intend to use collision detection, so I set the DetectCollisionSelf property to True.

    On the surface, I set the AutoBlank property to True and the AutoBlankColor property to Black. On the form, I set KeyPreview to True.

    Note that so far, all the magic is happening in KeyDown, KeyUp (input feedback) and Shown (initialization).

    Public Class Form1
    
       'A reference to the player (created in the Shown event) and the mover.
       Private Player As SCG.TurboSprite.PolygonSprite
       Private PlayerMover As SCG.TurboSprite.DestinationMover
    
       Private Sub Form1_Shown(ByVal sender As Object, ByVal e As _
    System.EventArgs) Handles Me.Shown
    
          'Set the desired number of frames per second and activate the surface.
          SpriteSurface1.DesiredFPS = 40
          SpriteSurface1.Active = True
    
          'Create the player and save the reference.
          Me.Player = New SCG.TurboSprite.PolygonSprite(-20, -10, 20, 10, -20, 10)
    
          'Set player original position.
          Me.Player.Position = New Point(100, 100)
    
          'Set player color.
          Me.Player.Color = Color.Yellow
    
          'Add the player to the sprite engine.
          SpriteEngineDestination1.AddSprite(Me.Player)
    
          'Save a reference to the mover.
          Me.PlayerMover = SpriteEngineDestination1.GetMover(Me.Player)
    
       End Sub
    
       Private Sub Form1_KeyDown(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
          If e.KeyData = Keys.A Then
    
             'If user presses "a", move up!
             Me.PlayerMover.DestY = 0
             Me.PlayerMover.SpeedY = -2
    
          ElseIf e.KeyData = Keys.Z Then
    
             'If user presses "z", move down!
             Me.PlayerMover.DestY = SpriteSurface1.Height
             Me.PlayerMover.SpeedY = 2
    
          End If
       End Sub
    
       Private Sub Form1_KeyUp(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    
          'Stop moving!
          Me.PlayerMover.SpeedY = 0
    
       End Sub
    
    End Class
    

    Continued here.

  • TurboSprite

    TurboSprite is a software driven (GDI+) sprite engine written by Dion Kurczek in C#. The reason that I’m picking this up, is that it shows (like I have mentioned before) the amazing capabilities and performance of the GDI+ library in .NET. TurboSprite provides a sprite container, bitmap sprites, vector sprites and collision detection.

    To be able to use TurboSprite, download and compile it, add the components to your toolbox. Then, add a surface and an engine to a form. Note that you have two engines. One is called SpriteEngine and another is called SpriteEngineDestination. Use the SpriteEngineDestination object, because it extends the SpriteEngine class with the ability to move sprites. You might want to set a few properties to connect the engine to the surface, to tell the engine if you want collision detection and so on. Collision detection is a reasonable cause for using a sprite engine. I use the AutoBlank feature of the surface.

    About the moving of the sprites, you can acquire the sprite mover using the GetMover function for the engine (if you use the SpriteEngineDestination), and set the X and Y speed of a sprite. Or you can do as I do in this example below. That is, just set a speed and a destination position. The destination position determines the sprite movement direction.

    This code does two thinks (written in the Show event of a regular form). The first line activates the sprite engine so that sprites will move. The rest of the code initializes one vector sprite.

    'When the form is loaded, activate the surface
    SpriteSurface1.Active = True
    
    'Create a space ship. These points makes a ship.
    Dim S As New SCG.TurboSprite.PolygonSprite(0, -10, 5, 10, 0, 5, -5, 10)
    
    'Tell the sprite to rotate.
    S.Spin = SCG.TurboSprite.SpinType.Clockwise
    S.SpinSpeed = 3
    
    'Start position.
    S.Position = New Point(30, 30)
    
    'Add the sprite to the engine, and acquire the sprite mover from the engine.
    SpriteEngineDestination1.AddSprite(S)
    Dim Mover As SCG.TurboSprite.DestinationMover = SpriteEngineDestination1.GetMover(S)
    
    'Tell the mover about speed and direction.
    Mover.Speed = 5
    Mover.Destination = New Point(50, 40)
    
    'Tell the mover that the destination really isn't a destination,
    'it's just used as a direction.
    Mover.StopAtDestination = False
    

    This is the sprite:

    Download the engine from the Code Project web site.