Stars

 
 

program Stars;

uses Types, QuickDraw, Events, MiscTool;


type

  Star = Record

    x, y, speed: Integer;

  end;


var

  i: Integer;

  tick: LongInt;

  r: Rect;

  poly: Handle;

  stars: Array[0..19] of Star;


begin

  graphics(320);

  clearScreen(black);

  hideCursor;

  tick := getTick;


  { Draw spaceship }


  setSolidPenPat(7);

  setRect(r, 0, 93, 25, 97);

  paintOval(r);


  poly := openPoly;

  moveTo(10, 91);

  lineTo(30, 95);

  lineTo(10, 99);

  closePoly;

  setSolidPenPat(4);

  paintPoly(poly);


  setSolidPenPat(5);

  moveTo(8, 91);

  line(8, 0);

  moveTo(8, 98);

  line(8, 0);


  { Setup stars }


  for i := 0 to 19 do begin

    stars[i].x := random mod 320;

    stars[i].y := i * 10;

    stars[i].speed := random mod 6 + 1;

  end;


  repeat

    for i := 0 to 19 do begin


      { Erase stars }


      setSolidPenPat(black);

      moveTo(stars[i].x, stars[i].y);

      line(0, 0);


      { Move stars }


      stars[i].x := stars[i].x - stars[i].speed;

      if stars[i].x < 0 then begin

        stars[i].x := 319;

        stars[i].speed := random mod 6 + 1;

      end;


      { Draw stars }


      setSolidPenPat(i mod 15 + 1);

      moveTo(stars[i].x, stars[i].y);

      line(0, 0);

    end;


    { Draw fire }


    if random > 0 then setSolidPenPat(9)

    else setSolidPenPat(7);


    moveTo(5, 94);

    line(4, 0);


    repeat until getTick > tick;

    tick := tick + 1;

  until button(0);

end.

Spaceship flying in star field.


This program teaches the use of array and record to create multiple objects. It uses array to generate many stars and animate them all at once at different speed. It also teaches how to use polygon to draw a shape.


You can see the alternate Orca/Pascal and GSoft BASIC versions here. Orca/Pascal version is written using Object Pascal, an object oriented extension of the Pascal language.