简体   繁体   English

我怎样才能让这个玩家移动?

[英]How can I make this player move?

So I am trying to make my own version of the Pong game.所以我正在尝试制作我自己的 Pong 游戏版本。 My problems with this program is that my player doesn't move.我对这个程序的问题是我的播放器不动。 I think I made a mistake but because I am really new to java I don't fully understand game mechanics in code.我想我犯了一个错误,但因为我对 Java 真的很陌生,所以我并不完全了解代码中的游戏机制。 Could one of you please review my code carefully and tell me whats wrong and how to correct it.你们中的一个人能否仔细检查我的代码并告诉我什么是错误的以及如何纠正它。 Also, would one of you tell me where did you learn or where can I learn game development with Java.另外,你们中的一个能告诉我你在哪里学习的或者我可以在哪里学习使用 Java 进行游戏开发。 Your answers will be really appreciated.您的回答将不胜感激。

Window Class窗口类

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;

public class Window{
  public Window(){
    JFrame window = new JFrame();
    GamePanel gamePanel = new GamePanel();
    window.setSize(720, 480);
    window.setResizable(true);
    window.setTitle("2DGame");
    window.add(gamePanel);
    window.setUndecorated(false);
    window.setLocationRelativeTo(null);
    window.getContentPane().setBackground(Color.BLACK);
    window.setVisible(true);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setMinimumSize(new Dimension(720, 480));
  }
}

GamePanel Class游戏面板类

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class GamePanel extends JPanel{
  Wall wall = new Wall();
  Player player = new Player();
  public GamePanel(){
    setBackground(new Color(16, 16, 16));
  }
  public void paintComponent(Graphics g){
    super.paintComponent(g);
    wall.paintComponent(g);
    player.paintComponent(g);
  }
}

Player Class球员等级

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Player extends JPanel implements ActionListener, KeyListener{
  Timer t = new Timer(5, this);
  private int x = 28;
  private int y = 190;
  private int ydirection;
  private int speed = 1;
  public Player(){
    t.start();
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
  }
  public void paintComponent(Graphics g){
    g.setColor(Color.WHITE);
    g.fillRect(x, y, 20, 100);
  }
  public void actionPerformed(ActionEvent e){
    y += ydirection;
    repaint();
  }
  public void keyPressed(KeyEvent e){
    int KeyCode = e.getKeyCode();
    if(KeyCode == KeyEvent.VK_W){
      ydirection -= speed;
    }
    if(KeyCode == KeyEvent.VK_S){
      ydirection += speed;
    }
  }
  public void keyReleased(KeyEvent e){
    int KeyCode = e.getKeyCode();
    if(KeyCode == KeyEvent.VK_W){
      ydirection = 0;;
    }
    if(KeyCode == KeyEvent.VK_S){
      ydirection = 0;
    }
  }
  public void keyTyped(KeyEvent e){}
}

Your logic is not entirely good, but you can easily fix it.您的逻辑并不完全正确,但您可以轻松修复它。 Please study the OOP (principle).请研究OOP(原理)。 For example:例如:

  • The player is not JPanel.播放器不是 JPanel。
  • The ActionListener and KeyListener interface management is not the Player class job, please create a separate class for her. ActionListener 和 KeyListener 接口管理不是 Player 类的工作,请为她创建一个单独的类。
  • Please study the access modifiers.请研究访问修饰符。

Pay more attention to the transparency of your code.更加注意代码的透明度。 For example:例如:

  • Use the ovverride annotation.使用 override 注释。
  • Use the formatter(Ctrl + Shift + F in eclipse)使用格式化程序(Eclipse 中的 Ctrl + Shift + F)

The problem was that you wrote Keylistener on another Jpanel.问题是您在另一个 Jpanel 上编写了 Keylistener。 What I think you inadvertently put on the player class.我认为你不经意间在播放器类上放了什么。 I quickly reworked the code a bit.我很快重新编写了代码。

Window class:窗口类:

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;

    import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;

public class Window {
    public Window() {
        JFrame window = new JFrame();
        GamePanel gamePanel = new GamePanel();
        window.setSize(720, 480);
        window.setResizable(true);
        window.setTitle("2DGame");
        window.add(gamePanel);
        window.setUndecorated(false);
        window.setLocationRelativeTo(null);
        window.getContentPane().setBackground(Color.BLACK);
        window.setVisible(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setMinimumSize(new Dimension(720, 480));
    }

    public static void main(String args[]) {
        Window window = new Window();
    }
}

PlayerDirections enum: PlayerDirections 枚举:

public enum PlayerDirections {
    UP, DOWN
}

Player class:球员等级:

import java.awt.Color;
import java.awt.Graphics;

public class Player {
    private int x = 28;
    private int y = 190;
    private int speed = 1;

    public Player() {

    }

    public void draw(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(x, y, 20, 100);
    }

    public void Move(PlayerDirections direction) {
        switch (direction) {
        case UP:
            y -= speed;
            break;
        case DOWN:
            y += speed;
            break;
        }
    }

}

GamePanel class:游戏面板类:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JPanel;
import javax.swing.Timer;

public class GamePanel extends JPanel implements ActionListener {

    Player player = new Player();

    public GamePanel() {
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int KeyCode = e.getKeyCode();
                if (KeyCode == KeyEvent.VK_W) {
                    player.Move(PlayerDirections.UP);
                }
                if (KeyCode == KeyEvent.VK_S) {
                    player.Move(PlayerDirections.DOWN);
                }
            }
        });
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
        setBackground(new Color(16, 16, 16));
        Timer t = new Timer(5, this);
        t.start();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        player.draw(g);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM