Java游戏难度级别

时间:2018-05-16 16:41:42

标签: java

我需要一些帮助,我正在制作的java壁球或网球比赛,当选择难度级别时,我希望球拍的大小减少一半,球移动得更快。我已经实现了快速移动的球,但我似乎无法找到减少球拍大小的方法。这是我的"难度级"类: 另外,我如何实现MOUSEDRAGGED选项来移动球拍?

import java.awt.*;
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.io.BufferedWriter;
import java.io.FileWriter;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class DifficultyLevel extends JPanel {
private JButton aButton = new JButton("Exit Game");
private Game sg;
private MainFrame sf;
private int selection;
//JRadioButton easyButton, mediumButton, hardButton;
JComboBox<String> difficulty;
ButtonGroup radioGroup;

private void changeDiff(){
    if (selection == 0){
        sg.speed=1;
        sg.Score=0;
        sg.setBackground(Color.cyan);
    }else if (selection == 1){
        sg.speed=2;
        sg.Score=0;
        sg.setBackground(Color.pink);

    }if (selection == 2){
        sg.speed=3;
        sg.Score=0;
        sg.setBackground(Color.yellow);
    }
}

public DifficultyLevel(Game g, MainFrame sfr){

    sg = g;
    sf = sfr;

    setPreferredSize(new Dimension(100, 100));

    difficulty = new JComboBox<String>();

    difficulty.addItem("Easy");
    difficulty.addItem("Medium");
    difficulty.addItem("Hard");
    difficulty.setSelectedIndex(0);
    changeDiff();
    add(difficulty);

    difficulty.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e){
            selection = difficulty.getSelectedIndex();
            changeDiff();
        }
    });

}
}

这是我的网球课:

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.MouseWheelEvent;

public class Racquet {
private static final int Y = 330;
private static final int WIDTH = 60;
private static final int HEIGHT = 10;

int x = 0;
int xa = 0;

private Game game;

public Racquet(Game game) {
    this.game = game;
}

public void move() {
    if (x + xa > 0 && x + xa < game.getWidth() - WIDTH)
        x = x + xa;
}

public void paint(Graphics2D g) {
    g.fillRect(x, Y, WIDTH, HEIGHT);
}

public void keyReleased(KeyEvent e) {
    xa = 0;
}

public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_LEFT)
        xa = -1;
    if (e.getKeyCode() == KeyEvent.VK_RIGHT)
        xa = 1;
}

public Rectangle getBounds() {
    return new Rectangle(x, Y, WIDTH, HEIGHT);
}

public int getTopY() {
    return Y;
}

public void mouseWheel(MouseWheelEvent m){

    if(m.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
        xa = -1;
    if(m.getWheelRotation() == MouseWheelEvent.WHEEL_BLOCK_SCROLL)
        xa = 1;

}
public void mouseWheelMoved(MouseWheelEvent m) {
    xa = 0;
}
  }

1 个答案:

答案 0 :(得分:0)

我认为在你的Racket类中你必须删除final和static到WIDTH,然后在Racket的构造函数中添加宽度:

public Racket(Game game, int width) {
     this.width = width;
}

如果困难很难创建球拍:

Racket(game, 20);

当它很简单时:

Racket(game, 100);