2015-04-24 2 views
0

Существует прорывная игра, которая была частично создана для меня; в настоящее время он имеет только прыгающий мяч и летучую мышь; нет кирпичей, на которые может ударить мяч.Как нарисовать кирпичи в игре Java Breakout?

Мне нужно добавить код для генерации кирпичей, но я боюсь; Я не знаю, как подойти к этому, так как я не очень хорошо разбираюсь в Java GUI.

Я включил классы, в которые необходимо добавить код; области, которые нуждаются код написаны в комментариях

Modelbreakout класс:

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 класс:

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 класс:

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()); 
    } 
    } 

} 

Теперь я не ожидаю, вы все делаете для меня, я просто хочу знать, как нарисовать один кирпич на экране; оттуда я мог бы, вероятно, самому себе.

Если вы хотите весь пакет; загрузка ссылку на мой проект является here

Любая помощь приветствуется :)

+0

Это домашнее задание или курсовая работа? Если нет, кто частично создал этот код для вас? – Bobulous

+0

Я бы предложил начать изучать класс GameObject и посмотреть, как создается 'bat' (так как я предполагаю, что' bat' необходим просто плоский, подвижный «кирпич»). – Turing85

+0

Вы пытались использовать JPanels? Я не знаю ничего о «шаре», но если вы обнаружите обнаружение столкновения (это уже в вашем коде, я полагаю, из-за того, что «bat» должен работать), тогда вы просто обнаруживаете, когда граница JPanel встречается с мячом , если да, 'Parent.remove (JPanel);' –

ответ

2

Для синего кирпича в позиции 0, 0 изменить createGameObjects метод ModelBreakout класса:

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); 
} 

Затем нарисуйте кирпича в drawActualPicture способе ViewBreakout класса:

// 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); 
    } 
} 

Это гр есть назначение, если это курсовая работа.

Смежные вопросы