Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Dino game in Turbo Paskal

Name: Anonymous 2012-10-02 10:45

http://www.youtube.com/watch?v=tYwHQpvMZTE

Noice. This gave me a retro feel.

Name: Anonymous 2012-10-02 13:14

Sure, it's in Pascal, but is it in Abelson Pascal?

Name: Anonymous 2012-10-02 14:10

Converted to Sepples.

/* Dino - a text-based game where you try to avoid a dinosaur in a maze
         while looking for some treasure and the exit.
  by Kostas Michalopoulos. */


const integer mazesize = 10;

typedef sring anystring;

enum roomcelltype {emptyroom, treasureroom, lavaroom, waterroom, blockedroom, last_roomcelltype};

struct roomrecord {
  roomcelltype roomtype;
  roomcelltype underground;
  boolean dug, visited;
};

matrix<1,mazesize, 1,mazesize,roomrecord> rooms;
byte dinox, dinoy, playerx, playery, mapx, mapy, exitx, exity;
boolean running;
integer score;
boolean dead;
boolean hasmap;
boolean exitfound;

void savegame()
{
  untyped_file f;

  assign(f, "DINO.SAV");
  /*$I-*/
  rewrite(f, 1);
  /*$I+*/
  if (ioresult != 0)  {
    output << "Failed to create the DINO.SAV file." << NL;
    return;
  }
  blockwrite(f, rooms, sizeof(rooms));
  blockwrite(f, dinox, 1);
  blockwrite(f, dinoy, 1);
  blockwrite(f, mapx, 1);
  blockwrite(f, mapy, 1);
  blockwrite(f, playerx, 1);
  blockwrite(f, playery, 1);
  blockwrite(f, exitx, 1);
  blockwrite(f, exity, 1);
  blockwrite(f, score, 2);
  blockwrite(f, hasmap, 1);
  blockwrite(f, exitfound, 1);
  close(f);
  output << "The game has been saved." << NL;
}

void restoregame()
{
  untyped_file f;

  assign(f, "DINO.SAV");
  /*$I-*/
  reset(f, 1);
  /*$I+*/
  if (ioresult != 0)  {
    output << "Failed to open DINO.SAV for reading." << NL;
    return;
  }
  blockread(f, rooms, sizeof(rooms));
  blockread(f, dinox, 1);
  blockread(f, dinoy, 1);
  blockread(f, mapx, 1);
  blockread(f, mapy, 1);
  blockread(f, playerx, 1);
  blockread(f, playery, 1);
  blockread(f, exitx, 1);
  blockread(f, exity, 1);
  blockread(f, score, 2);
  blockread(f, hasmap, 1);
  blockread(f, exitfound, 1);
  close(f);
  dead=false;
  output << "The game has been restored." << NL;
}

void addscore(integer newscore)
{
  if (newscore > 0)
    output << "You gained " << newscore << " extra treasure.  ";
  else if (score > 0)
    output << "You lost " << -newscore << " treasure.  ";
  score=score + newscore;
  if (score < 0)  score=0;
  output << "Your treasure now is " << score << '.' << NL;
}

void die()
{
  output << "You are now dead." << NL;
  dead=true;
}

void showhelp()
{
  output << "You can use the following commands:" << NL;
  output << NL;
  output << "  help       this screen" << NL;
  output << "  listen     listen for sounds from nearby rooms" << NL;
  output << "  dig        dig the ground" << NL;
  output << "  north      move north" << NL;
  output << "  south      move south" << NL;
  output << "  east       move east" << NL;
  output << "  west       move west" << NL;
  output << "  wait       wait a bit" << NL;
  output << "  map        check your map (if you have it)" << NL;
  output << "  treasure   show your current treasure" << NL;
  output << "  leave      leave the maze (only if you find the exit)" << NL;
  output << "  save       save the game to disk" << NL;
  output << "  restore    restore the game from disk" << NL;
  output << "  restart    restart the game in a new maze" << NL;
  output << "  exit       exit from the game" << NL;
  output << NL;
}

Name: Anonymous 2012-10-02 14:11


