Pascal中的数组问题

时间:2016-05-19 20:50:29

标签: arrays pascal

我是Pascal阵列的新手,我非常喜欢学习它们,因为我的梦想是开发游戏。我开始创造一个游戏,其中有一个玩家像波浪一样移动(比如Geonetry Dash游戏,但更简单)使用键“w”和“s”。问题是它不起作用。我想向玩家做一个小道,但看起来它不起作用!请告诉我,这有什么不对。

program gameArrays;
uses crt;     (*only crt*)
var
nx: array[1..20] of integer;                
ny: array[1..20] of integer;
x,y:integer; {*Not much used.. y not used but x only once*}
input:char;  {*Input from player*}
f,l:integer;  {*I don't know how to explain these elements.. see the code and you will understand*}
 begin
 clrscr;
 x:=10;
 nx[1]:=x;    (*nx[X] is the trail of the player.. so nx[10] is 1 so it is the last*)
 nx[2]:=x-1;
 nx[3]:=x-2;
 nx[4]:=x-3;
 nx[5]:=x-4;
 nx[6]:=x-5;
 nx[7]:=x-6;
 nx[8]:=x-7;
 nx[9]:=x-8;
 nx[10]:=x-9;
 gotoxy(nx[1],15);   (*player is  on x:10 y:15*)
 write('�');
 ny[1]:=15;
 for f:=1 to 10 do
     ny[f]:=15;           (*ny is the trail that moves up and down following the player and all are y:20 at beginning*)
 repeat
 input:=readkey;   (*if input is w then player moves up, if input is s then player moves down*)
 if input='w' then
    repeat
    for f:=1 to 10 do
        begin
             gotoxy(nx[f],ny[f]);
             write(' ');
        end;            (*delete recent activities on the screen*)
    for f:=2 to 10 do
        ny[f]:=ny[f]-1;  (*once the  "repeat until" ends, ny[2 to 10] changes.. for ex y:2 become y:1 and y:3 become y:2*)
    for f:=1 to 10 do
        begin
             gotoxy(nx[f],ny[f]);  (*write all*)
             write('�');
        end;
    ny[1]:=ny[1]-1;
    delay(100);
    until keypressed;
 if input='s' then
    repeat
    for f:=1 to 10 do
        begin
             gotoxy(nx[f],ny[f]);
             write(' ');
        end;
    for f:=2 to 10 do
        ny[f]:=ny[f]+1;
    for f:=1 to 10 do
        begin
             gotoxy(nx[f],ny[f]);
             write('�');
        end;
    ny[1]:=ny[1]+1;
    delay(100);
    until keypressed;
 until input='q';
  end.

1 个答案:

答案 0 :(得分:0)

虽然我不知道你是如何尝试这个解决方案的思维模式,但从经验我可以告诉你,对象是做这些事情的最佳方式。下面的程序产生了我相信你的目标。

program gameArrays;
uses crt;
type
   piece= object
    x,y:integer;
    S:char;
   end;
var
  trail:array[0..11] of piece;{we want 11 images trailing behind}
  i,e:integer;
  input:char;
  distancemax:integer;
  distance:integer;
  run:boolean; {you should know}
begin
  distancemax:=length(trail)-1;
  distance:=distance;

for i:=0 to length(trail)-1 do {incase you change the amount of images trailing}
begin
    trail[i].y:=15;
    trail[i].x:=15;{all pieces are originally in one position then stretch out}
end;

for i:=0 to length(trail)-2 do {passing characters to the trail pieces so you can see them}
  trail[i].S:=char(2);
  {loop ends here}
  trail[length(trail)-1].S:='O'; {giving the head a different character}
  run:=true;

  while run do
  begin
    clrscr;
    for i:=0 to length(trail)-1 do {drawing}
    begin
      gotoxy(trail[i].x,trail[i].y);
      write(trail[i].S);
    end;

    if distance<distancemax then {there is a limit to how far the body should stretch}
    begin
      for i:=0 to distance do
        dec(trail[i].x); {stretches out one at a time starting from the tail}
      {loop ends here}
      inc(distance); {increases the distance at each frame}
    end;

     for i:=0 to length(trail)-2 do {makes pieces from the tail up copy the y value of the one ahead /except for the head/}
       trail[i].y:=trail[i+1].y;

    if keypressed then input:=readkey; {prevent waiting for a keypress}

    case char(input) of {button press responses}
      'w':if trail[0].y>1 then dec(trail[length(trail)-1].y);
      's':if trail[0].y<25 then inc(trail[length(trail)-1].y);
      'q':run:=false;
    end;
    delay(100);
  end;
end.      

至于你提供的代码,余像并没有像我一样向后移动屏幕,我没有花太多时间查看你的代码,我真的不得不说。尝试一下。