1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Object oriented version of a game

Discussion in 'Java' started by blavatsky, Nov 30, 2011.

  1. blavatsky

    blavatsky New Member

    Joined:
    Nov 30, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    DJ
    Location:
    Helsinki
    Hi,

    I should modify a version of a ball dodge game to create an object oriented version of the game (make balls into objects). Don't really have an idea how I should start and what to modify. Thanks for your help in advance :)

    Code:
    public class Dodge { 
        public static void main(String[] args) {
            int MAXTHREATS = 50;
            int nthreats = 1;
            boolean gameOver = false;
            
            // set the scale of the coordinate system
            StdDraw.setXscale(-1.0, 1.0);
            StdDraw.setYscale(-1.0, 1.0);
    
            // initial values for the target
            double target_r = 0.04;
            double target_x = 0.04 + (0.92 * Math.random());
            double target_y = 0.04 + (0.92 * Math.random());
            
            // initial values for the player
            double player_r = 0.04;
            double player_x = 0.04 + (0.92 * Math.random());
            double player_y = 0.04 + (0.92 * Math.random());
            
            // initial values for the red balls;
            double threat_r = 0.02;
            double[][] threat = new double[MAXTHREATS][4];
            threat[0][0] = 0.02 + (0.96 * Math.random()); // x coordinate
            threat[0][1] = 0.02 + (0.96 * Math.random()); // y coordinate
            threat[0][2] = 0.01;                          // horizontal velocity
            threat[0][3] = 0.02;                          // vertical velocity
            
            while (!gameOver) {
              // draw the objects
              // clear the background
              StdDraw.setPenColor(StdDraw.GRAY);
              StdDraw.filledSquare(0, 0, 1.0);
    
              // draw target on the screen
              StdDraw.setPenColor(StdDraw.GREEN); 
              StdDraw.filledCircle(target_x, target_y, target_r);
            
              // draw player on the screen
              StdDraw.setPenColor(StdDraw.BLUE);
              StdDraw.filledCircle(player_x, player_y, player_r);
    
              // move and draw threats on the screen
              StdDraw.setPenColor(StdDraw.RED);
              for (int i=0; i<nthreats; i++) {
                if (Math.abs(threat[i][0] + threat[i][2]) > 1.0 - threat_r) threat[i][2] = -threat[i][2];
                if (Math.abs(threat[i][1] + threat[i][3]) > 1.0 - threat_r) threat[i][3] = -threat[i][3];
               
                // update positions
                threat[i][0] += threat[i][2];
                threat[i][1] += threat[i][3];
                
                // draw threats
                StdDraw.filledCircle(threat[i][0], threat[i][1], threat_r);
              }
    
              if (Math.sqrt((target_x - player_x)*(target_x - player_x) + (target_y - player_y)*(target_y - player_y)) <= target_r + player_r) {
                target_x = 0.04 + (0.92 * Math.random());
                target_y = 0.04 + (0.92 * Math.random());
                
                threat[nthreats][0] = 0.02 + (0.96 * Math.random());
                threat[nthreats][1] = 0.04 + (0.92 * Math.random());
                threat[nthreats][2] = 0.01;
                threat[nthreats][3] = 0.02;
                nthreats++;
              }
              
              double mouse_x = StdDraw.mouseX();
              double mouse_y = StdDraw.mouseY();
              if (mouse_x >= -1 + target_r && mouse_x <= 1 - target_r && mouse_y >= -1 + target_r && mouse_y <= 1 - target_r) {
                player_x = mouse_x;
                player_y = mouse_y;
              }
              
              // display and pause for 20 ms
              StdDraw.show(20);
              
              for (int i=0; i<nthreats; i++) {
                double distance_x = player_x - threat[i][0];
                double distance_y = player_y - threat[i][1];
                if (Math.sqrt((distance_x * distance_x) + (distance_y * distance_y)) <= player_r + threat_r)
                  gameOver = true;
              }
            }
        }
    }
     

Share This Page