void listenfordino()
{
  integer dist;

  dist=trunc(sqrt((playerx - dinox)*(playerx - dinox) + (playery - dinoy)*(playery - dinoy)));
  if (dist < 1)  output << "There is a weird sensation of something heavy breathing over your neck." << NL;
  else if (dist < 2)  output << "There is a LOUD and HEAVY sound of steps nearby!" << NL;
  else if (dist < 3)  output << "There is a heavy sound of steps on dirt." << NL;
  else if (dist < 4)  output << "There is a distant sound of heavy steps somewhere." << NL;
}

void movedino()
{
  byte n, newx, newy;

  newx=dinox;
  newy=dinoy;
  n=Random(100);
  if (n < 25)  newx=dinox - 1; else
  if (n < 50)  newx=dinox + 1; else
  if (n < 75)  newy=dinoy - 1; else
                 newy=dinoy + 1;
  if ((newx > 0) && (newy > 0) &&
     (newx <= mazesize) && (newy <= mazesize) &&
     (rooms[newx][newy].roomtype != blockedroom))  {
    dinox=newx;
    dinoy=newy;
  }
}

void movetoroom(byte roomx, byte roomy)
{
  if ((roomx < 1) || (roomy < 1) || (roomx > mazesize) || (roomy > mazesize))  return;
  if ((roomx != playerx) || (roomy != playery))  {
    switch (rooms[roomx][roomy].roomtype) {
      case treasureroom: {
        output << "You have found some treasure." << NL;
        addscore(10 + Random(10));
        rooms[roomx][roomy].roomtype=emptyroom;
      }
      break;
      case lavaroom: {
        output << "You fell in a lava pit." << NL;
        die();
      }
      break;
      case waterroom: if (score > 0)  {
        output << "You fell in a water pit and lost some treasure." << NL;
        addscore(-(1 + Random(2)));
      } else {
        output << "You fell in a water pit.  Thankfully you have no treasure with you" << NL;
        output << "otherwise it would be lost." << NL;
      }
      break;
      case blockedroom: {
        output << "You cannot move there" << NL;
        roomx=playerx;
        roomy=playery;
      }
      break;
    }
  }
  playerx=roomx;
  playery=roomy;
  rooms[playerx][playery].visited=true;

  if ((mapx==playerx) && (mapy==playery) && (! dead) && (! hasmap))  {
    output << "You have found a map!" << NL;
    hasmap=true;
  }

  if ((exitx==playerx) && (exity==playery) && (! dead) && (! exitfound))  {
    output << "You have found a tunnel that seems to be the exit!" << NL;
    output << "Now you can leave from here at any point, or continue exploring" << NL;
    output << "these cavelike rooms." << NL;
    exitfound=true;
  }

  listenfordino();
  if ((playerx==dinox) && (playery==dinoy))  {
    output << "You have finally met the dinosaur that inhabits these rooms." << NL;
    output << "The creature was not pleased by your visit here and decided" << NL;
    output << "to make his dislike for your presense known by breaking your" << NL;
    output << "skull in half." << NL;
    die();
  }
}

void listenaround();


static void listenin(byte x, byte y, boolean& lavasound, boolean& watersound)
{
  if ((x < 1) || (y < 1) || (x > mazesize) || (y > mazesize))  return;
  if ((rooms[x][y].roomtype==lavaroom) || (rooms[x][y].underground==lavaroom))  lavasound=true;
  if (rooms[x][y].roomtype==waterroom)  watersound=true;
}

void listenaround()
{
  boolean lavasound, watersound;


  lavasound=false;
  watersound=false;
  listenin(playerx, playery, lavasound, watersound);
  listenin(playerx - 1, playery, lavasound, watersound);
  listenin(playerx + 1, playery, lavasound, watersound);
  listenin(playerx, playery - 1, lavasound, watersound);
  listenin(playerx, playery + 1, lavasound, watersound);
  if (lavasound)  output << "You can hear the cracking of fire";
  if (watersound)
    if (lavasound)
      output << " and water running somewhere else." << NL;
    else
      output << "You can hear water running." << NL;
  else output << NL;
  listenfordino();
}

