Pascal小游戏 俄罗斯方块

俄罗斯方块已经成为了和“Hello World”一样的程序了吧?

不要直接复制,可能需要事先 Format.

program cube;
uses crt,graph,dos;
var gd,gm:smallint;
fillin:fillpatterntype;
board:array[0..26,0..26]of boolean;
cube1,cube2,cube3,cube4:array[1..2]of byte;
h,min,s,ss,ls,i,j,k,r,lin,cu1,cu2,cu3,cu4,c,t1,t2,t:word;
x,y,m:byte;
moving:boolean;
ch:char;
procedure fail;
begin
gotoxy(1,1);
clrscr;
closegraph;
writeln('Fail');
readln;
halt;
end;
procedure do1;
begin
setcolor(blue);setbkcolor(blue);setfillpattern(fillin,blue);
rectangle((cube1[2]-1)*20,(cube1[1]-1)*20,(cube1[2]-1)*20+20,(cube1[1]-1)*20+20);
bar((cube1[2]-1)*20,(cube1[1]-1)*20,(cube1[2]-1)*20+20,(cube1[1]-1)*20+20);
rectangle((cube2[2]-1)*20,(cube2[1]-1)*20,(cube2[2]-1)*20+20,(cube2[1]-1)*20+20);
bar((cube2[2]-1)*20,(cube2[1]-1)*20,(cube2[2]-1)*20+20,(cube2[1]-1)*20+20);
rectangle((cube3[2]-1)*20,(cube3[1]-1)*20,(cube3[2]-1)*20+20,(cube3[1]-1)*20+20);
bar((cube3[2]-1)*20,(cube3[1]-1)*20,(cube3[2]-1)*20+20,(cube3[1]-1)*20+20);
rectangle((cube4[2]-1)*20,(cube4[1]-1)*20,(cube4[2]-1)*20+20,(cube4[1]-1)*20+20);
bar((cube4[2]-1)*20,(cube4[1]-1)*20,(cube4[2]-1)*20+20,(cube4[1]-1)*20+20);
end;
procedure do2;
begin
setcolor(green);setbkcolor(green);setfillpattern(fillin,green);
rectangle((cube1[2]-1)*20,(cube1[1]-1)*20,(cube1[2]-1)*20+20,(cube1[1]-1)*20+20);
bar((cube1[2]-1)*20,(cube1[1]-1)*20,(cube1[2]-1)*20+20,(cube1[1]-1)*20+20);
rectangle((cube2[2]-1)*20,(cube2[1]-1)*20,(cube2[2]-1)*20+20,(cube2[1]-1)*20+20);
bar((cube2[2]-1)*20,(cube2[1]-1)*20,(cube2[2]-1)*20+20,(cube2[1]-1)*20+20);
rectangle((cube3[2]-1)*20,(cube3[1]-1)*20,(cube3[2]-1)*20+20,(cube3[1]-1)*20+20);
bar((cube3[2]-1)*20,(cube3[1]-1)*20,(cube3[2]-1)*20+20,(cube3[1]-1)*20+20);
rectangle((cube4[2]-1)*20,(cube4[1]-1)*20,(cube4[2]-1)*20+20,(cube4[1]-1)*20+20);
bar((cube4[2]-1)*20,(cube4[1]-1)*20,(cube4[2]-1)*20+20,(cube4[1]-1)*20+20);
end;
begin
randomize;
cursoroff;
getfillpattern(fillin);
c:=1;
writeln('Easiest <--');
writeln('Very Easy ');
writeln('Easy ');
writeln('Medium ');
writeln('Hard ');
writeln('Very Hard ');
writeln('Hardest ');
writeln('Random ');
repeat
if keypressed then
case readkey of
#72:begin gotoxy(11,c);write(' ');if c=1 then c:=8 else dec(c);gotoxy(11,c);write('<--');end;
#80:begin gotoxy(11,c);write(' ');if c=8 then c:=1 else inc(c);gotoxy(11,c);write('<--');end;
' ':break;
end;
until false;
if c=8 then c:=1+random(7);
case c of
1:begin x:=25;y:=25;m:=13;end;
2:begin x:=21;y:=21;m:=11;end;
3:begin x:=17;y:=17;m:=9;end;
4:begin x:=15;y:=15;m:=8;end;
5:begin x:=13;y:=13;m:=7;end;
6:begin x:=11;y:=11;m:=6;end;
7:begin x:=7;y:=11;m:=6;end;
end;
gd:=1;
gm:=detect;
initgraph(gm,gd,'temp');
setbkcolor(red);
cleardevice;
setcolor(blue);
setbkcolor(blue);
setfillpattern(fillin,blue);
rectangle(0,0,y*20,x*20);
bar(0,0,y*20,x*20);
gettime(h,min,s,ss);
ls:=s;
moving:=false;
for i:=0 to x+1 do for j:=0 to y+1 do board[i,j]:=true;
for i:=1 to x do for j:=1 to y do board[i,j]:=false;
clrscr;writeln('The game will start in 5 seconds...');delay(5000);clrscr;
repeat
gettime(h,min,s,ss);
if(s<>ls)and(moving)then
begin
if board[cube1[1]+1,cube1[2]]then moving:=false;
if board[cube2[1]+1,cube2[2]]then moving:=false;
if board[cube3[1]+1,cube3[2]]then moving:=false;
if board[cube4[1]+1,cube4[2]]then moving:=false;
if moving then
begin
do1;
setcolor(green);setbkcolor(green);setfillpattern(fillin,green);
rectangle((cube1[2]-1)*20,cube1[1]*20,(cube1[2]-1)*20+20,cube1[1]*20+20);
bar((cube1[2]-1)*20,cube1[1]*20,(cube1[2]-1)*20+20,cube1[1]*20+20);
rectangle((cube2[2]-1)*20,cube2[1]*20,(cube2[2]-1)*20+20,cube2[1]*20+20);
bar((cube2[2]-1)*20,cube2[1]*20,(cube2[2]-1)*20+20,cube2[1]*20+20);
rectangle((cube3[2]-1)*20,cube3[1]*20,(cube3[2]-1)*20+20,cube3[1]*20+20);
bar((cube3[2]-1)*20,cube3[1]*20,(cube3[2]-1)*20+20,cube3[1]*20+20);
rectangle((cube4[2]-1)*20,cube4[1]*20,(cube4[2]-1)*20+20,cube4[1]*20+20);
bar((cube4[2]-1)*20,cube4[1]*20,(cube4[2]-1)*20+20,cube4[1]*20+20);
cube1[1]:=cube1[1]+1;cube2[1]:=cube2[1]+1;cube3[1]:=cube3[1]+1;cube4[1]:=cube4[1]+1;
end
else
begin
board[cube1[1],cube1[2]]:=true;
board[cube2[1],cube2[2]]:=true;
board[cube3[1],cube3[2]]:=true;
board[cube4[1],cube4[2]]:=true;
for i:=1 to x do
begin
lin:=0;
for j:=1 to y do
if board[i,j]then inc(lin);
if lin=y then
begin
setcolor(blue);setbkcolor(blue);setfillpattern(fillin,blue);
rectangle(0,0,y*20,i*20);bar(0,0,y*20,i*20);
setcolor(green);setbkcolor(green);setfillpattern(fillin,green);
for k:=1 to y do
if board[i-1,k]then
for j:=i downto 2 do
begin
board[j,k]:=board[j-1,k];
if board[j-1,k]then begin rectangle((k-1)*20,(j-1)*20,(k-1)*20+20,(j-1)*20+20);
bar((k-1)*20,(j-1)*20,(k-1)*20+20,(j-1)*20+20);end;
end
else
for j:=i downto 3 do
begin
board[j,k]:=board[j-2,k];
if board[j-2,k]then begin rectangle((k-1)*20,(j-1)*20,(k-1)*20+20,(j-1)*20+20);
bar((k-1)*20,(j-1)*20,(k-1)*20+20,(j-1)*20+20);end;
end
end;
end;
end;
ls:=s;
end;
if moving=false then
begin
r:=1+random(7);c:=0;
case r of
1:begin for i:=1 to 2 do for j:=m-1 to m do if board[i,j]then fail;
cube1[1]:=1;cube1[2]:=m-1;cube2[1]:=1;cube2[2]:=m;cube3[1]:=2;cube3[2]:=m-1;cube4[1]:=2;cube4[2]:=m;do2;
end;
2:begin for i:=1 to 2 do for j:=m-1 to m+1 do if board[i,j]then fail;
cube1[1]:=1;cube1[2]:=m;cube2[1]:=2;cube2[2]:=m-1;cube3[1]:=2;cube3[2]:=m;cube4[1]:=2;cube4[2]:=m+1;do2;
end;
3:begin for j:=m-2 to m+1 do if board[1,j]then fail;
cube1[1]:=1;cube1[2]:=m-2;cube2[1]:=1;cube2[2]:=m-1;cube3[1]:=1;cube3[2]:=m;cube4[1]:=1;cube4[2]:=m+1;do2;
end;
4:begin for i:=1 to 2 do for j:=m-1 to m+1 do if board[i,j]then fail;
cube1[1]:=1;cube1[2]:=m+1;cube2[1]:=2;cube2[2]:=m-1;cube3[1]:=2;cube3[2]:=m;cube4[1]:=2;cube4[2]:=m+1;do2;
end;
5:begin for i:=1 to 2 do for j:=m-1 to m+1 do if board[i,j]then fail;
cube1[1]:=1;cube1[2]:=m;cube2[1]:=1;cube2[2]:=m-1;cube3[1]:=2;cube3[2]:=m;cube4[1]:=2;cube4[2]:=m+1;do2;
end;
6:begin for i:=1 to 2 do for j:=m-1 to m+1 do if board[i,j]then fail;
cube1[1]:=1;cube1[2]:=m-1;cube2[1]:=2;cube2[2]:=m-1;cube3[1]:=2;cube3[2]:=m;cube4[1]:=2;cube4[2]:=m+1;do2;
end;
7:begin for i:=1 to 2 do for j:=m-1 to m+1 do if board[i,j]then fail;
cube1[1]:=1;cube1[2]:=m;cube2[1]:=1;cube2[2]:=m+1;cube3[1]:=2;cube3[2]:=m;cube4[1]:=2;cube4[2]:=m-1;do2;
end;
end;
moving:=true;
end;
if keypressed then
case upcase(readkey)of
' ':begin repeat if keypressed then ch:=readkey;if upcase(ch)=' 'then break;until false;end;
#80:begin cu1:=0;cu2:=0;cu3:=0;cu4:=0;
while(board[cube1[1]+cu1,cube1[2]]=false)do inc(cu1);
while(board[cube2[1]+cu2,cube2[2]]=false)do inc(cu2);
while(board[cube3[1]+cu3,cube3[2]]=false)do inc(cu3);
while(board[cube4[1]+cu4,cube4[2]]=false)do inc(cu4);do1;
if cu1<cu2 then t1:=cu1 else t1:=cu2;if cu3<cu4 then t2:=cu3 else t2:=cu4;if t1<t2 then t:=t1 else t:=t2;
t:=t-1;if t>5 then t:=5;
inc(cube1[1],t);inc(cube2[1],t);inc(cube3[1],t);inc(cube4[1],t);do2;end;
#75:begin
if board[cube1[1],cube1[2]-1] then continue;if board[cube2[1],cube2[2]-1] then continue;
if board[cube3[1],cube3[2]-1] then continue;if board[cube4[1],cube4[2]-1] then continue;
do1;cube1[2]:=cube1[2]-1;cube2[2]:=cube2[2]-1;cube3[2]:=cube3[2]-1;cube4[2]:=cube4[2]-1;do2;
end;
#77:begin
if board[cube1[1],cube1[2]+1] then continue;if board[cube2[1],cube2[2]+1] then continue;
if board[cube3[1],cube3[2]+1] then continue;if board[cube4[1],cube4[2]+1] then continue;
do1;cube1[2]:=cube1[2]+1;cube2[2]:=cube2[2]+1;cube3[2]:=cube3[2]+1;cube4[2]:=cube4[2]+1;do2;
end;
#72:begin
case r of
2:case c mod 4 of
0:begin if board[cube2[1],cube2[2]+2]then continue;
if board[cube3[1]-1,cube3[2]+1]then continue;if board[cube4[1]-2,cube4[2]]then continue;
do1;cube2[2]:=cube2[2]+2;cube3[1]:=cube3[1]-1;cube3[2]:=cube3[2]+1;cube4[1]:=cube4[1]-2;do2;end;
1:begin if board[cube2[1]-2,cube2[2]]then continue;
if board[cube3[1]-1,cube3[2]-1]then continue;if board[cube4[1],cube4[2]-2]then continue;
do1;cube2[1]:=cube2[1]-2;cube3[1]:=cube3[1]-1;cube3[2]:=cube3[2]-1;cube4[2]:=cube4[2]-2;do2;end;
2:begin if board[cube2[1],cube2[2]-2]then continue;
if board[cube3[1]+1,cube3[2]-1]then continue;if board[cube4[1]+2,cube4[2]]then continue;
do1;cube2[2]:=cube2[2]-2;cube3[1]:=cube3[1]+1;cube3[2]:=cube3[2]-1;cube4[1]:=cube4[1]+2;do2;end;
3:begin if board[cube2[1]+2,cube2[2]]then continue;
if board[cube3[1]+1,cube3[2]+1]then continue;if board[cube4[1],cube4[2]+2]then continue;
do1;cube2[1]:=cube2[1]+2;cube3[1]:=cube3[1]+1;cube3[2]:=cube3[2]+1;cube4[2]:=cube4[2]+2;do2;end;
end;
3:case c mod 2 of
0:begin if board[cube1[1]+1,cube1[2]+1]then continue;
if board[cube3[1]-1,cube3[2]-1]then continue;if board[cube4[1]-2,cube4[2]-2]then continue;
do1;cube1[1]:=cube1[1]+1;cube1[2]:=cube1[2]+1;cube3[1]:=cube3[1]-1;cube3[2]:=cube3[2]-1;
cube4[2]:=cube4[2]-2;cube4[1]:=cube4[1]-2;do2;end;
1:begin if board[cube1[1]-1,cube1[2]-1]then continue;
if board[cube3[1]+1,cube3[2]+1]then continue;if board[cube4[1]+2,cube4[2]+2]then continue;
do1;cube1[1]:=cube1[1]-1;cube1[2]:=cube1[2]-1;cube3[1]:=cube3[1]+1;cube3[2]:=cube3[2]+1;
cube4[2]:=cube4[2]+2;cube4[1]:=cube4[1]+2;do2;end;
end;
4:case c mod 4 of
0:begin if board[cube1[1]-1,cube1[2]-1]then continue;if board[cube2[1],cube2[2]+2]then continue;
if board[cube3[1]-1,cube3[2]+1]then continue;if board[cube4[1]-2,cube4[2]]then continue;
do1;cube1[1]:=cube1[1]-1;cube1[2]:=cube1[2]-1;cube2[2]:=cube2[2]+2;cube3[1]:=cube3[1]-1;
cube3[2]:=cube3[2]+1;cube4[1]:=cube4[1]-2;do2;end;
1:begin if board[cube1[1]+1,cube1[2]-1]then continue;if board[cube2[1]-2,cube2[2]]then continue;
if board[cube3[1]-1,cube3[2]-1]then continue;if board[cube4[1],cube4[2]-2]then continue;
do1;cube1[1]:=cube1[1]+1;cube1[2]:=cube1[2]-1;
cube2[1]:=cube2[1]-2;cube3[1]:=cube3[1]-1;cube3[2]:=cube3[2]-1;cube4[2]:=cube4[2]-2;do2;end;

