| 那个五子棋的显示棋盘和棋子用display_img,画方框用putpixel: 举个鼠标移动时重画棋盘和边框的例子:
 SDL_MOUSEMOTION:
 begin
 if (event.button.y > boardy + 2) and (event.button.y < boardy + 400 - 2) and (event.button.x > boardx + 2) and (event.button.x < boardx + 400 - 2) then
 begin
 Fx := (event.button.x - boardx) * 2 div 53;
 Fy := (event.button.y - boardy) * 2 div 53;
 if (Fx <> tempx) or (Fy <> tempy) then
 begin
 tempx := Fx;
 tempy := Fy;
 DrawSquare(tempx, tempy);
 DrawBoard;
 event.key.keysym.sym := 0;
 event.button.button := 0;
 end;
 end;
 
 上面是先用的DrawSquare,再用DrawBoard。我开始的时候写的是先用DarwBoard再用DrawSquare,结果边框总是显示的是上一次画的位置,而调整顺序后就好了,不知道为什么?
 
 其中DrawBoard和DrawSquare是自己定义的procedure:
 
 procedure DrawSquare(x, y: integer);
 var
 i: integer;
 begin
 for i := 0 to 27 do
 begin
 putpixel(screen, x * 53 div 2 + boardx + i, y * 53 div 2 + boardy, colcolor($64));
 putpixel(screen, x * 53 div 2 + boardx + i, y * 53 div 2 + boardy + 28, colcolor($64));
 putpixel(screen, x * 53 div 2 + boardx, y * 53 div 2 + boardy + i, colcolor($64));
 putpixel(screen, x * 53 div 2 + boardx + 28, y * 53 div 2 + boardy + i, colcolor($64));
 end;
 end;
 
 procedure DrawBoard;
 var
 ix, iy: integer;
 begin
 SDL_UpdateRect(screen, boardx, boardy, boardx + 400, boardy + 400);
 display_img('resource\board.png', boardx, boardy);
 for ix := 0 to maxX do
 begin
 for iy := 0 to maxY do
 begin
 if chess[ix][iy] > 0 then
 DrawChess(ix, iy, chess[ix][iy] - 1);
 end;
 end;
 end;
 
 DrawBoard中的DrawChess也是自己定义的:
 
 procedure DrawChess(x, y, mode: integer);
 var
 chx, chy: integer;
 str: string;
 begin
 chx := x * 53 div 2 + boardx;
 chy := y * 53 div 2 + boardy;
 str := 'resource\chess' + inttostr(mode) + '.png';
 if fileexists(str) then
 display_img(@str[1], chx, chy);
 chess[x][y] := mode + 1;
 end;
 
 在DrawBoard中那句SDL_UpdateRect,其中有一个参数是boardx + 400,如果改成boardx + 401棋盘就不能显示出来了,也不知道为什么?(400是是棋盘图片的宽度)
 
 补充:
 文件开头新定义了一些
 chess: array of array of integer;
 Fx, Fy, boardx, boardy, maxX, maxY: integer;
 
 
 
 [ 本帖最后由 真正的强强 于 2009-5-14 19:52 编辑 ]
 |