游戏类不调用其他类



我是Java代码的新手,它是新鲜的代码(我曾经制作过我的世界模组(,我正在尝试创建一个简单的乒乓球游戏。我拥有我认为游戏正常运行所需的一切,但是似乎主游戏类在我需要时没有调用其他类。这里有什么问题,你可以看到我失踪了吗?这将是一个很大的帮助。

主要游戏类

package crim.pong.main;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Game extends JPanel{

    int x = 0;
    int y = 0;
    int xa = 1;
    int ya = 1;
    Ball ball = new Ball(this);
    Racquet racquet = new Racquet(this);
    private void moveBall(){
        ball.move();
        racquet.move();
    }
    @Override
    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.fillOval(x, y, 30, 30);
        ball.paint(g2d);
        racquet.paint(g2d);
}
    public void gameOver(){
        JOptionPane.showMessageDialog(this, "Game Over", "Game Over", JOptionPane.YES_NO_OPTION);
        System.exit(ABORT);
    }
    public static void main(String[] args) throws InterruptedException{
        JFrame frame = new JFrame("Pong");
        Game game = new Game();
        frame.add(game);
        frame.setSize(300, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        while(true){
            game.moveBall();
            game.repaint();
            Thread.sleep(10);
        }
    }
}

键盘类

package crim.pong.main;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class KeyboardInput extends JPanel{
    public KeyboardInput(){
        KeyListener listener = new MyKeyListener();
        addKeyListener(listener);
        setFocusable(true);
    }
    public static void main(String[] args){
        JFrame frame = new JFrame("Pong");
        KeyboardInput keyboardInput = new KeyboardInput();
        frame.add(keyboardInput);
        frame.setSize(200, 200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public class MyKeyListener implements KeyListener{
        @Override
        public void keyTyped(KeyEvent e){
        }
        @Override
        public void keyPressed(KeyEvent e){
            System.out.println("keyPressed="+KeyEvent.getKeyText(e.getKeyCode()));
        }
        @Override
        public void keyReleased(KeyEvent e){
            System.out.println("keyReleased="+KeyEvent.getKeyText(e.getKeyCode()));
        }
    }
}

球类

package crim.pong.main;
import java.awt.Graphics2D;
import java.awt.Rectangle;
public class Ball {
    private static final int DIAMETER = 30;
    int x = 0;
    int y = 0;
    int xa = 1;
    int ya = 1;
    private Game game;
    public Ball(Game game){
        this.game = game;
    } 
    void move(){
        if(x + xa < 0)
            xa = 1;
        if(x + xa > game.getWidth() - DIAMETER)
            xa = -1;
        if(y + ya < 0)
            ya = 1;
        if(y + ya > game.getHeight() - DIAMETER)
            game.gameOver();
            if(collision()){
                ya = -1;
                y = game.racquet.getTopY() - DIAMETER;
            }

        x = x - xa;
        y = y - ya;
    }
        private boolean collision(){
            return game.racquet.getBounds().intersects(getBounds());
        }
    public void paint(Graphics2D g){
        g.fillOval(x, y, 30, 30);
    }
    public Rectangle getBounds(){
        return new Rectangle(x, y, DIAMETER, DIAMETER);
    }
}

球拍课

package crim.pong.main;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
public class Racquet {
    private static final int Y = 330;
    private static final int WIDTH = 60;
    private static final int HEIGHT = 20;
    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() - 60)
            x = xa + xa;
    }
    public void paint(Graphics2D g){
        g.fillRect(x, 330, 60, 10);
    }
    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;
    }
}

如评论中所述,您有 2 个主要类文件。这是错误的。

具有"main"函数的类是程序的入口点,显然只能有一个入口点,大概您已经将其设置为"游戏"。

键盘输入也不应该尝试创建新的 JFrame。你只需要一个,它应该由你的"游戏"类创建。

如果你想将键盘输入保存在一个单独的类中,那么你想要'KeyboardInput'直接实现KeyListener,然后将其添加到'Game'创建的jframe中,例如(在"游戏中"(

KeyboardInput listener = new KeyboardInput();
addKeyListener(listener);

(在"键盘输入"中(

public class KeyboardInput implements KeyListener
{
    public void keyTyped(KeyEvent e){
    //... etc etc

相关内容

  • 没有找到相关文章

最新更新