Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hehe, i'm back and in need of suggestions pleeze =P 1

Status
Not open for further replies.

mpaone12

Technical User
Oct 15, 2000
13
US
hello again, just want to thank Miggy and everyone who helped me out before. i took your suggestions and put some comments in pretty clearly explaing my program and i also updated it a lil

yet i'm having problems with speed...see, the computers at school have QB1.1 and are very slow, and i program here on a PIII 800 with QB4.5. so when i bring them to school to show them off, it runs pretty crappy. SOoo, my question is..is there a way in QB to determine the speed of a system so that i could make the delay in the program speedup/slow down in accordance to the speed of the computer??? this has been bugging me forever. and i know it can be done, because professional programmers must do this all the time for games and similar things, right???

also, there is one other problem...if the points reaches a certain number then there is an OverFlow error. i think this is because of DEFINT A-Z, but i'm not sure. is there a way to work around this??

so if there are any graphics geniuses out there or just anyone who can think of a way to make my program faster/more logical i would greatly appreciate some feedback
THANKS AGAIN ALL! s-)


DECLARE SUB ShotHit ()
DECLARE SUB Shotsetback ()
DECLARE SUB StarField ()
DEFINT A-Z
DIM SHARED xboard, xshot, yshot, shot, x, y, speed, sc, counter2
DIM SHARED bc, points, ppoints, r, collision, loops
DECLARE SUB Fire ()
RANDOMIZE TIMER
SCREEN 12: CLS
counter2 = 62: shot = 0: bc = 1: speed = 5: sc = 1 'Set initial values.

LINE (xboard + 320, 460)-(xboard + 20 + 320, 463), 1, BF 'Draw Initial Board(Paddle)

'***Variable Descriptions
' xboard = x pos of the board
' xshot = x pos of the board's shot (bullet) (used in shot SUB)
' yshot = y pos of the board's shot
' x and y = x and y pos of the missle: r = rate or speed of the missle
' sc = star color: counter = star palette counter
' bc = board color: counter2 = board palette counter
' points = acual calculated points, ppoints = displayed or printed points
' loops = times to loop the explosion
' speed = speed of bullet
'***Flags
' shot = tells that a shot has been fired
' collison = tells that a collision between missle and bullet has happened
' rite = tells that the right key has been pressed and animates board to right
' lehft = opposite of rite

DO

y = INT(RND * 400) + 1 'Gets random y pos and rate for missle
r = INT(RND * 3) + 1
IF r = 1 THEN 'Adds trail color according to speed.
trailcol = 9
ELSEIF r = 2 THEN trailcol = 4
ELSE IF r = 3 THEN trailcol = 14
END IF
SOUND INT(RND * 300) + (r * 100), 1


FOR x = 0 TO 650 STEP r 'Main Loop of Program

LOCATE 1, 10: PRINT "Speed ="; speed
LOCATE 1, 50: COLOR 9: PRINT "Points: "; ppoints 'Prints points and speed.

mcol = INT(RND * 9) + 6 'Random color of missle.(dum i know, i'll change it later, heh)

LINE (x, y)-(x, y), trailcol 'Draw Missle
CIRCLE (x, y), 3, mcol
CIRCLE (x - r, y), 3, 0

StarField 'Calls the sub to draw stars

key$ = INKEY$

IF key$ <> &quot;&quot; THEN GOTO getkey ELSE GOTO skipkey 'Skips if commands is nothing entered
'does this speeed up program? i don't know
getkey:
IF key$ = CHR$(0) + CHR$(77) THEN
rite = 1

ELSEIF key$ = CHR$(0) + CHR$(75) THEN 'gets right or left flag
lehft = 1

ELSEIF key$ = &quot;x&quot; AND shot = 0 THEN 'speed up or slow down bullet
IF speed = 10 THEN speed = speed ELSE speed = speed + 1

ELSEIF key$ = &quot;z&quot; AND shot = 0 THEN
IF speed = 5 THEN speed = speed ELSE speed = speed - 1

ELSEIF key$ = CHR$(27) THEN END

ELSEIF shot = 0 THEN
IF key$ = CHR$(32) THEN 'checks if shot is already fired and if not, then sets shot
SOUND 400, .5 'flag to 1 (or on)
shot = 1
END IF
END IF

