Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2015
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.52 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\Documents\Visual Studio 2015\Projects\Tris\Tris\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.    
  71.     do this.AddCells(lista_celle)
  72.  
  73.     member this.AddCells (lista_celle) =
  74.         match lista_celle with
  75.         | x::xs -> this.Controls.Add(x); this.AddCells(xs)
  76.         | _ -> ()
  77.  
  78.     member this.CellSize
  79.         with get () = cell_size
  80.         and set(v) = cell_size <- v
  81.  
  82.     override this.OnPaint e =
  83.         let g = e.Graphics
  84.         let savedGraph = g.Save()
  85.         let p = new Pen(Color.White, width=6.f)
  86.         let lenght = this.Width // = this.Height
  87.         g.TranslateTransform(float32(lenght/2), float32(lenght/2) )
  88.         for i = 1 to 4 do
  89.             g.DrawLine(p, -lenght/2, lenght/6, lenght/2, lenght/6)
  90.             g.RotateTransform(90.f)
  91.         g.Restore(savedGraph)
  92.  
  93.     override this.OnResize e =
  94.         this.CellSize <- this.Width / 5
  95.         this.Invalidate()
  96.        
  97.  
  98. let set_grid_size (e:Grid) =
  99.     let width = f.ClientSize.Width
  100.     let height = f.ClientSize.Height
  101.     let cx = width / 2
  102.     let cy = height / 2
  103.     (*Divido ora l'area utile in 10 parti, la griglia occuperà 6/10 di area*)
  104.     let decimo = if width > height then height/10 else width/10
  105.     e.Width <- decimo * 6
  106.     e.Height <- decimo * 6
  107.     e.Location <- Point(cx-(3*decimo), cy-(3*decimo))
  108.  
  109. let e = new Grid()
  110. set_grid_size e
  111. f.Controls.Add(e)
  112. f.Controls.Add(new Cell())
  113. f.Resize.Add(fun _ ->
  114.     set_grid_size e
  115.     f.Invalidate()
  116. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement