mausevents2
Deutsch   English   

9. MOUSE EVENTS II

 

 

YOU LEARN HERE...

 

how you can capture the actors precisely with the mouse. Unlike pressCallback() in the previous chapter, which captures the entire cell of the mouse click, the methods of GGMouseListener give you the pixel coordinates of the mouse click. The mouseTouched() method of the GGMouseTouchListener has several parameters:
actor: the actor you click on
mouse: an object of GGMouse
spot: a point with the relative coordinates of the mouse click in relation to the sprite image

The GGMouseTouchListener is registered with the actor using addMouseTouchListener().

 

 

EXAMPLES

 

 

Example 1: Removing matches
 

The matches are not arranged in a grid, but are placed arbitrarily close to each other. It is not the grid cells, but the sprite images that define the area active for mouse clicks. With match.addMouseTouchListener MouseTouched, GGMouse.lPress), the TouchListener is registered for each match and reacts when the left mouse button is pressed. In the mouseTouched() function, you specify what should happen when the mouse clicks on the actor.

Program:

# Gg9.py
from gamegrid import *

def mouseTouched(actor, mouse, spot):
    actor.removeSelf()
            
makeGameGrid(600, 120, 1, False)
nbMatches = 20
for i in range(nbMatches):
    match = Actor("sprites/match.gif")
    addActor(match, Location (12 + 30 * i, 60))
    match.addMouseTouchListener(mouseTouched, GGMouse.lPress)
show()
doRun()
► Copy to clipboard

Example 2: Move a car with the mouse button pressed.

The car is moved with the mouse. Its image always rotates in the direction of movement.

You use the MouseListener from the GGMouseListener class. Since Python allows multiple inheritance, Car can be derived from the Actor class and also from the GGMouseListener class. The callback method mouseEvent() is defined in the Car class.

 

To ensure that the car always turns in the direction of movement, the direction of movement is continuously calculated from the difference difference between the old and new coordinates. The program always waits until the distance between the old and new points is greater than 5 pixesl.

Program:

# Gg9a.py
from gamegrid import *
import math

# --------------------- class car --------------------------
class Car(Actor, GGMouseListener):
    def __init__(self):
        Actor.__init__(self, True, "sprites/redcar.gif") 
        self.oldLocation = Location()
    
    def mouseEvent(self, e):
        location = toLocationInGrid(e.getX(), e.getY())
        self.setLocation(location)
        dx = location.x - self.oldLocation.x;
        dy = location.y - self.oldLocation.y;
        if dx * dx + dy * dy < 25:
            return True
        phi = math.atan2(dy, dx)
        self.setDirection(math.degrees(phi))
        self.oldLocation = location
 
# --------------------- main ---------------------------------
makeGameGrid(600, 600, 1, False)
setTitle("Move car using mouse drag")
setSimulationPeriod(50)
setBgColor(Color.gray)
car = Car()
addActor(car, Location(50, 50))
addMouseListener(car, GGMouse.lDrag)
show()
doRun()
► Copy to clipboard

 

 

REMEMBER YOU...

 

With the help of GGMouseTouchListener, you can obtain the pixel coordinates of the mouse click and capture the actors precisely by clicking on the sprite image. Moving the actors with the mouse button pressed down is a continuous process.

 

 

TO SOLVE BY YOURSELF

 

1.

The red balls should be removed with a mouse click..

a) Solve the task with pressCallback() as in example 3 in the previous chapter.

b)Solve the task with the help of GGMouseListener as in example 1.

 
 

2.
These 10 aliens should also be removed with a mouse click.
 
Solve the task with pixel coordinates using GGMouseTouchListener.
You will use the aliens:
       alien = Actor("sprites/alien.png")

and position the actors with a for loop
for i in range(10):    
    addActor(alien, Location (30 + 60 * i, 60))
 

 



.