Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.
2010-07-19
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.
Categories: Visual Basic 9
Tags: 2D game
Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!
[…] Continued from here. […]