“ KeyInput”代码无效。游戏角色不会动弹

时间:2018-10-07 01:15:10

标签: java swing awt keylistener

我是Java的新手,并且是该站点的新手,并且正在学习教程并需要一些帮助。本教程教您如何制作简单的游戏。玩家由彩色正方形表示。我开始讨论他如何使用“ KeyInput”类使玩家移动的部分。它没有说有任何错误,所以我不确定我在做什么错或者我错过了什么。我非常仔细地跟踪了他在视频中所做的事情。有6个班级和一个枚举。这些类称为“ Game.java”,“ GameObject.java”,“ Handler.java”,“ KeyInput.java”,“ Player.java”和“ Window.java”。枚举称为“ ID.java”。我将在下面发布这些类和枚举的代码。

KeyInput.java

package com.tutorial.game;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyInput extends KeyAdapter {

private Handler handler;

public KeyInput(Handler handler) {
    this.handler = handler;
}

public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();

    for(int i = 0; i < handler.object.size(); i++) {
        GameObject tempObject = handler.object.get(i);

        if(tempObject.getId() == ID.Player) {
            if(key == KeyEvent.VK_W) tempObject.setVelY(tempObject.getY() - 1); 
        }
    }


}

public void keyReleased(KeyEvent e) {
    int key = e.getKeyCode();

}
}

Player.java

package com.tutorial.game;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

public class Player extends GameObject {

Random r = new Random();

public Player(int x, int y, ID id) {
    super(x, y, id);

}

public void tick() {
    x += velX;
    Y += velY;

}

public void render(Graphics g) {
    if(id == ID.Player)g.setColor(Color.white);
    else if(id == ID.Player2) g.setColor(Color.red);
    g.fillRect(x,  Y,  32, 32);

}

}

ID.java

package com.tutorial.game;

public enum ID {

Player(),
Enemy(), Player2();

}

GameObject.java

package com.tutorial.game;

import java.awt.Graphics;

public abstract class GameObject {

protected int x, Y;
protected ID id;
protected int velX, velY;

public GameObject(int x, int Y, ID id) {
    this.x = x;
    this.Y = Y;
    this.id = id;
}

public abstract void tick();
public abstract void render(Graphics g);

public void setX(int x) {
    this.x = x;
}
public void setY(int Y) {
    this.Y = Y;
}
public int getX() {
    return x;
}

public int getY() {
    return Y;
}
public void setId(ID id) {
    this.id = id;
}
public ID getId() {
    return id;

}
public void setVelX(int velX) {
    this.velX = velX;
}
public void setVelY(int velY) {
    this.velY = velY;
}
public int getVelX(){
    return velX;
}
public int getVelY() {
    return velY;
}
}

Game.java

package com.tutorial.game;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.Random;

public class Game extends Canvas implements Runnable {

private static final long serialVersionUID = -7728264178317937023L;

public static final int WIDTH = 640, HEIGHT = 640;

private Thread thread;
private boolean running = false;

private Random r;

private Handler handler;

public Game(){

    this.addKeyListener(new KeyInput(handler));
    handler = new Handler();

    new Window(WIDTH, HEIGHT, "Let's build a game", this);

    r = new Random();

    handler.addObject(new Player(WIDTH/2-32, HEIGHT/2-32, ID.Player));
    handler.addObject(new Player(WIDTH/2+64, HEIGHT/2-32, ID.Player2));

    }



public synchronized void start(){
    thread = new Thread(this);
    thread.start();
    running = true;

}

public synchronized void stop(){
    try {
        thread.join();
        running = false;
    }catch(Exception e) {
        e.printStackTrace();
    }
}

public void run(){
    long lastTime = System.nanoTime();
    double amountOfTicks = 60.0;
    double ns = 1000000000 / amountOfTicks;
    double delta = 0;
    long timer = System.currentTimeMillis();
    int frames = 0;
    while(running){
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        while(delta >= 1) {
            tick();
            delta--;
        }
        if(running)
            render();
        frames++;

        if(System.currentTimeMillis() - timer > 1000) {
            timer += 1000;
            //System.out.println("FPS: " + frames);
            frames = 0;
        }
    }
    stop();
    }
private void tick(){
    handler.tick();

}

private void render() {
    BufferStrategy bs = this.getBufferStrategy();
    if(bs == null) {
        this.createBufferStrategy(3);
        return;
    }

    Graphics g = bs.getDrawGraphics();

    g.setColor(Color.black);
    g.fillRect(0, 0, WIDTH, HEIGHT);

    handler.render(g);

    g.dispose();
    bs.show();
}

public static void main(String args[]) {
    new Game();

}
}

Handler.java

package com.tutorial.game;

import java.awt.Graphics;
import java.util.LinkedList;

public class Handler {

LinkedList<GameObject> object = new LinkedList<GameObject>();

public void tick() {
    for(int i = 0; i < object.size(); i++) {
        GameObject tempObject = object.get(i);

        tempObject.tick();
}

}

public void render(Graphics g) {
    for(int i = 0; i < object.size(); i++) {
        GameObject tempObject = object.get(i);

        tempObject.render(g);
    }

}
public void addObject(GameObject object) {
    this.object.add(object);
}
public void removeObject(GameObject object) {
    this.object.remove(object);
}

}

Window.java

package com.tutorial.game;

import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

public class Window extends Canvas {

private static final long serialVersionUID = -1413099016987259526L;

public Window(int WIDTH, int HEIGHT, String title, Game game) {
    JFrame frame = new JFrame(title);

    frame.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    frame.setMaximumSize(new Dimension(WIDTH, HEIGHT));
    frame.setMinimumSize(new Dimension(WIDTH, HEIGHT));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.add(game);
    frame.setVisible(true);
    game.start();
}
}

1 个答案:

答案 0 :(得分:0)

尝试使用新的Thread循环(在Game.java中):

Game.this.requestFocus();

您应该在两个请求焦点之间添加一些延迟。

线程中的代码

while(isDisplayable()){
    Game.this.requestFocus();
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
        e.printStackTrace();
    }
}