Реализовать секундомер для игры - Turbo Pascal
Формулировка задачи:
Решение задачи: «Реализовать секундомер для игры»
program game; uses crt, graph,dos; const x=9; y=9; kol=10; var a: array[0..x+1,0..y+1] of integer; i,j,f,g,h,i1, j1, ind, rez : integer; key :char; tx: text; ch: string; vm,vs,dx:integer; so,sm,ss:string; hs,mn,sc,t,z,w:word; begin Assign(tx, 'output.txt'); rewrite(tx); randomize; rez:=0; for f:= 1 to kol do begin i:=random(x)+1; j:=random(y)+1; if a[i,j]<>-1 then begin a[i,j]:=-1; if (a[i-1,j-1]<>-1) and (x+1>i-1) and (i-1>0) and (y+1>j-1) and (j-1>0) then a[i-1,j-1]:=a[i-1,j-1]+1; if (a[i,j-1]<>-1) and (x+1>i) and (i>0) and (y+1>j-1) and (j-1>0) then a[i,j-1]:=a[i,j-1]+1; if (a[i+1,j-1]<>-1) and (x+1>i+1) and (i+1>0) and (y+1>j-1) and (j-1>0) then a[i+1,j-1]:=a[i+1,j-1]+1; if (a[i+1,j]<>-1) and (x+1>i+1) and (i+1>0) and (y+1>j) and (j>0) then a[i+1,j]:=a[i+1,j]+1; if (a[i+1,j+1]<>-1) and (x+1>i+1) and (i+1>0) and (y+1>j+1) and (j+1>0) then a[i+1,j+1]:=a[i+1,j+1]+1; if (a[i,j+1]<>-1) and (x+1>i) and (i>0) and (y+1>j+1) and (j+1>0) then a[i,j+1]:=a[i,j+1]+1; if (a[i-1,j+1]<>-1) and (x+1>i-1) and (i-1>0) and (y+1>j+1) and (j+1>0) then a[i-1,j+1]:=a[i-1,j+1]+1; if (a[i-1,j]<>-1) and (x+1>i-1) and (i-1>0) and (y+1>j) and (j>0) then a[i-1,j]:=a[i-1,j]+1; end else f:=f-1; end; initgraph(g,h,''); setbkcolor(lightgray); setfillstyle(1,white); setlinestyle(0,0,3); vm:=0; vs:=0; for i:=1 to x do for j:=1 to y do bar(10+20*(i-1),10+20*(j-1),10+15*i+5*(i-1),10+15*j+5*(j-1)); i:=1; j:=1; setcolor(10); rectangle(290,20,350,45); outtextXY(302,30,'00:00'); gettime(hs,mn,sc,t);{определим время начала} z:=sc;{запомним секунды} w:=sc; vm:=0;{обнулим минуты} vs:=sc-w;{обнулим секунды} repeat gettime(hs,mn,sc,t);{будем постоянно определять время} if z<>sc then{если секунды изменились} begin vs:=vs+1;{наращиваем секунды для вывода} if vs=60 then{если минута прошла} begin vm:=vm+1;{корректируем время для вывода} vs:=0 end; str(vm,sm);{преобразуем в строку и выводим} if vm<10 then sm:='0'+sm; str(vs,ss); if vs<10 then ss:='0'+ss; so:=sm+':'+ss; setcolor(0); setfillstyle(1,0); bar(290,20,350,45); setcolor(10); rectangle(290,20,350,45); settextstyle(0,0,1); SetTextJustify (LeftText, TopText); outtextXY(302,30,so); z:=sc end; setcolor(yellow); if i=x+1 then i:=1; if i=0 then i:=X; if j=Y+1 then j:=1; if j=0 then j:=Y; rectangle(8+20*(i-1),8+20*(j-1),12+15*i+5*(i-1),12+15*j+5*(j-1)); if keypressed then{если нажата клавиша} begin key:=readkey; {если для игры, играем} if ord(key)=115 then begin setcolor(lightgray); rectangle(8+20*(i-1),8+20*(j-1),12+15*i+5*(i-1),12+15*j+5*(j-1)); j:=j+1; end; if ord(key)=119 then begin setcolor(lightgray); rectangle(8+20*(i-1),8+20*(j-1),12+15*i+5*(i-1),12+15*j+5*(j-1)); j:=j-1; end; if ord(key)=97 then begin setcolor(lightgray); rectangle(8+20*(i-1),8+20*(j-1),12+15*i+5*(i-1),12+15*j+5*(j-1)); i:=i-1; end; if ord(key)=100 then begin setcolor(lightgray); rectangle(8+20*(i-1),8+20*(j-1),12+15*i+5*(i-1),12+15*j+5*(j-1)); i:=i+1; end; if ord(key)=101 then begin if a[i,j]=-1 then begin rez:=-1; for j:=1 to y do begin for i:=1 to x do begin if a[i,j]=-1 then begin setfillstyle(1,red); bar(10+20*(i-1),10+20*(j-1),10+15*i+5*(i-1),10+15*j+5*(j-1)); end; end; end; end else if a[i,j]=0 then begin a[i,j]:=-2; i1:=i; j1:=j; repeat ind:=0; for j:=1 to y do begin for i:=1 to x do begin if a[i,j]<>-1 then begin if a[i,j]=-2 then begin if a[i-1,j-1]=0 then begin a[i-1,j-1]:=-2; ind:=ind+1; end else if a[i-1,j-1]>0 then begin rez:=rez+1; str(a[i-1,j-1],ch); settextjustify(centertext, centertext); settextstyle(7,0,1); setcolor(a[i-1,j-1]); outtextXY((10+20*(i-2)+10+15*(i-1)+5*(i-2)) div 2,(10+20*(j-2)+10+15*(j-1)+5*(j-2)) div 2 - 2, ch); a[i-1,j-1]:=-3; end; if a[i,j-1]=0 then begin a[i,j-1]:=-2; ind:=ind+1; end else if a[i,j-1]>0 then begin rez:=rez+1; str(a[i,j-1],ch); settextjustify(centertext, centertext); settextstyle(7,0,1); setcolor(a[i,j-1]); outtextXY((10+20*(i-1)+10+15*i+5*(i-1)) div 2,(10+20*(j-2)+10+15*(j-1)+5*(j-2)) div 2 - 2, ch); a[i,j-1]:=-3; end; if a[i+1,j-1]=0 then begin a[i+1,j-1]:=-2; ind:=ind+1; end else if a[i+1,j-1]>0 then begin rez:=rez+1; str(a[i+1,j-1],ch); settextjustify(centertext, centertext); settextstyle(7,0,1); setcolor(a[i+1,j-1]); outtextXY((10+20*i+10+15*(i+1)+5*i) div 2,(10+20*(j-2)+10+15*(j-1)+5*(j-2)) div 2 - 2, ch); a[i+1,j-1]:=-3; end; if a[i+1,j]=0 then begin a[i+1,j]:=-2; ind:=ind+1; end else if a[i+1,j]>0 then begin rez:=rez+1; str(a[i+1,j],ch); settextjustify(centertext, centertext); settextstyle(7,0,1); setcolor(a[i+1,j]); outtextXY((10+20*(i)+10+15*(i+1)+5*i) div 2,(10+20*(j-1)+10+15*j+5*(j-1)) div 2 - 2, ch); a[i+1,j]:=-3; end; if a[i+1,j+1]=0 then begin a[i+1,j+1]:=-2; ind:=ind+1; end else if a[i+1,j+1]>0 then begin rez:=rez+1; str(a[i+1,j+1],ch); settextjustify(centertext, centertext); settextstyle(7,0,1); setcolor(a[i+1,j+1]); outtextXY((10+20*i+10+15*(i+1)+5*i) div 2,(10+20*j+10+15*(j+1)+5*j) div 2 - 2, ch); a[i+1,j+1]:=-3; end; if a[i,j+1]=0 then begin a[i,j+1]:=-2; ind:=ind+1; end else if a[i,j+1]>0 then begin rez:=rez+1; str(a[i,j+1],ch); settextjustify(centertext, centertext); settextstyle(7,0,1); setcolor(a[i,j+1]); outtextXY((10+20*(i-1)+10+15*i+5*(i-1)) div 2,(10+20*j+10+15*(j+1)+5*j) div 2 - 2, ch); a[i,j+1]:=-3; end; if a[i-1,j+1]=0 then begin a[i-1,j+1]:=-2; ind:=ind+1; end else if a[i-1,j+1]>0 then begin rez:=rez+1; str(a[i-1,j+1],ch); settextjustify(centertext, centertext); settextstyle(7,0,1); setcolor(a[i-1,j+1]); outtextXY((10+20*(i-2)+10+15*(i-1)+5*(i-2)) div 2,(10+20*j+10+15*(j+1)+5*j) div 2 - 2, ch); a[i-1,j+1]:=-3; end; if a[i-1,j]=0 then begin a[i-1,j]:=-2; ind:=ind+1; end else if a[i-1,j]>0 then begin rez:=rez+1; str(a[i-1,j],ch); settextjustify(centertext, centertext); settextstyle(7,0,1); setcolor(a[i-1,j]); outtextXY((10+20*(i-2)+10+15*(i-1)+5*(i-2)) div 2,(10+20*(j-1)+10+15*j+5*(j-1)) div 2 - 2, ch); a[i-1,j]:=-3; end; end; end; end; end; if ord(key)=13 then exit; for j:=1 to y do for i:=1 to x do if a[i,j]=-2 then begin a[i,j]:=-3; rez:=rez+1; setfillstyle(1,brown); bar(10+20*(i-1),10+20*(j-1),10+15*i+5*(i-1),10+15*j+5*(j-1)); end; until ind=0; i:=i1; j:=j1; end else if a[i,j]>0 then begin rez:=rez+1; str(a[i,j],ch); settextjustify(centertext, centertext); settextstyle(7,0,1); setcolor(a[i,j]); outtextXY((10+20*(i-1)+10+15*i+5*(i-1)) div 2,(10+20*(j-1)+10+15*j+5*(j-1)) div 2 - 2, ch); a[i,j]:=-3; end; end; end; until (rez=-1) or (rez=x*y-kol)or(ord(key)=13); write(tx,rez); close(tx); end.
Объяснение кода листинга программы
The code you provided is a game timer written in Turbo Pascal. It implements a секундомер for games. The timer keeps track of the number of seconds elapsed since the start of the game. If the game is over, the timer stops. The code also has a function to determine if a cell in the game grid is empty or not. This function checks if the cell's value is -1 or if it is equal to the global variable kol
. If the cell is empty, the timer is incremented by one. The code also sets the color of the cell in question to brown using the setcolor function. The game timer is controlled by the global variables rez
, ind
and x
which keep track of the number of seconds, the current index and the size of the game grid respectively.
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д