游戏类没有调用其他类

时间:2017-02-09 07:47:06

标签: java game-development pong

我是刚接触新代码的java代码的新手(我曾经制作过Minecraft mods),我正在尝试创建一个简单的乒乓球游戏。我拥有我相信我需要的所有游戏才能正常播放,但似乎主游戏类在我需要的时候没有调用其他类。这里有什么不对的,你可以看到我失踪了吗?这将是一个非常好的帮助。

主要游戏类

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

1 个答案:

答案 0 :(得分:0)

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

有一个主要&#39;的课程。函数是程序的入口点,显然只有一个入口点,可能你已将其设置为&#39; Game&#39;。

KeyboardInput也不应该尝试创建新的JFrame。你只需要一个,它应该由你的游戏&#39;创建。类。

如果您想将键盘输入保持在单独的类中,那么您需要&#39; KeyboardInput&#39;直接实现KeyListener,然后将其添加到由&#39; Game&#39;创建的jframe中,例如 (在&#39;游戏&#39;)

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

(&#39; KeyboardInput&#39;)

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