Basic Game

edited January 2016 in Questions about Code

Hello!

I need some help creating a game if possible, I started taking lessons in processing at school recently and I had some team project where I recently found out that I am the only one that did his part and I kinda found myself short on time to finish it all by myself and I can't figure it out; Here's what I managed to do untill now:

1. Red Moving Bar(Arrow Keys)

int px=350; int py=750; int taillex=100; int tailley=25; int level;

// PALET void palet() { fill(255,0,0); rect(px,py,taillex,tailley); } // keyPressed // LEFT RIGHT void keyPressed() { if (key == CODED) { if(keyCode == LEFT) { px=px-5; } if(keyCode == RIGHT) { px=px+5; } } if (px<0) {px=699;} if (px>700) {px=0;} // Q D if (keyPressed) { if (key == 'q' || key == 'Q') { px=px-5; } if (key == 'd' || key == 'D') { px=px+5;} } }

// BALL void ball() { int ballx=400; int ballsize=25; int bally=50; float ballcolor=random(255); fill (ballcolor); ellipse(ballx,bally,ballsize,ballsize); while(bally<height) {bally=bally++;}

} void setup () { size (800,800); background(255); } void draw () { background(255); palet (); keyPressed ();

}

2.Switch Screen

int level=0; void draw () { if(level==0){ open(); } else if(level==1){ game(); } else {gameover(); } }

void open () { if (buttonpressed()){ level++; }

} boolean buttonPressed () { if (x<mouseX et mouseX<x+hy et y<mouseY et mouseY<y+hhy et mousePressed){ return true; } }

I'm basically trying to make a simple game where you have to either bounce balls off or avoid them, becoming a bit harder over time The level part is basically trying to get a screen in the beginning with something like press here/any key to start and then game over, press here to try again

Thanks for the help!

Tagged:

Answers

  • Answer ✓

    I'm on it

  • Answer ✓

    you need some tricks to reflect the ball on the screen borders

    // topic: 
    // move a Red Moving Bar with Arrow Keys
    
    //--------------------------------------
    // global vars  
    
    int px=350; 
    int py=750; 
    
    int taillex=100; 
    int tailley=25;
    
    int level=0;
    
    // the ball  
    int ballx; 
    int bally; 
    
    int ballsize;
    
    color ballcolor;
    
    float ballSpeedX;
    float ballSpeedY; 
    
    // -------------------------------------
    // core functions 
    
    void setup () { 
      size (800, 800); 
      background(255);
      // reset
      reset();
    } 
    
    void draw () {
      background(255);
      // Switch Screen
      if (level==0) { 
        open();
      } else if (level==1) { 
        game();
      } else  if (level==2) {
        gameover();
      } else {
        // big error
        println ("big error");
        exit(); // terminates
      }
    }// func 
    
    // -------------------------------------
    // other functions 
    
    // PALET 
    void palet() { 
      fill(255, 0, 0); 
      rect(px, py, taillex, tailley);
    } 
    
    // keyPressed 
    // LEFT RIGHT 
    void keyPressed() { 
      if (level==1) { 
        if (key == CODED) { 
          if (keyCode == LEFT) { 
            px=px-5;
          } 
          if (keyCode == RIGHT) { 
            px=px+5;
          }
        }// if 
    
        if (px<0) {
          px=699;
        } 
        if (px>700) {
          px=0;
        } 
    
        // Q and D
        if (key == 'q' || key == 'Q') { 
          px=px-5;
        } 
        if (key == 'd' || key == 'D') { 
          px=px+5;
        }
      }// if
    }//func 
    
    // BALL 
    void ball() { 
      // runs the ball while playing
      // show ball
      fill (ballcolor); 
      ellipse(ballx, bally, ballsize, ballsize);
    
      // move ball
      ballx+=ballSpeedX;
    
      if (bally<height-60) {
        bally+=ballSpeedY;
      } else {
        // lower border
        // On palet? 
        if (ballx>px && ballx<px+taillex) {
          ballSpeedY=-1*abs(ballSpeedY);
          // bally=height-60;
          bally+=ballSpeedY;
        } else { 
          level=2;
        }
      }//else
    }//func 
    
    void open () { 
      fill(255, 0, 0);
      text("WELCOME"
        +"\n"+"\n"+"\n"
        +"Please press Mouse", 200, height/2); 
      if (buttonPressed()) { 
        level++;
      }
    } 
    
    void game() { 
      ball();
      palet();
    }
    
    void gameover() {
      fill(255, 0, 0);
      text ("Game over", 100, height / 2 );
      if (buttonPressed()) { 
        level=0; // reset
        reset();
      }
    }
    
    void reset() {
      // reset
      ballx=int(random(60)); 
      bally=50; 
    
      ballsize=25;
    
      ballcolor=color(random(255), random(255), random(255));
    
      ballSpeedX=1;
      ballSpeedY=2;
    }
    
    boolean buttonPressed () { 
    
      int x=0; 
      int y=0; 
      int w=width;
      int h=height;
    
      if (x<mouseX && mouseX<x+w && 
        y<mouseY && mouseY<y+h && 
        mousePressed) { 
        return true;
      }
      // otherwise 
      return false;
    }
    //
    
  • edited January 2016 Answer ✓

    alternatively you could start with 3 Lives and on each miss say

    Lives--;

    (which means to decrease Lives by 1)

    and when you are down to 0 lives, say game over

Sign In or Register to comment.