void digabit()
{
  if (rooms[playerx][playery].dug)  {
    output << "You already dug here." << NL;
  } else {
    switch (rooms[playerx][playery].underground) {
      case treasureroom: {
        output << "You have found some underground treasure!" << NL;
        addscore(20 + Random(35));
        rooms[playerx][playery].underground=emptyroom;
      }
      break;
      case lavaroom: {
        output << "You just dug to your grave.  A lava pit was in a cave below." << NL;
        die();
      }
      break;
      default: output << "There is nothing to be found here." << NL;
    }
    rooms[playerx][playery].dug=true;
  }
}

void showmap()
{
  byte x, y;

  output << "Your map:" << NL;
  output << "=========" << NL;
  for( y=1; y <= mazesize; y ++) {
    output << "  |";
    for( x=1; x <= mazesize; x ++) {
      roomrecord& with = rooms[x][y]; 
      if ((x==playerx) && (y==playery))
        output << " X |";
      else if (hasmap || with.visited)  {
        if (with.dug)  output << '>'; else output << ' ';
        switch (with.roomtype) {
          case emptyroom: if (exitfound && (exitx==x) && (exity==y))  output << 'E'; else output << '.'; break;
          case treasureroom: output << '$'; break;
          case lavaroom: output << 'v'; break;
          case waterroom: output << '='; break;
          case blockedroom: output << '#'; break;
        }
        if (with.dug)  output << "<|"; else output << " |";
      } else {
        output << " ? |";
      }
                                   }
    output << NL;
  }
  output << "=========" << NL;
  output << "Legend: X You, . Empty, $ Treasure, v Lava, = Water, # Blocked";
  if (exitfound)  output << ", E Exit";
  output << NL;
}

Name: Anonymous 2012-10-02 14:11


void preparemaze()
{
  byte x, y;

  for( y=1; y <= mazesize; y ++)
    for( x=1; x <= mazesize; x ++)
      {
        roomrecord& with = rooms[x][y]; 
        with.roomtype=emptyroom;
        if (Random(200) > 190)  with.roomtype=blockedroom;
        if (Random(200) > 160)  with.roomtype=treasureroom;
        if (Random(200) > 180)  with.roomtype=lavaroom;
        if (Random(200) > 170)  with.roomtype=waterroom;
        with.underground=emptyroom;
        if (Random(100) > 60)  with.underground=treasureroom;
        if (Random(100) > 70)  with.underground=lavaroom;
        with.visited=false;
        with.dug=false;
      }
}

void startgame()
{
  textcolor(7);
  textbackground(0);
  clrscr;
  output << "Dino, the game.  By Kostas Michalopoulos." << NL;
  showhelp();
  output << NL;

  preparemaze();

  dinox=Random(mazesize) + 1;
  dinoy=Random(mazesize) + 1;
  playerx=mazesize + 1 - dinox;
  playery=mazesize + 1 - dinoy;
  rooms[playerx][playery].roomtype=emptyroom;
  do {
    mapx=Random(mazesize) + 1;
    mapy=Random(mazesize) + 1;
  } while (!((playerx != mapx) && (playery != mapy) && (rooms[mapx][mapy].roomtype==emptyroom)));
  do {
    exitx=Random(mazesize) + 1;
    exity=Random(mazesize) + 1;
  } while (!((rooms[exitx][exity].roomtype==emptyroom) &&
        (exitx != playerx) && (exity != playery) &&
        (sqrt((exitx - playerx)*(exitx - playerx) + (exity - playery)*(exity - playery)) > 5)));
  dead=false;
  exitfound=false;
  hasmap=false;
  score=0;
  movetoroom(playerx, playery);
}

void parsecommand(anystring cmd);


static void skipspaces(byte& head, byte& len, anystring& cmd)
{
  while ((head <= len) && (set::of('\11', '\40', eos).has(cmd[head])))  head=head + 1;
}



static void nexttoken(byte& head, byte& len, anystring& cmd, anystring& token, byte& tokenlen)
{
  head=1;
  len=length(cmd);
  skipspaces(head, len, cmd);
  token="";
  tokenlen=0;
  while ((head <= len) && (! (set::of('\11', '\40', eos).has(cmd[head]))))  {
    if (tokenlen < 255)  {
      token=token + cmd[head];
      tokenlen=tokenlen + 1;
    }
    head=head + 1;
  }
}



