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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to slow down a pixel

Status
Not open for further replies.

wehttam2007

Programmer
Feb 19, 2003
5
US
i made a program to make a pixel move around the screen using a do loop loop like this:

screen 13
do
randomize timer
x = int(rnd * 320)
y = int(rnd * 200)
pset (x,y)
a = int(rnd * 4)
if a = 1 then x = x + 1
if a = 2 then x = x - 1
if a = 3 then y = y + 1
if a = 4 then y = y - 1
cls
loop

that works to make a pixel move around the screen but way way too fast. how can i slow it down?
 
Put a delay.

A simple example would be.
FOR i% = 1 to 30000:next

The 30000 would vary from computer to computer. But this is a crude one. Instead try developing a more sophisticated using the TIMER function.
 
you could make a loop like this:

for n = 0 to 300
for n = 0 to 300

next
next

this works better than that other one. for fast computers, you don't need a double integer. later i might show how to make an efficient loop that will slow itself down or speed itself up.
 
Or you could decrease the interval you move at, instead of your code you could use

screen 13
do
randomize timer
x = int(rnd * 320)
y = int(rnd * 200)
pset (x,y)
a = int(rnd * 4)
if a = 1 then x = x + .1
if a = 2 then x = x - .1
if a = 3 then y = y + .1
if a = 4 then y = y - .1
cls
loop
 
i don't see how that would work. as far as i can see, i don't think you'd need a. why increase x or y by one when you give x and y completely new coordinates each time? here's how it would work. you give x and y completely random coordinates. you pset the pixel, then you add one or decrease one from the original coordinates, then you give x and y completely new coordinates again! and shanely, that wouldn't work. even if you do fix the coordinate problem, what do you think would happen if the computer tried to pset .5 and .8? i've tooken the liberty to completely re-write the code so it's slower and it'd actually work.


screen 13

randomize timer 'you don't need this in the loop
x = int(rnd * 320) 'give x and y random coordinates
y = int(rnd * 200)
do 'start the loop

if x mod int(x) = 0 and y mod int(y) = 0 then 'if the remainder
'of x and y divided by themselves rounded down is zero...
pset (x,y), 5 'pset the color. you missed the color
a = int(rnd * 4)
if a = 1 then x = x + .1
if a = 2 then x = x - .1
if a = 3 then y = y + .1
if a = 4 then y = y - .1
cls
loop
 
You want to slow it down right? So use a timer loop:

a = TIMER
DO
LOOP WHILE TIMER < a + .5
 
that'd make the program too slow. half a second is very slow. try .09.
 
or you could use...

D = 1 / 30 'Delay... 30 fps... 1 second / 30 frames
...
Begin_Loop
T = Timer
DO: Until Abs(Timer-T) > D
End_Loop

Since TIMER returns the seconds past since midnight...
if you are the a midnight gamer, and happen to be the unlucky person playing as the clock ticks from 11:59.99 to 12:00.00, the TIMER < a + .5 code could freeze up...

there are 86,400 seconds in a 24hr day...
if A (or T) is read at 86,399.99, and the TIMER returns to 0 at midnight, Timer will never be greater than 86,400, much less 86,400.49

where as with Abs(Timer-T) > D...
if Delay is .03...
if T is read at 86,399.99 and the TIMER loops around to 0 ... then Abs(Timer-T) would = Abs(-86,399.99) or 86,399.99, which is greater than .03
This might cause a little jump, but will be better than freezeing up...

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
um.. the t should be t! otherwise it'll round up or down.

t would be the same as timer since it's in the loop.
 
actually it should be t# (double)...
since timer is a double value...

qbasic defaults to t! (single)...

unless you use...
DEFINT A-Z 'default integer%
DEFLNG A-Z 'default long&
DEFDBL A-Z 'default double#
or
DEFSTR A-Z 'default string$

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
ah... now i understand what that means lol. i always wanted to know what defint meant.

