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.

keypressed

UltimateEndUltimateEnd Posts: 21Member
i am doing a little slot machine game, where the reels continue to spin in a repeat-until (keypressed=true). the problem is, there are three of these such loops in succesion, and i need a way to make keypressed false again, here is my code, could you make a suggestion?

[code]program multipak;
uses wincrt;
type slots = array [1..3] of char;
var slot:slots;
points,generic,choose:integer;
addy:real;
x:longint;
check:boolean;


procedure delay;

begin
for x:= 1 to 20000000 do
for x:= 1 to 20000000 do
for x:= 1 to 20000000 do
for x:= 1 to 20000000 do
for x:= 1 to 20000000 do
addy:=20+20*20/20+45-2;
end;

procedure slotmachine;
begin
slot[1]:='a';
slot[2]:='c';
slot[3]:='b';
begin
repeat
for generic:=1 to 3 do
slot[generic]:=succ(slot[generic]);

for generic:=1 to 3 do
begin
if slot[generic]='d' then slot[generic]:='a';
end;
delay;
clrscr;
writeln (slot[1],slot[2],slot[3]);
if keypressed then check:=true;
until (check=true);
check := false;

repeat
for generic:=2 to 3 do
slot[generic]:=succ(slot[generic]);

for generic:=2 to 3 do
begin
if slot[generic]='d' then slot[generic]:='a';
end;
delay;
clrscr;
writeln (slot[1],slot[2],slot[3]);
if keypressed then check:=true;
until (check=true);

check := false;
repeat
slot[3]:=succ(slot[3]);
if slot[3]='d' then slot[3]:='a';
delay;
writeln (slot[1],slot[2],slot[3]);

clrscr;
if keypressed then check:=true;
until (check=true);
check := false;
end;
if (slot[1]=slot[2]) and (slot[2]=slot[3]) then
begin
writeln ('50 Points');
points:=points+50;
readln;
clrscr;
end;
end;[/code]


Comments

  • ManningManning Posts: 1,621Member
    [b][red]This message was edited by Moderator at 2003-2-21 7:23:52[/red][/b][hr]
    : i am doing a little slot machine game, where the reels continue to spin in a repeat-until (keypressed=true). the problem is, there are three of these such loops in succesion, and i need a way to make keypressed false again, here is my code, could you make a suggestion?

    To make keypressed false, you could use ReadKey. But they may have hit an extended key which means ReadKey would need to be called twice, so you should actually use this:

    while KeyPressed do ReadKey;

    That will ensure the input buffer is cleared.

    And on a somewhat related matter, I would suggest you search on google for the SWAG archive. It has some timeslice code you could call within your loop that would ensure your program doesnt use 100% of the CPU and cause other running programs to slow down. If you run under pure DOS this isnt an issue, but if you are using Windows then using 100% of the CPU is not a nice thing to do :)

    EDIT:

    I noticed you also call ClrScr in your loop, which is quite slow. You may want to consider using Write() instead of WriteLn() to write the information to the screen. Then instead of ClrScr, you could use (for example) GotoXY(WhereX - 3, WhereY); which would move the cursor left 3 spaces. Then the next time Write() is called it would overwrite the text you previously wrote, which would be much faster (and have less flickering I would imagine).

    And I also noticed you are using the WinCrt unit, which means you must be using TPW, which I have no experience with. So it is entirely possible that the suggestions I have made are not possible. I think WinCrt unit does have GotoXY() and ReadKey() though, so it should be alright.
Sign In or Register to comment.