Page 1 sur 1

Dessiner un damier

Publié : ven. 03/févr./2012 16:29
par Guimauve
Bonjour à tous,

Une petite commande de dessin d'un damier. J'en ai eu besoin pour un projet sur lequel je travaille en ce moment.

A+
Guimauve

Code : Tout sélectionner

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Nom du projet : DrawChecker
; Nom du fichier : DrawChecker.pb
; Version du fichier : 1.0.1
; Programmation : OK
; Programmé par : Guimauve
; Correction par : Le Soldat Inconnu
; Date : 29-01-2012
; Mise à jour : 03-02-2012
; Code PureBasic : 4.61
; Plateforme : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Procedure DrawChecker(ImageID, Size.w, BoxCount.l, Color00.l, Color01.l) 
  
  If CreateImage(ImageID, Size, Size) 
    
    If StartDrawing(ImageOutput(ImageID)) 
      
      BoxSize.l = Size / BoxCount 
      
      For LineCount = 0 To BoxCount 
        
        For RowCount = 0 To BoxCount 
          
          If (LineCount + RowCount) & %1 = 1 
            Box(x, y, BoxSize, BoxSize, Color01) 
          Else 
            Box(x, y, BoxSize, BoxSize, Color00) 
          EndIf 
          
          x + BoxSize 
          
        Next 
        
        y + BoxSize 
        x = 0 
        
      Next 
      
      StopDrawing() 
      
    EndIf 
    
  EndIf 
  
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< !!! ATTENTION - CODE D'ESSAI !!! <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

#Size = 512

