โ† Back to Part 4 โ€“ Arrays & Core Concepts

Turing Programming โ€“ Part 5

Advanced Projects & Interactive Programs

1. Combining Arrays with Graphics

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
๐Ÿ’ก Arrays help you manage multiple objects efficiently.

2. Moving Objects

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
โœ… Smooth animation requires clearing the screen each frame using cls.

3. Simple Collision Detection

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
โš ๏ธ This is the foundation of interactive games.

4. Mini Shooting Game Example

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
๐Ÿš€ Students can add enemies, scoring, and multiple bullets as challenges.

5. Project Ideas & Next Steps