Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4098
Number of posts: 14002

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Artificial input :P Posted by belzebubukas on 30 Mar 2011 at 9:51 AM
I am making kinda game on pascal and I have faced a problem - "spaceship" should move when appropriate button is clicked, but you need to click the same button for 5 times. So I think a solution would be to make it think you pressed the same key 5 times while you do it only once, but is it possible to do?

PS

here's the whole code (very little yet) if you can think of a better solution :)


program Bevarde1;
uses Crt;
var x, y: integer;
left, right, up, down: string;

procedure SetKey;
begin
Left := ('a');
Right := ('d');
Up := ('w');
Down := ('s');
end;

procedure DrawMe;
begin;
GoToxy(x+2, y-1);
Write('I');
GoToxy(x, y);
Write('<=M=>');
GoToxy(x+1, y+1);
Write('I I');
end;

procedure PS;
begin
readkey;
ClrScr;
If readkey = left then x := x - 1;
If readkey = right then x := x + 1;
If readkey = up then y := y - 1;
If readkey = down then y := y + 1;
DrawMe;
end;


begin

SetKey;

x := 38;
y := 15;
DrawMe;

while 1 = 1 do PS;

Readln;
end.

Report
Re: Artificial input :P Posted by quikcarl on 30 Mar 2011 at 11:33 AM
What's with the parentheses around the setkey values?

: procedure SetKey;
: begin
: Left := ('a');
: Right := ('d');
: Up := ('w');
: Down := ('s');
: end;

Try this:
   procedure SetKey;
     begin
         Left := 'a';
         Right := 'd';
         Up := 'w';
         Down := 's'
     end;

And then try poking these lowercase keys to see if you get any movement.
Report
Re: Artificial input :P Posted by _Atex_ on 30 Mar 2011 at 7:45 PM
: I am making kinda game on pascal and I have faced a problem -
: "spaceship" should move when appropriate button is clicked, but you
: need to click the same button for 5 times. So I think a solution
: would be to make it think you pressed the same key 5 times while you
: do it only once, but is it possible to do?
:
: PS
:
: here's the whole code (very little yet) if you can think of a better
: solution :)
:
:
 
: program Bevarde1;
: uses Crt;
: var x, y: integer;
:     left, right, up, down: string;
:     
:       procedure SetKey;
:     begin
:         Left := ('a');
:         Right := ('d');
:         Up := ('w');
:         Down := ('s');
:     end;
:     
:       procedure DrawMe;
:     begin;
:       GoToxy(x+2, y-1);
:       Write('I');
:       GoToxy(x, y);
:       Write('<=M=>');
:       GoToxy(x+1, y+1);
:       Write('I I');
:     end;
:     
:     function PS:boolean;
      var ch:char;
:     begin
:       ch:=readkey;
:       ClrScr;
:       If ch  = left  then x := x - 1;
:       If ch  = right then x := x + 1;
:       If ch  = up    then y := y - 1;
:       If ch  = down  then y := y + 1;
:       DrawMe;
        PS:=ch<>#27; { Esc to Quit}
:     end;
:     
:     
: begin
: 
:      SetKey;
:    
:    x := 38;
:    y := 15;
:    DrawMe;
: 
:    while PS do; 
: 
: Readln;
: end.

:

That's a good start, if you read through this forum then you'll find lots of examples on this subject. Here's a simple textmode game you could look at: http://www.programmersheaven.com/mb/pasprog/392302/392335/simple-snake-game-and--maze-generator/?S=B10000

Report
Re: Artificial input :P Posted by belzebubukas on 31 Mar 2011 at 10:04 AM
Firstly, thanks for replies :)

Ok, so I checked out that sample (the snake game) and I understood approximately nothing... I'm new to programming, you see... Plus - I copied the code to the pascal I use and it seems not to have some functions. Anyway, I took another route, and everything seems OK to me (even though I'm not sure what I did with the imput :D), but I get an error right away after starting the thing. Any help again?

Oh, here's the code:

 program TheGame;
 uses Crt;
 var x, y, x1, y1, sp, sx1, sx2, sx3, sx4, sx5, sy1, sy2, sy3, sy4, sy5, score: integer;
     left, right, up, down, sho1: string;
     ch: char;
{--------------------------------------------------------}

       procedure spawn;
     begin;
       randomize;
       sp := round (random * 64 + 5);
     end;

{--------------------------------------------------------}

       procedure DrawMe;
     begin;
       GoToxy(x+2, y-1);
       Write('I');
       GoToxy(x, y);
       Write('<=M=>');
       GoToxy(x+1, y+1);
       Write('I I');
     end;
     
{--------------------------------------------------------}
     
       procedure shoot1;
     begin
       x1 := x + 2;
       y1 := y - 1;
     end;

     
{--------------------------------------------------------}
     
       procedure SetKey;
     begin
         Left  := 'a';
         Right := 'd';
         Up    := 'w';
         Down  := 's';
         sho1  := 'g';
     end;
     
