所以我花了大约3个小时试图围绕如何制作“键绑定”并将其应用于我开发的游戏。其中一些至少有点意义,而其余部分超过我的头脑。
这可能只是我设置游戏的方式,这使得无法使用“键绑定”但我不知道。
我查看过这些帖子及其提供的链接,试图了解它们,但仍然不知道哪里可以开始。
How do I move a graphic across the screen with arrow keys?
How to make an image move while listening to a keypress in Java.
addKeyListener() doesn't work for JPanel
KeyListener, keyPressed versus keyTyped
How to use Key Bindings instead of Key Listeners
这是我的WIP游戏的一个工作示例,除了用于移动玩家的键绑定之外的所有内容(左上角的红点)。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class gamePanel extends JPanel
{
public gamePanel()
{
setBounds(115,93,480,480);//sets the size and location of gamePanel (x,y,w,h)
}
private Random tGenerator = new Random();//initialize a random number generator
private Random mGenerator = new Random();//initialize a random number genterator
int floor = 0; //initializes the variable floor to zero for later use
int dirt = 1; //initializes the variable dirt to one for later use
int stone = 2; //initializes the variable stone to two for later use
int water = 3; //initializes the variable water to three for later use
int iron = 4; //initializes the variable iron to four for later use
int gold = 5; //initializes the variable gold to five for later use
int diamond = 6; //initializes the variable diamond to six for later use
int emerald = 7; //initializes the variable emerald to seven for later use
int width = 24; // width of playing area
int height = 24; //height of playing area
int x, y; // my x & y variables for coordinates
int[][] coords = new int[width][height]; //my array that I want to store the coordinates for later use in painting
int[] terrain = {floor, dirt, stone, water, iron, gold, diamond, emerald}; //my terrain that will determine the color of the paint
public void mapGen() //what should mark/generate the JPanel
{
for(x = 0; x < width; x++)
{
for(y = 0; y < height; y++)
{
int t = tGenerator.nextInt(20); // random generator for terrain
double m = 9.5*mGenerator.nextDouble()+0.5; //random generator for materials
if(t <= 10)
{
coords[x][y] = terrain[floor]; //should mark the coordinates as floor
}
if(t == 11)
{
coords[x][y] = terrain[water]; //should mark the coordinates as water
}
if(t >= 12 && t <=16)
{
coords[x][y] = terrain[stone]; //should mark the coordinates as stone
}
if(t >=17 && t <= 19)
{
coords[x][y] = terrain[dirt]; //should mark the coordinates as dirt
}
if(m >= 1.0 && m<= 1.5)
{
coords[x][y] = terrain[iron]; //marks as iron
}
if(m >= 3.0 && m <= 3.1)
{
coords[x][y] = terrain[gold]; //marks as gold
}
if(m == 7.0)
{
coords[x][y] = terrain[diamond]; //marks as diamond
}
if(m == 5.0)
{
coords[x][y] = terrain[emerald]; //marks as emerald
}
}
}
coords[0][0] = terrain[0]; // sets coordinate 0,0 to floor
coords[23][23] = terrain[0]; // sets coordinate 24,24 to floor
}//end mapGen
int pcX,pcY;// x,y for the player
int[][] player = new int[pcX][pcY];//array holding x,y coords for player
@Override
public void paintComponent(Graphics g)//what will paint each 20x20 square on the grid what it is assigned
{
super.paintComponent(g);
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
mapGen();
if(coords[x][y] == terrain[floor])// paints floor color at marked coordinates
{
g.setColor(new Color(249,249,249));
g.fillRect((x*20), (y*20), 20, 20);
}
if(coords[x][y] == terrain[dirt])// paints dirt color at marked coordinates
{
g.setColor(new Color(121,85,58));
g.fillRect((x*20), (y*20), 20, 20);
}
if(coords[x][y] == terrain[stone])// paints stone color at marked coordinates
{
g.setColor(new Color(143,143,143));
g.fillRect((x*20),(y*20),20,20);
}
if(coords[x][y] == terrain[water])// paints water color at marked coordinates
{
g.setColor(new Color(58,89,245));
g.fillRect((x*20),(y*20),20,20);
}
if(coords[x][y] == terrain[iron])// paints iron color at marked coordinates
{
g.setColor(new Color(216,175,147));
g.fillRect((x*20),(y*20), 20, 20);
}
if(coords[x][y] == terrain[gold])// paints gold color at marked coordinates
{
g.setColor(new Color(255,215,0));
g.fillRect((x*20), (y*20), 20, 20);
}
if(coords[x][y] == terrain[diamond])// paints diamond color at marked coordinates
{
g.setColor(new Color(140,244,226));
g.fillRect((x*20), (y*20), 20, 20);
}
if(coords[x][y] == terrain[emerald])// paints emerald color at marked coordinates
{
g.setColor(new Color(0,166,51));
g.fillRect((x*20), (y*20), 20, 20);
}
}
}
g.setColor(Color.red);//creates the player "model"
g.fillOval((pcX*20),(pcY*20),20,20);
}//end paintComponent
public static void main(String[] args)
{
gamePanel panel = new gamePanel();
JFrame frame = new JFrame();
frame.setSize(495,518);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setVisible(true);
}//end main
}// end gamePanel
我不希望听起来像是一个想要为我完成编码的人,我需要的只是我需要完成目标的过程类型的一个例子。
请记住,我是一名仍在学习的新手程序员,对任何不被视为“基本”的代码的解释将不胜感激:)