0

我正在尝试获得一个有效的 tron 游戏。我的问题是我不知道如何使用键盘和 WASD 更改动画的方向。

import java.applet.Applet;
import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;

public class Grid extends Applet implements KeyListener
{
    int xCoord;   int yCoord;
    boolean firstPaint;
    int currentDirection1 = 1;
    int currentDirection2 = 3;
    int t=1;    int ponex = 400; int poney= 201;
    int ptwox= 400; int ptwoy = 401;

    public void init()  {  firstPaint = true;   this.addKeyListener(this); }

    public void paint(Graphics g)
    {
        if (firstPaint)
            firstPaint = false; 
        else
        {
            g.setColor(Color.cyan);
            g.fillRect(xCoord,yCoord,01,10);
        }

        g.setColor(Color.black);
        g.fillRect(0, 0, 801, 601);
        g.setColor(Color.red);

        for(int x = 0; x < 800; x += 20)
            g.drawLine(x, 600, x, 0);
        for(int y=0;y<600;y+=20)
            g.drawLine(800,y,0,y);
        try {
            Thread.sleep(500);
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        draw(g);

    }
    public void draw(Graphics g)
    {
        g.setColor(Color.cyan);
        g.fillRect(ponex, poney, 20, 20);
        if(currentDirection1 == 1)
        {
            g.fillRect(ponex+=t, poney, 20, 20);
        }
        else if(currentDirection1 == 2)
        {
            g.fillRect(ponex, poney+=t, 20, 20);
        }
        else if(currentDirection1 == 3)
        {
            g.fillRect(ponex-=t, poney, 20, 20);
        }
        else if(currentDirection1 == 0)
        {
            g.fillRect(ponex, poney-=t, 20, 20);
        }
        g.setColor(Color.green);
        g.fillRect(ptwox, ptwoy, 20, 20);
        if(currentDirection2 == 1)
        {
            g.fillRect(ptwox+=t, ptwoy, 20, 20);
        }
        else if(currentDirection2 == 2)
        {
            g.fillRect(ptwox, ptwoy+=t, 20, 20);
        }
        else if(currentDirection2 == 3)
        {
            g.fillRect(ptwox-=t, ptwoy, 20, 20);
        }
        else if(currentDirection2 == 0)
        {
            g.fillRect(ptwox, ptwoy-=t, 20, 20);
        }
        try {
            Thread.sleep(30);
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        draw(g);
    }

    public boolean mouseDown(Event e, int x, int y)
    {
        xCoord = x;
        yCoord = y;
        repaint();
        return true;
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == 38) {
            if (currentDirection1 != 2){//up
                currentDirection1 = 0;
            }
        } else if (e.getKeyCode() == 40) {//down
            if (currentDirection1 != 0){
                currentDirection1 = 2;
            }
        } else if (e.getKeyCode() == 39) {//right
            System.out.print("right");
            if (currentDirection1 != 3){
                currentDirection1 = 1;
            }
        } else if (e.getKeyCode() == 37) {//left
            if (currentDirection1 != 1){
                currentDirection1 = 3;
            }
        }
        if (e.getKeyCode() == KeyEvent.VK_W){
            System.out.println("W");
            if (currentDirection2 != 2){
                currentDirection2 = 0;
            }
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            if (currentDirection2 != 0){
                currentDirection2 = 2;
            }
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            if (currentDirection2 != 3){
                currentDirection2 = 1;
            }
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            if (currentDirection2 != 1){
                currentDirection2 = 3;
            }
        }
    }
}
4

0 回答 0