Text Only
|
Text with Attachments
QB64.org Forum
Active Forums => QB64 Discussion => Topic started by: bplus on February 10, 2021, 01:23:37 am
Title:
Collide Circle and Box
Post by:
bplus
on
February 10, 2021, 01:23:37 am
This is like the Game Operation get mouse from one side to other wo a squeak:
Code: QB64:
[Select]
_Title
"Collide Circle and Box"
'b+ 2021-02-09
' Johnno submits ref: https://www.qb64.org/forum/index.php?topic=3499.msg129726#msg129726
Randomize
Timer
Const
True
=
-
1
,
False
=
Not
True
Type
box
x
As
Single
y
As
Single
w
As
Single
h
As
Single
End
Type
Screen
_NewImage
(
1024
,
700
,
32
)
_Delay
.25
_ScreenMove
_Middle
'Print True, False
ReDim
b
(
1
To
50
)
As
box
For
i
=
1
To
50
b
(
i
)
.x
=
Rnd
*
1024
b
(
i
)
.y
=
Rnd
*
700
b
(
i
)
.w
=
Rnd
*
400
+
20
b
(
i
)
.h
=
Rnd
*
50
+
5
Next
mr
=
5
Do
Cls
While
_MouseInput
:
Wend
mx
=
_MouseX
: my
=
_MouseY
Circle
(
mx
,
my
)
,
mr
,
&HFF0000FF
For
i
=
1
To
50
Line
(
b
(
i
)
.x
,
b
(
i
)
.y
)
-
Step
(
b
(
i
)
.w
,
b
(
i
)
.h
)
,
_RGB32
(
i
*
5
,
-
1
*
(
i
<
25
)
+
128
+
(
i
>
25
)
,
255
-
i
*
5
)
,
B
If
collide
(
mx
,
my
,
mr
,
b
(
i
)
.x
,
b
(
i
)
.y
,
b
(
i
)
.w
,
b
(
i
)
.h
)
Then
Beep
Next
_Display
_Limit
60
Loop
While
_KeyDown
(
27
)
=
0
Function
collide
(
cx
,
cy
,
radius
,
rx
,
ry
,
rw
,
rh
)
distX
=
Abs
(
cx
-
rx
-
rw
/
2
)
distY
=
Abs
(
cy
-
ry
-
rh
/
2
)
If
distX
>
(
rw
/
2
+
radius
)
Then
collide
=
False:
Exit
Function
End
If
If
distY
>
(
rh
/
2
+
radius
)
Then
collide
=
False:
Exit
Function
End
If
If
distX
<=
rw
/
2
Then
collide
=
True:
Exit
Function
End
If
If
distY
<
rh
/
2
Then
collide
=
True:
Exit
Function
End
If
'still here?
dx
=
distX
-
rw
/
2
dy
=
distY
-
rh
/
2
collide
=
(
dx
*
dx
+
dy
*
dy
<=
(
radius
*
radius
)
)
End
Function
Title:
Re: Collide Circle and Box
Post by:
bplus
on
February 10, 2021, 01:26:39 am
Press spacebar to change scenery:
Code: QB64:
[Select]
_Title
"Collide Circle and Box"
'b+ 2021-02-09
' Johnno submits ref: https://www.qb64.org/forum/index.php?topic=3499.msg129726#msg129726
Randomize
Timer
Const
True
=
-
1
,
False
=
Not
True
Type
box
x
As
Single
y
As
Single
w
As
Single
h
As
Single
End
Type
Screen
_NewImage
(
1024
,
700
,
32
)
_Delay
.25
_ScreenMove
_Middle
'Print True, False
ReDim
b
(
1
To
50
)
As
box
For
i
=
1
To
50
b
(
i
)
.x
=
Rnd
*
1024
b
(
i
)
.y
=
Rnd
*
700
b
(
i
)
.w
=
Rnd
*
400
+
20
b
(
i
)
.h
=
Rnd
*
50
+
5
Next
mr
=
5
Do
Cls
While
_MouseInput
:
Wend
mx
=
_MouseX
: my
=
_MouseY
Circle
(
mx
,
my
)
,
mr
,
&HFF0000FF
For
i
=
1
To
50
Line
(
b
(
i
)
.x
,
b
(
i
)
.y
)
-
Step
(
b
(
i
)
.w
,
b
(
i
)
.h
)
,
_RGB32
(
i
*
5
,
-
1
*
(
i
<
25
)
+
128
+
(
i
>
25
)
,
255
-
i
*
5
)
,
B
If
collide
(
mx
,
my
,
mr
,
b
(
i
)
.x
,
b
(
i
)
.y
,
b
(
i
)
.w
,
b
(
i
)
.h
)
Then
Beep
Next
If
Len
(
InKey$
)
Then
For
i
=
1
To
50
b
(
i
)
.x
=
Rnd
*
1024
b
(
i
)
.y
=
Rnd
*
700
b
(
i
)
.w
=
Rnd
*
400
+
20
b
(
i
)
.h
=
Rnd
*
50
+
5
Next
End
If
_Display
_Limit
60
Loop
While
_KeyDown
(
27
)
=
0
Function
collide
(
cx
,
cy
,
radius
,
rx
,
ry
,
rw
,
rh
)
distX
=
Abs
(
cx
-
rx
-
rw
/
2
)
distY
=
Abs
(
cy
-
ry
-
rh
/
2
)
If
distX
>
(
rw
/
2
+
radius
)
Then
collide
=
False:
Exit
Function
End
If
If
distY
>
(
rh
/
2
+
radius
)
Then
collide
=
False:
Exit
Function
End
If
If
distX
<=
rw
/
2
Then
collide
=
True:
Exit
Function
End
If
If
distY
<
rh
/
2
Then
collide
=
True:
Exit
Function
End
If
'still here?
dx
=
distX
-
rw
/
2
dy
=
distY
-
rh
/
2
collide
=
(
dx
*
dx
+
dy
*
dy
<=
(
radius
*
radius
)
)
End
Function
Title:
Re: Collide Circle and Box
Post by:
bplus
on
February 10, 2021, 01:29:09 am
Fix colors
Code: QB64:
[Select]
_Title
"Collide Circle and Box"
'b+ 2021-02-09
' Johnno submits ref: https://www.qb64.org/forum/index.php?topic=3499.msg129726#msg129726
Randomize
Timer
Const
True
=
-
1
,
False
=
Not
True
Type
box
x
As
Single
y
As
Single
w
As
Single
h
As
Single
End
Type
Screen
_NewImage
(
1024
,
700
,
32
)
_Delay
.25
_ScreenMove
_Middle
'Print True, False
ReDim
b
(
1
To
50
)
As
box
For
i
=
1
To
50
b
(
i
)
.x
=
Rnd
*
1024
b
(
i
)
.y
=
Rnd
*
700
b
(
i
)
.w
=
Rnd
*
400
+
20
b
(
i
)
.h
=
Rnd
*
50
+
5
Next
mr
=
5
Do
Cls
While
_MouseInput
:
Wend
mx
=
_MouseX
: my
=
_MouseY
Circle
(
mx
,
my
)
,
mr
,
&HFF0000FF
For
i
=
1
To
50
Line
(
b
(
i
)
.x
,
b
(
i
)
.y
)
-
Step
(
b
(
i
)
.w
,
b
(
i
)
.h
)
,
_RGB32
(
i
*
5
,
-
50
*
(
i
<
25
)
+
128
+
50
*
(
i
>
25
)
,
255
-
i
*
5
)
,
B
If
collide
(
mx
,
my
,
mr
,
b
(
i
)
.x
,
b
(
i
)
.y
,
b
(
i
)
.w
,
b
(
i
)
.h
)
Then
Beep
Next
If
Len
(
InKey$
)
Then
For
i
=
1
To
50
b
(
i
)
.x
=
Rnd
*
1024
b
(
i
)
.y
=
Rnd
*
700
b
(
i
)
.w
=
Rnd
*
400
+
20
b
(
i
)
.h
=
Rnd
*
50
+
5
Next
End
If
_Display
_Limit
60
Loop
While
_KeyDown
(
27
)
=
0
Function
collide
(
cx
,
cy
,
radius
,
rx
,
ry
,
rw
,
rh
)
distX
=
Abs
(
cx
-
rx
-
rw
/
2
)
distY
=
Abs
(
cy
-
ry
-
rh
/
2
)
If
distX
>
(
rw
/
2
+
radius
)
Then
collide
=
False:
Exit
Function
End
If
If
distY
>
(
rh
/
2
+
radius
)
Then
collide
=
False:
Exit
Function
End
If
If
distX
<=
rw
/
2
Then
collide
=
True:
Exit
Function
End
If
If
distY
<
rh
/
2
Then
collide
=
True:
Exit
Function
End
If
'still here?
dx
=
distX
-
rw
/
2
dy
=
distY
-
rh
/
2
collide
=
(
dx
*
dx
+
dy
*
dy
<=
(
radius
*
radius
)
)
End
Function
Dang I thought I was in the Programs Board, sorry. :p
Title:
Re: Collide Circle and Box
Post by:
Dav
on
February 10, 2021, 12:35:22 pm
Nice little snippet for learning and building on to make a new game. Easy to comprehend. Thanks for sharing.
- Dav
Text Only
|
Text with Attachments