If OpenWindow(0, 0, 0, #Size + 10, #Size + 10, "DrawChecker", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  DrawChecker(0, #Size, 8, RGB(255,255,000), RGB(000,000,000))
  ImageGadget(0, 5, 5, #Size, #Size, ImageID(0))
  
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf

; <<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< FIN DU FICHIER <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<

Re: Dessiner un damier

Publié : ven. 03/févr./2012 17:09
par Ar-S
Sympa ça, merci Guimauve

Re: Dessiner un damier

Publié : ven. 03/févr./2012 17:11
par GallyHC
ouep par moment c'est les codes simple qui donne les meilleurs idées :) (enfin perso cela m'en donne).

Merci,
GallyHC

Re: Dessiner un damier

Publié : ven. 03/févr./2012 21:46
par Le Soldat Inconnu
Sauf que ton code ne fonctionne pas pour un damier de 9*9 par exemple

Je te propose cette correction

Code : Tout sélectionner

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Nom du projet : DrawChecker
; Nom du fichier : DrawChecker.pb
; Version du fichier : 1.0.0
; Programmation : OK
; Programmé par : Guimauve
; Date : 29-01-2012
; Mise à jour : 03-02-2012
; Code PureBasic : 4.61
; Plateforme : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Procedure DrawChecker(ImageID, Size.w, BoxCount.l, Color00.l, Color01.l)
	
	If CreateImage(ImageID, Size, Size)
		
		If StartDrawing(ImageOutput(ImageID))
			
			ColorA.l = Color00
			ColorB.l = Color01
			BoxSize.l = Size / BoxCount
			
			For LineCount = 0 To BoxCount
				
				For RowCount = 0 To BoxCount
					
					If (LineCount + RowCount) & %1 = 1
						Color = ColorA
					Else
						Color = ColorB
					EndIf
					
					Box(x, y, BoxSize, BoxSize, Color)
					
					x + BoxSize
					
				Next
				
				y + BoxSize
				x = 0
				
			Next
			
			StopDrawing()
			
		EndIf
		
	EndIf
	
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< !!! ATTENTION - CODE D'ESSAI !!! <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

#Size = 512

If OpenWindow(0, 0, 0, #Size + 10, #Size + 10, "DrawChecker", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	
	DrawChecker(0, #Size, 8, RGB(255,255,000), RGB(000,000,000)) ; Changer la taille du damier ici, par exemple 9
	ImageGadget(0, 5, 5, #Size, #Size, ImageID(0))
	
	Repeat
	Until WaitWindowEvent() = #PB_Event_CloseWindow
	
EndIf

; <<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< FIN DU FICHIER <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<

Re: Dessiner un damier

Publié : ven. 03/févr./2012 21:54
par Guimauve
C'est modifié !

Petite précision, je m'étais pas rendu compte du problème pour le nombre impair de case. En effet, dans le cadre de mon projet, le nombre est toujours un nombre pair de case.

A+
Guimauve

Re: Dessiner un damier

Publié : sam. 04/févr./2012 9:48
par Mesa
Une variante avec ajout de "fittage" pour ne pas afficher des portions de damier.

Code : Tout sélectionner

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Nom du projet : DrawChecker
; Nom du fichier : DrawChecker.pb
; Version du fichier : 1.0.0
; Programmation : OK
; Programmé par : Guimauve
; Date : 29-01-2012
; Mise à jour : 03-02-2012
; Code PureBasic : 4.61
; Plateforme : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Procedure DrawChecker(ImageID, Size.w, BoxCount.l, Color00.l, Color01.l, fit.b=#False)
  
  Dim ColorC(1)
  ColorC(0)=Color00
  ColorC(1)=Color01
  
  If BoxCount < 1 ; evite un plantage
    BoxCount = 1
  EndIf
  
  If Size < BoxCount  ; evite un plantage
    BoxCount = Size
  EndIf
    
  If fit = #True  ; Pour eviter des portions de damier
    Size = Size - (Size % BoxCount)
  EndIf
  
  
  If CreateImage(ImageID, Size, Size)
   
    If StartDrawing(ImageOutput(ImageID))

      BoxSize.l = Size / BoxCount
     
      For LineCount = 1 To BoxCount
       
        For RowCount = 1 To BoxCount
                  
          Box(x, y, BoxSize, BoxSize, ColorC(RowCount % 2))
          x + BoxSize
         
        Next
       
        y + BoxSize
        x = 0
        Swap ColorC(0), ColorC(1)
        
      Next
     
      StopDrawing()
     
    EndIf
   
  EndIf
 
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< !!! ATTENTION - CODE D'ESSAI !!! <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

#Size = 512

If OpenWindow(0, 0, 0, #Size + 10, #Size + 10, "DrawChecker", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
 
  DrawChecker(0, #Size, 9, RGB(255,255,000), RGB(000,000,000), #True)
  ImageGadget(0, 5, 5, #Size, #Size, ImageID(0))
 
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
 
EndIf
Mesa.

Re: Dessiner un damier

Publié : sam. 04/févr./2012 11:11
par Kwai chang caine
Merci 8)

Re: Dessiner un damier

Publié : sam. 04/févr./2012 16:24
par Backup
mis en Récursif :)

Code : Tout sélectionner

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Nom du projet : DrawChecker
; Nom du fichier : DrawChecker.pb
; mis en recursif : Dobro
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< !!! ATTENTION - CODE D'ESSAI !!! <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

#Size = 512
enumeration
	#window
	#image
endenumeration
Declare  case_dam (x,y,boxsize,coul1,coul2)
Declare  DrawChecker(ImageID, Size.w, BoxCount.l, coul1.l, coul2.l,i)


If OpenWindow(#window, 0, 0, #Size + 10, #Size + 10, "DrawChecker", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	coul1 = rgb(255,0,0)
	coul2 = rgb(255,255,255)
	If CreateImage(#image, #Size, #Size)
		DrawChecker(#image, #Size, 9, coul1, coul2,1)
	endif
	ImageGadget(#image, 5, 5, #Size, #Size, ImageID(#image))
	Repeat
	Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf


;- zone procedure
procedure case_dam (x,y,boxsize,coul1,coul2)
	Box(x, y, BoxSize, BoxSize,coul1 )
	x + BoxSize
	Box(x, y, BoxSize, BoxSize, coul2)
endprocedure

Procedure DrawChecker(ImageID, Size.w, BoxCount.l, coul1.l, coul2.l,i)
	static x
	if isimage(ImageID)
		StartDrawing(ImageOutput(ImageID))
			BoxSize.l = Size / BoxCount
			For LineCount = 1 To BoxCount
				case_dam (x,y,boxsize,coul1,coul2)
				Swap coul1, coul2
				y + BoxSize
			Next LineCount
		StopDrawing()
		if i<BoxCount.l-1
			y=1
			i=i+1
			x=x+boxsize
			DrawChecker(ImageID, Size.w, BoxCount.l, coul1.l, coul2.l,i)
			Else
			i=1:y=1:x=1
			ProcedureReturn
		endif
	EndIf
EndProcedure

Re: Dessiner un damier

Publié : sam. 04/févr./2012 17:37
par djes
J'peux jouer ? :mrgreen:

Code : Tout sélectionner

Procedure DrawChecker(ImageID, Size.i, BoxCount.i, Color00.l, Color01.l) 
  
  BoxSize.d = Size / BoxCount

  If CreateImage(ImageID, Size, Size) 
    
    If StartDrawing(ImageOutput(ImageID)) 
      
      Box(0, 0, Size, Size, Color01)
            
      For u = 1 To BoxCount * BoxCount
      
        If ((u % BoxCount) + (u / BoxCount)) & 1
          
          Box((u % BoxCount) * BoxSize, (u / BoxCount) * BoxSize, BoxSize, BoxSize, Color00)
          
        EndIf
          
      Next 
      
      StopDrawing() 
      
    EndIf 
    
  EndIf 
  
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< !!! ATTENTION - CODE D'ESSAI !!! <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

#Size = 512

If OpenWindow(0, 0, 0, #Size + 10, #Size + 10, "DrawChecker", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  DrawChecker(0, #Size, 10, RGB(255,255,000), RGB(000,000,000))
  ImageGadget(0, 5, 5, #Size, #Size, ImageID(0))
  
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf

; <<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< FIN DU FICHIER <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<

Re: Dessiner un damier

Publié : sam. 04/févr./2012 18:31
par Guimauve
Personnellement, je n'aurais jamais pensé que ce sujet allait soulever les passions à ce point.

Comme quoi il y a autant de méthode de faire les choses qu'il peut y avoir de programmeur dans le monde.

A+
Guimauve