Advertisement
JackerDeluxe

Processing - Pixel Runner

Jun 28th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. float counter = 0;
  2. float speed = 0.1;
  3. int index = 0;
  4. float spdV = 3;
  5. float spdH = 3;
  6. float x, y;
  7. ArrayList<PImage> sprites;
  8.  
  9. void setup() {
  10.   size(800, 800);
  11.   noSmooth();
  12.   imageMode(CENTER);
  13.  
  14.   x = width/2;
  15.   y = height/2;
  16.   sprites = new ArrayList<PImage>();
  17.  
  18.   PImage img = loadImage("spriteMan.png"); //GET FROM http://imgur.com/ShfsN99
  19.   for (int i = 0; i < 4; i++) {
  20.     sprites.add(img.get(16*i, 0, 16, 16));
  21.   }
  22. }
  23.  
  24. void draw() {
  25.   background(128);
  26.   walk(mouseX, mouseY);
  27.   animation(100);
  28. }
  29.  
  30. void walk(float tx, float ty) {
  31.   if (x != tx) {
  32.     if (dist(x, 0, tx, 0) < spdH) {
  33.       x = tx;
  34.     } else if (x < tx) {
  35.       x+=spdH;
  36.     } else {
  37.       x-=spdH;
  38.     }
  39.   }
  40.   if (y != ty) {
  41.     if (dist(y, 0, ty, 0) < spdV) {
  42.       y = ty;
  43.     } else if (y < ty) {
  44.       y+=spdV;
  45.     } else {
  46.       y-=spdV;
  47.     }
  48.   }
  49. }
  50.  
  51. void animation(float size) {
  52.   if (mouseX > x) {
  53.     image(sprites.get(index), x, y, -size, size);
  54.   } else {
  55.     pushMatrix();
  56.     translate(x, y);
  57.     scale(-1,1);
  58.     image(sprites.get(index), 0, 0, size, size);
  59.     popMatrix();
  60.   }
  61.  
  62.   counter += speed;
  63.   while (counter > 1 || counter < 0) {
  64.     if (counter > 1) {
  65.       index++;
  66.       counter-=1;
  67.       if (index > sprites.size()-1) {
  68.         index = 0;
  69.       }
  70.     } else if (counter < 0) {
  71.       index--;
  72.       counter+=1;
  73.       if (index < 0) {
  74.         index = sprites.size()-1;
  75.       }
  76.     }
  77.   }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement