Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.33 KB | None | 0 0
  1. open System.Windows.Forms
  2. open System.Drawing
  3.  
  4. let f = new Form(Text="Tetris", Width=300, Height=500, TopMost=true)
  5. f.Show()
  6. let bgimg = new Bitmap(@"C:\Users\Alessandro\Desktop\wood_background.jpg")
  7. f.BackgroundImage <- bgimg
  8. f.BackgroundImageLayout <- ImageLayout.Tile
  9.  
  10. f.MinimumSize <- new Size(370, 50)
  11. let mutable count = 0
  12.  
  13. type Cell () as this =
  14.     inherit UserControl()
  15.     do this.DoubleBuffered <- true
  16.     do this.BackColor <- Color.Transparent
  17.     let mutable value = 2 //Vuota
  18.         // = 1 -> X
  19.         // = 0 -> O
  20.     let mutable cell_size = 0
  21.     do this.Width <- 100
  22.     do this.Height <- 100
  23.     member this.CellSize
  24.         with get () = cell_size
  25.         and set(v) = cell_size <- v
  26.  
  27.     member this.Value
  28.         with get() = value
  29.         and set(v) = value <- v
  30.          
  31.     member this.DrawCross (g:Graphics) =
  32.         let savedGraph = g.Save()
  33.         g.TranslateTransform(float32(cell_size/2), float32(cell_size/2))
  34.         g.RotateTransform(45.f)
  35.         for i = 0 to 4 do
  36.             g.RotateTransform(90.f)
  37.             let p = new Pen(Color.OrangeRed, Width=6.f)
  38.             g.DrawLine(p, 0, 0, cell_size/2, 0)
  39.         g.Restore(savedGraph)
  40.  
  41.     member this.DrawCircle (g:Graphics)=
  42.         let savedGraph = g.Save()
  43.         g.TranslateTransform(float32(cell_size/2), float32(cell_size/2))
  44.         let p = new Pen(Color.CadetBlue, Width=6.f)
  45.         g.DrawEllipse(p, -cell_size/2 + cell_size/10, -cell_size/2 + cell_size/10, cell_size - cell_size/5, cell_size - cell_size/5)
  46.         g.Restore(savedGraph)
  47.  
  48.     override this.OnPaint(e) =
  49.         let g = e.Graphics
  50.         if value = 1 then
  51.             this.DrawCross(g)
  52.         else
  53.             this.DrawCircle(g)
  54.  
  55.     override this.OnClick(e) =
  56.         if this.Value = 2 then
  57.             this.Value <- count % 2
  58.             count <- count+1
  59.             this.Invalidate()
  60.  
  61.  
  62. type Grid () as this =
  63.     inherit UserControl()
  64.     do this.DoubleBuffered <- true
  65.     do this.BackColor <- Color.Transparent
  66.  
  67.     let mutable count = 0
  68.     let lista_celle = [|for i in 1 .. 9 -> new Cell()|]
  69.     let mutable cell_size = 0
  70.     for i in 1 .. 9 ->
  71.         this.Controls.Add(lista_celle[i])
  72.  
  73.     member this.CellSize
  74.         with get () = cell_size
  75.         and set(v) = cell_size <- v
  76.  
  77.     override this.OnPaint e =
  78.         let g = e.Graphics
  79.         let savedGraph = g.Save()
  80.         let p = new Pen(Color.White, width=6.f)
  81.         let lenght = this.Width // = this.Height
  82.         g.TranslateTransform(float32(lenght/2), float32(lenght/2) )
  83.         for i = 1 to 4 do
  84.             g.DrawLine(p, -lenght/2, lenght/6, lenght/2, lenght/6)
  85.             g.RotateTransform(90.f)
  86.         g.Restore(savedGraph)
  87.  
  88.     override this.OnResize e =
  89.         this.CellSize <- this.Width / 5
  90.         this.Invalidate()
  91.        
  92.  
  93. let set_grid_size (e:Grid) =
  94.     let width = f.ClientSize.Width
  95.     let height = f.ClientSize.Height
  96.     let cx = width / 2
  97.     let cy = height / 2
  98.     (*Divido ora l'area utile in 10 parti, la griglia occuperà 6/10 di area*)
  99.     let decimo = if width > height then height/10 else width/10
  100.     e.Width <- decimo * 6
  101.     e.Height <- decimo * 6
  102.     e.Location <- Point(cx-(3*decimo), cy-(3*decimo))
  103.  
  104. let e = new Grid()
  105. set_grid_size e
  106. f.Controls.Add(e)
  107.  
  108. f.Resize.Add(fun _ ->
  109.     set_grid_size e
  110.     f.Invalidate()
  111. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement