Making a spider bounce in qbasic
Posted: Thu May 11, 2006 7:37 pm
how do you make a spider bounce off the edge without leaving the screen?
Discuss QBasic, Freebasic, QB64 and more
http://petesqbsite.com/phpBB3/
Code: Select all
' Create a vector of random angle and give it a length specified
TYPE Vector
x AS DOUBLE
y AS DOUBLE
END TYPE
CONST PI = 3.141592654
DIM a AS Vector
DIM angle AS DOUBLE
SUB Multiply (a AS Vector, Factor AS DOUBLE)
a.x = a.x*Times
a.y = a.y*Times
END SUB
FUNCTION PrintOut$ (a AS Vector)
DIM b AS STRING
b = "("+STR$(a.x)+","+STR$(a.y)+")"
Return b
END FUNCTION
SUB Add (a AS Vector, b AS Vector)
a.x = a.x + b.x
a.y = a.y + b.y
END SUB
'Randomize an angle
Print "Randomized angle was: "; RND*2; "*pi"
angle = RND(0)*2*PI
a.y = SIN(angle)
a.x = COS(angle)
PRINT STR$(angle)
'Ask for a length
DIM Factor AS DOUBLE
INPUT "Please input length:", Factor
'Multiply
CALL Multiply(a, Factor)
'Print out the result
PRINT "Result: ";PrintOut$(a)
Code: Select all
cls
SCREEN 12
highbounce = 460
lowbounce = 1
highx = 640
lowx = 1
count = 1
x = INT((highx - lowx + 1) * RND + lowx)
y = INT((highy - lowy + 1) * RND + lowy)
DO
CIRCLE (x, y - 5), 5, 4
PAINT (x, y - 5), 4, 4
CIRCLE (x + 30, y - 5), 5, 4
PAINT (x + 30, y - 5), 5, 4
LINE (x, y)-(x + 30, y + 30), 14, BF
FOR count = 1 TO 20000
NEXT count
LINE (x, y)-(x + 30, y + 30), 0, BF
CIRCLE (x, y - 5), 5, 0
PAINT (x, y - 5), 0, 0
CIRCLE (x + 30, y - 5), 5, 0
PAINT (x + 30, y - 5), 0, 0
y = y + 1
IF y > 460 THEN
DO
y = y - 1
CIRCLE (x, y - 5), 5, 4
PAINT (x, y - 5), 4, 4
CIRCLE (x + 30, y - 5), 5, 4
PAINT (x + 30, y - 5), 4, 4
LINE (x, y)-(x + 30, y + 30), 14, BF
FOR count = 1 TO 2000
NEXT
LINE (x, y)-(x + 30, y + 30), 0, BF
CIRCLE (x, y - 5), 5, 0
PAINT (x, y - 5), 0, 0
CIRCLE (x + 30, y - 5), 5, 0
PAINT (x + 30, y - 5), 0, 0
IF bounce = y THEN EXIT DO
LOOP
bounce = INT((highbounce - lowbounce + 1) * RND + lowbounce)
IF bounce > 460 THEN EXIT DO
END IF
LOOP
END
Code: Select all
CIRCLE (x, y - 5), 5, 4
PAINT (x, y - 5), 4, 4
CIRCLE (x + 30, y - 5), 5, 4
PAINT (x + 30, y - 5), 4, 4
LINE (x, y)-(x + 30, y + 30), 14, BF
Code: Select all
LINE (x - 5, y - 10) - (x + 35, y + 30), 0, BF
Code: Select all
SCREEN 12
CLS
RANDOMIZE TIMER
xposition = INT(RND * 640)
yposition = INT(RND * 480)
oldxpos = xposition
oldypos = yposition
escapekey$ = CHR$(27)
xdirection = 1
IF INT(RND * 2) = 0 then xdirection = -1
ydirection = 1
IF INT(RND * 2) = 0 then ydirection = -1
DO
LINE (oldxpos - 5, oldypos - 10) - (oldxpos + 35, oldypos + 30), 0, BF
CIRCLE (xposition, yposition - 5), 5, 4
PAINT (xposition, yposition - 5), 4, 4
CIRCLE (xposition + 30, yposition - 5), 5, 4
PAINT (xposition + 30, yposition - 5), 4, 4
LINE (xposition, yposition)-(xposition + 30, yposition + 30), 14, BF
pausetime = TIMER
DO
LOOP UNTIL TIMER >= pausetime + .01
oldxpos = xposition
oldypos = yposition
xposition = xposition + xdirection
yposition = yposition + ydirection
IF xposition <0 or xposition > 640 THEN xdirection = xdirection * -1
IF yposition <0 or yposition > 480 THEN ydirection = ydirection * -1
LOOP UNTIL INKEY$ = escapekey$
END