skipkey:
IF rite = 1 THEN
LINE (xboard + 320, 460)-(xboard + 20 + 320, 463), 0, BF
xboard = xboard + 3
LINE (xboard + 320, 460)-(xboard + 20 + 320, 463), 1, BF
count = count + 1
IF count = 12 THEN rite = 0: count = 0 'does either left or right board
ELSEIF lehft = 1 THEN 'animation
LINE (xboard + 320, 460)-(xboard + 20 + 320, 463), 0, BF
xboard = xboard - 3
LINE (xboard + 320, 460)-(xboard + 20 + 320, 463), 1, BF
count = count + 1
IF count = 12 THEN lehft = 0: count = 0
END IF

IF shot = 1 THEN Fire 'calls shot sub if flag is on

IF collision = 1 THEN ShotHit 'calls shothit sub if flag is on

IF shot = 0 THEN 'brightens board if shot sub ends and board is dim
IF counter2 < 62 THEN '(my little &quot;reload&quot; effect)
PALETTE 1, 0 * 256 + counter2 * 256 ^ 2 + 0
counter2 = counter2 + 2
END IF
END IF

IF yshot > 440 THEN Shotsetback 'sets necesary variables back if board shot leaves screen

LINE (0, y)-(x, y), trailcol 'redraw missle to avoid erasing

FOR tt = 1 TO 18
FOR t = 1 TO 20000: NEXT t 'THIS IS THE SUCKY PART, lol, this is my delay loop
NEXT tt

NEXT x

LINE (0, y)-(x, y), 0 'erase missle after it leaves screen

LOOP

'LOCATE 1, 50: PRINT &quot;x =&quot;; x
'LOCATE 2, 50: PRINT &quot;r =&quot;; r
'LOCATE 3, 50: PRINT &quot;xboard =&quot;; xboard
'LOCATE 4, 50: PRINT &quot;shot =&quot;; shot 'used for debugging, REMed out for now
'LOCATE 5, 50: PRINT &quot;xshot =&quot;; xshot
'LOCATE 6, 50: PRINT &quot;yshot =&quot;; yshot
'LOCATE 7, 50: PRINT &quot;y =&quot;; y
'LOCATE 8, 50: PRINT &quot;480 - y =&quot;; 480 - y

SUB Fire

IF xshot = 0 THEN
xshot = xboard + 10 + 320 'sets x pos of board to x pos of bullet (middle of board)
END IF


IF counter2 > 30 THEN
PALETTE 1, 0 * 256 + counter2 * 256 ^ 2 + 0 'dims board for &quot;reload&quot; effect hehe
counter2 = counter2 - 1
END IF


LINE (xshot - 5, 430 - yshot)-(xshot, 420 - yshot), 8
LINE (xshot, 420 - yshot)-(xshot + 5, 430 - yshot), 8
LINE (xshot + 5, 430 - yshot)-(xshot - 5, 430 - yshot), 8
PAINT (xshot, 422 - yshot), 15, 8

LINE (xshot - 5, 430 - yshot + speed)-(xshot, 420 - yshot + speed), 0
LINE (xshot, 420 - yshot + speed)-(xshot + 5, 430 - yshot + speed), 0
LINE (xshot + 5, 430 - yshot + speed)-(xshot - 5, 430 - yshot + speed), 0
PAINT (xshot, 422 - yshot + speed), 0, 0

yshot = yshot + speed 'does bullet animation

IF yshot + 40 < (480 - y) AND yshot + 50 > (480 - y) THEN
IF xshot > x - 10 AND xshot < x + 15 THEN 'my collision detection
collision = 1 'this was a pain!, checks if bullet hits missle
END IF
END IF
points = points + 2 'points increase as missle is higher
END SUB

SUB ShotHit


x1 = (RND * 15) - xy#
y1 = (RND * 15) - xy#
FOR r# = 1 TO 8 STEP .1
CIRCLE (x - 5 + x1, y - 5 + y1), r#, col
IF col = 0 THEN
col = 12
ELSEIF col = 12 THEN col = 14 'does explosion animation
ELSEIF col = 14 THEN col = 0
END IF
loops = loops + 1: xy# = xy# + .001
FOR t = 1 TO 1000: NEXT t
NEXT
IF loops > 1000 THEN collision = 0: loops = 0

ppoints = ppoints + ((r / 2) * points) / 2 'calculates points (needs some work)
END SUB

