Mouse Handler
Read about event system in CodeWorld and possibly consult some demonstration code.
Essential Warmup
Mouse location is reported in “computer coordinates”, Double
s in the
range [-10,10] for both x and y. We will do some math to change these
into the board coordinates.
You need to have made decisions about the size of your board squares and their location. Consult your diagram for the draw handler. Here, I will use the numbers from my drawing, but any method works.
toBoard :: (Double, Double) -> Posn
Tests, written while looking at my diagram:
testToBoard = [
toBoard (-2,-2) == (0,0)
,toBoard (-1.9, -2.8) == (0,0)
,toBoard (-2,0) == (0,1)
,toBoard (2,-2) == (2,0)
]
The formula is the same for both x and y, so you might just try writing a function for x.
warm :: Double -> Int
testWarm = [
warm (-2) == 0
,warm (-1.9) == 0
,warm (-1.1) == 0
,warm (0.9) == 1
,warm (1.2) == 2
]
Expect to use round
or floor
along with some addition,
subtraction, multiplication, or division.
Mouse Handler
Hopefully you have learned from class or the examples how the event handler works. Here is a skeleton and some do-nothing starter code.
event_h :: Event -> GameState -> GameState
event_h (PointerPress (x,y)) state = state
event_h _ state = state