here's some code i played around with. i really wants to go up.

SCREEN 13
DIM SHARED x AS SINGLE
DIM SHARED y AS SINGLE


RANDOMIZE TIMER 'you don't need this in the loop
x = INT(RND * 320) 'give x and y random coordinates
y = INT(RND * 200)
DO 'start the loop

IF x MOD INT(x) = 0 AND y MOD INT(y) = 0 THEN 'if the remainder
'of x and y divided by themselves rounded down is zero...

PSET (xx, yy), 0
PSET (x, y), 5 'pset the color. you missed the color

WAIT &H3DA, 8
WAIT &H3DA, 8, 8
xx = x
yy = y

FOR f = 0 TO 100
FOR t = 0 TO 100
NEXT t
NEXT f
END IF

RANDOMIZE TIMER
a = INT(RND * 4)
IF a = 1 THEN x = x + 1
IF a = 2 THEN x = x - 1
IF a = 3 THEN y = y - 1
IF a = 4 THEN y = y + 1
IF x > 320 THEN x = 319
IF x < 0 THEN x = 0
IF y > 200 THEN y = 199
IF y < 0 THEN y = 5


LOOP UNTIL INKEY$ = CHR$(27)


works like a charm. except it has a tendency to go up. I put in wait&h3da because it does a good job of slowing down the program :p
 
isn't WAIT &H3DA... wait for vertical retrace...

That should also work for removing glitches...

It's been a few years since I used it...

Another fun thing to do is use SIN & COS with angles to make a pixel move around the screen...
Make the change in angle random with a range of 3 to 7 degrees or so...
Code:
SCREEN 13
X = 160
Y = 100
A = 0
S = 2
RAD = 3.1415 / 180
RANDOMIZE TIMER
PSET (X,Y)
DO
  OX = X
  OY = Y
  DX = COS(A * RAD) * S
  DY = SIN(A * RAD) * S
  X = X + DX
  Y = Y + DY
  IF X > 319 THEN A = (A + 90) MOD 360
  IF X < 0 THEN A = (A + 90) MOD 360
  IF Y > 199 THEN A = (A + 90) MOD 360
  IF y < 0 THEN A = (A + 90) MOD 360
  A = (A + (INT(RND * 10) - 5) + 360) MOD 360
  PSET (OX,OY) ,0
  PSET (X,Y)
LOOP UNTIL INKEY$<>&quot;&quot;
try that out...
it 'should' work, but I can't test it because I don't have QB @ work...

I'll test it when I get home and post any nessisary changes...

Have fun!

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
yeah. that'd work, but unfortunately, we haven't taken much of sin, cos and tan in class, so i don't really understand it that much :(

here's some code i made on the fly for a random bouncing ball.

SCREEN 13
DEF SEG = &HA000
DEFINT A-Z
x = 100
y = 100

direct1$ = &quot;s&quot;
direct2$ = &quot;w&quot;
xx = x
yy = y

DO


CIRCLE (xx, yy), 10, 0
IF direct1$ = &quot;s&quot; AND direct2$ = &quot;e&quot; THEN
PAINT (xx + 3, yy + 3), 0
ELSEIF direct1$ = &quot;s&quot; AND direct2$ = &quot;w&quot; THEN
PAINT (xx - 3, yy + 3), 0
ELSEIF direct1$ = &quot;n&quot; AND direct2$ = &quot;e&quot; THEN
PAINT (xx + 3, yy - 3), 0
ELSEIF direct1$ = &quot;n&quot; AND direct2$ = &quot;w&quot; THEN
PAINT (xx - 3, yy - 3), 0
END IF
CIRCLE (x, y), 10, 15
IF direct1$ = &quot;n&quot; AND direct2$ = &quot;w&quot; THEN
PAINT (x, y), 4, 15
ELSEIF direct1$ = &quot;n&quot; AND direct2$ = &quot;e&quot; THEN
PAINT (x, y), 4, 15
ELSEIF direct1$ = &quot;s&quot; AND direct2$ = &quot;w&quot; THEN
PAINT (x, y), 4, 15
ELSEIF direct1$ = &quot;s&quot; AND direct2$ = &quot;e&quot; THEN
PAINT (x, y), 4, 15
END IF
WAIT &H3DA, 8

