Advanced Projects & Interactive Programs
Use arrays to store multiple objects and display them on the screen.
var x : array 1 .. 5 of int
var y : array 1 .. 5 of int
x(1) := 50; y(1) := 50
x(2) := 150; y(2) := 80
x(3) := 250; y(3) := 120
x(4) := 350; y(4) := 200
x(5) := 450; y(5) := 180
for i : 1 .. 5
drawfilloval(x(i), y(i), 20, 20, brightred)
end for
Animate objects by updating their positions in a loop.
var x := 50
loop
cls
drawfilloval(x, 200, 30, 30, blue)
x += 2
if x > maxx then
x := 0
end if
delay(10)
end loop
cls.
Check if two objects overlap.
var playerX := 200
var playerY := 50
var enemyX := 200
var enemyY := 200
if playerX >= enemyX - 20 and playerX <= enemyX + 20 and playerY >= enemyY - 20 and playerY <= enemyY + 20 then
put "Collision!"
end if
Combine what you've learned into a small game.
var playerX := 200
var bulletX := -1
var bulletY := -1
var chars : array char of boolean
loop
Input.KeyDown(chars)
if chars(KEY_LEFT_ARROW) then
playerX -= 4
end if
if chars(KEY_RIGHT_ARROW) then
playerX += 4
end if
if chars(' ') and bulletY = -1 then
bulletX := playerX + 20
bulletY := 60
end if
if bulletY not= -1 then
bulletY += 6
if bulletY > maxy then
bulletY := -1
end if
end if
cls
drawfillbox(playerX,50,playerX+40,80,green)
if bulletY not= -1 then
drawfilloval(bulletX, bulletY, 5, 5, yellow)
end if
delay(10)
end loop