static void lowerize(anystring& token)
{
  byte i;

  for( i=1; i <= length(token); i ++)
    if (set::of(range('A','Z'), eos).has(token[i]))
      token[i]=chr(ord(token[i]) - ord('A') + ord('z'));
}

void parsecommand(anystring cmd)
{
  byte i, head, len, tokenlen;
  anystring token;


  nexttoken(head, len, cmd, token, tokenlen);
  lowerize(token);
  if (token=="help")  {
    showhelp();
    if (dead)  output << "By the way, you are dead!" << NL;
  } else if (token=="exit")  {
    running=false;
  } else if (token=="dig")  {
    if (dead)
      output << "You are too dead to dig here..." << NL;
    else {
      digabit();
      movedino();
      listenfordino();
    }
  } else if (token=="listen")  {
    if (dead)
      output << "Dead people cannot listen" << NL;
    else
      listenaround();
  } else if (token=="north")  {
    if (dead)
      output << "You are dead and cannot move north." << NL;
    else {
      movedino();
      movetoroom(playerx, playery - 1);
    }
  } else if (token=="south")  {
    if (dead)
      output << "You are dead and cannot move south." << NL;
    else {
      movedino();
      movetoroom(playerx, playery + 1);
    }
  } else if (token=="east")  {
    if (dead)
      output << "You are dead and cannot move east." << NL;
    else {
      movedino();
      movetoroom(playerx + 1, playery);
    }
  } else if (token=="west")  {
    if (dead)
      output << "You are dead and cannot move west." << NL;
    else {
      movedino();
      movetoroom(playerx - 1, playery);
    }
  } else if (token=="wait")  {
    if (dead)
      output << "You can wait for as long as you like, it is the only thing you can do anyway." << NL;
    else {
      output << "You wait a bit" << NL;
      movedino();
      movetoroom(playerx, playery);
    }
  } else if (token=="map")  {
    showmap();
  } else if (token=="treasure")  {
    output << "Your current treasure is " << score << NL;
  } else if (token=="restart")  {
    startgame();
  } else if (token=="save")  {
    if (dead)
      output << "It is not a good idea to save the game when you are dead." << NL;
    else
      savegame();
  } else if (token=="restore")  {
    restoregame();
  } else if (token=="leave")  {
    if (dead)
      output << "You cannot leave the cave while you are dead." << NL;
    else if (! exitfound)
      output << "You need to find the exit first." << NL;
    else if (! ((exitx==playerx) && (exity==playery)))
      output << "You need to go back to the exit to leave the cave." << NL;
    else {
      output << "You have finally found the cave's exit!" << NL;
      output << "Unfortunately, while you try to leave through the old tunnel," << NL;
      output << "a big boulder falls on you and crushes you instantly." << NL;
      output << "Well, better luck next time!" << NL;
      die();
    }
  } else {
    output << "I don't understand the command `" << token << "', could you rephase it?" << NL;
  }
}

Name: Anonymous 2012-10-02 14:11


anystring prompt()
{
  anystring s;

  anystring prompt_result;
  do {
    output << "> ";
    input >> s >> NL;
  } while (!(s != ""));
  prompt_result=s; /* GRAAAAH FUCKITFUCKITUFUCKFUFKOITUCK [oiasjdfpoaijsdfpaodifn */
  return prompt_result;
}

void playgame()
{
  anystring cmd;

  running=true;
  while (running)  {
    cmd=prompt();
    parsecommand(cmd);
  }
}

int main(int argc, const char* argv[])
{
  pio_initialize(argc, argv);
  startgame();
  playgame();
  return EXIT_SUCCESS;
}

Name: Anonymous 2012-10-02 14:17

>>3-6
That's a really irritating habit you have there.

Name: Anonymous 2012-10-03 5:52

>>3-6
It's horrible!

Name: Anonymous 2012-10-03 7:58

I love all of Turbumann's 7.0 Pascaglias.

Name: Anonymous 2012-10-03 8:44

****
ツルッボ
パスカル
****

Don't change these.
Name: Email:
Entire Thread Thread List