Deutsch   English   

17. Worksheets

 

 

WORKSHEET 1: PYTHON CITY

 

The worksheet depicts a realistic scenario in the IT industry: You are employed as a programmer in a company and are supposed to continue a project of your predecessor. You are given a complex, executable programme as a template. Your task is to understand the programme and make certain adjustments.

The programme draws a picture of a city. As the programme code contains random elements, the city will look different every time you run the programme.


1.

Copy the programme to WebTigerPython or WebTigerJython by clicking on the following button and run it several times.

Program:   

 

2.

 

Study the programme. The comments will help you to understand the programme structure.

3.

 

Change the number of houses. Initially only 30, then 70 and finally 45 houses should be drawn.

4.

The windows should be red, not yellow.

5.

 

You like the yellow windows better, but not all the windows should be illuminated. 30% of the windows should be black, the rest yellow. To do this, insert the following code in the right place
if random() < 0.4: meineFarbe = [0, 0, 0]

6.

 

The houses do not all have to be grey either. Half of the houses should have a reddish colour (e.g. [170, 100, 100]).

7.

 

A small star should be visible next to the moon. You draw the star round with the fillCircle() function.

8.

Draw 100 random stars in the night sky.

9.

The stars are not the same size. 30% have a diameter of 3, the rest 2.

10.

 

Add your own ideas to the picture (shooting stars, ships on the water, houses reflected in the water...)

 

 

WORKSHEET 2: MONDRIAN

 

The programme randomly divides the GPanel window vertically and horizontally into coloured rectangles. The colours are randomly selected from the colours list.

The graphics are reminiscent of the artwork of painter Piet Mondrian.

 

 

Program:     

# Mondrian.py
from gpanel import *
from random import *

def between(a, b):
    return a + (0.2 + 0.3 * random()) * (b - a)

def randomColor():
    while True:
        result = choice(colors)
        return result
    
def rect(xMin, yMin, xMax, yMax):
    for aColor in ('black', randomColor()):
        setColor(aColor)        
        fillRectangle(xMin, yMin, xMax, yMax)           
        xMin += delta
        yMin += delta
        xMax -= delta
        yMax -= delta
    
def maybe(bias = None):
    return choice([False,True,bias,bias] if bias!=None else [False,True])   

def draw(xMin = 0, yMin = 0, xMax = 500, yMax = 500):
    if xMax - xMin > threshold and yMax - yMin > threshold:
        if maybe(xMax - xMin > yMax - yMin):
            xMid = between(xMin, xMax)
            if maybe():
                draw(xMin, yMin, xMid, yMax)
                rect(xMid, yMin, xMax, yMax)
            else:
                rect(xMin, yMin, xMid, yMax)
                draw(xMid, yMin, xMax, yMax)
        else:
            yMid = between(yMin, yMax)
            if maybe():
                draw(xMin, yMin, xMax, yMid)
                rect(xMin, yMid, xMax, yMax)
            else:
                rect(xMin, yMin, xMax, yMid)
                draw(xMin, yMid, xMax, yMax)
    else:
        rect(xMin, yMin, xMax, yMax)

makeGPanel(0, 500, 0, 500)
colors = ['gray', 'lime', 'red', 'white', 'blue', 'yellow']
delta = 6
threshold = 100
setColor ('black')
draw()
► Copy to clipboard

 

1. Run the programme several times and try to understand its structure

2.

What does the Python function choise() do?

3.

Extend the list of colours with additional colours, e.g. cyan, magenta...

4.

The function draw(xMin, yMin, xMax, yMax) is recursive. What is the significance of the variable threshold? Change the value of this variable and observe the effect.

5.

What is the significance of the variable delta? Change its value and observe the effect.

6.

In the given programme, a new graphic is drawn after each programme start. Now, a new image should be drawn with each mouse click. The draw() function should not be called in the main programme, but in the callback function onMousePressed(), which you must first define and register with makeGPanel().

7.

 

Use the left and right mouse buttons to switch between two different colour lists.
You can find more colours under https://www.w3schools.com/colors/colors_x11.asp

8.

Expand the programme with your own ideas (thinner/thicker black lines, different area division, etc.)