Advertisement
DJSandmannTV

animation.java

Dec 7th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.58 KB | None | 0 0
  1. import processing.core.*;
  2.  
  3. public class animation extends PApplet{
  4.  
  5.     public static void main(String[] args) {
  6.         PApplet.main("animation");
  7.     }
  8.    
  9.     controls keyboard;
  10.    
  11.     PImage[] images = new PImage[14];
  12.     PImage bg;
  13.     PFont fpsFont;
  14.    
  15.     public void setup() {
  16.        
  17.         frameRate(60);
  18.        
  19.     }
  20.    
  21.     public void settings() {
  22.        
  23.         fullScreen(P2D);
  24.         loop();
  25.        
  26.         for(int i = 0; i < images.length; i++) {
  27.             images[i] = loadImage("data/harambe/frame" + i + ".png");
  28.             System.out.println("#// loaded 'data/harambe/frame" + i + ".png'");
  29.         }
  30.        
  31.         bg = loadImage("data/bg.jpg");
  32.                        
  33.         System.out.println("#// LOADED ALL DATA");
  34.         System.out.println();
  35.        
  36.     }
  37.    
  38.     int frame = 0;
  39.     float framefloat = 0;
  40.     float xpos = 100,
  41.             ypos = 100,
  42.             triggerrate = 0;
  43.    
  44.     int speed = 1;
  45.     int dir = 1;
  46.     boolean moving = false,
  47.             triggered = false;
  48.    
  49.     boolean goUp, goDown, goLeft, goRight;
  50.    
  51.     float curTime = millis(), prevTime = millis(), elapTime = 0;
  52.    
  53.     public void draw() {
  54.        
  55.         curTime = millis();
  56.         elapTime = (float)1e-3 * (curTime - prevTime);
  57.         prevTime = curTime;
  58.        
  59.         if(moving) {
  60.             framefloat = framefloat + images.length * elapTime;
  61.         }
  62.        
  63.         else {
  64.             framefloat = 0;
  65.         }
  66.        
  67.         frame = (int)framefloat;
  68.        
  69.         if(framefloat > images.length) { framefloat = framefloat - images.length; }
  70.         if(frame > images.length - 1) { frame = frame - images.length; }
  71.        
  72.        
  73.         image(bg, 0, 0, (int)(height * 1.8), height);
  74.         fill(0);
  75.         textSize(20);
  76.         textAlign(LEFT);
  77.         text("FPS: " + frameRate, 0, 20);
  78.        
  79.         fill(triggerrate / 100 * 255, 0, 0);
  80.         textAlign(CENTER);
  81.         textSize(20 + triggerrate);
  82.         text(
  83.                 (int)(triggerrate) + "%",
  84.                 xpos + images[0].width / 2 + triggerrate / 20 * random(-1, 1),
  85.                 ypos - triggerrate + triggerrate / 20 * random(-1, 1));
  86.        
  87.         if (triggered && triggerrate > 100) { triggered = false; }
  88.        
  89.        
  90.         pushMatrix();
  91.        
  92.             scale(dir, 1);
  93.            
  94.             if(triggered) { translate(triggerrate * random(-1, 1),
  95.                                       triggerrate * random(-1, 1));
  96.                            
  97.                             triggerrate = (float)(triggerrate + 20 * elapTime);
  98.                            
  99.                             tint(255, 255 - triggerrate / 100 * 255, 255 - triggerrate / 100 * 255);
  100.                            
  101.                             }
  102.             else if(triggerrate > 0) {
  103.                 translate(triggerrate * random(-1, 1),
  104.                           triggerrate * random(-1, 1));
  105.                
  106.                 triggerrate = (float)(triggerrate - 20 * elapTime);
  107.                
  108.                 tint(255, 255 - triggerrate / 100 * 255, 255 - triggerrate / 100 * 255);
  109.             }
  110.            
  111.             if(dir == -1) { translate(- images[0].width, 0); }
  112.             image(images[frame], xpos * dir, ypos);
  113.             tint(255);
  114.            
  115.         popMatrix();
  116.        
  117.         moving = false;
  118.        
  119.        
  120.         keyboard.check();
  121.        
  122.         if(keyboard.goUp) {
  123.             if(ypos > 0) {
  124.                 moving = true;
  125.                 ypos = ypos - speed * 100 * elapTime;
  126.             }
  127.         }
  128.        
  129.         if(keyboard.goDown) {
  130.             if(ypos < height - images[0].height) {
  131.                 moving = true;
  132.                 ypos = ypos + speed * 100 * elapTime;
  133.             }
  134.         }
  135.        
  136.         if(keyboard.goLeft) {
  137.             if(xpos > 0) {
  138.                 dir = 1;
  139.                 moving = true;
  140.                 xpos = xpos - speed * 100 * elapTime;
  141.             }
  142.         }
  143.        
  144.         if(keyboard.goRight) {
  145.             if(xpos < width - images[0].width) {
  146.                 dir = -1;
  147.                 moving = true;
  148.                 xpos = xpos + speed * 100 * elapTime;
  149.             }
  150.         }
  151.        
  152.         if(keyPressed && key == 't' && !triggered) {
  153.             triggered = true;
  154.         }
  155.  
  156.     }
  157.    
  158.         /*
  159.         boolean setMove(int key, boolean b) {
  160.        
  161.             switch (key) {
  162.            
  163.             case UP:
  164.                
  165.                 return goUp = b;
  166.                    
  167.             case DOWN:
  168.                
  169.                 return goDown = b;
  170.                
  171.             case LEFT:
  172.                
  173.                 return goLeft = b;
  174.                
  175.             case RIGHT:
  176.                
  177.                 return goRight = b;
  178.                
  179.             default:
  180.                
  181.                 return b;
  182.                
  183.             }
  184.        
  185.        
  186.         }
  187.         */
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement