Qbasicnews.com

Full Version: trying to do a simple genetics sim
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Okay, i'm writing a simple genetics sim for my first (comparatively) big Qbasic project (not school related). I've gotten to the point where the 'beasties' in the sim have genes, and they move around randomly. eventually, if they touch each other, they'll reproduce and pass their genes on to the offspring. i haven't programmed the reproduction yet though.

my problem is that when i start the sim, all the 'beasties' eventually move to the left side of the screen. i suspect it has something to do with the 'movethem' sub (it doesn't actually move them, but it gives them new coordinates. the 'drawthem' sub draws them in the new location). Can anyone tell why?

The other problem i have is that the beasties move waaaay too fast. how can i slow down the loop?

Thanks for the help, sorry for the length of the post, and oh, i'm using qb4.5 on xp if it matters.


Quote:DECLARE SUB title () 'sub for drawing the title screen
DECLARE SUB start () 'sub for making the first 10 beasties
DECLARE SUB drawthem () 'sub for drawing the beasties
DECLARE SUB movethem () 'sub for moving the beasties
DECLARE SUB drawbox () 'draws a box
DECLARE SUB erasedem ()

CONST bnum = 50

DIM SHARED beasties(bnum) AS INTEGER ' 1 is existant, 0 is not
DIM SHARED bcolor(bnum) AS INTEGER ' the gene for color
DIM SHARED bgender(bnum) AS INTEGER ' the gene for gender
DIM SHARED bshape(bnum) AS INTEGER ' the gene for shape
DIM SHARED bsize(bnum) AS INTEGER ' the gene for size
DIM SHARED bage(bnum) AS INTEGER ' the gene for age
DIM SHARED bxpos(bnum) AS INTEGER ' the gene for the beastie's x coordinate
DIM SHARED bypos(bnum) AS INTEGER ' the gene for the beastie's y coordiante
DIM SHARED oldbxpos(bnum) AS INTEGER ' the gene for the beastie's old x coordinate
DIM SHARED oldbypos(bnum) AS INTEGER ' the gene for the beastie's old y coordiante


SCREEN 12
CLS

title 'prints the title screen
start 'gets all the data needed to start while user reads the title screen


DO
LOOP UNTIL INKEY$ <> ""


CLS
drawbox
drawthem


start:
choice$ = INKEY$
erasedem
drawbox
movethem
drawthem
IF choice$ = "q" THEN GOTO endprog: ELSE GOTO start




endprog:
END


SUB drawbox

'draws the box
COLOR 4
LINE (1, 1)-(638, 478), , B

END SUB


SUB drawthem
x = 1
DO
SELECT CASE bshape(x)
CASE 1:
one = bxpos(x) - bsize(x)
two = bypos(x) - bsize(x)
three = bxpos(x) + bsize(x)
four = bypos(x) + bsize(x)
LINE (one, two)-(three, four), bcolor(x), B
CASE 2:
CIRCLE (bxpos(x), bypos(x)), bsize(x), bcolor(x)
END SELECT


x = x + 1
LOOP WHILE beasties(x) = 2
END SUB

SUB erasedem
x = 1
DO
oldbxpos(x) = bxpos(x)
oldbypos(x) = bypos(x)
x = x + 1
LOOP WHILE beasties(x) = 2

x = 1
DO
SELECT CASE bshape(x)
CASE 1:
one = oldbxpos(x) - bsize(x)
two = oldbypos(x) - bsize(x)
three = oldbxpos(x) + bsize(x)
four = oldbypos(x) + bsize(x)
LINE (one, two)-(three, four), 0, B
CASE 2:
CIRCLE (oldbxpos(x), oldbypos(x)), bsize(x), 0
END SELECT

x = x + 1
LOOP WHILE beasties(x) = 2
END SUB

SUB movethem

x = 1

DO
RANDOMIZE TIMER

mv = INT(RND * 10) + 1
dir = INT(RND * 8 ) + 1


SELECT CASE dir

CASE 1:
bxpos(x) = bxpos(x) - mv
CASE 2:
bxpos(x) = bxpos(x) - mv
bypos(x) = bypos(x) + mv
CASE 3:
bypos(x) = bypos(x) + mv
CASE 4:
bypos(x) = bypos(x) + mv
bxpos(x) = bxpos(x) + mv
CASE 5:
bxpos(x) = bxpos(x) + mv
CASE 6:
bxpos(x) = bxpos(x) - mv
bypos(x) = bypos(x) - mv
CASE 7:
bypos(x) = bypos(x) - mv
CASE 8:
bypos(x) = bypos(x) - mv
bxpos(x) = bxpos(x) + v
END SELECT

IF bxpos(x) < 0 + bsize(x) THEN 'checks to make sure
bxpos(x) = (bxpos(x) + bsize(x)) ' that the coordinates
END IF 'don't go offscreen
IF bxpos(x) > (638 - bsize(x)) THEN
bxpos(x) = (bxpos(x) - bsize(x))
END IF
IF bypos(x) < (0 + bsize(x)) THEN
bypos(x) = (bypos(x) + bsize(x))
END IF
IF bypos(x) > (478 - bsize(x)) THEN
bypos(x) = (bypos(x) - bsize(x))
END IF


x = x + 1
LOOP WHILE beasties(x) = 2



END SUB

SUB start
RANDOMIZE TIMER
'bring the beasties to life (0 in this array is dead, 1 is being born (for later use) and 2 is alive)
FOR x = 1 TO 10
beasties(x) = 2
NEXT x

'sets the other beasties as nonexistant
FOR x = 11 TO bnum
beasties(x) = 0

NEXT x
>

'give the beasties some genes
x = 1
WHILE beasties(x) = 2

'assign colors for the beasties
rndr = INT(RND * 4) + 1
SELECT CASE rndr
CASE 1:
bcolor(x) = 1 'blue gene
CASE 2:
bcolor(x) = 2 'green gene
CASE 3:
bcolor(x) = 4 ' red gene
CASE 4:
bcolor(x) = 14 'yellow gene
END SELECT


'assign genders for the beasties
bgender(x) = INT(RND * 2) + 1 'male gene is 1 ;female gene is 2

'assign size for the beasties
bsize(x) = (INT(RND * 5) + 1) * 3 'the size is a num. between 1 and 15

'assign shape for the beasties
rndr = INT(RND * 2) + 1
SELECT CASE rndr
CASE 1:
bshape(x) = 1 'square shape gene
CASE 2:
bshape(x) = 2 'circle shape gene
END SELECT

'assign age to 0
bage(x) = 0


RANDOMIZE TIMER

'give the beasties starting coordinates
bxpos(x) = INT(RND * 638) + 1
bypos(x) = INT(RND * 478) + 1

x = x + 1
WEND

LOCATE 28, 1
PRINT "press any key to continue"


END SUB

SUB title
COLOR 5
PRINT "Little Beasties v1.0"
PRINT "--------------------"

LOCATE 26, 1


END SUB
1)
CASE 8:
bypos(x) = bypos(x) - mv
bxpos(x) = bxpos(x) + v
_________________ :roll:

2)
You can use TIMER:
DO
LOOP UNTIL tim!<TIMER
tim!=TIMER
You had a typo in the movethem sub (the usual cause of strange behavior in a prog.)
Code:
...
CASE 8:
bypos(x) = bypos(x) - mv
bxpos(x) = bxpos(x) + v
END SELECT
should read
Code:
...
CASE 8
bypos(x) = bypos(x) - mv
bxpos(x) = bxpos(x) + mv
END SELECT
BTW you don't need to include a colon after CASE statements, unless you continue on the same line Smile

and to slow them down add a slight pause like
Code:
FOR i = 1 TO 2000: NEXT i
and adjust the value to change the speed.
lol i should have known it was something like that- thanks a lot
Agravein and Phydaux. it works great now.
The FOR I = 1 to x:NEXT method sucks when ported to a different computer. Use the timer method and go:

t# = TIMER
WHILE t# + delayinseconds > TIMER: WEND
Unrelated, but you may want to consider using TYPEs for the "beasties" to make the program more readable/editable. But's it's really just a matter of personal preferance.