Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Artificial input :P

belzebubukasbelzebubukas Posts: 6Member
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 :)


[size=2][color=Orange]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.[/color][/size]

Comments

  • quikcarlquikcarl Posts: 59Member
    What's with the parentheses around the setkey values?

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

    Try this:
    [code][color=Green] procedure SetKey;
    begin
    Left := 'a';
    Right := 'd';
    Up := 'w';
    Down := 's'
    end;[/color][/code]
    And then try poking these [italic]lowercase[/italic] keys to see if you get any movement.
  • _Atex__Atex_ Posts: 163Member
    : 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 :)
    :
    :[code]
    : [color=Orange]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;
    :
    : [red]function PS:boolean[/red];
    [red]var ch:char;[/red]
    : begin
    : [red]ch:=[/red]readkey;
    : ClrScr;
    : If [red]ch [/red] = left then x := x - 1;
    : If [red]ch [/red] = right then x := x + 1;
    : If [red]ch [/red] = up then y := y - 1;
    : If [red]ch [/red] = down then y := y + 1;
    : DrawMe;
    [red]PS:=ch<>#27; { Esc to Quit}[/red]
    : end;
    :
    :
    : begin
    :
    : SetKey;
    :
    : x := 38;
    : y := 15;
    : DrawMe;
    :
    : [red]while PS do; [/red]
    :
    : Readln;
    : end.[/color][/code]
    :

    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

  • belzebubukasbelzebubukas Posts: 6Member
    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:

    [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.
    [/code]
  • _Atex__Atex_ Posts: 163Member
    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.
  • belzebubukasbelzebubukas Posts: 6Member
    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[color=Orange](1-5)[/color] and sy[color=Orange](1-5)[/color] 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
  • belzebubukasbelzebubukas Posts: 6Member
    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!!!
  • _Atex__Atex_ Posts: 163Member
    : Download the game
    : [link=http://www.2shared.com/file/BQ7tkPss/Zaidas.html]here[/link] :)
    :

    Why don't you post the source instead ? Someone may learn a thing or two...
  • belzebubukasbelzebubukas Posts: 6Member
    OK, [link=http://www.2shared.com/file/IMvNfDhr/v1_online.html]here's[/link] 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.


Sign In or Register to comment.