SUB Shotsetback
yshot = 0: shot = 0: xshot = 0: points = 0: collision = 0 'resets varibles
END SUB

SUB StarField STATIC

IF starcount < 100 THEN
xstar = INT(RND * 640)
ystar = INT(RND * 480) 'Draw Stars
chance = INT(RND * 20)
IF chance = 5 THEN
PSET (xstar, ystar), 15
starcount = starcount + 1
END IF
END IF

counter = counter + 1
IF counter <= 31 THEN
PALETTE 15, sc * 256 ^ 2 + sc * 256 + sc
sc = sc + 2
ELSEIF counter > 31 THEN
PALETTE 15, sc * 256 ^ 2 + sc * 256 + sc 'fades star palette
sc = sc - 2
END IF
IF counter = 62 THEN counter = 0: sc = 1

END SUB

 
in reference to speed check out this post.

thread314-20062 should provide some insight. After you read it, you may want to change the lines:

FOR tt = 1 TO 18
FOR t = 1 TO 20000 'THIS IS THE SUCKY PART,
NEXT t 'lol, this is my delay loop
NEXT tt


to incorporate one of the ideas suggested there.


As for the Mem OverFlow, I believe you're right. I haven't run your &quot;updated&quot; version yet, but just from reading the code you will note that in the &quot;ShotHit&quot; sub you're declaring XY as a double instead of integer. You'll probable need to fix it by either not declaring ALL (EVERY SINGLE VARIABLE) as an integer (removing DEFINT A-Z will do it) -or- defining the variables starting with say W through Z as Double (DEFDBL W-Z) and fix up DEFINT A-Z accordingly, -or- if you don't need XY# then remove the # to make it an integer again.

See ya, MiggyD &quot;It's mind over matter. They don't mind so you don't matter to them.&quot;
 
Rather than using a simple 'for x=1 to nnnn:next x', use the system clock for a time delay. (hint: TIMER). This should make your program processor-independent and compiler/interpreter-independent.

 
that's right philz, this type of thingy should do the job nicely:

tq = timer
while timer < tq + .05
wend

checkitout.
btw, 1 timer increment is == 1 second, so the above routine pauses for 1/20 sec.
unfortunately, the smallest increment that timer will count is 1/100 sec.
 
Actually, TIMER is linked to the Programmable Interrupt Timer that causes a count in memory to go up by 1 roughly 18.2 times per second. The PIT itself has an internal clock that ticks 1,193,181 times per second. In its default mode, each time it ticks, it first increments a 16-bit register, and then checks that register to see if it has become a certain trigger value, initially 0. If it has, then it resets the register to 0, and then raises a CPU interrupt that allows a service routine to increment that count in memory by 1. Now, since it increments the register before checking, that means that with the default reset value of 0, it &quot;misses&quot; the trigger right at the start, when the register is originally 0, and counts all the way up to the maximum unsigned 16-bit value, which is 65535. When you increment a binary number past its maximum value, it wraps back to 0, which would then match the trigger value. This means that only every 65536 ticks actually result in a CPU interrupt, and since there are 1,193,181 ticks every second, the value in TIMER changes (1,193,181 / 65536) ~= 18.2065 times every second. However, you can modify that trigger value to a higher value, at the expense of your computer's time going all wonky. When you reboot, the time should be set back to normal, since the time is loaded from the CMOS at startup, and the CMOS clock isn't affected by the PIT. Here is some code that will set TIMER to tick at a given frequency. In most cases, it won't be able to give you the exact frequency you request, and it can't give you less than 18.2065 or more than 1,193,181 updates/sec (but you probably don't want that anyway =}). This is because the trigger value on the chip is an integer, so you can only set it to integer quotients of 1,193,181. In general, the higher the frequency, the less accurately it will be able to reproduce it. Anyway, here is the code:
[tt]
SUB setuptimer(frequency#)
IF (frequency# < 18.2065) OR (frequency# > 1193181) THEN ERROR 5
tickspertick& = 1193181 / frequency#
OUT &H43, &H34
OUT &H40, tickspertick& AND 255
OUT &H40, tickspertick& \ 256
END SUB[/tt]
 
Wow, thanks alot man, very much appreciated. Lots more good info than I expected. =) But if you don't mind me asking, how do you know so much detail? I'm just curious, and a tad envious, heh. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top