CS 1410-20 Homework 4

Due: Friday, September 23rd, 2011 10:45am

It’s finally time to smash some blocks with your duck. For this assignment, start with either your HW 2 code or start with the HW 2 solution. You won’t have to modify any of your old program; just add new functions to the end. You should use templates to implement functions, but the templates need not be included in the work that you handin this time.

The main data structure for this assignment is a block, which has a position and color. The position refers to the location of the block’s top-left corner in the scene, and each block is 30 pixels wide and 30 pixels high. The specific block data definition is up to you.

If you’d like a peek at the result of this homework, executables are available for Windows and Mac OS X. The executables were created from the instructor’s solution using “Create Executable” in the “Racket” menu.

Part 1 – Generate Blocks

Implement the function gen-blocks, which takes three arguments: a natural number n, a number x, a number y, and a string col. The gen-blocks function should produce a list of n blocks with color col, where the last block (for n≤1) is positioned x pixels from the left edge of the scene and y pixels down, the next-to-last one (for n≥2) is also x pixels from the left edge but y+30 pixels down, and so on—forming a solid vertical wall of n blocks. Alternatively, your gen-blocks can produce the list in the reverse order; either order is ok.

Part 2 – Removing Blocks

Implement the function remove-blocks, which takes a list of blocks and a posn. The result should be a list of blocks that do not touch the given posn.

A block touches a posn if the block’s left edge is no greater than the posn’s x-coordinate, the block’s right edge is no less than the posn’s x-coordinate, the block’s top edge is no greater than the posn’s y-coordinate, and the block’s bottom edge is no less than the posn’s y-coordinate.

For example, (remove-blocks (gen-blocks 2 10 10 "blue") (make-posn 20 20)) should produce the same result as (gen-blocks 1 10 40 "blue").

Don’t forget that when you’re writing a function that consumes a list of blocks, you almost certainly want a function that handles each individual block. The function that takes a block will have its own contract, purpose, template (whether you keep it or not), and tests.

Part 3 – Adding Blocks to a Scene

Implement add-blocks-to-scene, which takes a list of blocks and a scene and returns a new scene with the blocks added. Each block should be added to the scene at its position and using the block’s color.

Part 4 – The Game

A game combines a launch (from HW 2) with a list of blocks:

  ;; A game is
  ;;   (make-game launch list-of-block)
  (define-struct game (launch blocks))


With those pieces, you can try running you game so far:

   (make-game false (gen-blocks 10 600 300 "blue"))
   [to-draw game-scene]
   [on-tick step-game]
   [on-mouse mouse-game])

Of course, the duck doesn’t yet smash the blocks.

Part 5 – Smash Blocks

Change step-game so that after the game’s launch is stepped, if the launch is a flying duck, then any blocks that are touched by the duck’s center are removed from the game returned by step-game.

You will probably want to implement a helper function combine-game, which takes a (new) launch and a list of blocks and returns the launch combined with a suitably filtered list of blocks. The combine-game helper, in turn, should use other functions that you’ve written for this assignment.

Part 6 – Watch out for Falling Blocks

After a flying duck smashes blocks, blocks that are not supported by other blocks should fall.

Implement drop-blocks, which takes a list of blocks and moves each block in the list down by 1 (i.e., increases its y-position by 1) if no block with a greater y position anywhere in the list touches the block and if the block’s bottom edge is not already touching the ground (which is at 600 pixels).

You’ll probably want several helper functions:

Part 7 – Mildy Annoyed Ducks!

Adjust step-game so that after the launch is stepped and before blocks are smashed, the blocks drop as implemented by drop-blocks.

Use the big-bang form above to play your game.

Last update: Friday, November 4th, 2011