首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Java越狱游戏中画砖块?

如何在Java越狱游戏中画砖块?
EN

Stack Overflow用户
提问于 2018-10-16 05:54:55
回答 1查看 0关注 0票数 0

有一个突破性的游戏,部分为我创造; 目前它只有弹跳球和蝙蝠; 球没有砖块可以击中。

我需要添加代码来生成砖块,但我正在努力; 我不知道如何解决这个问题,因为我对Java GUI不是很了解。

我已经包含了需要添加代码的类; 需要代码的区域写在注释中

Modelbreakout类:

代码语言:javascript
复制
package breakout;

import java.util.Observable;
import static breakout.Global.*;
/**
 * Model of the game of breakout
 *  The active object ActiveModel does the work of moving the ball
 * @author Mike Smith University of Brighton
 */

public class ModelBreakout extends Observable
{
  private GameObject ball;      // The ball
  private GameObject bricks[];  // The bricks
  private GameObject bat;       // The bat

  private ModelActivePart am  = new ModelActivePart( this );
  private Thread activeModel  = new Thread( am );

  private int score = 0;

  public void createGameObjects()
  {
    ball   = new GameObject(W/2, H/2, BALL_SIZE, BALL_SIZE, Colour.RED );
    bat    = new GameObject(W/2, H - BRICK_HEIGHT*4, BRICK_WIDTH*3, 
                            BRICK_HEIGHT, Colour.GRAY);
    bricks = new GameObject[BRICKS];



    // *[1]**********************************************************
    // * Fill in code to place the bricks on the board              *
    // **************************************************************




  }

  public void startGame()             { activeModel.start(); }

  public GameObject getBall()         { return ball; }

  public GameObject[] getBricks()     { return bricks; }

  public GameObject getBat()          { return bat; }

  public void addToScore( int n )     { score += n; }

  public int getScore()               { return score; }

  public void stopGame()              { }


  /**
   * Move the bat dist pixels. (-dist) is left or (+dist) is right
   * @param dist - Distance to move
   */
  public void moveBat( float dist )
  {
    // *[2]**********************************************************
    // * Fill in code to prevent the bat being moved off the screen *
    // **************************************************************
    Debug.trace( "Model: Move bat = %6.2f", dist );
    bat.moveX(dist);
    //modelChanged();
  }

  /**
   * Model has changed so notify observers so that they
   *  can redraw the current state of the game
   */
  public void modelChanged()
  {
    setChanged(); notifyObservers();
  }

}

ViewBreakout类:

代码语言:javascript
复制
package breakout;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;

import static breakout.Global.*;

/**
 * Displays a graphical view of the game of breakout
 *  Uses Garphics2D would need to be re-implemented for Android
 * @author Mike Smith University of Brighton
 */
public class ViewBreakout extends JFrame implements Observer
{ 
    private ControllerBreakout controller;
    private GameObject   ball;             // The ball
    private GameObject[] bricks;           // The bricks
    private GameObject   bat;              // The bat
    private int          score =  0;       // The score
    private long         timeTaken = 0;    // How long
    private int          frames = 0;       // Frames output
    private final static int  RESET_AFTER = 200;

    /**
     * Construct the view of the game
     */

    public ViewBreakout()
    {
        setSize( W, H );                        // Size of window
        addKeyListener( new Transaction() );    // Called when key press
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Timer.startTimer();
    }

    /**
     *  Code called to draw the current state of the game
     *   Uses draw:       Draw a shape
     *        fill:       Fill the shape
     *        setPaint:   Colour used
     *        drawString: Write string on display
     *  @param g Graphics context to use
     */
    public void drawActualPicture( Graphics2D g )
    {


        frames++;
        // White background

        g.setPaint( Color.white );
        g.fill( new Rectangle2D.Float( 0, 0, W, H ) );

        Font font = new Font("Monospaced",Font.BOLD,24); 
        g.setFont( font ); 

        // Blue playing border

        g.setPaint( Color.blue );              // Paint Colour
        g.draw( new Rectangle2D.Float( B, M, W-B*2, H-M-B ) );

        // Display the ball
        display( g, ball );

        // Display the bricks that make up the game
        // *[3]**********************************************************
        // * Fill in code to display bricks (A brick may not exist)     *
        // **************************************************************

        // Display the bat
        display( g, bat );

        // Display state of game
        g.setPaint( Color.black );
        FontMetrics fm = getFontMetrics( font );
        String fmt = "BreakOut: Score = [%6d] fps=%5.1f";
        String text = String.format(fmt, score, 
                frames/(Timer.timeTaken()/1000.0)
            );
        if ( frames > RESET_AFTER ) 
        { frames = 0; Timer.startTimer(); }
        g.drawString( text, W/2-fm.stringWidth(text)/2, (int)M*2 );
    }

    private void display( Graphics2D g, GameObject go )
    {
        switch( go.getColour() )
        {
            case GRAY: g.setColor( Color.gray );
            break;
            case BLUE: g.setColor( Color.blue );
            break;
            case RED:  g.setColor( Color.red );
            break;
        }
        g.fill( new Rectangle2D.Float( go.getX(),     go.getY(), 
                go.getWidth(), go.getHeight() ) );
    }

    /**
     * Called from the model when its state has changed
     * @param aModel Model to be displayed
     * @param arg    Any arguments
     */
    @Override
    public void update( Observable aModel, Object arg )
    {
        ModelBreakout model = (ModelBreakout) aModel;
        // Get from the model the ball, bat, bricks & score
        ball    = model.getBall();              // Ball
        bricks  = model.getBricks();            // Bricks
        bat     = model.getBat();               // Bat
        score   = model.getScore();             // Score
        //Debug.trace("Update");
        repaint();                              // Re draw game
    }

    /**
     * Called by repaint to redraw the Model
     * @param g    Graphics context
     */
    @Override
    public void update( Graphics g )          // Called by repaint
    {
        drawPicture( (Graphics2D) g );          // Draw Picture
    }

    /**
     * Called when window is first shown or damaged
     * @param g    Graphics context
     */
    @Override
    public void paint( Graphics g )           // When 'Window' is first
    {                                         //  shown or damaged
        drawPicture( (Graphics2D) g );          // Draw Picture
    }

    private BufferedImage theAI;              // Alternate Image
    private Graphics2D    theAG;              // Alternate Graphics

    public void drawPicture( Graphics2D g )   // Double buffer
    {                                         //  to avoid flicker
        if (  theAG == null )
        {
            Dimension d = getSize();              // Size of curr. image
            theAI = (BufferedImage) createImage( d.width, d.height );
            theAG = theAI.createGraphics();
        }
        drawActualPicture( theAG );             // Draw Actual Picture
        g.drawImage( theAI, 0, 0, this );       //  Display on screen
    }

    /**
     * Need to be told where the controller is
     * @param aPongController The controller used
     */
    public void setController(ControllerBreakout aPongController)
    {
        controller = aPongController;
    }

    /**
     * Methods Called on a key press 
     *  calls the controller to process
     */
    class Transaction implements KeyListener  // When character typed
    {
        @Override
        public void keyPressed(KeyEvent e)      // Obey this method
        {
            // Make -ve so not confused with normal characters
            controller.userKeyInteraction( -e.getKeyCode() );
        }

        @Override
        public void keyReleased(KeyEvent e)
        {
            // Called on key release including specials
        }

        @Override
        public void keyTyped(KeyEvent e)
        {
            // Send internal code for key
            controller.userKeyInteraction( e.getKeyChar() );
        }
    }
}

ModelActivePart类:

代码语言:javascript
复制
package breakout;

import static breakout.Global.*;

/**
 * A class used by the model to give it an active part.
 *  Which moves the ball every n millesconds and implements
 *  an appropirate action on a collision involving the ball.
 * @author Mike Smith University of Brighton
 */
public class ModelActivePart implements Runnable
{
  private ModelBreakout model;
  private boolean runGame = true;           // Assume write to is atomic

  public ModelActivePart(ModelBreakout aBreakOutModel)
  {
    model = aBreakOutModel;
  }

  /**
   * Stop game, thread will finish
   */
  public void stopGame() { runGame = false; }

  /**
   * Code to position the ball after time interval
   *  and work out what happens next
   */

  @Override
  public void run()
  {
    final float S = 6; // Units to move the ball
    try
    {
      GameObject ball     = model.getBall();     // Ball in game
      GameObject bricks[] = model.getBricks();   // Bricks
      GameObject bat      = model.getBat();      // Bat

      while (runGame)
      {
        double x = ball.getX();
        double y = ball.getY();
        // Deal with possible edge of board hit
        if (x >= W - B - BALL_SIZE)  ball.changeDirectionX();
        if (x <= 0 + B            )  ball.changeDirectionX();
        if (y >= H - B - BALL_SIZE) 
        { 
          ball.changeDirectionY(); model.addToScore( HIT_BOTTOM ); 
        }
        if (y <= 0 + M            )  ball.changeDirectionY();

        ball.moveX(S);  ball.moveY(S);

        // As only a hit on the bat/ball is detected it is assumed to be
        // on the top or bottom of the object
        // A hit on the left or right of the object
        //  has an interesting affect

        boolean hit = false;
        // *[4]**********************************************************
        // * Fill in code to check if a brick has been hit              *
        // *  Remember to remove a brick in the array  (if hit)         *
        // *    [remove] - set the array element to null                *
        // **************************************************************
        if (hit)
          ball.changeDirectionY();

        if (bat.hitBy(ball) == GameObject.Collision.HIT)
          ball.changeDirectionY();

        model.modelChanged(); // Model changed refresh screen

        Thread.sleep(20);     // About 50 Hz
      }
    } catch (Exception e) 
    { 
      Debug.error("ModelActivePart - stopped\n%s", e.getMessage() );
    }
  }

}

现在我不希望你为我做任何事情,我只想知道如何在屏幕上画一块砖; 从那以后我可能会自己休息。

EN

回答 1

Stack Overflow用户

发布于 2018-10-16 15:37:50

对于位置0的蓝砖,0修改类的createGameObjects方法ModelBreakout

代码语言:javascript
复制
public void createGameObjects() 
{
    ball = new GameObject(W / 2, H / 2, BALL_SIZE, BALL_SIZE, Colour.RED);
    bat = new GameObject(W / 2, H - BRICK_HEIGHT * 4, BRICK_WIDTH * 3,
            BRICK_HEIGHT, Colour.GRAY);
    bricks = new GameObject[BRICKS];

    // *[1]**********************************************************
    // * Fill in code to place the bricks on the board *
    // **************************************************************

    bricks[0] = new GameObject(0, 0, BRICK_HEIGHT, BRICK_WIDTH, Colour.BLUE);
}

然后drawActualPictureViewBreakout类的方法中绘制砖块:

代码语言:javascript
复制
// Display the bricks that make up the game
// *[3]**********************************************************
// * Fill in code to display bricks (A brick may not exist)     *
// **************************************************************

for (GameObject brick : bricks) 
{
    if (null != brick)
    {
        display ( g, brick );
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档