applet上的键盘输入移动

am46iovg  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(208)

我正在使用applet创建一个非常基本的游戏,我不知道如何编写代码让计算机将箭头注册为移动(上、下、左、右)。
如何根据键盘输入让黄色的pacman角色移动?
这是我的密码:

import java.applet.*;
import java.awt.*;

public class GameShopRoofCashOut extends Applet 
{
    public void paint(Graphics g) 
    {
        // Setup the board
        int rows = 20;
        int columns = 20;
        int cellSize = 30;                          
        int board [ ][ ] = new int [rows][columns]; 
        int dirt = 12;
        int lightstone = 13;
        int darkstone = 17;
        int slotx = 12;
        int sloty = 0;
        int linex = 30;
        int liney = 390;
        int layer = 0;

        // Store objects in the board
        board[11][4] = 7;
        board[11][5] = 7;
        board[10][14] = 8;
        board[11][14] = 9;
        board[0][19] = 10;
        while (sloty<20)
        {
            while (slotx<20)
            { 
                for (int r=0; r< board.length; r++)  
                {
                    for (int c=0; c< board[r].length; c++)
                    { 
                        //store objects in the board
                        board[dirt][sloty] = 1;
                        board[lightstone][sloty] = 2;
                        board[darkstone][sloty]= 3;
                        if (lightstone<17)
                        {
                            lightstone++;
                        }
                        if (darkstone<19)
                        {
                            darkstone++;
                        }
                        // Draw the board

                        if (board [r][c] == 1)      
                        {
                            g.setColor(Color.blue);
                            g.fillRect(c*cellSize,r*cellSize,cellSize,cellSize);
                            layer++;
                        }
                        if (board [r][c] == 2)      
                        {
                            g.setColor(Color.lightGray);
                            g.fillRect(c*cellSize,r*cellSize,cellSize,cellSize);
                            layer++;
                        }
                        if (board [r][c] == 3)      
                        {
                            g.setColor(Color.darkGray);
                            g.fillRect(c*cellSize,r*cellSize,cellSize,cellSize);
                            layer++;
                        }
                        if (board [r][c] == 7)      
                        {
                            g.setColor(Color.magenta);
                            g.fillRect(c*cellSize,r*cellSize,cellSize,cellSize);
                            layer++;
                        }
                        if (board[r][c] == 8)
                        {
                            int x = c*cellSize;
                            int y = r*cellSize;
                            Polygon p = new Polygon(); 
                            p.addPoint(x+cellSize/2,y); 
                            p.addPoint(x+cellSize, y+cellSize); 
                            p.addPoint(x, y+cellSize); 
                            p.addPoint(x+cellSize/2,y); 
                            g.setColor(Color.orange);
                            g.fillPolygon(p);
                        }
                        if (board[r][c] == 9)
                        {
                            g.setColor(Color.orange);
                            g.fillRect(c*cellSize,r*cellSize,cellSize,cellSize);

                        }
                        if (board [r][c] == 10)      
                        {
                            g.setColor(Color.yellow);
                            g.fillArc(c*cellSize,r*cellSize,cellSize,cellSize,30,300);
                        }
                    }
                }
                slotx++;
            }
            lightstone = 13;
            darkstone = 17;
            slotx=12;
            sloty++;
        }
        g.setColor(Color.black);
        g.drawString("Shop",420,295);
        g.drawString("Cash Out",120,320);
        while (linex<600)
        {
            g.setColor(Color.black);
            g.drawLine(linex,360,linex,600);
            g.drawLine(0,liney,600,liney);
            linex+=30;
            liney+=30;
        }
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题