richardsmythe
asked on
Moving Pixels
Hi
I need a cirlce of pixels to move around the picture box bouncing of the walls. So far I have got it to move but I am stuck as from what I do now.
Here is what I have done so far...
Dim Horizontal As Boolean
Dim Vertical As Boolean
Dim Xcoord As Integer
Dim Ycoord As Integer
Dim Speed As Integer
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
Do
Sleep 5
If Horizontal = 0 Then Xcoord = Xcoord - Speed
If Horizontal = 1 Then Xcoord = Xcoord + Speed
If Vertical = 0 Then Ycoord = Ycoord + Speed
If Vertical = 1 Then Ycoord = Ycoord - Speed
Picture1.Cls
Picture1.Circle (Xcoord, Ycoord), 10, vbRed
DoEvents
Loop
End Sub
Private Sub Form_Load()
Xcoord = 400
Ycoord = 100
Speed = 1
Horizontal = 0
Vertical = 1
End Sub
Thanks
I need a cirlce of pixels to move around the picture box bouncing of the walls. So far I have got it to move but I am stuck as from what I do now.
Here is what I have done so far...
Dim Horizontal As Boolean
Dim Vertical As Boolean
Dim Xcoord As Integer
Dim Ycoord As Integer
Dim Speed As Integer
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
Do
Sleep 5
If Horizontal = 0 Then Xcoord = Xcoord - Speed
If Horizontal = 1 Then Xcoord = Xcoord + Speed
If Vertical = 0 Then Ycoord = Ycoord + Speed
If Vertical = 1 Then Ycoord = Ycoord - Speed
Picture1.Cls
Picture1.Circle (Xcoord, Ycoord), 10, vbRed
DoEvents
Loop
End Sub
Private Sub Form_Load()
Xcoord = 400
Ycoord = 100
Speed = 1
Horizontal = 0
Vertical = 1
End Sub
Thanks
ASKER
Heheh good ol' Idle_Mind. What was wrong with my way? would it not have worked?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i cant get that to work
ASKER
Is it possible in VB to test the current pixel colour in a particular situation. eg if pixel colour = black then do something.
Private Sub Command1_Click()
Dim pixelColor As Long
' grab the color of the point at (100, 100) in Picture1
pixelColor = Picture1.Point(100, 100)
' like this...
If pixelColor = vbBlack Then
' do something
End If
' or this...
Dim r, g, b As Byte
Red = pixelColor Mod 256
Green = (pixelColor \ 256) Mod 256
Blue = (pixelColor \ 65536) Mod 256
If Red = 0 And Green = 0 And Blue = 0 Then
' do something
End If
End Sub
Dim pixelColor As Long
' grab the color of the point at (100, 100) in Picture1
pixelColor = Picture1.Point(100, 100)
' like this...
If pixelColor = vbBlack Then
' do something
End If
' or this...
Dim r, g, b As Byte
Red = pixelColor Mod 256
Green = (pixelColor \ 256) Mod 256
Blue = (pixelColor \ 65536) Mod 256
If Red = 0 And Green = 0 And Blue = 0 Then
' do something
End If
End Sub
ASKER
Cool. In my pong came I am trying to get that pixel circle thing to bounce off padles. I have paddles on all sides of the form. left, bottom, right, top. I got the left side to work so that It bounces off the paddle and its game over if it doesnt. But I cant seem to get the other sides to work.
If horizontal Then
Xcoord = Xcoord - Speed
'If Xcoord = BallRadius Then horizontal = False
If Xcoord <= Left_Paddle.Width Then
If Ycoord >= Left_Paddle.Top And Ycoord <= (Left_Paddle.Top + Left_Paddle.Height) Then
horizontal = False
lbl_score.Caption = lbl_score.Caption + 3
Else
If Xcoord < -BallRadius Then
MsgBox "Game over"
End If
End If
End If
Thats for the left side. But my attempts at doing the other sides dont worK :(
If horizontal Then
Xcoord = Xcoord - Speed
'If Xcoord = BallRadius Then horizontal = False
If Xcoord <= Left_Paddle.Width Then
If Ycoord >= Left_Paddle.Top And Ycoord <= (Left_Paddle.Top + Left_Paddle.Height) Then
horizontal = False
lbl_score.Caption = lbl_score.Caption + 3
Else
If Xcoord < -BallRadius Then
MsgBox "Game over"
End If
End If
End If
Thats for the left side. But my attempts at doing the other sides dont worK :(
It would be something like this:
If Not horizontal Then
Xcoord = Xcoord + Speed
If Xcoord + BallRadius >= Right_Paddle.Left Then
If Ycoord >= Right_Paddle.Top And Ycoord <= (Right_Paddle.Top + Right_Paddle.Height) Then
horizontal = true
lbl_score.Caption = lbl_score.Caption + 3
Else
If Xcoord >= Picture1.Width - BallRadius Then
MsgBox "Game over"
End If
End If
End If
If Not horizontal Then
Xcoord = Xcoord + Speed
If Xcoord + BallRadius >= Right_Paddle.Left Then
If Ycoord >= Right_Paddle.Top And Ycoord <= (Right_Paddle.Top + Right_Paddle.Height) Then
horizontal = true
lbl_score.Caption = lbl_score.Caption + 3
Else
If Xcoord >= Picture1.Width - BallRadius Then
MsgBox "Game over"
End If
End If
End If
ASKER
ah youre a star
Option Explicit
Private Const radius = 10
Private x As Single
Private y As Single
Private xStep As Single
Private yStep As Single
Private xDir As Boolean
Private yDir As Boolean
Private Sub Form_Load()
Timer1.Enabled = False
Me.ScaleMode = vbPixels
Picture1.ScaleMode = vbPixels
Picture1.Appearance = 0 ' flat
Picture1.BorderStyle = 1 ' fixed single
Randomize Timer
' Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
x = CSng(((Picture1.Width - radius - 2) - radius + 1) * Rnd + radius)
y = CSng(((Picture1.Height - radius - 2) - radius + 1) * Rnd + radius)
xStep = 5
yStep = 5
If Rnd <= 0.5 Then
xDir = True
Else
xDir = False
End If
If Rnd <= 0.5 Then
yDir = True
Else
yDir = False
End If
Timer1.Interval = 25
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
If xDir Then
If x + xStep <= Picture1.Width - radius - 2 Then
x = x + xStep
Else
x = Picture1.Width - radius - 2
xDir = Not xDir
End If
Else
If x - xStep >= radius Then
x = x - xStep
Else
x = radius
xDir = Not xDir
End If
End If
If yDir Then
If y + yStep <= Picture1.Height - radius - 2 Then
y = y + yStep
Else
y = Picture1.Height - radius - 2
yDir = Not yDir
End If
Else
If y - yStep >= radius Then
y = y - yStep
Else
y = radius
yDir = Not yDir
End If
End If
Picture1.Cls
Picture1.Circle (x, y), radius, vbBlack
End Sub