Recent

Author Topic: Restart a program  (Read 6899 times)

copY

  • New member
  • *
  • Posts: 8
Restart a program
« on: April 25, 2017, 01:04:52 pm »
I'm writing a simple XO game, and after someone wins the game, i added an option: if the player writes 'reset' then he become enable to play another game, where i need to run my program from the start, or from a spacific point (after the two player added their name). Is there some command to do this?
Thx for helping
~copY

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Restart a program
« Reply #1 on: April 25, 2017, 01:42:08 pm »
If you use game builder software, they may have such 'reset' feature. But Free Pascal and Lazarus are for general purpose programming, there is no a single command to do the 'reset'. You have program it yourself.

Usually you just need to use a (or 2) repeat - until looping. I found a video tutorial that suits your case:
https://www.youtube.com/watch?v=oyCwGhqxFUg

You can see the author solves the "Play Again Y/N" problem using 2 loopings in the video tutorial above on the video time of 12:10.

Have fun!
« Last Edit: April 25, 2017, 01:47:12 pm by Handoko »

Eugene Loza

  • Hero Member
  • *****
  • Posts: 663
    • My games in Pascal
Re: Restart a program
« Reply #2 on: April 25, 2017, 01:44:36 pm »
Very ugly solution to this would be
Code: Pascal  [Select][+][-]
  1. PlayAgain:
  2. (your game code)
  3. goto(PlayAgain)

A much nicer solution would be creating a "Game cycle" like Handoko suggested, e.g. like:
Code: Pascal  [Select][+][-]
  1. repeat
  2.    repeat
  3.    (your game code)
  4.    until (victory) or (defeat);
  5.    WriteLn('Play again?');
  6.    Readln(s);
  7.    if (s='no') or (s='n') then endGame := true else endGame := false;
  8. until endGame;
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Restart a program
« Reply #3 on: April 25, 2017, 01:52:18 pm »
I do not against using goto. Actually it is very easy to understand for newbies. But the problem of using several gotos, especially when overlapped together, will usually make the code hard to predict or debug.

Read more about goto:
http://wiki.freepascal.org/Goto

Read more about repeat - until:
http://wiki.freepascal.org/REPEAT..UNTIL
« Last Edit: April 25, 2017, 01:58:40 pm by Handoko »

copY

  • New member
  • *
  • Posts: 8
Re: Restart a program
« Reply #4 on: April 25, 2017, 01:58:50 pm »
Thank you, these replies helps a lot, im actualy a newbie. I am in the 9th grade, i started programming in C++ in the 5th grade, so Pascal is a little bit new for me. I have another question, i saw something like this but i didnt understand the method. I have to draw to the console a little window or table for the XO game, and the characters aren't the same after i write the uses crt command (i have to use that for gotoxy and clrscr). Can i solve this somehow?
~copY

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Restart a program
« Reply #5 on: April 25, 2017, 02:11:12 pm »
I saw that post, it's already answered. I do not use crt, nothing much I can say. I recommend to to use 'better' graphics or game engine.

Here is a list of available game engines (for Pascal):
http://wiki.freepascal.org/Game_Engine

I recommend you to use SDL, Castle Game Engine or Allegro. They are easier than you expected. But if just want to create very very simple games, you can use BGRABitmap as the graphics engine. It has very good tutorials:
http://wiki.lazarus.freepascal.org/BGRABitmap_tutorial

But if you just want to write games easily, you may try ZGameEditor. It has everything you need to write games (audio generator, collision, 2D, 3D, etc). It allow you to export your games to Android, and it has Pascal-like scripting language.

copY

  • New member
  • *
  • Posts: 8
Re: Restart a program
« Reply #6 on: April 25, 2017, 02:19:38 pm »
Thank you man. But there is some problems, i dont have an android phone, I'm using iPhone, but i already discoveres Xcode (uses swift language) which is a good program for writing games and applications for iOS.
This XO game is need to be in pascal, because it's a homework and the teacher asked us to write in Pascal, using crt and stuff.
In freepascal the method worked with crt too, but I wanted to change to a better program to write in Pascal, because for me the FreePascal is very old fashioned. (Before that i used Codeblocks).
I saw that post is answered but i cant understand that. (i downloaded Lazarus yesterday).
~copY

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Restart a program
« Reply #7 on: April 25, 2017, 02:30:11 pm »
Good news for you. Castle Game Engine can export games to iOS. Not sure about  ZGameEditor, but I know it supports Mac OS X.

I was surprised, first time I try ZGameEditor. It makes writing games so easy. For users who have programming knowledge can write games in hours first time use ZGameEditor.

http://zgameeditor.org/index.php/Main/Features

If you interested in programming rather than writing games, you should try Castle Game Engine. It harder to learn but more rewarding.

https://castle-engine.sourceforge.io/

Some readings you may interested:

http://forum.lazarus.freepascal.org/index.php/topic,32316.msg208266.html#msg208266
http://forum.lazarus.freepascal.org/index.php/topic,30799.msg196282.html#msg196282

 

TinyPortal © 2005-2018