I'm trying to initialize the breakout game so that the game starts with the paddle added to the canvas at a specified location then have the same paddle move with the mouse.

Breakout GUI

So, 1. create paddle and add on canvas 2. move paddle as it tracks the mouse's position.

PROBLEM: Paddle added to canvas stays where it is but another paddle NOT ADDED to canvas moves according to event listener. One paddle stays stationary and another tracks the mouse. I have moved the add(paddle) statement to the mouse

ENV: Mac OSX 10.8.4 , JVM 1.6 (installed with OSX)

Is there some kind of setting in java that needs to be set to refresh the paddle upon mouse event? or is it an environmental issue?

/*
 * File: Breakout.java
 * -------------------
 * Name:
 * Section Leader:
 * 
 * This file will eventually implement the game of Breakout.
 */

import acm.graphics.*;
import acm.program.*;
import acm.util.*;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Breakout extends GraphicsProgram {

/** Width and height of application window in pixels */
    public static final int APPLICATION_WIDTH = 400;
    public static final int APPLICATION_HEIGHT = 600;

/** Dimensions of game board
 *  Should not be used directly (use getWidth()/getHeight() instead).
 *  * */
    private static final int WIDTH = APPLICATION_WIDTH;
    private static final int HEIGHT = APPLICATION_HEIGHT;

/** Dimensions of the paddle */
    private static final int PADDLE_WIDTH = 60;
    private static final int PADDLE_HEIGHT = 10;

/** Offset of the paddle up from the bottom */
    private static final int PADDLE_Y_OFFSET = 30;

/** Number of bricks per row */
    private static final int NBRICKS_PER_ROW = 10;

/** Number of rows of bricks */
    private static final int NBRICK_ROWS = 10;

/** Separation between bricks */
    private static final int BRICK_SEP = 4;

/** Width of a brick */
    private static final int BRICK_WIDTH =
      (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;

/** Height of a brick */
    private static final int BRICK_HEIGHT = 8;

/** Radius of the ball in pixels */
    private static final int BALL_RADIUS = 10;

/** Offset of the top brick row from the top */
    private static final int BRICK_Y_OFFSET = 70;

/** Number of turns */
    private static final int NTURNS = 3;

/** ADDED KNM Private Instance Variables */
    private GRect paddle;

/* Method: run() */
/** Runs the Breakout program. */
    public void run() { 
        /* You fill this in, along with any subsidiary methods */
        init();

    }

    public void init(){
        addMouseListeners();
        setSize(APPLICATION_WIDTH,APPLICATION_HEIGHT);
        prepTiles();
        prepPaddle();
    }

    private void prepTiles(){

        for (int i = 1; i <= NBRICK_ROWS; i++){
            int x_pos_start = 0;//(CANVAS_MIDDLE - BRICK_WIDTH/2) - (NBRICKS_PER_ROW/2 - 2)*BRICK_WIDTH;
            int y_pos = BRICK_Y_OFFSET + (i-1)*(BRICK_HEIGHT+BRICK_SEP);
            Color BRICK_COLOR = getBRICK_COLOR(i);          

            //case for color fills
            for(int y = 1;  y <= NBRICKS_PER_ROW; y++ ){
                //adjust to beginning of row then add bricks
                int x_pos = x_pos_start + ((y-1)*(BRICK_WIDTH+BRICK_SEP));
                G3DRect grect = new G3DRect(x_pos, y_pos, BRICK_WIDTH, BRICK_HEIGHT);
                grect.setFilled(true);
                grect.setColor(BRICK_COLOR);
                add(grect);

            }
        }
    }

    public void prepPaddle(){
        double x_pos = APPLICATION_WIDTH/2 - PADDLE_WIDTH/2;
        double y_pos = APPLICATION_HEIGHT - PADDLE_HEIGHT - PADDLE_Y_OFFSET;
        paddle = new GRect(x_pos, y_pos, PADDLE_WIDTH, PADDLE_HEIGHT);
        paddle.setFilled(true);
        paddle.setColor(Color.DARK_GRAY);
        add(paddle); // want to keep this.
    }

    public void mouseMoved(MouseEvent e){
        // add(paddle); // I tried moveing the statement here but, that doesn't create the paddle until the mouse hovers above the application canvas.
        paddle.setLocation(e.getX() - PADDLE_WIDTH/2, paddle.getY());
        if (paddle.getX() <= 0) paddle.setLocation(0, paddle.getY());
        if (paddle.getX() + PADDLE_WIDTH >= getWidth()) paddle.setLocation(getWidth() - PADDLE_WIDTH, paddle.getY());
    }

    private Color getBRICK_COLOR(int i_row){
        switch (i_row){
        case 1 : 
        case 2 : return Color.RED;
        case 3 : 
        case 4 : return Color.ORANGE;
        case 5 : 
        case 6 : return Color.yellow;
        case 7 : 
        case 8 : return Color.green;
        case 9 : 
        case 10: return Color.cyan;
        default: return Color.black;
        }
    }
}
有帮助吗?

解决方案 2

turns out i was calling init() procedure for the wrong reasons. I renamed the called caused the problem. i changed the function to something else and it all worked out fine. trying to figure out what happened ther

其他提示

You probably just need to call repaint() after moving the paddle.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top