-
hi,
how would i create a little ai bot that when it was placed onto a picture, the colour of the picture would tell the robot in which direction to go, for example if the colour was black the bot, would not cross the line but if it was white it could cross that line. i would like the bot to be anything as long as i could see it move and also be able to change the picture, limited to 8 colours,
any suggestions or want to help email me at
[email protected]
all help will be gratefully recieved
Merlin ?
-
Assuming you have your bot's position's stored in a Variable, you can use the GetPixel API to check what colour the Pixel in front of it is. If it's black, then change directions.
-
hi,
how would i iniciate this api into a picture box for example, so this could be the bot, would it be able to get the bot to turn (visibly ?)
Merlin ?
-
I wouldn't bother with the API unless you really need some speed on this thing, use the Point Method instead, Picture1.Point(X,Y) will return the Colour of the Pixel at Coordinates X,Y Simple as that.
-
hi,
Please could i have some code examples as these would help a lot
Merlin ?
-
Point is many times slower than GetPixel, so I do not recommend using it.
Put this in your Declarations.
Code:
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Type Position
X As Integer
Y As Integer
Direction As Byte
End Type
Dim Bot As Position
When the Bot moves, use this.
Code:
'This assumes that you are trying to move down
lColor = GetPixel(Picture1.hdc, Bot.X, Bot.Y + 1)
If lColor = vbBlack Then
'Assuming there are 4 directions (Left, Up, Right and Button)
'So switch to the Next direction
Bot.Direction = Bot.Direction + 1
Else
'If it's not balck then move it
Bot.Y = Bot.Y + 1
SetPixel Picture1.hdc, Bot.x, Bot.y, vbBlack
End If
[Edited by Megatron on 07-11-2000 at 04:07 PM]
-
Sample Code
I wrote a small code that should do what you want. You can add to it for more advanced features.
WHAT YOU NEED
Timer called Timer1
PictureBox called Picture1
Make sure that you have a Picture loaded into the PictureBox. Also, make sure that you
have some Black lines in your Picture for the Bot to move around.
Code:
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
'Constants for the Different directions
Const B_LEFT = 1
Const B_UP = 2
Const B_RIGHT = 3
Const B_DOWN = 4
'Create a User Defined Type that will hold the X and Y coordines of the Bot
'as well at what its current direction is
Private Type Position
x As Integer
y As Integer
Direction As Byte
End Type
'Declare a Variable as our UDT
Dim Bot As Position
Private Function ChangeDirection()
'This function will Randomize between 1 and 4 to choose a new direction.
Dim NewDirection
Randomize Timer
NewDirection = Int(Rnd * 4) + 1
ChangeDirection = NewDirection
End Function
Private Sub Form_Initialize()
'These properties are set when the Form is initialized (created)
'and will only occur once per Instace of the App.
'These are the starting proerties for the Bot. As you can see, it
'starts at 16x16. Make sure that you have room at cordinates 16x16
'for the Bot to move. If not, you can always change this to whatever
'position suits you.
Bot.x = 16
Bot.y = 16
Bot.Direction = 3
'Draw our Bot's in Blue
SetPixel Picture1.hdc, Bot.x, Bot.y, vbBlue
Picture1.Refresh
'The Timers Interval will be set to 15. you can change this if it is too
'fast or too slow for you.
Timer1.Interval = 15
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
'Clear's the PictureBox so the Bot does not leave a trail
Picture1.Cls
'Here is a Select Case statement for each direction. Only one of them
'is commented. The rest are pretty much the same except for the direction
'bit, but you can probably figure that out
Select Case Bot.Direction
Case B_RIGHT
'Get the colour of the Pixel it is about to move to
lColor = GetPixel(Picture1.hdc, Bot.x + 1, Bot.y)
'If the colour Black then change the direction
If lColor = vbBlack Then
Bot.Direction = ChangeDirection
Else
'If it is not Black, then move our character to the next
'position. First, we move the coordinates from the Variable
Bot.x = Bot.x + 1
'Then we Re-Draw it in its new location
SetPixel Picture1.hdc, Bot.x, Bot.y, vbBlue
'Refresh the PictreBox, so we can see it
Picture1.Refresh
End If
Case B_LEFT
lColor = GetPixel(Picture1.hdc, Bot.x - 1, Bot.y)
If lColor = vbBlack Then
Bot.Direction = ChangeDirection
Else
Bot.x = Bot.x - 1
SetPixel Picture1.hdc, Bot.x, Bot.y, vbBlue
Picture1.Refresh
End If
Case B_DOWN
lColor = GetPixel(Picture1.hdc, Bot.x, Bot.y + 1)
If lColor = vbBlack Then
Bot.Direction = ChangeDirection
Else
Bot.y = Bot.y + 1
SetPixel Picture1.hdc, Bot.x, Bot.y + 1, vbBlue
Picture1.Refresh
End If
Case B_UP
lColor = GetPixel(Picture1.hdc, Bot.x, Bot.y - 1)
If lColor = vbBlack Then
Bot.Direction = ChangeDirection
Else
Bot.y = Bot.y - 1
SetPixel Picture1.hdc, Bot.x, Bot.y - 1, vbBlue
Picture1.Refresh
End If
End Select
End Sub