bezo97

r/processing, [PWC44] Worms, https://redd.it/5mxutx

Jan 10th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.62 KB | None | 0 0
  1. // r/processing, [PWC44] Worms, https://redd.it/5mxutx , code by u/bezo97
  2. //feel free to improve it
  3. int g = 1000;//bullet gravity
  4.  
  5. //player1
  6. int p1pos = 1280/3;
  7. float p1ang = -PI/4;
  8.  
  9. //bullet1
  10. boolean b1flying = false;
  11. int b1startpos = p1pos;
  12. float b1ang = -PI/4;
  13. int b1dt=0;
  14.  
  15. //player2
  16. int p2pos = 2*1280/3;
  17. float p2ang = -3*PI/4;
  18.  
  19. //bullet2
  20. boolean b2flying = false;
  21. int b2startpos = p2pos;
  22. float b2ang = -3*PI/4;
  23. int b2dt=0;
  24.  
  25. int[] heights = new int[1280];//terrain
  26.  
  27. boolean[] keys = new boolean[8];//wasd ijkl //storing pressed keys in an array so more than one can be pressed at the same time
  28.  
  29. void setup()
  30. {
  31.   size(1280,720);
  32.   for(int x = 0; x < width; x++)//generate terrain
  33.     heights[x] = int(height/3+noise(x*0.005)*height/2);
  34. }
  35.  
  36. void draw()
  37. {
  38.   background(50,100,255);
  39.  
  40.   //draw terrain
  41.   loadPixels();
  42.   for(int x = 0; x < width; x++)
  43.     for(int y = heights[x]; y < height; y++)
  44.       pixels[x+y*width]=color(150,25,25);//earth
  45.   updatePixels();
  46.  
  47.   //p1 controls
  48.   if(keys[0])//a
  49.     p1pos += p1pos > 0 && heights[p1pos]-heights[p1pos-1]<2 ? -1 : 0; //not out of the window and not too steep
  50.   if(keys[1])//s
  51.     p1ang+=0.05;
  52.   if(keys[2])//d
  53.     p1pos += p1pos < width  && heights[p1pos]-heights[p1pos+1]<2 ? 1 : 0;
  54.   if(keys[3])//w
  55.     p1ang-=0.05;
  56.    
  57.   //p2 controls
  58.   if(keys[4])//j
  59.     p2pos += p2pos > 0  && heights[p2pos]-heights[p2pos-1]<2 ? -1 : 0;
  60.   if(keys[5])//k
  61.     p2ang-=0.05;
  62.   if(keys[6])//l
  63.     p2pos += p2pos < width   && heights[p2pos]-heights[p2pos+1]<2 ? 1 : 0;
  64.   if(keys[7])//i
  65.     p2ang+=0.05;
  66.      
  67.   ellipse(p1pos, heights[p1pos],15,25);//worm1
  68.   ellipse(p2pos, heights[p2pos],15,25);//worm2
  69.   line(p1pos+20*cos(p1ang), heights[p1pos]+20*sin(p1ang), p1pos+50*cos(p1ang), heights[p1pos]+50*sin(p1ang));//aim1
  70.   line(p2pos+20*cos(p2ang), heights[p2pos]+20*sin(p2ang), p2pos+50*cos(p2ang), heights[p2pos]+50*sin(p2ang));//aim2
  71.  
  72.   if(b1flying)
  73.   {
  74.     float b1pos = b1startpos+b1dt*cos(b1ang);
  75.     float b1height = heights[b1startpos]+b1dt*sin(b1ang)+b1dt*b1dt/g;
  76.     ellipse(b1pos,b1height,25,25);
  77.     b1dt+=5;//speed of the bullet (moves 5 pixels / draw)
  78.     if(b1height>heights[int(b1pos)])
  79.     {//bullet explodes
  80.       b1flying=false;
  81.       for(int x = int(b1pos)-75;x<b1pos+75;x++)//75 radius
  82.       {
  83.         heights[x]+=75-abs(x-b1pos);
  84.       }
  85.     }
  86.   }
  87.  
  88.   if(b2flying)
  89.   {
  90.     float b2pos = b2startpos+b2dt*cos(b2ang);
  91.     float b2height = heights[b2startpos]+b2dt*sin(b2ang)+b2dt*b2dt/g;
  92.     ellipse(b2pos,b2height,25,25);
  93.     b2dt+=5;
  94.     if(b2height>heights[int(b2pos)])
  95.     {//bullet explodes
  96.       b2flying=false;
  97.       for(int x = int(b2pos)-75;x<b2pos+75;x++)//75 radius
  98.       {
  99.         heights[x]+=75-abs(x-b2pos);
  100.       }
  101.     }
  102.   }
  103. }
  104.  
  105. void keyPressed() {
  106.   //p1
  107.   if (key == 'a')  keys[0] = true;
  108.   if (key == 's')  keys[1] = true;
  109.   if (key == 'd')  keys[2] = true;
  110.   if (key == 'w')  keys[3] = true;
  111.   if (key == 'e')
  112.   {//shoot bullet
  113.     b1flying=true;
  114.     b1startpos = p1pos;
  115.     b1ang = p1ang;
  116.     b1dt=0;
  117.   }
  118.   //p2
  119.   if (key == 'j')  keys[4] = true;
  120.   if (key == 'k')  keys[5] = true;
  121.   if (key == 'l')  keys[6] = true;
  122.   if (key == 'i')  keys[7] = true;
  123.   if (key == 'u')
  124.   {//shoot bullet
  125.     b2flying=true;
  126.     b2startpos = p2pos;
  127.     b2ang = p2ang;
  128.     b2dt=0;
  129.   }
  130. }
  131.  
  132. void keyReleased() {
  133.   //p1
  134.   if (key == 'a')  keys[0] = false;
  135.   if (key == 's')  keys[1] = false;
  136.   if (key == 'd')  keys[2] = false;
  137.   if (key == 'w')  keys[3] = false;
  138.   //p2
  139.   if (key == 'j')  keys[4] = false;
  140.   if (key == 'k')  keys[5] = false;
  141.   if (key == 'l')  keys[6] = false;
  142.   if (key == 'i')  keys[7] = false;
  143. }
Add Comment
Please, Sign In to add comment