0

我正在尝试初始化突破游戏,以便游戏从在指定位置添加到画布的桨开始,然后用鼠标移动相同的桨。

突破图形用户界面

所以,1. 创建桨并添加到画布上 2. 移动桨,因为它跟踪鼠标的位置。

问题:添加到画布的桨保持原位,但另一个未添加到画布的桨根据事件侦听器移动。一个桨保持静止,另一个跟踪鼠标。我已将 add(paddle) 语句移至鼠标

ENV:Mac OSX 10.8.4,JVM 1.6(随 OSX 一起安装)

java中是否有某种设置需要设置为在鼠标事件时刷新桨?还是环境问题?

/*
 * 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;
        }
    }
}
4

2 回答 2

0

您可能只需要repaint()在移动桨后打电话。

于 2013-07-03T15:42:25.230 回答
0

原来我出于错误的原因调用了 init() 过程。我重命名了被调用导致的问题。我将功能更改为其他功能,一切正常。试图弄清楚那里发生了什么

于 2013-07-26T09:47:07.120 回答