HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
ereignisse
Deutsch   English   

11. MAUSEVENTS

 

 

YOU LEARN HERE...

 

that event-driven programming is a new programming concept. Instead of giving commands to the Turtle with your program, you can control the Turtle with mouse clicks.

 

 

EXAMPLES

 

Mouse events are used to trigger mouse-controlled actions. The mouse clicks are recorded by the system and lead to a so-called callback function being called. This returns the coordinates of the mouse click and defines what should happen when the mouse is clicked. The callback function is registered as a named parameter of makeTurtle().

In your example, the turtle moves to the position of the mouse click each time the mouse is clicked and draws a point there. You define this behavior in the callback function onMousePressed(x, y). The callback function also returns the coordinates of the mouse click.

The callback function is registered via a parameter of makeTurtle(). This tells the system that it should call this function every time the mouse is clicked.

 

Program:

from gturtle import *

def onMousePressed(x, y):
    moveTo(x, y)
    dot(20)

makeTurtle(mousePressed = onMousePressed)
hideTurtle()
setPenColor("blue")
dot(20)
►Copy to clipboard
 

 

In the second example, a star is drawn each time the mouse is clicked. To do this, you first define a function drawStar(), which draws such a star. In the callback function onMousePressed(x, y), you define what the Turtle should do when a mouse button is pressed. Here it calls your drawStar() function at the position of the mouse click, which draws a star.

The callback function is registered as a parameter of makeTurtle(). This tells the system that it should call this function each time the mouse is clicked. The coordinates x, y of the mouse click are returned

 

Program:

from gturtle import *

def drawStar():
    repeat 9:
        forward(40)
        right(160)

def onMousePressed(x, y):
    setPos(x, y)
    drawStar()

makeTurtle(mousePressed = onMousePressed)
hideTurtle()
setPenColor("magenta")
Copy to clipboard
 

 

Here a filled n-corner is drawn with mouse clicks. To do this, you define a callback function drawPoint(x, y) , which connects the current mouse position with the position of the mouse click and draws a point there. You can enter the number of corners using an input dialog. If the last mouse click matches the starting point, the n-corner is filled in red with the fillPath() unction (otherwise the last point is connected to the starting point (0, 0)).

The number of points already drawn is stored in the variable n. This is ncreased by 1 each time the mouse is clicked. As the variable n is used both in the main program and in the drawPoint() function, it must be defined global as a.

 

Program:

from gturtle import *

def drawPoint(x, y):
    global n
    moveTo(x, y)
    dot(5)
    n += 1
    if n == nbCorners:
        fillPath()

makeTurtle(mousePressed = drawPoint)
hideTurtle()
n = 0
nbCorners = InputInt("GibAnzahl Ecken an")
setFillColor("red")
startPath()
► Copy to clipboard

 

 

 

REMEMBER...

 

The new programming technique can be recognized by the fact that the function onMousePressed(x, y) is not called anywhere by your program. It is called by the system when an event occurs. We call such a function a callback function or callback for short.

The variable n in the last example is called a global variable because it is used both in the main program and in the callback function drawPoint(x,y). A variable that is only used in one function is called a local variable.

 

 

TO SOLVE BY YOURSELF

 

1.

A filled circle (dot) is to be drawn each time the mouse is clicked. The fill colors and diameter of the circle are chosen randomly. You can see how to create random colors in the second sample example in the chapter Random numbers.
 

2.
A yellow-filled star is drawn on a dark blue background each time the mouse is clicked. You draw the blue background with the clear(“darkblue”) command.  

3.

As in the previous task, the stars are drawn at the positions of the mouse clicks. The fill color of the stars changes between red and blue.
(After you have drawn a red star, you must set the color to red and vice versa).

 

 

 

 

ADDITIONAL MATERIAL

 

YOUR FIRST GAME

 

Tic-tac-toe is a well-known game in which two players take it in turns to place crosses or squiggles in the squares of a 3x3 grid with the aim of being the first to have three of their own symbols in a horizontal, vertical line or one of the diagonals.

 

Here you draw a red or green dot instead of crosses and circles, depending on the player.

First define the drawBoard() function, which draws the 3x3 grid. To switch the color, introduce a player variable that identifies the player (takes the value 1 or 2). You must designate this as global in the drawDot() callback function, as it is initialized in the main program and switches between the two values 1 and 2 in the drawDot() function after each mouse click.

 


Program:
from gturtle import *

def square():
    for i in range(4):
        forward(60)
        right(90)

def drawBoard():
    for x in range(3):
        for y in range(3):
            setPos(60 * x, 60 * y)
            square()

def drawDot(x, y):
    global player
    if player == 1:
        setPenColor("red")
        player = 2
    elif player == 2:
        setPenColor("lime")
        player = 1
    setPos(x, y)
    dot(45)

player = 1        
makeTurtle(mousePressed = drawDot)
hideTurtle()
drawBoard()
► Copy to clipboard

It would be interesting to extend the game so that it automatically recognizes when one player has won or when the game is tied.

 

 

TO SOLVE BY YOURSELF

 
4*.


Create a Nim game with 15 red tiles. Each player can remove a maximum of 3 tiles with one mouse click and must then click on the green button, the OK button, so to speak. Then the other player takes his turn. Whoever removes the last stone loses. To see whether a player has clicked the red stone or the green button with the mouse, use the getPixelColorStr() function. This returns the background color (e.g. “red”, “green”). You can make the stones disappear by painting over them in white. The game is usually played with matches.