Ch08 Quiz A
In this game you will work with four kinds of shapes. The circles have radius 1.5 and the squares have side length 3.0.
Clicking on a shape means anywhere within the shape, not just its exact center.
-
Make a data type
MyShape
that can represent onlyCircle
,Square
,Complete
, orGremlin
. -
Make a data type
Mode
that is eitherPlacing
orGaming
. -
Make a type alias called
ShapeLoc
that is an ordered pair of aPoint
and aMyShape
. -
Make a data type
Game
that holds aMode
and a list ofShapeLoc
. Use the record syntax where you name the fieldsmode
andshapes
in the definition. -
Write a
drawBasic :: MyShape -> Picture
function to create the image for a each shape, centered at (0,0).- Circle - radius 1.5, outline
- Square - side length 3.0, outline
- Compete - radius 1.5 solid orange circle
- Gremlin - side length 1.5 x 3.0 solid green rectangle
-
Write a drawing function
drawh :: Game -> Picture
that places the basic shapes from eachShapeLoc
at the point it is paired with. -
Write the
clickedOn :: Point -> ShapeLoc -> Bool
function to determine if a click is on the shape.clickedOn :: Point -> ShapeLoc -> Bool clickedOn pt shapepos = False
-
Write
advance
which moves aMyShape
to the next one.Circle
toSquare
toComplete
. AfterComplete
do not advance more.Gremlin
should not advance -
Write an event handler that handles the clicks of the mouse. In Placing mode, place a circle at the mouse click. In Gaming mode, “
advance
” the shape clicked on to the next shape. -
Write an event handler for key presses:
- “A” key sets Placing mode
- “D” key sets Gaming mode
-
Write a function to find what shape was clicked on.
findGremlin :: Point -> [ShapeLoc] -> Maybe ShapeLoc
-
Write a function to remove a given
ShapeLoc
from the list, convert it into aGremlin
at the same location, and put it on top of any other shapes.convertGremlin :: ShapeLoc -> [ShapeLoc] -> [ShapeLoc]
-
Modify your event handler so that mouse clicks inside of an existing shape (
findGremlin
) in Placing mode turn that shape and the one you place into Gremlins.
Helper Functions
dist :: Point -> Point -> Double
dist (x0,y0) (x1,y1) = sqrt $ (x0-x1)**2 + (y0-y1)**2
-- only good for squares as written
distrect :: Point -> Point -> Double
distrect (x0,y0) (x1,y1) = max dx dy
where dx = abs(x0-x1)
dy = abs(y0-y1)
Extras
You could have a visible circle follow the mouse in Placing
mode. That would require some changes.