Graphics, Animation, and Your First Game
Graphics transform programming from text into something visual and interactive. Many real-world applications — games, simulations, engineering tools — depend on graphical programming.
Turing includes powerful built-in drawing tools.
drawbox(100,100,300,300,blue) drawoval(400,250,50,80,red) drawfillbox(320,100,380,160,green)✔ drawbox → rectangle ✔ drawoval → ellipse ✔ drawfillbox → solid rectangle
You can use loops to create graph-like visuals. Example: plotting a simple mathematical pattern.
for x : 0 .. 400
var y := x div 2
drawdot(x, y, brightblue)
end for
This produces a straight-line graph!
Animation works by repeatedly drawing an object in a new position.
var x := 0
loop
cls
drawfilloval(x,200,20,20,red)
x += 2
delay(10)
end loop
The circle moves across the screen!
Let the player control an object.
var x := 200
var chars : array char of boolean
loop
Input.KeyDown(chars)
if chars(KEY_LEFT_ARROW) then
x -= 3
end if
if chars(KEY_RIGHT_ARROW) then
x += 3
end if
cls
drawfillbox(x,50,x+60,80,cyan)
delay(10)
end loop
Now the object moves with arrow keys!
Let’s combine everything into a mini game. The player moves left/right and shoots upward.
var playerX := 200
var bulletX := -1
var bulletY := -1
var chars : array char of boolean
loop
Input.KeyDown(chars)
% Move player
if chars(KEY_LEFT_ARROW) then
playerX -= 4
end if
if chars(KEY_RIGHT_ARROW) then
playerX += 4
end if
% Shoot bullet
if chars(' ') and bulletY = -1 then
bulletX := playerX + 20
bulletY := 80
end if
% Move bullet
if bulletY not= -1 then
bulletY += 6
if bulletY > maxy then
bulletY := -1
end if
end if
cls
% Draw player
drawfillbox(playerX,50,playerX+40,80,green)
% Draw bullet
if bulletY not= -1 then
drawfilloval(bulletX, bulletY, 5, 5, yellow)
end if
delay(10)
end loop