1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
uses
WinCrt, WinGraph, WinMouse;
const
cBallDiameter = 33;
cPaddleWidth = 65;
cPaddleHeight = 16;
cPaddleAreaHeight = 32;
cBallAreaHeight = 481;
cPaddleY = cBallAreaHeight;
cScreenWidth = 321;
cScreenHeight = cBallAreaHeight + cPaddleAreaHeight;
cMinPaddleCenterX = cPaddleWidth div 2;
cMaxPaddleCenterX = Pred(cScreenWidth) - cPaddleWidth div 2;
var
vBitmap: pointer;
vBallX, vBallY: integer;
vMoveX, vMoveY: integer;
vPaddleCenterX: integer;
vGameOver: boolean;
procedure LoadImage;
var
vSize: longint;
vFile: file;
begin
vSize := ImageSize(1, 1, cBallDiameter, cBallDiameter);
GetMem(vBitmap, vSize);
{$I-}
Assign(vFile, 'balle.bmp');
Reset(vFile, 1);
{$I+}
if (IOResult = 0) and (FileSize(vFile) = vSize) then
begin
BlockRead(vFile, vBitmap^, vSize);
Close(vFile);
end;
end;
procedure FreeImage;
begin
FreeMem(vBitmap);
end;
function PaddleStartX: integer;
begin
result := vPaddleCenterX - cPaddleWidth div 2;
end;
function PaddleEndX: integer;
begin
result := vPaddleCenterX + cPaddleWidth div 2;
end;
procedure SetBallPosition;
var
vBallCenterX: integer;
begin
vBallCenterX := vBallX + cBallDiameter div 2;
if (vBallX + vMoveX < 0) or (vBallX + vMoveX > cScreenWidth - cBallDiameter) then
vMoveX := -vMoveX;
if (vBallY + vMoveY < 0) then
vMoveY := -vMoveY
else
if (vBallY + vMoveY > cBallAreaHeight - cBallDiameter) then
if (vBallCenterX >= PaddleStartX) and (vBallCenterX <= PaddleEndX) then
vMoveY := -vMoveY
else
vGameOver := true;
Inc(vBallX, vMoveX);
Inc(vBallY, vMoveY);
end;
procedure DrawBall;
begin
PutImage(vBallX, vBallY, vBitmap^, OrPut);
end;
procedure EraseBall;
begin
SetFillStyle(SolidFill, Black);
Bar(vBallX, vBallY, Pred(vBallX + cBallDiameter), Pred(vBallY + cBallDiameter));
end;
procedure SetPaddlePosition;
var
x: integer;
begin
x := GetMouseX;
if x < cMinPaddleCenterX then
vPaddleCenterX := cMinPaddleCenterX
else
if x > cMaxPaddleCenterX then
vPaddleCenterX := cMaxPaddleCenterX
else
vPaddleCenterX := x;
end;
procedure DrawPaddle;
begin
SetFillStyle(SolidFill, White);
Bar(PaddleStartX, cPaddleY, PaddleEndX, Pred(cPaddleY + cPaddleHeight));
end;
procedure ErasePaddle;
begin
SetFillStyle(SolidFill, Black);
Bar(0, cPaddleY, Pred(cScreenWidth), Pred(cPaddleY + cPaddleHeight));
end;
procedure CreateWindow;
var
vDriver, vMode: smallint;
begin
vDriver := NoPalette;
vMode := mCustom;
SetWindowSize(cScreenWidth, cScreenHeight);
InitGraph(vDriver, vMode, 'WinGraph Pong');
end;
begin
vBallX := 32;
vBallY := 32;
vMoveX := 4;
vMoveY := 4;
vPaddleCenterX := cScreenWidth div 2;
vGameOver := false;
CreateWindow;
LoadImage;
DrawBall;
DrawPaddle;
UpdateGraph(UpdateOff);
SetMouseXY(vPaddleCenterX, cPaddleY);
Delay(1000);
repeat
Delay(1);
ErasePaddle;
SetPaddlePosition;
DrawPaddle;
EraseBall;
SetBallPosition;
DrawBall;
UpdateGraph(UpdateNow);
until KeyPressed or vGameOver;
if vGameOver then
Delay(2000);
FreeImage;
CloseGraph;
end. |
Partager