{--------------------------------------------------------}

 begin
      SetKey;
    x := 38;
    y := 15;
    DrawMe;
    
    score := 0;
    x1  := 1000;
    sx1 := 1000;
    sx2 := 1000;
    sx3 := 1000;
    sx4 := 1000;
    sx5 := 1000;
    y1  := 10;
    sy1 := 10;
    sy2 := 10;
    sy3 := 10;
    sy4 := 10;
    sy5 := 10;
    
 While 1 = 1 do begin

  {THE BULLET}
  
  if x1 <> 1000 then y1 := y1 - 1;
  if y1 = 1 then x1 := 1000;

  {IF GET SHOT}

  if x1 = sx1 then if
     y1 = sy1 then begin
     sy1 := 1;
     sx1 := 1000;
     x1 := 1000;
     score := score + 10;        end;

  if x1 = sx2 then if
     y1 = sy2 then begin
     sy2 := 1;
     sx2 := 1000;
     x1 := 1000;
     score := score + 10;        end;

  if x1 = sx3 then if
     y1 = sy3 then begin
     sy3 := 1;
     sx3 := 1000;
     x1 := 1000;
     score := score + 10;        end;

  if x1 = sx4 then if
     y1 = sy4 then begin
     sy4 := 1;
     sx4 := 1000;
     x1 := 1000;
     score := score + 10;        end;

  if x1 = sx5 then if
     y1 = sy5 then begin
     sy5 := 1;
     sx5 := 1000;
     x1 := 1000;
     score := score + 10;        end;

{SPAWNS 1 TO 5}

    if sx1 = 1000 then
  begin
    spawn;
    sx1 := sp;
    sy1 := 1;
  end else
    sy1 := sy1 + 1;
  
    if sx2 = 1000 then
  begin
    spawn;
    sx2 := sp;
    sy2 := 1;
  end else
    sy2 := sy2 + 1;
  
    if sx3 = 1000 then
  begin
    spawn;
    sx3 := sp;
    sy3 := 1;
  end else
    sy3 := sy3 + 1;
  
    if sx4 = 1000 then
  begin
    spawn;
    sx4 := sp;
    sy4 := 1;
  end else
    sy4 := sy4 + 1;
  
    if sx1 = 1000 then
  begin
    spawn;
    sx5 := sp;
    sy5 := 1;
  end else
    sy5 := sy5 + 1;

  
{OK... NOW DETECT INPUT AND APPLY IT}

  If keypressed then
    begin
       ch := readkey;
       If ch  = left   then if x > 3  then x := x - 1;
       If ch  = right  then if x < 74 then x := x + 1;
       If ch  = up     then if y > 3  then y := y - 1;
       If ch  = down   then if y < 90 then y := y + 1;
       If ch  = sho1   then shoot1;
    end;
                
{COOL, GOT COORDINATES, NOW THE DRAWING}

    If x1 <> 1000 then GoToXY(x1, y1);
    If x1 <> 1000 then Write('*');
    GoToXY(sx1, sy1);
    Write('U');
    GoToXY(sx2, sy2);
    Write('O');
    GoToXY(sx3, sy3);
    Write('Y');
    GoToXY(sx4, sy4);
    Write('H');
    GoToXY(sx5, sy5);
    Write('V');
    DrawMe;
    
{END OF FRAME}
    
    ClrScr;
    Delay(10);
  End;
 end.

Report
Re: Artificial input :P Posted by _Atex_ on 31 Mar 2011 at 9:22 PM
What compiler are you using ? The game, what is all about ? Explain what you're trying to achieve, time permitting will put ya on the right track.
Report
Re: Artificial input :P Posted by belzebubukas on 1 Apr 2011 at 5:09 AM
Ok, so firstly I use Free Pascal 0.6.4.

The game is pretty much like this: you control the "spaceship" and destroy objects falling from the top of screen by shooting them. Once object is destroyed, it respawns immediately in the top.

The objects are basically coordinates. If object is not in use, I just set the x value to 1000.

The x and y values refer to the "spaceship"
The x1 and y1 values refer to the bullet
The sx(1-5) and sy(1-5) refers to 5 independant falling objects.

The whole thing I've done firstly checks for imput (even though I'm not sure will it work), then counts all coordinates and finally draws all objects that are in use. All this should repeat every 10 miliseconds.

One more thing - The objects fall in the same speed as bullet moves, so I'll make them to fall once in about ten loops (that would be approximately 100 miliseconds).

PS

I'm making this game not to make it, but to gain experience in such stuff, so in the end I don't really care if I finish it sucessfully :P
Report
Re: Artificial input :P Posted by belzebubukas on 1 Apr 2011 at 9:28 AM
OMG. I have control of situation :) no need of help. when I finish the game I'll post it here (if you want, of coure, and if won't face any more problems).

I'm so happy!!!
Report
Re: Artificial input :P Posted by belzebubukas on 1 Apr 2011 at 12:26 PM
Download the game here :)
Report
Re: Artificial input :P Posted by _Atex_ on 3 Apr 2011 at 4:25 PM
: Download the game
: here :)
:

Why don't you post the source instead ? Someone may learn a thing or two...
Report
Re: Artificial input :P Posted by belzebubukas on 4 Apr 2011 at 9:05 AM
OK, here's first I found and last versions of it (First one way more simple) :) both of them have an .exe and .pas files, so you can check the source.





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.