Graphics Fundamentals
SCREEN n switches between text and graphics modes.
22.1 SCREEN modes
SCREEN n switches between text and graphics modes. The classic QBasic modes (all supported by QB64-PE):
VGA — popular for 16-color work VGA — popular for games (chunky pixels)
QB64 adds arbitrary sizes and 32-bit color with SCREEN _NEWIMAGE(width, height, 32) (Chapter 23). Coordinates start at (0, 0) in the top-left corner; y grows downward.
22.2 PSET, PRESET, POINT
STEP (dx, dy) makes a coordinate relative to the last point drawn: PSET STEP(5, 0).
SCREEN 12
PSET (320, 240), 15 ' white pixel in the center
PRESET (321, 240) ' draws in the background color
FOR i = 1 TO 2000
PSET (RND * 640, RND * 480), INT(RND * 16)
NEXT
c = POINT(320, 240) ' read a pixel's color
PRINT "Center color:"; c
SLEEP22.3 LINE — lines, boxes, filled boxes
SCREEN 12
LINE (10, 10)-(630, 470), 14 ' diagonal line, yellow
LINE (100, 100)-(300, 200), 4, B ' rectangle outline (Box)
LINE (120, 120)-(280, 180), 2, BF ' filled rectangle (Box Filled)
LINE (400, 100)-(600, 100), 15, , &HF0F0 ' dashed line (16-bit style pattern)
LINE -(500, 300), 11 ' from the last point to (500, 300)
SLEEP22.4 CIRCLE — circles, ellipses, arcs
Syntax: CIRCLE (x, y), radius, color, startAngle, endAngle, aspect.
SCREEN 12
CIRCLE (320, 240), 100, 15 ' circle, radius 100
CIRCLE (320, 240), 150, 9, , , 0.5 ' ellipse: aspect 0.5 (squashed)
CIRCLE (320, 240), 80, 12, 0, 3.14159 ' upper half arc (angles in radians)
CIRCLE (100, 100), 40, 10, -0.5, -5.78 ' negative angles draw radius lines: a pie slice (Pac-Man)
SLEEP22.5 PAINT — flood fill
PAINT (x, y), fillColor, borderColor — fills outward until it reaches borderColor. If the shape has a gap, paint leaks out to the whole screen. QB64 can also paint with a tile pattern string in palette modes.
SCREEN 12
CIRCLE (320, 240), 100, 15
PAINT (320, 240), 1, 15 ' fill with blue (1) up to the white (15) border
LINE (50, 50)-(150, 150), 4, B
PAINT (100, 100), 12, 4
SLEEP22.6 DRAW — a turtle-graphics mini-language
U n, D n, L n, R n up, down, left, right E n, F n, G n, H n diagonals: up-right, down-right, down-left, up-left M x,y / M+x,+y move to absolute / relative position move without drawing draw, then return to the starting point scale (4 = 1:1) rotate 0–3 (× 90°) turn angle in degrees P fill,border paint at the current position X + VARPTR$(s$) execute a substring (legacy)
SCREEN 12
DRAW "BM320,240" ' Blank Move (no drawing) to the center
DRAW "C14 U50 R50 D50 L50" ' yellow square: Up, Right, Down, Left
DRAW "BM100,400 C10 E40 F40 L80" ' triangle using diagonals
DRAW "BM500,300 C12"
FOR i = 1 TO 5
DRAW "R60 TA" + STR$((i * 144) MOD 360) ' star: turn angle must stay within -360..360
NEXT
DRAW "BM450,100 C11 S8 R10 D10 L10 U10" ' S8 = scale factor (S4 is normal size)
SLEEP22.7 Logical coordinates: WINDOW and VIEW
VIEW (x1, y1)-(x2, y2) restricts drawing to a rectangular viewport; PMAP converts between logical and physical coordinates.
Classic BASIC: GW-BASIC had the same SCREEN, PSET, LINE, CIRCLE, PAINT, DRAW. Applesoft used HGR, HCOLOR=, HPLOT x,y TO x2,y2; ZX Spectrum PLOT, DRAW dx,dy, CIRCLE; BBC BASIC MODE, MOVE, DRAW, PLOT, GCOL; Commodore 64 BASIC had no graphics commands (POKE into VIC-II registers, or extensions such as Simons' BASIC). FreeBASIC: Screen 12 / ScreenRes 800, 600, 32, PSet, Line, Circle, Paint, Draw — nearly identical, plus ScreenLock/ScreenUnlock and RGB(r, g, b). VB.NET: Windows Forms uses System.Drawing: in a Paint event, e.Graphics.DrawLine(Pens.Red, x1, y1, x2, y2), FillEllipse(Brushes.Blue, x, y, w, h).
SCREEN 12
WINDOW (-10, -2)-(10, 2) ' math coordinates: y up, x from -10 to 10
LINE (-10, 0)-(10, 0), 8 ' x axis
LINE (0, -2)-(0, 2), 8 ' y axis
PSET (-10, SIN(-10)), 14
FOR x = -10 TO 10 STEP 0.02
LINE -(x, SIN(x) * 1.5), 14 ' plot y = 1.5 sin x
NEXT
SLEEP