Loading...
Logo
Processing Forum

Hello!

I made this simple game for a class. I am running into a few problems. First of all, the shooter at the bottom cannot move and shoot at the same time, which is a big downfall in terms of the interactivity. The other problem I have is with the swinging line at the top. The moving target is supposed to bounce off the line, which it does for the most part, but sometimes it does not. Maybe there is an error that I cannot catch?

For the game, you choose a color, then shoot the opposite color, using the left and right arrow keys to move and the space bar to shoot.

Any advice helps, thanks!


Copy code
  1. int page=1;
  2. PFont font;
  3. //CIRCLE
  4. float circleX=240;
  5. float circleY=160;
  6. float circleR=25;
  7. float speedX = 2.5;
  8. float lastStart;
  9. float m;
  10. int start;
  11. float time=.01;
  12. float speedY = 1.0;
  13. int directionX = 1;
  14. int directionY = -1;

  15. //RECTANGLE SHOOTER
  16. float rectX=240;
  17. float rectY=300;

  18. //RECTANGLE PENDULUM
  19. float rectPx=22.5;
  20. float rectPy=-30;
  21. //float rectPh=200;
  22. //float rectPw=45;
  23. float rectR=0;
  24. float speedP=1;
  25. float directionP=radians(1);
  26. float pendX=240;
  27. float pendY=-10;
  28. float pendX2;
  29. float pendY2;
  30. float angle;

  31. //CIRCLE COLOR CHANGE
  32. float pinkR=252;
  33. float pinkG=71;
  34. float pinkB=145;
  35. float blueR=33;
  36. float blueG=146;
  37. float blueB=249;
  38. float currentR;
  39. float currentG;
  40. float currentB;
  41. float r=252;
  42. float g=71;
  43. float b=145;
  44. float speedC=1;
  45. float colorCr=1.5;
  46. float colorCg=1.5;
  47. float colorCb=1.5;

  48. //BULLET
  49. float bullX=240;
  50. float bullY=300;
  51. float bullR=3;

  52. // BOOLEANS
  53. boolean Blue=false;
  54. boolean Pink=false;
  55. boolean shoot=false;
  56. boolean move= true;
  57. boolean right=false;
  58. boolean left=false;
  59. boolean hitting=false;

  60. //score
  61. int score=0;

  62. PImage instruct;

  63. void setup() {
  64.  size(480,320);
  65.  smooth();
  66.  rectMode(CENTER);
  67.  ellipseMode(RADIUS);
  68.  font=loadFont("AndaleMono-40.vlw");
  69.  start=millis();
  70.  instruct= loadImage("instruct.jpg");
  71.  lastStart=0;
  72. }

  73. void draw() {

  74.  if(page==1) {
  75.    intro();
  76.  }

  77.  if (page==2) {
  78.    gameStartPINK();
  79.  }
  80.  if (page==3){
  81.  restart();}
  82.  if (page==4){
  83.  gameStartBLUE();
  84.  }
  85. }


  86. void intro() {
  87.  noStroke();
  88.  background(0);
  89.  fill(75);
  90.  textSize(24);
  91.  text("CHOOSE A COLOR.",133,220);
  92.  ///ADD INTRO PAGE---------------------------------------------------
  93.  fill(252,71,145);
  94.  rect(160,120,100,100);
  95.  fill(33,146,249);
  96.  rect(320,120,100,100);
  97.  lastStart=millis();

  98.  if(mousePressed && move) {
  99.    float d=dist(mouseX,mouseY,210,170);
  100.    if(d<100){
  101.    page=2;
  102.    move=false;
  103.  }}
  104.    if(mousePressed && move) {
  105.    float d=dist(mouseX,mouseY,370,170);
  106.    if(d<100){
  107.    page=4;
  108.    move=false;
  109.  }}
  110. }

  111. //BEGIN GAME CHOOSE PINK==========================================

  112. void gameStartPINK() {
  113.  background(0);
  114.  m=millis()-lastStart;
  115.  println("m equals"+ m);
  116.  textSize(10);
  117.  text("SCORE:"+ score,410,15);
  118.  //BOTTOM RECTANGLE SHOOTER================================
  119.  fill(75);
  120.  noStroke();
  121.  float Rx=constrain(rectX,30,width-30);
  122.  rect(Rx,rectY,20,20);


  123.  //CIRCLE TARGET COLOR START PINK============================================
  124.  currentR=r;
  125.  currentG=g;
  126.  currentB=b;
  127.  println(currentR + " " + currentG + " " + currentB);

  128.  fill(currentR,currentG,currentB);

  129.  r-=speedC*colorCr;
  130.  if((r>=252) || (r<=33)) {
  131.    colorCr=-colorCr;
  132.  }
  133.  g+=speedC*colorCg;
  134.  if((g<=71) || (g>=146)) {
  135.    colorCg=-colorCg;
  136.  }

  137.  b+=speedC*colorCb;
  138.  if((b<=145) || (b>=249)) {
  139.    colorCb=-colorCb;
  140.  }
  141.  //   CIRCLE TARGET=========================================
  142.  noStroke();
  143.  ellipse(circleX,circleY,circleR,circleR);
  144.  circleX += (speedX+m/10000) * directionX;

  145.  if ((circleX > width-circleR) || (circleX < circleR)) {
  146.    directionX = -directionX;
  147.  }
  148.  circleY += speedY * directionY;
  149.  if ((circleY > 252) || (circleY < circleR)) {
  150.    directionY = -directionY;
  151.  }


  152.  if (key == CODED) {
  153.    if (keyCode == LEFT) {
  154.      if (keyPressed == true) {
  155.        rectX-=6;
  156.      }
  157.    }
  158.  }

  159.  if (key == CODED) {
  160.    if (keyCode == RIGHT) {
  161.      if (keyPressed == true) {
  162.        rectX+=6;
  163.      }
  164.    }
  165.  }

  166.  //  COLOR BOOLEAN===================================

  167.  if(currentR<=114 && currentR>=33) {

  168.    Blue=true;
  169.  }
  170.  else {
  171.    Blue=false;
  172.  }
  173.  if(Blue) {
  174.    println("BLUE");
  175.  }
  176.  // PENDULUM================================

  177.  float pendX2=((cos(angle))*250)+width/2;
  178.  float pendY2=(sin(angle))*250;
  179.  angle+=speedP*directionP;
  180.  stroke(75);
  181.  strokeWeight(5);
  182.  strokeCap(SQUARE);
  183.  line(pendX,pendY,pendX2,pendY2);

  184.  if((angle>PI) || (angle<0)) {
  185.    directionP=-directionP;
  186.  }

  187. if (side(pendX,pendY,pendX2,pendY2,circleX,circleY)<0){
  188. left=true;
  189. }

  190. if (side(pendX,pendY,pendX2,pendY2,circleX,circleY)>0){
  191. right=true;
  192. }

  193. //TARGET HIT PENDLULUM LEFT SIDE
  194.  if (circleLineIntersect(pendX, pendY,pendX2,pendY2, circleX, circleY, circleR) == true){
  195.    hitting=true;}
  196.    if (hitting ==true && left==true){
  197.      directionX=-directionX;
  198.      speedX=4.5;
  199.    hitting=false;
  200.    circleR=random(7,25);
  201.    }

  202. //TARGET HIT PENDULUM RIGHT SIDE
  203.    if (hitting ==true && right==true){
  204.      directionX=-directionX;
  205.      speedX=4.5;
  206.    hitting=false;
  207.    circleR=random(7,25);
  208.    }

  209.  // SHOOT BOOLEAN=======================
  210.  fill(75);
  211.  noStroke();
  212.  float Bx= constrain(bullX,30,width-40);
  213.  ellipse(Bx,bullY,bullR,bullR);
  214.  if (shoot == true) {
  215.    bullY = bullY - 35;
  216.  }

  217.  if (bullY < -10) {
  218.    shoot = false;
  219.  }

  220.  if (circlecircleIntersect(circleX,circleY,circleR,bullX,bullY,bullR) == true) {
  221.    if(Blue == true) {
  222.      shoot=false;
  223.      score+=1;
  224.      println(score);
  225.    }
  226.  }

  227.  if (circlecircleIntersect(circleX,circleY,circleR,bullX,bullY,bullR) == true && shoot ==true) {
  228.    if(Blue == false) {
  229.      shoot=false;
  230.      time=m;
  231.      page=3;
  232.    }
  233.  }

  234.  if(shoot == false) {
  235.    bullY=300;
  236.  }

  237. if (key == CODED) {
  238.    if (keyCode == LEFT) {
  239.      if (keyPressed == true) {
  240.        bullX-=6;
  241.      }
  242.    }
  243.  }

  244.  if (key == CODED) {
  245.    if (keyCode == RIGHT) {
  246.      if (keyPressed == true) {
  247.        bullX+=6;
  248.      }
  249.    }
  250.  }

  251. }

  252. // BEGIN GAME CHOOSE BLUE===============================================
  253. void gameStartBLUE() {
  254.  background(0);
  255.  m=millis()-lastStart;
  256.  println("m equals"+ m);
  257.  textSize(10);
  258.  text("SCORE:"+ score,410,15);
  259.  //BOTTOM RECTANGLE SHOOTER================================
  260.  fill(75);
  261.  noStroke();
  262.   float Rx=constrain(rectX,30,width-30);
  263.  rect(Rx,rectY,20,20);


  264.  //CIRCLE TARGET COLOR START BLUE============================================
  265.  currentR=r;
  266.  currentG=g;
  267.  currentB=b;
  268.  println(currentR + " " + currentG + " " + currentB);

  269.  fill(currentR,currentG,currentB);

  270.  r-=speedC*colorCr;
  271.  if((r>=252) || (r<=33)) {
  272.    colorCr=-colorCr;
  273.  }
  274.  g+=speedC*colorCg;
  275.  if((g<=71) || (g>=146)) {
  276.    colorCg=-colorCg;
  277.  }

  278.  b+=speedC*colorCb;
  279.  if((b<=145) || (b>=249)) {
  280.    colorCb=-colorCb;
  281.  }
  282.  //   CIRCLE TARGET=========================================
  283.  noStroke();
  284.  ellipse(circleX,circleY,circleR,circleR);
  285.  circleX += (speedX+ (m/10000)) * directionX;

  286.  if ((circleX > width-circleR) || (circleX < circleR)) {
  287.    directionX = -directionX;
  288.  }
  289.  circleY += speedY * directionY;
  290.  if ((circleY > 252) || (circleY < circleR)) {
  291.    directionY = -directionY;
  292.  }


  293.  if (key == CODED) {
  294.    if (keyCode == LEFT) {
  295.      if (keyPressed == true) {
  296.        rectX-=6;
  297.      }
  298.    }
  299.  }

  300.  if (key == CODED) {
  301.    if (keyCode == RIGHT) {
  302.      if (keyPressed == true) {
  303.        rectX+=6;
  304.      }
  305.    }
  306.  }

  307.  //  COLOR BOOLEAN===================================

  308.  if(currentR<=252 && currentR>=216) {

  309.    Pink=true;
  310.  }
  311.  else {
  312.    Pink=false;
  313.  }
  314.  if(Pink) {
  315.    println("PINK");
  316.  }
  317.  // PENDULUM================================

  318.  float pendX2=((cos(angle))*250)+width/2;
  319.  float pendY2=(sin(angle))*250;
  320.  angle+=speedP*directionP;
  321.  stroke(75);
  322.  strokeWeight(5);
  323.  strokeCap(SQUARE);
  324.  line(pendX,pendY,pendX2,pendY2);

  325.  if((angle>PI) || (angle<0)) {
  326.    directionP=-directionP;
  327.  }

  328. if (side(pendX,pendY,pendX2,pendY2,circleX,circleY)<0){
  329. left=true;
  330. }

  331. if (side(pendX,pendY,pendX2,pendY2,circleX,circleY)>0){
  332. right=true;
  333. }

  334. //TARGET HIT PENDLULUM LEFT SIDE
  335.  if (circleLineIntersect(pendX, pendY,pendX2,pendY2, circleX, circleY, circleR) == true){
  336.    hitting=true;}
  337.    if (hitting ==true && left==true){
  338.      directionX=-directionX;
  339.      speedX=4.5;
  340.    hitting=false;
  341.    circleR=random(7,25);
  342.    }

  343. //TARGET HIT PENDULUM RIGHT SIDE
  344.    if (hitting ==true && right==true){
  345.      directionX=-directionX;
  346.      speedX=4.5;
  347.    hitting=false;
  348.    circleR=random(7,25);
  349.    }



  350.  // SHOOT BOOLEAN=======================
  351.  fill(75);
  352.  noStroke();
  353.  float Bx= constrain(bullX,30,width-40);
  354.  ellipse(Bx,bullY,bullR,bullR);
  355.  if (shoot == true) {
  356.    bullY = bullY - 35;
  357.  }

  358.  if (bullY < -10) {
  359.    shoot = false;
  360.  }

  361.  if (circlecircleIntersect(circleX,circleY,circleR,bullX,bullY,bullR) == true) {
  362.    if(Pink == true) {
  363.      shoot=false;
  364.      score+=1;
  365.      println(score);
  366.    }
  367.  }

  368.  if (circlecircleIntersect(circleX,circleY,circleR,bullX,bullY,bullR) == true && shoot==true) {
  369.    if(Pink == false) {
  370.      shoot=false;
  371.      page=3;
  372.    }
  373.  }

  374.  if(shoot == false) {
  375.    bullY=300;
  376.  }

  377. if (key == CODED) {
  378.    if (keyCode == LEFT) {
  379.      if (keyPressed == true) {
  380.        bullX-=6;
  381.      }
  382.    }
  383.  }

  384.  if (key == CODED) {
  385.    if (keyCode == RIGHT) {
  386.      if (keyPressed == true) {
  387.        bullX+=6;
  388.      }
  389.    }
  390.  }

  391. }
  392. //CIRCLE LINE INTERSECT FUNCTION===============================================

  393. float side ( float Sx1, float Sy1, float Sx2, float Sy2, float pointSx, float pointSy )
  394. {
  395.  return (Sx2 - Sx1) * (pointSy - Sy1) - (Sy2 - Sy1) * (pointSx - Sx1);
  396. }

  397. boolean circlecircleIntersect(float Cx1, float Cy1, float Cr1, float Cx2, float Cy2, float Cr2) {
  398.  return dist(Cx1, Cy1, Cx2, Cy2) < Cr1 + Cr2;
  399. }

  400. //Following code adapted from http://www.openprocessing.org/visuals/?visualID=8009

  401. boolean circleLineIntersect(float x1, float y1, float x2, float y2, float cx, float cy, float cr ) {
  402.  float dx = x2 - x1;
  403.  float dy = y2 - y1;
  404.  float a = dx * dx + dy * dy;
  405.  float b = 2 * (dx * (x1 - cx) + dy * (y1 - cy));
  406.  float c = cx * cx + cy * cy;
  407.  c += x1 * x1 + y1 * y1;
  408.  c -= 2 * (cx * x1 + cy * y1);
  409.  c -= cr * cr;
  410.  float bb4ac = b * b - 4 * a * c;

  411.  //println(bb4ac);

  412.  if (bb4ac < 0) {  // Not intersecting
  413.    return false;
  414.  }
  415.  else {

  416.    float mu = (-b + sqrt( b*b - 4*a*c )) / (2*a);
  417.    float ix1 = x1 + mu*(dx);
  418.    float iy1 = y1 + mu*(dy);
  419.    mu = (-b - sqrt(b*b - 4*a*c )) / (2*a);
  420.    float ix2 = x1 + mu*(dx);
  421.    float iy2 = y1 + mu*(dy);

  422.    // The intersection points
  423.    //ellipse(ix1, iy1, 10, 10);
  424.    //ellipse(ix2, iy2, 10, 10);

  425.    float testX;
  426.    float testY;
  427.    // Figure out which point is closer to the circle
  428.    if (dist(x1, y1, cx, cy) < dist(x2, y2, cx, cy)) {
  429.      testX = x2;
  430.      testY = y2;
  431.    }
  432.    else {
  433.      testX = x1;
  434.      testY = y1;
  435.    }

  436.    if (dist(testX, testY, ix1, iy1) < dist(x1, y1, x2, y2) || dist(testX, testY, ix2, iy2) < dist(x1, y1, x2, y2)) {
  437.      return true;
  438.    }
  439.    else {
  440.      return false;
  441.    }
  442.  }
  443. }

  444. void keyPressed() {
  445.  if (key == ' ') {
  446.    shoot=true;
  447.  }
  448. }

  449. //RESTART================================================
  450. void restart(){
  451. background(0);
  452. fill(75);
  453. textSize(50);
  454. text("YOU LOSE",120,160);
  455. fill(255);
  456. textSize(10);
  457. text("Click to restart.",200,180);
  458. score=0;


  459. if(mousePressed && move){
  460. move=false;
  461. page=1;
  462. }
  463. }


  464. //MOVE BOOLEAN===================================
  465. void mouseReleased()
  466. {
  467.  move=true;
  468. }









Replies(1)