![]()
DU LERNST HIER... |
präzise Kollisionserkennung mit Hilfe von sogenannten Pixelkollisionen. Im Unterschied zu den Gitterkollisionen, bei welchen sich zwei Actors in der gleichen Zelle befinden, kollidieren hier zwei Actors, wenn sich mindestens zwei Pixel ihrer Figuren überlappen. Das Modul GameGrid stellt die notwendigen Funktionen bereit, die solchen Kollisionen erkennen. |
MUSTERBEISPIELE |
| Beispiel 1: Zwei rechteckige Figuren bewegen sich frei im Spielfenster und reflektieren dabei am Fensterrand. Wenn sie zusammenstossen, ertönt ein Ton und die beiden Stäbe ändern ihre Bewegungsrichtung um 180°. |
Programm: from gamegrid import * from soundsystem import * # --------------------- class Stick ---------------------------------- class Stick(Actor): def __init__(self): Actor.__init__(self, True, "sprites/stick.gif", 2) def act(self): step = 1 loc = self.getLocation() dir = (self.getDirection() + step) % 360; if loc.x < 50: dir = 180 - dir self.setLocation(Location(55, loc.y)) if loc.x > 450: dir = 180 - dir self.setLocation(Location(445, loc.y)) if loc.y < 50: dir = 360 - dir self.setLocation(Location(loc.x, 55)) if loc.y > 450: dir = 360 - dir self.setLocation(Location(loc.x, 445)) self.setDirection(dir) self.move() def collide(self, actor1, actor2): actor1.setDirection(actor1.getDirection() + 180) actor2.setDirection(actor2.getDirection() + 180) play() return 10 # --------------------- main ----------------------------------------- makeGameGrid(500, 500, 1, False) setSimulationPeriod(10) stick1 = Stick() addActor(stick1, Location(200, 200), 30) stick2 = Stick() addActor(stick2, Location(400, 400), 30) stick2.show(1) stick1.addCollisionActor(stick2) show() doRun() openSoundPlayer("wav/ping.wav")
Programm: from gamegrid import * from soundsystem import * from random import randint # --------------------- class Ball ---------------------------------- class Ball(Actor): def __init__(self): Actor.__init__(self, True, "sprites/peg.png", 5) def act(self): step = 1 loc = self.getLocation() dir = (self.getDirection() + step) % 360; if loc.x < 20: dir = 180 - dir self.setLocation(Location(20, loc.y)) if loc.x > 480: dir = 180 - dir self.setLocation(Location(478, loc.y)) if loc.y < 20: dir = 360 - dir self.setLocation(Location(loc.x, 22)) if loc.y > 480: dir = 360 - dir self.setLocation(Location(loc.x, 478)) self.setDirection(dir) self.move() def collide(self, actor1, actor2): actor1.setDirection(actor1.getDirection() + 180) actor2.setDirection(actor2.getDirection() + 180) play() return 10 # --------------------- main ----------------------------------------- makeGameGrid(500, 500, 1, False) setSimulationPeriod(10) balls = [] for i in range(5): ball = Ball() ball.show(i) addActor(ball, Location(randint(30, 470), randint(30, 470))) balls.append(ball) for i in range(5): for k in range(i + 1, 5): balls[i].addCollisionActor(balls[k]) openSoundPlayer("wav/ping.wav") show() doRun()
Programm: from gamegrid import * from soundsystem import * from random import randint # --------------------- class Dart -------------------------- class Dart(Actor, GGMouseListener): def __init__(self): Actor.__init__(self, True, "sprites/dart.gif") self.oldLocation = Location() def mouseEvent(self, e): location = toLocationInGrid(e.getX(),e.getY()) self.setLocation(location) self.oldLocation.x = location.x self.oldLocation.y = location.y def collide(self, actor1, actor2): play() actor2.removeSelf() return 5 # --------------------- main ----------------------------------- makeGameGrid(600, 600, 1, None, "sprites/town.jpg", False) setTitle("Move dart with mouse to pick the balloon") setSimulationPeriod(50) dart = Dart() addActor(dart, Location(100, 300)) addMouseListener(dart, GGMouse.lDrag) for i in range(20): balloon = Actor("sprites/balloon.gif") addActor(balloon, Location(randint(50,550),randint(50,550))) dart.addCollisionActor(balloon) show() openSoundPlayer("wav/explode.wav") doRun() |
MERKE DIR... |
| Kollisionen werden als Events erfasst. Dabei wird die Methode collide(self, actor1, actor2) aufgerufen, in welcher definiert wird, was geschehen soll, wenn die beiden Actors kollidieren. |
ZUM SELBST LÖSEN |
|
![]()