fangEi
 Deutsch   English

## WORKSHEET 1: "CATCH THE EGG"

### GAME DESCRIPTION

On the 5 by 5 LED display, a pixel moves from top to bottom in a random column, like an egg falling down from a table.

 On the bottom row, a pixel can be moved back and forth by tilting the board. This bottom pixel is like a basket that you use to catch the egg before it smashes to the ground. If  the basket pixel is on the same column as the falling egg, you  "caught" the egg and you score a point. The game is repeated 10 times and the aim is to catch as many eggs as possible so getting the highest score. According to the principle of "divide and conquer", the task is split into several steps.

### STEP 1: EGG PIXEL FALLING ON FIXED COLUMN

 First, write a program that moves the egg pixel on the fixed column x = 2 by one line each time the function moveEgg() is called. You start from the following program template and insert the missing code lines: Program: ```from microbit import * def moveEgg(): global y, yOld # erase old ... # draw new ... # update position yOld = y y = y + 1 if y == 5: y = 0 # Initialization x = 2 y = 0 yOld = 4 # Animation loop while True: moveEgg() sleep(300) # animation speed ``` # CatchTheEgg1.py # Falling in fixed column x = 2 from microbit import * def moveEgg(): global y, yOld # erase old display.set_pixel(x, yOld, 0) # draw new display.set_pixel(x, y, 9) # update position yOld = y y = y + 1 if y == 5: y = 0 # Initialization x = 2 y = 0 yOld = 4 # Animation loop while True: moveEgg() sleep(300) # animation speed ► Copy to clipboard

### STEP 2: FALLING PIXEL ON A RANDOM COLUMN

 Next you expand the program so that the column is chosen randomly. Use randint(a, b) from themodule random, which returns a random number between a and b (including a and b). Use xOld analogous to yOld.

### STEP 3: MOVABLE BASKET PIXEL

 In the same way, write the function moveBasket(), which shifts the basket pixel back and forth on the bottom line when you tilt the board left or right. The main program consists of: ```# Initialization z = 0 # Animation loop while True: moveBasket() sleep(300) # animation speed ``` where z is the current x-coordinate the basket pixel

### STEP 4: MERGE PRECEDING STEPS

 Now merge the pieces of code so that both the egg pixel and the basket pixel move (but still independent of each other). The main program loop is: ```# animation loop while True: moveEgg() moveBasket() sleep(300) # animation speed ``` Play a while and try to catch the egg with the basket in the right place.

### STEP 5: CHECK IF THE BASKET CATCHES THE EGG

 Now the program should find out by itself, whether the egg pixel and the basket pixel meet at the same place (collide). If  this is the case, increase the game score by one. Hint: Perform the collision test in the main loop, remembering that the position of the current egg pixel is xOld, yOld (x, y is the position of the next pixel). Display Image.YES in case of a success and Image.NO in case of a failure. ```if yOld == 4: # egg at bottom sleep(1000) if xOld == z: # hit display.show(Image.YES, clear = True) else: # miss display.show(Image.NO, clear = True) ```

### STEP 6: FINISHING THE GAME, PERSONALIZING IT

 The hardest work is behind you. Just add a counter variable hit, which  increases by one if the egg is caught and make sure that the game is repeated exactly 10 times. At the end of the game show the score on the LED display. You can restart the game any time by clicking the reset button. Of course, it is up to your imagination to give the game a personal touch with variants and improvements. Have fun!