xx = x
yy = y
IF direct1$ = &quot;n&quot; AND direct2$ = &quot;e&quot; THEN
x = x + 1
y = y - 1
ELSEIF direct1$ = &quot;n&quot; AND direct2$ = &quot;w&quot; THEN
y = y - 1
x = x - 1
ELSEIF direct1$ = &quot;s&quot; AND direct2$ = &quot;w&quot; THEN
y = y + 1
x = x - 1
ELSEIF direct1$ = &quot;s&quot; AND direct2$ = &quot;e&quot; THEN
y = y + 1
x = x + 1
END IF

IF x = 309 AND direct2$ = &quot;e&quot; THEN
direct2$ = &quot;w&quot;
ELSEIF x = 9 AND direct2$ = &quot;w&quot; THEN
direct2$ = &quot;e&quot;
ELSEIF y = 9 AND direct1$ = &quot;n&quot; THEN
direct1$ = &quot;s&quot;
ELSEIF y = 189 AND direct1$ = &quot;s&quot; THEN
direct1$ = &quot;n&quot;
END IF



LOOP UNTIL INKEY$ <> &quot;&quot;

 
SIN and COS are Easy too use...

Look at this...
mysincos.JPG

That usually helps make it a little more visual...

The 2 Y equations for ClockWise (CW) and CounterClockWise (CCW) rotation are because in 'normal' math, the 1st Quadrant is used so a positive angle goes CCW
Code:
     |
  II |  I
-----+-----
 III | IV
     |
but the computer screen is in quad 4 with an inverted Y axis... Where -Y is now +Y

So it looks like this...
Code:
     |
 III | IV
-----+-----
  II |  I
     |
...So if you want rotation to work like in standard math... just use -Y

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
wait... i'm remembering something from school... yes!

SOHCAHTOA

SinOppositeHypotenuse
CosineAdjacentHypotenuse
TanOppositeAdjacent
 
Taylor series approximations of sin and cos are

sin x=x-x^3/3!+x^5/5!-x^7/7!...

cos x=1-x^2/2!+x^4/4!-x^5/5!...
 
Also you could just pset((r*cos(d))+x,(r*sin(d))+y) where r is the radius of the cirle, x and y are the x and y coordinates on the screen, and d is the degree to rotate the point around (x,y)
 
Yes... BUT...
Degree Needs to be a RADIAN (where 360 degrees = 2(pi) radians...
Hence the RAD = (3.1415 / 180)...
360 / 180 = 2
2 * 3.1415 = 2(pi)
so...
(degree) * (3.1415) = (radian)

depending on how acurate you want to be...
RAD = 0.017453292519943295769236907684886

And a look up table is MUCH faster than the real time math
required in...
CONST RAD = 0.017453
...
D = Angle * RAD
PSET (X + (cos(D) * R), Y + (sin(D) * R))

compared to...
CONST RAD = 0.017453
Dim SinT(360) as single, CosT(360) as single
For I = 0 to 360
SinT(I) = Sin(I * Rad)
SinT(I) = Sin(I * Rad)
Next
...
PSET (X + (CosT(Angle) * R), Y + (SinT(Angle) * R))

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
bah, pset is slow. just point the defseg to the graphics buffer (&hA000) and poke it. alot faster.

poke ((r*sin(d))+y) * 320&) + (((r*cos(d))+x), color

i think that'd work... not sure.

geez, my interests change fast. now i'm into fading effects, melting screen and that kind of stuff. but sohcahtoa's still interesting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top