| HomeTurtlegraficsGPanelRoboticsGameGrid WebTigerPython |
| Python - Online |
| Deutsch English |
![]()
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.
|
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()
|
WORKSHEET 3: BRAIN GAME |
The problem is solved in 6 steps: Step 1: Displaying the playing field Program: # BrainGame0.py from gpanel import * def showLamp(col): pass def showButton(number): pass def setup(): for i in range(4): showButton(i) showLamp(-1) makeGPanel(-200, 200, -200, 200) setColor("black") fillRectangle(400, 400) addStatusBar(30) setup() Write the functions according to the following documentation::
Step 2: Use the mouse to switch on the lamp in 4 colours def onMousePressed(x,y): if getPixelColorStr(x, y) == "red": buttonIndex = 0 elif ... elif getPixelColorStr(x, y) == "white": buttonIndex = -1 else: buttonIndex = -2 if buttonIndex >= 0: showLamp(buttonIndex) def onMouseReleased(x,y): showLamp(-1) makeGPanel(-200, 200, -200, 200, mousePressed = onMousePressed, mouseReleased = onMouseReleased)
seq = [] n = 3 for i in range(n): seq.append(randint(0, 3)) setStatusText("Showing sequence with length " + str(n)) showSequence() Write the function showSequence() that displays these sequences. The display duration is set to 1000 milliseconds here. def showSequence(): for k in seq: delay(1000) showLamp(k) delay(1000) showLamp(-1)
The correct sequence is tested each time the mouse is released, i.e. in the callback onMouseReleased(x, y). To do this, the variable clickCount must be used to count the mouse clicks already made and with if seq[clickCount] == buttonIndex:
clickCounr += 1
check whether the correct click was made. If this is the case, increase clickCount; otherwise, set a flag isOk = False. To inform the main programme when the test is complete, use a flag isUserActive. global clickCount, isUserActive, isOk if seq[clickCount] == buttonIndex: setStatusText("Sequence confirmed") clickCount += 1 else: isOk = False setStatusText("Sequence false") if clickCount == len(seq): isUserActive = False isOk = True Once the entire sequence has been successfully tested, set isOk = True. The variables clickCount, isOk and isUserActive must be defined as global and initialised in the main programme. In the main programme, insert a wait loop that runs until the user action is complete. isUserActive = True isOk = False while isUserActive: delay(10) if isOk: setStatusText("Sequence confirmed") else: setStatusText("Sequence false") delay(2000) Stept 5: Extend the sequence So far, you have been playing with a fixed sequence length of 3. Now you have to start with a sequence length of 1 according to the game rules and increase it by 1 if the user guesses correctly. You continue doing this until they make a mistake. You have prepared the programme so well that this extension is easy to implement. After setup() and initialising n = 1, add the rest of the programme to an endless while loop and increase n in it when the user is successful. If they make a mistake, break the loop with break. setup() n = 1 while True: clickCount = 0 setStatusText("Showing sequence with length: " + str(n) + "...") delay(2000) seq = [] for i in range(n): .... showSequence() setStatusTex("Click tu repeat sequence") isUserActice = True isOk = False if isOk: setStatusText("Sequence confirmed") delay(2000) n += 1 else: break setStatusText("sequence failed") Step 6: Making the game robust The game can already be played if the user follows certain rules and does not click the mouse at the wrong moment. Therefore, you need to improve the programme so that a mouse click at the wrong moment does not lead to disaster. To do this, introduce another flag, isMouseEnabled, in the main programme. This is set to False at the beginning of the while True loop and is only set to True when the user is allowed to use the mouse, i.e. when they have to return the sequence. If isMouseEnabled = False, simply return to the two callbacks without taking any further action. if not isMouseEnabled: return
|
![]()