final int groundYPos = 420; int groundXPos = 0; int direction = 0; float yPos = groundYPos; float yVelocity = 0; // ZMIENNE DO MYSZKI float xVelocity = 0; // ruch bałwana boolean mouseControl = false; int mouseStartX; void setup() { size(640, 480, P3D); } void draw() { // grawitacja i kolizja z podłożem yVelocity += .5f; yPos += yVelocity; if (yPos > groundYPos) { yPos = groundYPos; yVelocity = 0.f; } // sterowanie LPM if (mouseControl && mousePressed && mouseButton == LEFT) { int dx = mouseX - mouseStartX; // wychylenie od pozycji startowej xVelocity = -dx * 0.1; // prędkość proporcjonalna do dx if (xVelocity < 0) direction = -1; else if (xVelocity > 0) direction = 1; else direction = 0; } // animacja podłoża – xVelocity groundXPos += int(xVelocity); groundXPos = (groundXPos + 40) % 40; // rendering background(0); stroke(200); // podłoże line(0, groundYPos, 639, groundYPos); for (int i = groundXPos; i < 640; i+=40) line(i, groundYPos, i + 10, groundYPos + 20); // bałwan noStroke(); fill(220); ellipse(320, int(yPos) - 25, 50, 50); ellipse(320, int(yPos) - 65, 30, 30); fill(#EB8921); triangle( 320-15*direction, int(yPos) - 68, 320-30*direction, int(yPos) - 65, 320-15*direction, int(yPos) - 62) ; if (direction == 0) ellipse(320, int(yPos) - 65, 6, 6); } void keyPressed() { // wciśnięte lewo/prawo + myszka if (!mouseControl) { // tylko gdy nie sterujemy myszką if ( (key != CODED && keyCode == 'A') || (key == CODED && keyCode == LEFT) ) { direction = 1; xVelocity = 3; // + myszka } else if ( (key != CODED && keyCode == 'D') || (key == CODED && keyCode == RIGHT) ) { direction = -1; xVelocity = -3; // + myszka } } } void keyTyped() { // wciśnięty skok if (key == ' ' && yPos == groundYPos) yVelocity = -12.f; } void keyReleased() { // puszczone lewo prawo if (!mouseControl) { // tylko gdy nie sterujemy myszką if ( (key != CODED && keyCode == 'A') || (key == CODED && keyCode == LEFT) || (key != CODED && keyCode == 'D') || (key == CODED && keyCode == RIGHT) ) { direction = 0; xVelocity = 0; // + myszka } } } // MYSZKA void mousePressed() { if (mouseButton == LEFT) { // zapamiętaj pozycję startową myszki mouseControl = true; mouseStartX = mouseX; } else if (mouseButton == RIGHT) { // PPM - skok if (yPos == groundYPos) yVelocity = -12.f; } } // po puszczeniu LPM - zatrzymanie void mouseReleased() { if (mouseButton == LEFT) { mouseControl = false; xVelocity = 0; direction = 0; } }