如何在最终屏幕上添加最终分数?

时间:2018-10-19 20:14:14

标签: java

您好,我是一名编码员,我一直在研究这个非常简单的破盒子游戏,该游戏是我根据YT的教程制作的。现在,我开始尝试对其进行修改,使其更具吸引力。我使用的教程相当不错,但是没有告诉我如何添加当前分数,直到显示“您赢了,分数:”。

代码:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.Timer;

import javax.swing.JPanel;

public class Gameplay extends JPanel implements KeyListener, 
ActionListener {

 private boolean play = false;
 private int score = 0;

 private int totalBricks = 21;


 private Timer timer;
 private int delay = 8;

 private int playerX = 310;

 private int ballposX = 200;
 private int ballposY = 350;
 private int ballXdir = -1;
 private int ballYdir = -2;

 private MapGenerator map;


 public Gameplay() {
     map = new MapGenerator(3, 7);
     addKeyListener(this);
     setFocusable(true);
     setFocusTraversalKeysEnabled(false);
     timer = new Timer(delay, this);
     timer.start();

 }

 public void paint(Graphics g) {
     // background
     g.setColor(Color.black);
     g.fillRect(1, 1, 692, 592);

     //drawing map
     map.draw((Graphics2D)g);

     //borders
     g.setColor(Color.yellow);
     g.fillRect(0, 0, 3, 592);
     g.fillRect(0, 0, 692, 3);
     g.fillRect(691, 0, 3, 592);

     //scores
     g.setColor(Color.white);
     g.setFont(new Font("serif", Font.BOLD, 25));
     g.drawString(""+score, 590, 30);

     //the paddel
     g.setColor(Color.green);
     g.fillRect(playerX, 550, 100, 8);


     //the ball
     g.setColor(Color.yellow);
     g.fillOval(ballposX, ballposY, 20, 20);

     if(totalBricks <= 0) {
         play = false;
         ballXdir = 0;
         ballYdir = 0;
         g.setColor(Color.red);
         g.setFont(new Font("serif", Font.BOLD, 30));
         g.drawString("You Won, score: ", 260, 300);

         g.setFont(new Font("serif", Font.BOLD, 20));
         g.drawString("Press ENTER to restart.", 260, 350);


     }

     if(ballposY > 570) {
         play = false;
         ballXdir = 0;
         ballYdir = 0;
         g.setColor(Color.red);
         g.setFont(new Font("serif", Font.BOLD, 30));
         g.drawString("Game Over, Score: ", 200, 300);

         g.setFont(new Font("serif", Font.BOLD, 20));
         g.drawString("Press ENTER to restart.", 230, 350);


     }


     g.dispose();

 }

@Override
public void actionPerformed(ActionEvent e) {
    timer.start();
    if(play) {
        if(new Rectangle(ballposX, ballposY, 20, 20).intersects(new 
Rectangle(playerX, 550, 100, 8))) {
            ballYdir = -ballYdir;
        }

        A: for(int i = 0; i<map.map.length; i++) {
            for(int j = 0; j<map.map[0].length; j++) {
                if(map.map[i][j] > 0) {
                    int brickX = j* map.brickWidth + 80;
                    int brickY = i* map.brickHeight + 50;
                    int brickWidth = map.brickWidth;
                    int brickHeight = map.brickHeight;

                    Rectangle rect = new Rectangle(brickX, brickY, 
brickWidth, brickHeight);
                    Rectangle ballRect = new Rectangle(ballposX, 
ballposY, 19,19);
                    Rectangle brickRect = rect;

                    if(ballRect.intersects(brickRect)) {
                        map.setBrickValue(0,  i, j);
                        totalBricks--;
                        score += 5;


                    if(ballposX + 19 <= brickRect.x && ballposX + 1 >= 
brickRect.x + brickRect.width) {
                        ballXdir = -ballXdir;
                       } else {
                           ballYdir = -ballYdir;

                       }
                    break A;
                    }
                }
            }

        }

        ballposX += ballXdir;
        ballposY += ballYdir;
        if(ballposX < 0) {
            ballXdir = -ballXdir;

        }
        if(ballposY < 0) {
            ballYdir = -ballYdir;

        }
        if(ballposX > 670) {
            ballXdir = -ballXdir;

        }
    }

    repaint();
 }

 @Override
 public void keyReleased(KeyEvent e) {}
 @Override
 public void keyTyped(KeyEvent e) {}

 @Override
 public void keyPressed(KeyEvent e) {
    if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
        if(playerX >=600) {
            playerX = 600;
        } else {
            moveRight();
        }

    }
    if(e.getKeyCode() == KeyEvent.VK_LEFT) {
        if(playerX < 10) {
            playerX = 10;
        } else {
            moveLeft();
        }
    }
    if(e.getKeyCode() == KeyEvent.VK_ENTER) {
        if(!play) {
            play = true;
            ballposX = 200;
            ballposY = 350;
            ballXdir = -1;
            ballYdir = -2;
            playerX = 310;
            score = 0;
            totalBricks = 21;
            map = new MapGenerator(3, 7);

            repaint();

        }
    }

 }

 public void moveRight() {
    play = true;
    playerX+=20;

 }
 public void moveLeft() {
    play = true;
    playerX-=20;

 }

 }

1 个答案:

答案 0 :(得分:2)

“您赢了,得分:” +得分

在Java中,您可以组合字符串以制作更长的字符串,在这种情况下,“ You Won,Score:”是一个字符串,当您将其添加到分数时,分数将转换为字符串,并与第一部分结合在一起你想要什么。

https://docs.oracle.com/javase/tutorial/java/data/strings.html