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

What are some useful graphic techniques in QB?

Graphical Techniques in QB

What are some useful graphic techniques in QB?

by  EvilWorkZsoft  Posted    (Edited  )
Ahhh yes, Graphics, essential in most QBasic games. Now, QB for some reason just isn't too good at handleing graphics, Leaveing you mostly with slow drawing functions, circles,lines and points. Good thing there are ways around this.

1> Sprite can make you a better programmer...
Sprite graphics load up much faster then simple point to point line drawing, and is much certainly faster then Draw strings (Which I was never fond of in the first place). Sprite style graphics also tend to be much more detailed then anything else you may conjure up. The thing is, drawing a sprite graphic with dots at a usefull speed. Fortunatly for us, QB uses arrays to store many values to one variable.

Example:
' Set screen mode
SCREEN 7
' Ok, First, we'll draw a circle...
Circle (5, 5),5
' Next we set our array up by dimming lotsa numbers to one
' variable...
DIM test (10 * 10) 'or 10 by 10 graphic
' then we put the graphic into the array we just made
GET (0,0)-(10, 10), test
' ok, its that simple, TEST is now a sprite and we can put
' it anywhere.
put (100, 100),test
END EXAMPLE

Note that the graphic will not be centered on 100, 100 coordinates... instead, it uses the upper left corner (where it started reading the varable) as it's 'x,y'

that about does it for sprite graphics...

2> pixel by pixel rendering
Next process in makeing a decent sprite is to render it pixel by pixel, this is done by reading data for each color of each point. It CAN be read from a seperate file, but for genral pourpuses of this FAQ, I'm useing data statements. Also note, I put my data statements at the end of my program, I don't know why, Perhaps its a habit...

EXAMPLE:
' assumeing you're programming on the end of that last part
cls

' 10 by 10 sprite
for y = 1 to 10
for x = 1 to 10
read a 'Reads numarical data
pset (x, y) , a 'paints a pixel the color designated
next
next
dim test1 (10 * 10) 'Dim it!
get (0,0)-(10, 10), test1 'Get it!
' you now have a crasshair
put (100, 100) test1
' and a circle
put (110, 100) test

' Your data staements, Each number refers to a color value
' This perticular string draws a nifty crosshair
data 4,0,0,2,2,2,2,0,0,4
data 0,0,0,0,2,2,0,0,0,0
data 0,0,0,10,2,2,10,0,0,0
data 2,0,10,2,0,0,2,10,0,2
data 2,2,2,0,0,0,0,2,2,2
data 2,2,2,0,0,0,0,2,2,2
data 2,0,10,2,0,0,2,10,0,2
data 0,0,0,10,2,2,10,0,0,0
data 0,0,0,0,2,2,0,0,0,0
data 4,0,0,2,2,2,2,0,0,4

END EXAMPLE

That's pretty cool huh? simple to use too.

3> Mmmmmmm.... page flipping...
Tired of flicker? wish you could see you're graphics all the time? Wish no longer, page flipping creates a smooth and fast way to compleatly eliminate flicker from redraw.
the example below assumes you already have both above graphics dim'ed up to memory.

EXAMPLE:
'first, we enter a new screen mode...
SCREEN 7,0,1,0
' this sets up the following:
' Screen 7 (320/200 pixels)
' Visible page is 0
' write page is 1, and remains unseeable
' copy to page is 0

' Now, we flip
' begin by starting a loop, this one will end at a keypress
do
' just a little geometry handeler...
if xm = 0 the x = x + 1, if x > 300 then xm = 1
if xm = 1 the x = x - 1, if x > 2 then xm = 0
if ym = 0 the y = y + 1, if y > 300 then ym = 1
if ym = 1 the y = y - 1, if y > 2 then ym = 0
' put you're graphic somewhere... preferable x and y
put (x, y), test1
' and here's the madd page flip:
pcopy 1, 0
' We just copied the contents of screen 1 to screen 0
' Clear the invisible screen..
cls 1
' loop that motha...
loop while inkey$ = ""

END OF EXAMPLE

Its that simple.

4> Zooming (oooooo... yeah)
This is a new technique i came up with, I whipped it up in about 10 minutes, and is direct scorce of how to zoom a sprite, at the time of this post, it had been run once. This code has no relation to the abouve code...

EXAMPLE:

warning: This code has been modified from it's original version, it has bneen formatted to fit this box... Also, note that I will just give you the routine, I'm not gonna show you how to use it effectivly. At this point that's for you to figure out. Call it a challenge...Here it is:

SCREEN 7
CIRCLE (5, 5), 5

FOR a = 2 TO 10
FOR d = 0 TO 10
FOR dd = 0 TO 10
FOR dx = 0 TO a
FOR dy = 0 TO a
PSET ((10 + (a * 10)) + ((dd * a) + dx) --cont. next line--
, (d * a) + dy), POINT(dd, d)
NEXT
NEXT
NEXT
NEXT
NEXT

END OF EXAMPLE

Have fun with that, I know I will.

5> Conclusion
Well, That concludes my graphics fAQ... I hope to have inspired at least one person.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top