2:begin if board[cube1[1]+1,cube1[2]+1]then continue;if board[cube2[1],cube2[2]-2]then continue;
if board[cube3[1]+1,cube3[2]-1]then continue;if board[cube4[1]+2,cube4[2]]then continue;
do1;cube1[1]:=cube1[1]+1;cube1[2]:=cube1[2]+1;
cube2[2]:=cube2[2]-2;cube3[1]:=cube3[1]+1;cube3[2]:=cube3[2]-1;cube4[1]:=cube4[1]+2;do2;end;
3:begin if board[cube1[1]-1,cube1[2]+1]then continue;if board[cube2[1]+2,cube2[2]]then continue;
if board[cube3[1]+1,cube3[2]+1]then continue;if board[cube4[1],cube4[2]+2]then continue;
do1;cube1[1]:=cube1[1]-1;cube1[2]:=cube1[2]+1;
cube2[1]:=cube2[1]+2;cube3[1]:=cube3[1]+1;cube3[2]:=cube3[2]+1;cube4[2]:=cube4[2]+2;do2;end;
end;
5:case c mod 2 of
0:begin if board[cube2[1]+1,cube2[2]+1]then continue;
if board[cube3[1]-1,cube3[2]+1]then continue;if board[cube4[1]-2,cube4[2]]then continue;
do1;cube2[1]:=cube2[1]+1;cube2[2]:=cube2[2]+1;cube3[1]:=cube3[1]-1;cube3[2]:=cube3[2]+1;cube4[1]:=cube4[1]-2;do2;end;
1:begin if board[cube2[1]-1,cube2[2]-1]then continue;
if board[cube3[1]+1,cube3[2]-1]then continue;if board[cube4[1]+2,cube4[2]]then continue;
do1;cube2[1]:=cube2[1]-1;cube2[2]:=cube2[2]-1;cube3[1]:=cube3[1]+1;cube3[2]:=cube3[2]-1;cube4[1]:=cube4[1]+2;do2;end;
end;
6:case c mod 4 of
0:begin if board[cube1[1]+1,cube1[2]+1]then continue;if board[cube2[1],cube2[2]+2]then continue;
if board[cube3[1]-1,cube3[2]+1]then continue;if board[cube4[1]-2,cube4[2]]then continue;
do1;cube1[1]:=cube1[1]+1;cube1[2]:=cube1[2]+1;cube2[2]:=cube2[2]+2;cube3[1]:=cube3[1]-1;
cube3[2]:=cube3[2]+1;cube4[1]:=cube4[1]-2;do2;end;
1:begin if board[cube1[1]-1,cube1[2]+1]then continue;if board[cube2[1]-2,cube2[2]]then continue;
if board[cube3[1]-1,cube3[2]-1]then continue;if board[cube4[1],cube4[2]-2]then continue;
do1;cube1[1]:=cube1[1]-1;cube1[2]:=cube1[2]+1;
cube2[1]:=cube2[1]-2;cube3[1]:=cube3[1]-1;cube3[2]:=cube3[2]-1;cube4[2]:=cube4[2]-2;do2;end;
2:begin if board[cube1[1]-1,cube1[2]-1]then continue;if board[cube2[1],cube2[2]-2]then continue;
if board[cube3[1]+1,cube3[2]-1]then continue;if board[cube4[1]+2,cube4[2]]then continue;
do1;cube1[1]:=cube1[1]-1;cube1[2]:=cube1[2]-1;
cube2[2]:=cube2[2]-2;cube3[1]:=cube3[1]+1;cube3[2]:=cube3[2]-1;cube4[1]:=cube4[1]+2;do2;end;
3:begin if board[cube1[1]+1,cube1[2]-1]then continue;if board[cube2[1]+2,cube2[2]]then continue;
if board[cube3[1]+1,cube3[2]+1]then continue;if board[cube4[1],cube4[2]+2]then continue;
do1;cube1[1]:=cube1[1]+1;cube1[2]:=cube1[2]-1;
cube2[1]:=cube2[1]+2;cube3[1]:=cube3[1]+1;cube3[2]:=cube3[2]+1;cube4[2]:=cube4[2]+2;do2;end;
end;
7:case c mod 2 of
0:begin if board[cube2[1]-1,cube2[2]-1]then continue;
if board[cube3[1]-1,cube3[2]+1]then continue;if board[cube4[1],cube4[2]+2]then continue;
do1;cube2[1]:=cube2[1]-1;cube2[2]:=cube2[2]-1;cube3[1]:=cube3[1]-1;cube3[2]:=cube3[2]+1;cube4[2]:=cube4[2]+2;do2;end;
1:begin if board[cube2[1]+1,cube2[2]+1]then continue;
if board[cube3[1]+1,cube3[2]-1]then continue;if board[cube4[1],cube4[2]-2]then continue;
do1;cube2[1]:=cube2[1]+1;cube2[2]:=cube2[2]+1;cube3[1]:=cube3[1]+1;cube3[2]:=cube3[2]-1;cube4[2]:=cube4[2]-2;do2;end;
end;
end;
inc(c);
end;
end;
until false;
end.
program project1;
{$APPTYPE GUI}
{$MODE DELPHI}
uses Windows, Messages, SysUtils,strings;
const AppName = 'ET_PureObjectPascalWindow';
function WindowProc(Window: HWND;
AMessage: UINT; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall; export;
var dc: HDC;
ps: TPaintStruct;
r: TRect;
begin Result := 0;
case AMessage of
WM_PAINT: begin
dc := BeginPaint(Window, ps);
GetClientRect(Window, r);
DrawText(dc, '不要按下鼠标', -1, r, DT_SINGLELINE or DT_CENTER or DT_VCENTER);
EndPaint(Window, ps);
end;
WM_DESTROY: begin
PostQuitMessage(0);
end;
WM_LBUTTONDOWN:Begin {按下鼠标左键的消息}
MessageBox(0, '叫你不要按你还按!', nil, mb_Ok);
Exit;
End;
else Result := DefWindowProc(Window, AMessage, WParam, LParam);
end;
end;
Function WinRegister: Boolean;
var
WindowClass : WndClass;
Begin
With WindowClass Do
Begin
Style := cs_hRedraw Or cs_vRedraw;
lpfnWndProc := WndProc(@WindowProc);
cbClsExtra := 0;
cbWndExtra := 0;
hInstance := system.MainInstance;
hIcon := LoadIcon (0,idi_Application);
hCursor := LoadCursor (0,idc_Arrow);
hbrBackground := GetStockObject(GRAY_BRUSH);
lpszMenuName := Nil;
lpszClassName := AppName;
End;
WinRegister := RegisterClass (WindowClass)<>0;
End;
function WinCreate: HWND;
var hWindow: HWND;
begin
hWindow := CreateWindow(AppName, '看到了吗', WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,0, 0, MainInstance, nil);
if hWindow <> 0 then
begin
ShowWindow(hWindow, CmdShow);
ShowWindow(hWindow, SW_SHOW);
UpdateWindow(hWindow);
end;
Result := hWindow;
end;
var AMessage: TMsg;
hWindow: HWND;
begin
if not WinRegister then begin
MessageBox(0, 'WinRegister failed', nil, MB_OK);
Exit;
end;
hWindow := WinCreate;
if LongInt(hWindow) = 0 then begin
MessageBox(0, 'WinCreate failed', nil, MB_OK);
Exit;
end;
while GetMessage(AMessage, 0, 0, 0) do
begin
TranslateMessage(AMessage);
DispatchMessage(AMessage);
end;
Halt(AMessage.wParam);
end.

不要想你能为世界做什么,想想你该为世界做什么!
原文地址:https://www.cnblogs.com/Chaobs/p/3837521.html