Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // r/processing, [PWC44] Worms, https://redd.it/5mxutx , code by u/bezo97
- //feel free to improve it
- int g = 1000;//bullet gravity
- //player1
- int p1pos = 1280/3;
- float p1ang = -PI/4;
- //bullet1
- boolean b1flying = false;
- int b1startpos = p1pos;
- float b1ang = -PI/4;
- int b1dt=0;
- //player2
- int p2pos = 2*1280/3;
- float p2ang = -3*PI/4;
- //bullet2
- boolean b2flying = false;
- int b2startpos = p2pos;
- float b2ang = -3*PI/4;
- int b2dt=0;
- int[] heights = new int[1280];//terrain
- boolean[] keys = new boolean[8];//wasd ijkl //storing pressed keys in an array so more than one can be pressed at the same time
- void setup()
- {
- size(1280,720);
- for(int x = 0; x < width; x++)//generate terrain
- heights[x] = int(height/3+noise(x*0.005)*height/2);
- }
- void draw()
- {
- background(50,100,255);
- //draw terrain
- loadPixels();
- for(int x = 0; x < width; x++)
- for(int y = heights[x]; y < height; y++)
- pixels[x+y*width]=color(150,25,25);//earth
- updatePixels();
- //p1 controls
- if(keys[0])//a
- p1pos += p1pos > 0 && heights[p1pos]-heights[p1pos-1]<2 ? -1 : 0; //not out of the window and not too steep
- if(keys[1])//s
- p1ang+=0.05;
- if(keys[2])//d
- p1pos += p1pos < width && heights[p1pos]-heights[p1pos+1]<2 ? 1 : 0;
- if(keys[3])//w
- p1ang-=0.05;
- //p2 controls
- if(keys[4])//j
- p2pos += p2pos > 0 && heights[p2pos]-heights[p2pos-1]<2 ? -1 : 0;
- if(keys[5])//k
- p2ang-=0.05;
- if(keys[6])//l
- p2pos += p2pos < width && heights[p2pos]-heights[p2pos+1]<2 ? 1 : 0;
- if(keys[7])//i
- p2ang+=0.05;
- ellipse(p1pos, heights[p1pos],15,25);//worm1
- ellipse(p2pos, heights[p2pos],15,25);//worm2
- line(p1pos+20*cos(p1ang), heights[p1pos]+20*sin(p1ang), p1pos+50*cos(p1ang), heights[p1pos]+50*sin(p1ang));//aim1
- line(p2pos+20*cos(p2ang), heights[p2pos]+20*sin(p2ang), p2pos+50*cos(p2ang), heights[p2pos]+50*sin(p2ang));//aim2
- if(b1flying)
- {
- float b1pos = b1startpos+b1dt*cos(b1ang);
- float b1height = heights[b1startpos]+b1dt*sin(b1ang)+b1dt*b1dt/g;
- ellipse(b1pos,b1height,25,25);
- b1dt+=5;//speed of the bullet (moves 5 pixels / draw)
- if(b1height>heights[int(b1pos)])
- {//bullet explodes
- b1flying=false;
- for(int x = int(b1pos)-75;x<b1pos+75;x++)//75 radius
- {
- heights[x]+=75-abs(x-b1pos);
- }
- }
- }
- if(b2flying)
- {
- float b2pos = b2startpos+b2dt*cos(b2ang);
- float b2height = heights[b2startpos]+b2dt*sin(b2ang)+b2dt*b2dt/g;
- ellipse(b2pos,b2height,25,25);
- b2dt+=5;
- if(b2height>heights[int(b2pos)])
- {//bullet explodes
- b2flying=false;
- for(int x = int(b2pos)-75;x<b2pos+75;x++)//75 radius
- {
- heights[x]+=75-abs(x-b2pos);
- }
- }
- }
- }
- void keyPressed() {
- //p1
- if (key == 'a') keys[0] = true;
- if (key == 's') keys[1] = true;
- if (key == 'd') keys[2] = true;
- if (key == 'w') keys[3] = true;
- if (key == 'e')
- {//shoot bullet
- b1flying=true;
- b1startpos = p1pos;
- b1ang = p1ang;
- b1dt=0;
- }
- //p2
- if (key == 'j') keys[4] = true;
- if (key == 'k') keys[5] = true;
- if (key == 'l') keys[6] = true;
- if (key == 'i') keys[7] = true;
- if (key == 'u')
- {//shoot bullet
- b2flying=true;
- b2startpos = p2pos;
- b2ang = p2ang;
- b2dt=0;
- }
- }
- void keyReleased() {
- //p1
- if (key == 'a') keys[0] = false;
- if (key == 's') keys[1] = false;
- if (key == 'd') keys[2] = false;
- if (key == 'w') keys[3] = false;
- //p2
- if (key == 'j') keys[4] = false;
- if (key == 'k') keys[5] = false;
- if (key == 'l') keys[6] = false;
- if (key == 'i') keys[7] = false;
- }
Add Comment
Please, Sign In to add comment