Programmation d'un FPS sur Processing

Publié le par Aurélien

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à :

  1. import java.awt.AWTException;
  2. import java.awt.Robot; //to set mouse pos
  3. Robot robot;
  4. import java.awt.Frame; //to get window pos
  5. //mouse data
  6. float mx;
  7. float my;
  8. int aimAttenuation = 3;
  9. //crosshair y data
  10. int oy;
  11. final int oyMax = 256;
  12. final int oyMin = 0;
  13. //crosshair x data
  14. int ox;
  15. final int oxMax = 256;
  16. final int oxMin = 0;
  17. void setup() {
  18.   size(256, 256);
  19.   frameRate(30);
  20.   try { 
  21.     robot = new Robot();
  22.   } 
  23.   catch (AWTException e) {
  24.     e.printStackTrace();
  25.   }
  26.   noCursor();
  27. }
  28. void  draw() {
  29.   background(0);
  30.   //make it smooth !
  31.   if (abs(mouseX - mx) > 0.5) {
  32.     mx = mx + (mouseX - mx) / aimAttenuation;
  33.   } else {
  34.     mx = mouseX;
  35.   }
  36.   if (abs(mouseY - my) > 0.5) {
  37.     my = my + (mouseY - my) / aimAttenuation;
  38.   } else {
  39.     my = mouseY;
  40.   }
  41.   //draw smoothed mouse pos
  42.   ellipse(mx, my, 10, 10);
  43.   //calculate crosshair pos
  44.   oy = constrain(oy+(int)my-width/2, oyMin, oyMax);
  45.   ox = constrain(ox+(int)mx-height/2, oxMin, oxMax);
  46.   ellipse(ox, oy, 10, 10);
  47.   //center mouse
  48.   robot.mouseMove( (int)frame.getLocation().getX() + width/2 +3, 
  49. (int)frame.getLocation().getY() + height/2 +25);
  50. //+3 & +25 due to window borders...
  51. }
  52. void keyPressed() {
  53.   switch(keyCode) {
  54.     case(27):
  55.     exit();
  56.     break; //ESC
  57.   }
  58. }
 

 

Publié dans Programmation

Pour être informé des derniers articles, inscrivez vous :
Commenter cet article