Loading...
Logo
Processing Forum
Simple Game, i want the ballto bounce of the rectangle, but for some reason(most likely obvious) it just floats pass :( here is the code and thank you for your help!!

Harrison.




Copy code
  1. float x = 400; // postion of the player
  2. float y = 193; //postion of player
  3. float speed = 10; // movement of player speed


  4. int size = 20;       // Width of the shape
  5. float xpos, ypos;    // Starting position of shape    

  6. float xspeed = 2.8;  // Speed of the shape
  7. float yspeed = 2.2;  // Speed of the shape

  8. int xdirection = 1;  // Left or Right
  9. int ydirection = 1;  // Top to Bottom
  10. int score = 50;

  11. void setup() 
  12. {
  13.   size(640, 200);
  14.   noStroke();
  15.   frameRate(30);
  16.   smooth();
  17.   // Set the starting position of the shape
  18.   xpos = width/2;
  19.   ypos = height/2;
  20. }

  21. void draw() 
  22. {
  23.   background(102);
  24.   moveright();
  25.   moveleft();
  26.   display();
  27.   collosions();
  28.   
  29.   // Update the position of the shape
  30.  // xpos = xpos + ( xspeed * xdirection );
  31.  // ypos = ypos + ( yspeed * ydirection );
  32.   
  33.   // Test to see if the shape exceeds the boundaries of the screen
  34.   // If it does, reverse its direction by multiplying by -1
  35.   if (xpos > width-size || xpos < 0) {
  36.     xdirection *= -1;
  37.   }
  38.   if (ypos > height-size) {
  39.   //  ydirection *= -1;
  40.     score += 1;
  41.     xspeed += 0.1;
  42.     yspeed += 0.1;
  43.   }
  44.     if (score == 100)  {
  45.       print("you win!");
  46.       exit(); 
  47.         
  48.   }
  49.   
  50.   if ( ypos < 0) {
  51.      ydirection *= -1;
  52.      score -= 1;
  53.  }
  54.      if (score == 0){
  55.        
  56.        print("game over");
  57.        exit();
  58.        
  59.      }
  60.      

  61.   // Draw the shape
  62.   float col= random(255);
  63.   float col2 = random(255);
  64.   float col3 = random(255);
  65.   fill(col, col2, col3);
  66.   ellipse(xpos+size/2, ypos+size/2, size, size);
  67.   print(score);
  68. }

  69. void moveright() {
  70.   if (keyPressed) {
  71.     if (key == 'd'){
  72.       x = x+speed;
  73.     }
  74.   }
  75.   if ((x + 20) < 0) {
  76.     x = 600;
  77. }
  78. }

  79. void moveleft() {
  80.   if (keyPressed){
  81.     if (key == 'a'){
  82.       x = x - speed;
  83.     }
  84.   }
  85.   if ((x + 20) > 640){
  86.     x = 40;
  87.   }
  88. }
  89. void display() {
  90.   fill(255);
  91.   rect(x,y,40,7);
  92. }

  93. void collosions() {
  94.   xpos = xpos + ( xspeed * xdirection );
  95.   ypos = ypos + ( yspeed * ydirection );
  96.   if(xpos == x){ 
  97.     ydirection *= -1; 
  98.   }
  99. }
  100.   
  101.     
  102.  

Astrophysics!

Replies(4)

this seems to work 
Copy code

  1. float x = 400; // postion of the player
  2. float y = 193; //postion of player
  3. float speed = 10; // movement of player speed


  4. int size = 20;       // Width of the shape
  5. float xpos, ypos;    // Starting position of shape    

  6. float xspeed = 2.8;  // Speed of the shape
  7. float yspeed = 2.2;  // Speed of the shape


  8. int score = 50;

  9. void setup() 
  10. {
  11.   size(640, 200);
  12.   noStroke();
  13.   frameRate(30);
  14.   smooth();
  15.   // Set the starting position of the shape
  16.   xpos = width/2;
  17.   ypos = height/2;
  18. }

  19. void draw() 
  20. {
  21.   background(102);
  22. move();
  23. moveright();
  24. moveleft();
  25.   if (xpos > width-size || xpos < 0) {
  26.     xspeed *= -1;
  27.         print(score);
  28.   }
  29.   if(ypos>=y-size&&abs(x-xpos)<=20){
  30.     if(yspeed>0){
  31.     yspeed*=-1;
  32.     }

  33.   }
  34.   if (ypos > height-size) {
  35. yspeed*=-1;
  36.     score += 1;
  37.     xspeed += 0.1;
  38.     yspeed += 0.1;
  39.   }
  40.     if (score == 100)  {
  41.       print("you win!");
  42.       exit(); 
  43.         
  44.   }
  45.   
  46.   if ( ypos < 0) {
  47.      yspeed *= -1;
  48.      score -= 1;
  49.  }
  50.      if (score == 0){
  51.        
  52.        print("game over");
  53.        exit();
  54.        
  55.      }
  56.      

  57.   // Draw the shape
  58.   float col= random(255);
  59.   float col2 = random(255);
  60.   float col3 = random(255);
  61.   fill(col, col2, col3);
  62.   ellipse(xpos+size/2, ypos+size/2, size, size);

  63.    rect(x,y,40,7);
  64. }
  65. void move(){
  66.   xpos = xpos+xspeed;
  67.   ypos=ypos+yspeed;
  68. }
  69.   
  70.   void moveright() {
  71.   if (keyPressed) {
  72.     if (key == 'd'){
  73.       x = x+speed;
  74.     }
  75.   }
  76.   if ((x + 20) < 0) {
  77.     x = 600;
  78. }
  79. }

  80. void moveleft() {
  81.   if (keyPressed){
  82.     if (key == 'a'){
  83.       x = x - speed;
  84.     }
  85.   }
  86.   if ((x + 20) > 640){
  87.     x = 40;
  88.   }
  89. }
  90.     
 
you don't need xdirectionyou can just multiply xspeed by -1 each time it hits
did you ever get the score to display on the screen or the lose screen?


Yeah, In the end the game was working very well if you go here  http://epiphanystudios.co.uk/games/ there is an example of a slightly more advance game with score and lives being displayed :)