Guys.. I need help in my RPG game project.. I used visual basic 2008, windows form applications. I used panel and its background is the map, and I used picture box as the character and with transparent gif pictures in every keyevents, I already can move the picturebox with keyevents. But the problem is, whenever I move the picturebox,I can see the box of picturebox even it is transparent. I want to learn bitblt or other things that can smooth it, I need a tutorial pls.:'(
rinoa0424 0 Newbie Poster
This attachment is potentially unsafe to open. It may be an executable that is capable of making changes to your file system, or it may require specific software to open. Use caution and only open this attachment if you are comfortable working with x-ms-bmp files.
nick.crane 342 Veteran Poster
To do what you want you are probably better drawing directly to the Panel from the Paint event.
(You might get even smoother graphics if you use a Picturebox instead as is does not have to deal with possible child controls when painting)
Here is a quick example of how to move an image on the face of a Panel.
Public Class Form1
Dim myCharacter As System.Drawing.Bitmap ' image of moving character
Dim myCharacterPosn As Point ' position of character
Sub New()
InitializeComponent()
' get character image from resources
myCharacter = My.Resources.MyCharacter
' set initial position of character
myCharacterPosn = New Point(300, 300)
End Sub
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
' draw character on to panel
e.Graphics.DrawImage(myCharacter, myCharacterPosn)
End Sub
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
' invalidate current character location (so it is removed)
Panel1.Invalidate(New Rectangle(myCharacterPosn, myCharacter.Size))
' move character to new location
Select Case e.KeyCode
Case Keys.Up
myCharacterPosn.Offset(0, -10)
e.Handled = True
Case Keys.Down
myCharacterPosn.Offset(0, 10)
e.Handled = True
Case Keys.Left
myCharacterPosn.Offset(-10, 0)
e.Handled = True
Case Keys.Right
myCharacterPosn.Offset(10, 0)
e.Handled = True
End Select
'invalidate new character location (so it is drawn)
Panel1.Invalidate(New Rectangle(myCharacterPosn, myCharacter.Size))
End Sub
End Class
Edited by nick.crane because: n/a
rinoa0424 0 Newbie Poster
wow thanks for the reply sir .. that was so smooth , but I don't know what method to use in collision of that myCharacterPosn to my panel walls? previously I used
if pcbChar.bounds.intersectsWith(pnlBlock.bounds) then
'stop the movement
Shall I make a rectangle for myCharacterPosn?
Since it was already a paint I can't use .gif image to step the foot of the image by a timer..
nick.crane 342 Veteran Poster
OK, I changed my sample to use a PictureBox to hold the character image and location.
This way you can use the Bounds property of the PictureBox as you did before.
Public Class Form1
Dim myCharacter As PictureBox ' image of moving character
Sub New()
InitializeComponent()
' create picture box to hold my character
' Note : This is not added to any form collection
' it is a convenient holder for both image and location and is used to get the image bounds
myCharacter = New PictureBox()
' get character image from resources
myCharacter.Image = My.Resources.MyCharacter
myCharacter.SizeMode = PictureBoxSizeMode.AutoSize
' set initial position of character
myCharacter.Location = New Point(300, 300)
End Sub
Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
' draw character on to picture box
e.Graphics.DrawImage(myCharacter.Image, myCharacter.Bounds)
End Sub
Dim lastMoveTime As DateTime ' track time of last move
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
' do nothing if key down repeat is too fast
If (DateTime.Now - lastMoveTime).Milliseconds < 50 Then
Return
End If
lastMoveTime = DateTime.Now
' invalidate current character location (so it is removed)
Panel1.Invalidate(myCharacter.Bounds)
' move character to new location
Dim newPosn = myCharacter.Location
Select Case e.KeyCode
Case Keys.Up
newPosn.Offset(0, -10)
e.Handled = True
Case Keys.Down
newPosn.Offset(0, 10)
e.Handled = True
Case Keys.Left
newPosn.Offset(-10, 0)
e.Handled = True
Case Keys.Right
newPosn.Offset(10, 0)
e.Handled = True
End Select
' set new position
myCharacter.Location = newPosn
'invalidate new character location (so it is drawn)
Panel1.Invalidate(myCharacter.Bounds)
End Sub
End Class
I also added a time test in the KeyDown event to stop the movement from running away on key repeats.
rinoa0424 0 Newbie Poster
Well thank you very much sir :) ...I got it now .. Can I get help from you next time? this is my first rpg game and Im gonna work hard for this because I will submit this project next friday.. My next plan to make is the battle, just like ninja saga in facebook..
nick.crane 342 Veteran Poster
Can I get help from you next time?
I'm usually found on the C# forum but sometime look at the VB forum.
There are plenty of others in this forum that can help so by all means post any questions and someone will help you if they can.
Edited by nick.crane because: n/a
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.