|
-
May 1st, 2011, 08:38 PM
#1
Thread Starter
New Member
HELP! Matching two-dimensional array to coordinates on a form
Hey,
I'm creating a game and i want to use the form as a gameboard. I want to be able to traverse the gameboard with a picturebox using keypresses and have the coordinates of the picturebox correspond with an element in an array. I'm not too familiar with arrays so any help would be appreciated!
-
May 2nd, 2011, 02:33 AM
#2
Re: HELP! Matching two-dimensional array to coordinates on a form
Not too sure what you're trying to do. However here's an example of moving a PictureBox around a Form, automatically and manually.
Code:
Option Explicit
'
'Moving a PictureBox around a Form
'
' Assumes the following Controls are Drawn on the Form:
' PictureBox Named Picture1
' Timer Named Timer1
'
Private intCoOrds() As Integer
Private intMove As Integer
Private boOn As Boolean
Private Sub SetUpCoOrds(intMoves As Integer)
Dim intRndX As Integer
Dim intRndY As Integer
Dim intX As Integer
ReDim intCoOrds(intMoves - 1, 1)
'
' Generate intMoves number of random
' co-ordinates for the PictureBox
'
For intX = 0 To intMoves - 1
intRndX = Int(Rnd * ((Me.ScaleWidth - Picture1.ScaleWidth) + 1))
intCoOrds(intX, 0) = intRndX
intRndY = Int(Rnd * ((Me.ScaleHeight - Picture1.ScaleHeight) + 1))
intCoOrds(intX, 1) = intRndY
Next intX
End Sub
Private Sub Form_Activate()
Picture1.Top = 0
Picture1.Left = 0
Form1.KeyPreview = True
End Sub
Private Sub Form_Keyup(KeyCode As Integer, Shift As Integer)
Dim intLeft As Integer
Dim intTop As Integer
Select Case KeyCode
Case vbKeySpace
'
' Start / Stop the automated moving
'
boOn = Not boOn
If boOn = False Then
'
' Stop
'
Timer1.Enabled = False
Else
'
' Start
'
Call SetUpCoOrds(50)
Timer1.Enabled = True
End If
Case vbKeyRight
'
' Right Arrow Key - move one picture width
' to the right
' Make sure it's still within the Form
'
intLeft = Picture1.Left + Picture1.ScaleWidth
If intLeft > Me.ScaleWidth - Picture1.ScaleWidth Then
intLeft = Me.ScaleWidth - Picture1.ScaleWidth
End If
Picture1.Left = intLeft
Case vbKeyLeft
'
' Left Arrow Key - move one picture width
' to the left
' Make sure it's still within the Form
'
intLeft = Picture1.Left - Picture1.ScaleWidth
If intLeft < 0 Then intLeft = 0
Picture1.Left = intLeft
Case vbKeyUp
'
' Up Arrow Key - move one picture height
' up
' Make sure it's still within the Form
'
intTop = Picture1.Top - Picture1.ScaleHeight
If intTop < 0 Then intTop = 0
Picture1.Top = intTop
Case vbKeyDown
'
' Down Arrow Key - move one picture height
' down
' Make sure it's still within the Form
'
intTop = Picture1.Top + Picture1.ScaleHeight
If intTop > Me.ScaleHeight - Picture1.ScaleHeight Then
intTop = Me.ScaleHeight - Picture1.ScaleHeight
End If
Picture1.Top = intTop
End Select
Me.Caption = "Left: " & Picture1.Left & " Top: " & Picture1.Top
End Sub
Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 1000
Randomize
intMove = 0
End Sub
Private Sub Form_Resize()
Timer1.Enabled = False
boOn = False
Call SetUpCoOrds(50)
End Sub
Private Sub Timer1_Timer()
If intMove <= UBound(intCoOrds, 1) Then
'
' Move the Picture to the next random Co-Ordinate
'
Picture1.Left = intCoOrds(intMove, 0)
Picture1.Top = intCoOrds(intMove, 1)
Me.Caption = "Left = " & CStr(Picture1.Left) & " Top = " & CStr(Picture1.Top)
intMove = intMove + 1
Else
Timer1.Enabled = False
MsgBox "Finished"
End If
End Sub
When you press the Space key the program will establish 50 random co-ordinates for the PictureBox and move it every second. If you press the Space key again the automatic moving will stop and you can move the PictureBox manually with the Arrow Keys.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|