Drawing a chessboard
Posted: Fri Jan 16, 2009 1:35 am
Hello i am going crazy trying to draw a chessboard using loop ...
can anyone help please!!!!
thanks
can anyone help please!!!!
thanks
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
For t = 0 To 7
Out$ = ""
For n = 0 To 7
Odd = (n+t) % 2
If Odd
Out$ + "X"
Else
Out$ + "O"
EndIf
Next
Debug Out$
Next
Code: Select all
CreateImage(0, 256, 256)
StartDrawing(ImageOutput(0))
For t = 0 To 7
For n = 0 To 7
Odd = (n+t) % 2
If Odd
Col = $FF0000
Else
Col = $00FFFF
EndIf
Box(32*n, 32*t, 32, 32, Col )
Next
Next
StopDrawing()
OpenWindow( 0, 0,0, 256,256, "Checkers")
ImageGadget( 0, 0,0, 256, 256, ImageID(0) )
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Code: Select all
ProcedureDLL DrawChessBoard(flip)
; netmaestro November 2006
dark = RGB(121,141,167)
light = RGB(239,239,231)
LoadFont(0, "Arial", 12, #PB_Font_Bold|#PB_Font_HighQuality)
img_board = CreateImage(#PB_Any, 580, 580, 32)
StartDrawing(ImageOutput(img_board))
Box(0,0,580,580,light)
Box(24,24,532,532,dark)
Box(29,29,522,522,light)
Box(33,33,514,514,dark)
For j=34 To 482 Step 128
For i = 34 To 482 Step 128
Box(i,j,64,64,light):Box(i+64,j,64,64,dark)
Box(i,j+64,64,64,dark):Box(i+64,j+64,64,64,light)
Next
Next
DrawingFont(FontID(0))
Select flip
Case 1
DrawText(0,3, " H G F E D C B A",dark,light)
DrawText(0,558," H G F E D C B A",dark,light)
Case 0
DrawText(0,3, " A B C D E F G H",dark,light)
DrawText(0,558," A B C D E F G H",dark,light)
EndSelect
If flip=0: cc=9 : Else : cc=0 : EndIf
For i=59 To 508 Step 64
If flip=0:cc-1:Else:cc+1:EndIf
DrawText(8,i,Chr(48+cc),dark,light)
DrawText(561,i,Chr(48+cc),dark,light)
Next
StopDrawing()
ProcedureReturn ImageID(img_board)
EndProcedure
;test...
OpenWindow(0,0,0,580,580,"",$CF0001)
ImageGadget(0,0,0,0,0,DrawChessBoard(0))
Repeat:Until WaitWindowEvent()=#WM_CLOSE
Code: Select all
For y=0 To 7
For x=0 To 7
a=((x+y+1)&1)*255
Plot(x,y,RGB(a,a,a))
Next
Next
Code: Select all
For y=0 To 7
For x=0 To 7
a=(~x!y&1)*255
Plot(x,y,RGB(a,a,a))
Next
Next
Code: Select all
Structure PointF : X.F : Y.F : EndStructure
Structure PF4 : P.PointF[4] : EndStructure
nSteps=8 ; try different nSteps
VX.PF4
With VX ;position CLOCKWISE for chessboard corners
\P[0]\X=200 : \P[0]\Y=120
\P[1]\X=500 : \P[1]\Y=60
\P[2]\X=600 : \P[2]\Y=400
\P[3]\X=100 : \P[3]\Y=450
EndWith
Procedure LineStep(n,X1,Y1,X2,Y2,nSteps,*PF.PointF)
;Get position of step n from line x1,y1,x2,y2 divided in nSteps
*PF\X=X1+(X2-X1)/nSteps*n
*PF\Y=Y1+(Y2-Y1)/nSteps*n
EndProcedure
Procedure GetPolygPix(StepX,StepY,nSteps,*VX.PF4,*PF.PointF)
; Get position of pixel StepX,StepY from polygon with 4 vertex in *VX divided in nSteps
With *VX
LineStep(StepX,\P[0]\X,\P[0]\Y,\P[1]\X,\P[1]\Y,nSteps,P1.PointF)
LineStep(StepX,\P[3]\X,\P[3]\Y,\P[2]\X,\P[2]\Y,nSteps,p2.PointF)
LineStep(StepY,P1\X,P1\Y,p2\X,p2\Y,nSteps,*PF)
EndWith
EndProcedure
Procedure GetSubPolyg(StepX,StepY,nSteps,*VX.PF4,*PF.PF4)
;get 4 Vertex of subpolygon starting at StepX,StepY from polygon with 4 vertex in *VX divided in nSteps
With *PF
GetPolygPix(StepX,StepY,nSteps,*VX,\P[0])
GetPolygPix(StepX+1,StepY,nSteps,*VX,\P[1])
GetPolygPix(StepX+1,StepY+1,nSteps,*VX,\P[2])
GetPolygPix(StepX,StepY+1,nSteps,*VX,\P[3])
EndWith
EndProcedure
Procedure DrawPolyg4(*VX.PF4,RimRGB,BkRGB=-1)
With *VX
LineXY(\P[0]\X,\P[0]\Y,\P[1]\X,\P[1]\Y,RimRGB)
LineXY(\P[1]\X,\P[1]\Y,\P[2]\X,\P[2]\Y,RimRGB)
LineXY(\P[2]\X,\P[2]\Y,\P[3]\X,\P[3]\Y,RimRGB)
LineXY(\P[3]\X,\P[3]\Y,\P[0]\X,\P[0]\Y,RimRGB)
If BkRGB>-1
GetPolygPix(1,1,2,*VX,Pf.PointF)
FillArea(Pf\X,Pf\Y, RimRGB, BkRGB)
EndIf
EndWith
EndProcedure
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
If OpenWindow(0, 100, 100,700,500 ,"Checkerboard", #WS_OVERLAPPEDWINDOW |1)
AddKeyboardShortcut(0, #PB_Shortcut_Escape, #PB_Shortcut_Escape)
_Img=CreateImage(-1,WindowWidth(0),WindowHeight(0),32)
_ImGad=ImageGadget(-1,0,0,0,0,ImageID(_Img))
StartDrawing(ImageOutput(_Img))
Pf.PF4
RimRGB=$080598
Blacks= $003300
Whites=$99FFFF
For i=0 To nSteps-1
For j=0 To nSteps-1
GetSubPolyg(i,j,nSteps,VX,Pf)
If (j+i)&1 : RGB=Blacks
Else : RGB=Whites
EndIf
DrawPolyg4(Pf,RimRGB,RGB)
Next
Next
DrawPolyg4(VX ,RimRGB)
StopDrawing()
SetGadgetState(_ImGad,ImageID(_Img))
Repeat
Ev=WaitWindowEvent()
If Ev=#PB_Event_Menu And EventMenu()=#PB_Shortcut_Escape:End:EndIf
Until Ev=#PB_Event_CloseWindow
EndIf