Мини оболочка для шахмат
Данный пример может стать первым шагом по созданию оболочки для шахмат. По сути программа умеет рисовать фигуры и перемещать их с помощью мыши.
Платформа: Windows, Linux
Автор: BasicCoder2
#INCLUDE "fbgfx.bi" Using fb Screenres 640,480,32 Dim Shared As Integer mx,my,btn,ox,oy,dx,dy Dim Shared As Integer opx,opy Type chessMove x1 As Integer x2 As Integer y1 As Integer y2 As Integer t1 As Integer t2 As Integer End Type Dim Shared As Integer board(12,12) 'internal representation of board Dim Shared As Integer exitFlag 'exit program Dim Shared As Integer bx,by 'position top/left of board Dim Shared As FB.Image Ptr sImage sImage = Imagecreate( 42, 42, 0 ) 'save screen data 'THIS DETERMINES WHERE TO PLACE BOARD IN WINDOW bx = 130 by = 30 Dim Shared As Integer px,py 'selected square of board 'read board For j As Integer = 0 To 11 For i As Integer = 0 To 11 Read board(i,j) Next i Next j '12 images Dim Shared As FB.Image Ptr iPiece(12) 'allocate for 12 images For i As Integer = 0 To 11 iPiece(i) = Imagecreate(42,42,0) Next i Dim As String datum 'copy images from data statements to screen For i As Integer = 0 To 5 'for each piece For y As Integer = 0 To 41 'for each row Read datum For x As Integer = 0 To 41 'for each column If Mid(datum,x+1,1)="." Then Pset(x+i*42,y),Rgb(255,0,255) Pset(x+i*42+252,y),Rgb(255,0,255) End If If Mid(datum,x+1,1)="#" Then Pset(x+i*42,y),Rgb(0,0,0) Pset(x+i*42+252,y),Rgb(0,0,0) End If If Mid(datum,x+1,1)="*" Then Pset(x+i*42,y),Rgb(100,100,150) Pset(x+i*42+252,y),Rgb(255,200,200) End If Next x Next y Next i 'get set of 12 images For i As Integer = 0 To 11 Get (i*42,0)-(i*42+41,41), iPiece(i) Next i Sub update() Screenlock() 'drawBoard Color Rgb(255,255,255),Rgb(100,255,100) Cls Dim As Integer shade shade = 1 For y As Integer = 0 To 7 shade = -shade For x As Integer = 0 To 7 If shade = -1 Then Line (bx+x*42,by+y*42)-(bx+x*42+41,by+y*42+41),Rgb(252,206,156),bf Else Line (bx+x*42,by+y*42)-(bx+x*42+41,by+y*42+41),Rgb(179,110,44),bf End If shade = -shade Next x Next y 'border of board Line (bx-1,by-1)-(bx+336,by+336),Rgb(0,0,0),b 'now draw image on square For y As Integer = 0 To 7 For x As Integer = 0 To 7 'put image onto square If board(x+2,y+2)<>7 And board(x+2,y+2)<>0 Then If board(x+2,y+2)<0 Then Put (bx+x*42,by+y*42),iPiece(Abs(board(x+2,y+2))+5),Trans Else Put (bx+x*42,by+y*42),iPiece(Abs(board(x+2,y+2))-1),Trans End If End If Next x Next y 'draw exit button Line (144,385)-(193,408),Rgb(255,255,255),bf Line (144,385)-(193,408),Rgb(0,0,0),b Locate 50,20 Color Rgb(0,0,0),Rgb(255,255,255) Print "EXIT" Screenunlock() End Sub Function getMove() As chessMove Dim As Integer pImage 'number of selected piece Dim As chessMove move 'wait for button press Getmouse mx,my,,btn While btn<>1 Getmouse mx,my,,btn Wend ox = mx oy = my 'is mouse over the board? If mx>bx And mx<bx+336 And my>by And my<by+336 And btn=1 Then 'over which square? px = Int((mx-bx)/42) py = Int((my-by)/42) 'does this square have a piece? If board(px+2,py+2)<>7 And board(px+2,py+2)<>0 Then move.x1 = px move.y1 = py If board(px+2,py+2)<0 Then 'black piece pImage = Abs(board(px+2,py+2))+5 'get image type Else pImage = Abs(board(px+2,py+2))-1 'get image type End If board(px+2,py+2)=0 'erase data px = bx + px * 42 py = by + py * 42 Line (px,py)-(px+41,py+41),Rgb(255,0,0),bf 'clear 'save background Get (px,py)-(px+41,py+41),sImage 'print piece Put (px,py),iPiece(pImage),Trans opx = px 'opx and opy are sprite top/left coordinates opy = py While btn = 1 Getmouse mx,my,,btn 'compute movement to add to sprite top/left opx,opy dx = mx-ox dy = my-oy If dx<>0 Or dy<>0 Then 'mouse has moved update data px = px+dx py = py+dy Put (opx,opy),sImage,Pset 'restore old back ground Get (px,py)-(px+41,py+41),sImage 'save new back ground Put (px,py),iPiece(pImage),Trans 'place on new back ground ox = mx oy = my opx = px opy = py End If Wend 'compute final square px = Int((mx-bx)/42) py = Int((my-by)/42) move.x2 = px move.y2 = py pImage = pImage+1 If pImage>6 Then pImage = 6-pImage board(move.x2+2,move.y2+2) = pImage update() End If End If 'is mouse over exit button If mx>144 And mx<193 And my>385 And my<408 And btn=1 Then Line (144,385)-(193,408),Rgb(255,255,0),b exitFlag = 1 End If 'wait for button release While btn=1 Getmouse mx,my,,btn Wend Return move End Function 'convert string chess moves to numbers Function strMoveToNumMove(move As String) As chessMove Dim As chessMove cm move = Ucase(move) cm.x1 = Asc(Mid(move,1,1))-65 cm.y1 = Val(Mid(move,2,1)) cm.x2 = Asc(Mid(move,3,1))-65 cm.y2 = Val(Mid(move,4,1)) Return cm End Function 'convert chess move numbers to string format Function numMovetoStrMove(cm As chessMove) As String Return Chr(cm.x1+65)+Str(cm.y1)+Chr(cm.x2+65)+Str(cm.y2) End Function '*************** MAIN LOOP ************************ exitFlag = 0 Dim As chessMove lastMove update() Do lastMove = getMove() update() Locate 55,10 Print "Last Move was: ";numMovetoStrMove(lastMove) Print lastMove.x1;" ";lastMove.y1;" ";lastMove.x2;" ";lastMove.y2 Loop Until exitFlag = 1 '*************************************************** ' 1 = pawn, 2 = knight, 3 = bishop, 4 = rook, 5 = queen, 6 = king, 7 = border ' black pieces given negative value, sgn() returns -1 for black and +1 for white ' 0 1 2 3 4 5 6 7 8 9 10 11 <--- internal coordinates ' A B C D E F G H <--- display coordinates Data 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 '0 Data 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 '1 Data 7, 7, 2, 3, 4, 5, 6, 4, 3, 2, 7, 7 '2 8 Data 7, 7, 1, 1, 1, 1, 1, 1, 1, 1, 7, 7 '3 7 Data 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7 '4 6 Data 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7 '5 5 Data 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7 '6 4 Data 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7 '7 3 Data 7, 7,-1,-1,-1,-1,-1,-1,-1,-1, 7, 7 '8 2 Data 7, 7,-2,-3,-4,-5,-6,-4,-3,-2, 7, 7 '9 1 Data 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 '10 Data 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 '11 Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data "....................####.................." Data "...................#****#................." Data "..................#******#................" Data ".................#********#..............." Data ".................#********#..............." Data ".................#********#..............." Data ".................#********#..............." Data "..................#******#................" Data "...................#****#................." Data "...................######................." Data "..................#******#................" Data "................##********##.............." Data "...............#************#............." Data "..............#**************#............" Data "..............################............" Data ".................#********#..............." Data ".................#********#..............." Data ".................#********#..............." Data "................#**********#.............." Data "................#**********#.............." Data "...............#************#............." Data "..............#**************#............" Data ".............#****************#..........." Data "............#******************#.........." Data "............#******************#.........." Data "............####################.........." Data "...........#********************#........." Data "...........#********************#........." Data "...........######################........." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data "...........#####..######..#####..........." Data "...........#***#..#****#..#***#..........." Data "...........#***#..#****#..#***#..........." Data "...........#***#..#****#..#***#..........." Data "...........#***####****####***#..........." Data "...........#******************#..........." Data "...........#******************#..........." Data "...........#******************#..........." Data "...........#******************#..........." Data "...........####################..........." Data "............#****************#............" Data ".............################............." Data ".............#****#****#****#............." Data ".............#****#****#****#............." Data ".............################............." Data ".............#**#****#****#*#............." Data ".............#**#****#****#*#............." Data ".............################............." Data ".............#****#****#****#............." Data ".............#****#****#****#............." Data ".............################............." Data ".............#**#****#****#*#............." Data ".............#**#****#****#*#............." Data "............##################............" Data "...........#******************#..........." Data "..........#********************#.........." Data "..........#********************#.........." Data ".........#**********************#........." Data ".........########################........." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data "....................#....................." Data "...................##....................." Data "..................#**#...................." Data "..................#**###.................." Data ".................#******###..............." Data "................#**********#.............." Data "................#***********#............." Data "...............#**#*********#............." Data "..............#**##**********#............" Data ".............#**##.#*********#............" Data ".............#**###***********#..........." Data "............#*****************#..........." Data "............#*****************#..........." Data "...........#******************#..........." Data "...........#*******************#.........." Data "..........#******#####*********#.........." Data "..........#******#..#**********#.........." Data ".........#*******#..#**********#.........." Data ".........#******#..#***********#.........." Data "..........#****#..#***********#..........." Data "..........#####..#************#..........." Data "................#*************#..........." Data "...............#*************#............" Data "...............#*************#............" Data "..............#***************#..........." Data "..............#***************#..........." Data ".............#*****************#.........." Data ".............#******************#........." Data "............#*******************#........." Data "............######################........" Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data "....................###..................." Data "...................#***#.................." Data "..................#*****#................." Data "..................#*****#................." Data "..................#*****#................." Data "...................#***#.................." Data "....................#*#..................." Data "...................#***#.................." Data "..................#*****#................." Data ".................#******#................." Data ".................#******#................." Data "................#******#.................." Data "................#******#.#................" Data "...............#******#..##..............." Data "...............#******#.#*#..............." Data "..............#******#..#**#.............." Data "..............#******#.#***#.............." Data ".............#******#..#****#............." Data ".............#******#.#*****#............." Data ".............#*****#..#*****#............." Data ".............#******##******#............." Data ".............#**************#............." Data ".............#**************#............." Data "..............#************#.............." Data "..............#************#.............." Data "..............#************#.............." Data "..............#************#.............." Data "...............#**********#..............." Data ".............###**********###............." Data "...........##****************##..........." Data "...........#******************#..........." Data "..........######################.........." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".....................#...................." Data "....................#*#..................." Data "...................#***#.................." Data "..................#*****#................." Data "..................#*****#................." Data "..................#*****#................." Data "............#.....#*****#.....#..........." Data "...........#*#.....#***#.....#*#.........." Data "..........#***#....#***#....#***#........." Data "..........#***#....#***#....#***#........." Data "...........#*#.....#***#.....#*#.........." Data "...........#*#.....#***#.....#*#.........." Data "..........#***#...#*****#...#***#........." Data "..........#***#..#*******#..#***#........." Data "..........#***#..#*******#..#***#........." Data ".....##...#***#...#*****#...#***#...##...." Data ".....#*#..#***#...#*****#...#***#..#*#...." Data ".....#**#.#***#...#*****#...#***#.#**#...." Data ".....#**#.#***#...#*****#...#***#.#**#...." Data "......#*#.#***#...#*****#...#***#.#*#....." Data "......#**##***#...#*****#...#***##**#....." Data "......#**#*****#.#*******#.#*****#**#....." Data ".......#*******##*********##*******#......" Data ".......#***************************#......" Data "........#*************************#......." Data "........###########################......." Data "........#*************************#......." Data "........#*************************#......." Data "........#*************************#......." Data "........#*************************#......." Data "........#*************************#......." Data "........###########################......." Data ".......#***************************#......" Data ".......#***************************#......" Data ".......#############################......" Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data ".........................................." Data "....................###..................." Data "....................#*#..................." Data "..................###*###................." Data "..................#*****#................." Data "..................###*###................." Data "....................#*#..................." Data "....................#*#..................." Data "...................#####.................." Data "..................#*****#................." Data "..................#*****#................." Data "..................#*****#................." Data ".......#########...#***#...#########......" Data "......#*********###########*********#....." Data ".....#**#######*****#*#*****#######**#...." Data ".....#*#.......######*######.......#*#...." Data ".....#*#..####......#*#......####..#*#...." Data ".....#*#.#****###...#*#...###****#.#*#...." Data ".....#*##********##.#*#.##********##*#...." Data ".....#*#***********##*##***********#*#...." Data ".....#*#************#*#************#*#...." Data ".....#*#*****####***#*#***####*****#*#...." Data ".....#*#****#****##*#*#*##****#****#*#...." Data "......#*****#******##*##******#*****#....." Data ".......#****#*******#*#*******#****#......" Data ".......#****#*******#*#*******#****#......" Data "........#****#******#*#******#****#......." Data "........#****#******#*#******#****#......." Data ".........#****#*****#*#*****#****#........" Data ".........#*****#****#*#****#*****#........" Data "..........#*****#***#*#***#*****#........." Data "..........#######################........." Data "..........#*********************#........." Data "..........#######################........." Data ".........##*********************##........" Data "........##***********************##......." Data "........###########################......." Data ".........................................." Data ".........................................." Data ".........................................."