Programmation d'un FPS sur Processing
Après quelques recherches, voilà comment programmer un FPS sous Processing :
Pour les touches, j'ai trouvé cet exemple.
Pour la visée, ben je viens de le faire avec des infos glanée par-ci par-là :
- import java.awt.AWTException;
- import java.awt.Robot; //to set mouse pos
- Robot robot;
- import java.awt.Frame; //to get window pos
- //mouse data
- float mx;
- float my;
- int aimAttenuation = 3;
- //crosshair y data
- int oy;
- final int oyMax = 256;
- final int oyMin = 0;
- //crosshair x data
- int ox;
- final int oxMax = 256;
- final int oxMin = 0;
- void setup() {
- size(256, 256);
- frameRate(30);
- try {
- robot = new Robot();
- }
- catch (AWTException e) {
- e.printStackTrace();
- }
- noCursor();
- }
- void draw() {
- background(0);
- //make it smooth !
- if (abs(mouseX - mx) > 0.5) {
- mx = mx + (mouseX - mx) / aimAttenuation;
- } else {
- mx = mouseX;
- }
- if (abs(mouseY - my) > 0.5) {
- my = my + (mouseY - my) / aimAttenuation;
- } else {
- my = mouseY;
- }
- //draw smoothed mouse pos
- ellipse(mx, my, 10, 10);
- //calculate crosshair pos
- oy = constrain(oy+(int)my-width/2, oyMin, oyMax);
- ox = constrain(ox+(int)mx-height/2, oxMin, oxMax);
- ellipse(ox, oy, 10, 10);
- //center mouse
- robot.mouseMove( (int)frame.getLocation().getX() + width/2 +3,
- (int)frame.getLocation().getY() + height/2 +25);
- //+3 & +25 due to window borders...
- }
- void keyPressed() {
- switch(keyCode) {
- case(27):
- exit();
- break; //ESC
- }
- }