- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
到目前为止,我能够使用 jlabel 制作一个由图 block 组成的板。游戏的下一步是让红色玩家和蓝色玩家轮流选择他们最初控制的行星(通过单击行星图 block )。一旦玩家点击一个星球,一个米波计数器就会被放置在该星球的顶部。
行星的数组数据也应该更新,以便知道行星的所有者。现在,我不知道如何将我的 jlabel 磁贴与我的变量链接起来。
如果您能为我指明正确的方向,我将不胜感激。谢谢。
package SpaceBattle;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
//import javax.smartcardio.Card;
import javax.swing.*;
public class spacebattle2 extends JFrame {
JLabel[] tilelabel = new JLabel[31];
JLabel[] hexlabel = new JLabel[31];
ImageIcon tileicon, hexicon;
int rownum, colnum;
int c, r, xloc, yloc, hexctr, tilectr;
int energy, ore, capacity;
String labelname;
String type;
int numtiles = 31;
int deckctr = 26;
String[] tiledeck = new String[numtiles];
int[] tilexloc = new int[numtiles];
int[] tileyloc = new int[numtiles];
int tileenergy[] = new int[numtiles];
int tileore[] = new int[numtiles];
int tilecapacity[] = new int[numtiles];
String tiletype[] = new String[numtiles];
String tilename[] = new String[numtiles];
int tilemeeple[] = new int[numtiles];
String[] hexnum = new String[numtiles];
int[] hexxloc = new int[numtiles];
int[] hexyloc = new int[numtiles];
String[] hexempty = new String[numtiles];
int hexenergy[] = new int[numtiles];
int hexore[] = new int[numtiles];
int hexcapacity[] = new int[numtiles];
String hextype[] = new String[numtiles];
String hexname[] = new String[numtiles];
String hexplayer[] = new String[numtiles];
String[] players = new String[6];
String playerturn;
int startingplanets = 2;
int bluemaxplanet = startingplanets;
int redmaxplanet = startingplanets;
int numplanets = 7;
int numspace = 24;
int nonplayertiles = numplanets + numspace;
String planetCode;
String planetselected = "none";
//selectPlanet
String player = "Red";
int i;
public spacebattle2() {
setLayout(null);
createDeck();
shuffleDeck();
drawBoard();
//selectPlanet();
}
private void selectPlanet() {
// TODO Auto-generated method stub
for(i = 0; i < numtiles; i++) {
tilelabel[i].addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
System.out.println(i);
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
}
}
private void shuffleDeck() {
// TODO Auto-generated method stub
System.out.println("shuffling deck...");
Random r = new Random();
int swapIndex;
String temp;
for(int startIndex = 0; startIndex < numtiles; startIndex++) {
System.out.println(startIndex + " startIndex " + tiledeck[startIndex]);
swapIndex = r.nextInt(numtiles);
System.out.println(swapIndex + " swapIndex " + tiledeck[swapIndex]);
if(swapIndex != startIndex) {
temp = tiledeck[swapIndex];
tiledeck[swapIndex] = tiledeck[startIndex];
System.out.println("Moving " + startIndex + " " + tiledeck[startIndex] + " startIndex to " + swapIndex + " swapIndex");
tiledeck[startIndex] = temp;
System.out.println("Moving " + temp + " temp to " + startIndex + " startIndex");
}
}
}
private void createDeck() {
// TODO Auto-generated method stub
int s, p, xloc = 667, yloc = 10;
/*******************************
*
* Create Space tiles
*
******************************/
System.out.println("creating space tiles...");
tilectr = 0;
for(s= 0; s < numspace; s++) {
tiledeck[tilectr] = "000";
System.out.println(tilectr + " " + tiledeck[tilectr]);
tilectr++;
}
/**************************
*
* Create Planet tiles
*
*************************/
System.out.println("creating planet tiles...");
for(p = 0; p < numplanets; p++) {
energy = (int) (Math.random()*3 + 1);
ore = (int) (Math.random()*3 +1);
capacity = (int) (Math.random()*3 +1);
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("");
stringBuffer.append(energy);
stringBuffer.append(ore);
stringBuffer.append(capacity);
String planetCode = stringBuffer.toString();
tiledeck[tilectr] = planetCode;
System.out.println(tilectr + " " + tiledeck[tilectr]);
tilectr++;
}
}
private void drawBoard() {
// TODO Auto-generated method stub
int xloc = 10;
int yloc = 61;
int hexctr = 0;
int colnum = 7;
int rownum = 4;
xloc = 10;
yloc = 61;
hexctr = 0;
colnum = 7;
rownum = 4;
int row = 0, col = 0, numtiles = 31;
for(int c = 0; c < numtiles; c ++) {
if(tiledeck[c] == "000") {
System.out.println(c + " " + tiledeck[c] + " tile is space");
tileenergy[c] = 0;
tileore[c] = 0;
tilecapacity[c] = 0;
tiletype[c] = "space";
tilename[c] = "space";
tileicon = new ImageIcon(getClass().getResource("/000.png"));
tilelabel[c] = new JLabel();
tilelabel[c].setIcon(tileicon);
tilelabel[c].setBounds(xloc, yloc, 115, 100);
add(tilelabel[c]);
yloc = yloc + 102;
row++;
if(row == rownum) {
xloc = xloc + 88;
row = 0;
col++;
if(col % 2 == 0) {
yloc = 61;
rownum = rownum - 1;
}
else {
yloc = 10;
rownum = rownum + 1;
}
}
}
else {
System.out.println(c + " " + tiledeck[c] + " tile is planet");
tileenergy[c] = Integer.parseInt(tiledeck[c].substring(0, 1));
tileore[c] = Integer.parseInt(tiledeck[c].substring(1, 2));
tilecapacity[c] = Integer.parseInt(tiledeck[c].substring(2, 3));
tiletype[c] = "planet";
tilemeeple[c] = 0;
tileicon = new ImageIcon(getClass().getResource("/" + tiledeck[c] + ".png"));
tilelabel[c] = new JLabel();
tilelabel[c].setIcon(tileicon);
tilelabel[c].setBounds(xloc, yloc, 115, 100);
add(tilelabel[c]);
yloc = yloc + 102;
row++;
if(row == rownum) {
xloc = xloc + 88;
row = 0;
col++;
if(col % 2 == 0) {
yloc = 61;
rownum = rownum - 1;
}
else {
yloc = 10;
rownum = rownum + 1;
}
}
}
}
}
private Object i(int numtiles2, int i) {
// TODO Auto-generated method stub
return null;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
spacebattle2 board = new spacebattle2();
board.setSize(900, 600);
board.setResizable(false);
board.setTitle("Space Battle");
board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
board.setVisible(true);
}//End void main
}
最佳答案
我建议重构程序以使用“tile”类来保存与给定图 block 相关的所有信息。拥有一个了解图 block 集合以及它们之间如何相互关联的“图 block 管理器”类也很有帮助。
对于您的程序来说,图 block 类可能如下所示。我试图不改变任何功能,只是重新组织事物。您应该能够将所有内容与您的原始程序相匹配。
class TileInfo {
TileManager manager;
public JLabel label = new JLabel();
public JLabel hexlabel = new JLabel();
int xloc;
int yloc;
int energy;
int ore;
int capacity;
String type;
String name;
int meeple;
String hexnum;
int hexxloc;
int hexyloc;
String hexempty;
// etc...
public TileInfo(Icon tileicon, int x, int y, TileManager manager) {
this.manager = manager;
label.setIcon(tileicon);
label.setBounds(x, y, 115, 100);
label.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
manager.clickedTile(TileInfo.this);
};
});
}
};
然后您可以为“空间”和“行星”图 block 扩展此基类:
class SpaceTile extends TileInfo {
public SpaceTile(Icon icon, int x, int y, TileManager manager) {
super(icon, x, y, manager);
energy = 0;
ore = 0;
capacity = 0;
type = "space";
name = "space";
}
};
class PlanetTile extends TileInfo {
public PlanetTile(Icon icon, int x, int y,
int energy, int ore, int capacity, TileManager manager) {
super(icon, x, y, manager);
this.energy = energy;
this.ore = ore;
this.capacity = capacity;
type = "planet";
meeple = 0;
}
};
在您的示例中,子类并不是严格必需的,但每种情况下的图 block 结构都略有不同,这有助于保持它们的直线性。
每个 TileInfo
创建自己的鼠标监听器,但将事件传递到图 block 管理器。磁贴管理器就像一个容器并保存数组:
class TileManager {
public int numtiles;
public TileInfo[] tile;
public String tiledeck[];
public TileManager(int numtiles) {
this.numtiles = numtiles;
this.tile = new TileInfo[numtiles];
this.tiledeck = new String[numtiles];
System.out.printf("TileManager(%d): tile[%d]\n", numtiles, numtiles);
}
public void clickedTile(TileInfo tile) {
if (tile instanceof SpaceTile) {
System.out.println("clicked space tile " + tile.label.getBounds());
} else if (tile instanceof PlanetTile) {
System.out.println("clicked planet tile " + tile.label.getBounds());
}
}
};
请注意,现在您只需要一个数组。如果您更喜欢的话,TileManager
可以覆盖 ArrayList
;我想尽可能地坚持你原来的计划。 clickedTile()
方法处理图 block 上的鼠标点击,但由于它位于图 block 管理器中,因此它可以看到所有图 block ,而不仅仅是一个图 block 。例如,如果您想在单击图 block B 时取消选择图 block A,则需要能够看到这两个图 block 。
拥有单独的图 block 管理器并不是绝对必要的;您可以将此功能保留在 spacebattle2
类中。我喜欢有一个管理器类来将创建、操作和绘制图 block 组的功能与游戏玩法分开。它的行为有点像“图 block 库”。
最后,这一切如何适合您的主类。为了清楚起见,省略了大量代码。
public class spacebattle2 extends JFrame {
TileManager tiles = new TileManager(31);
// didn't change anything except tiledeck[] now lives inside tiles
// this method could be moved into the TileManager
private void shuffleDeck() {
for(int startIndex = 0; startIndex < tiles.numtiles; startIndex++) {
swapIndex = r.nextInt(tiles.numtiles);
if(swapIndex != startIndex) {
temp = tiles.tiledeck[swapIndex];
tiles.tiledeck[swapIndex] = tiles.tiledeck[startIndex];
tiles.tiledeck[startIndex] = temp;
}
}
}
void createTile(String deck, int xloc, int yloc) {
tileicon = new ImageIcon(getClass().getResource("/" + deck + ".png"));
if (deck.equals("000")) {
tiles.tile[c] = new SpaceTile(tileicon, xloc, yloc, tiles);
} else {
int energy = Integer.parseInt(deck.substring(0, 1));
int ore = Integer.parseInt(deck.substring(1, 2));
int capacity = Integer.parseInt(deck.substring(2, 3));
tiles.tile[c] = new PlanetTile(tileicon, xloc, yloc, 0, 0, 0, tiles);
}
add(tiles.tile[c].label);
}
private void drawBoard() {
/* ... */
for(int c = 0; c < tiles.numtiles; c ++) {
createTile(tiles.tiledeck[c], xloc, yloc);
yloc = yloc + 102;
row++;
if(row == rownum) {
xloc = xloc + 88;
row = 0;
col++;
if(col % 2 == 0) {
yloc = 61;
rownum = rownum - 1;
} else {
yloc = 10;
rownum = rownum + 1;
}
}
}
}
}
简化了drawBoard()
中的循环;它有很多重复的代码,因此我将其排除在外,并添加了一个 createTile()
方法来创建两种类型的图 block 。
还有一些可以改进的地方。我不认为具有公共(public) TileInfo 变量的公共(public)数组是访问数据的理想方式,但不想对其进行太大更改,以致无法识别。
这并不是最终设计,而是建议一种组织代码的方法,使其更易于使用,并回答您有关如何将数据与图 block 关联的问题。
关于java - 我该如何用游戏计数器填充我的棋盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55835150/
我在leetcode上看到这段代码,是一道求众数的题,下面是题目描述: 给定一个大小为 n 的数组,找到多数元素。众数元素是出现次数超过 ⌊ n/2 ⌋ 次的元素。 你可以假设数组是非空的并且多数元素
每次在 JavaScript 中执行特定操作时,例如: $(function() { $('#typing').keyup(function () { switch($(this)
我一直在为网页设计一个计数器,但我一直被这个我无法解决的功能所困扰。 我有一个 4 个 div 的计数器,因为其中两个是小数字,另外两个是大数字,所以第一个运行得很快,我看不到它们的功能。 有人知道如
我已经在文档中进行了一些搜索,并在网上花了一段时间,但找不到解决方案!我希望警报告诉我单击 .thumb 时它处于each() 的哪一次迭代。 EG:有六个.thumb,我点击数字3,浏览器弹出3!
在 Handlebars 中,假设我有 names 的集合.我能怎么做 {{#each names}} {{position}} {{name}} {{/each}} 在哪里 {{position}}
这个问题在这里已经有了答案: Numbering rows within groups in a data frame (9 个回答) 4年前关闭。 我们如何在数据帧的每组中生成唯一的 ID 号?以下
我正在努力解决以下问题。我希望为给定的“一”序列创建一个计数器。例如,我有以下内容: 1 1 1 1 0 0 1 1 1 0 0 1 1 1 1 鉴于该序列,我希望为 1 的每个序列设置一个计数器直到
我正在努力解决以下问题。我希望为给定的“一”序列创建一个计数器。例如,我有以下内容: 1 1 1 1 0 0 1 1 1 0 0 1 1 1 1 鉴于该序列,我希望为 1 的每个序列设置一个计数器直到
我有一个jsfiddle here 这是一个简单的 JavaScript 函数,可以计算出设定的数字。 是否可以进行这种计数,但也保留一位小数 所以它算 1.1、1.2、1.3 等。 func
我正在构建一个计数器,当我按下鼠标时,它应该增加到 maxValue 并且减少不超过 0。我还可以选择将计数器重置为其初始值:0。另外,如果 maxValue 是偶数,它应该计数到该数字。但是,如果
所以我成功地为字母和单词构建了其他计数器,但现在我只能用这个来计算句子。我的代码如下,当我运行它时,它会返回很多错误消息: #include #include #include int main
Closed. This question is off-topic。它当前不接受答案。
我需要一个计数器,它会随着某些任务的完成而递增。我们只需要最后一小时的值,即窗口将移动而不是静态时间。 解决此问题的最佳方法是什么?我能想到的一种方法是拥有一个大小为 60 的数组,每分钟一个,并更新
我希望使用计数器来为我提供独特的引用系统。我想单击一个按钮,然后检查一个字段/文件中的最后一个数字,然后简单地向其添加 1,然后将其插入到屏幕上的字段中? 不确定执行此操作的最佳方法或具体如何执行此操
我有一个用 php 制作的表格,在该表格内我显示了数据库中的一些内容。我在每个 td 中创建了一个简单的按钮(类似于 Like),我希望每次点击它都会增加 1。这是带有按钮的行: echo "
如何将数据库中的值转换为可用于 if else 函数的 int 值? 例如:在我的数据库“armnumber = 3”中,如何在 if else 函数中使用它? 代码 string myConnect
我需要生成唯一的“ids”,问题是,它只能在 1 - 99999 之间。 “好”的是,它仅在与另一列组合时必须是唯一的。 我们有组,每个组都有自己的“group_id”,每个组都需要类似 unique
有这个简单的代码: UPDATE counter SET c= c +1 where id = 1; 并且它在开头的 c 字段中为 null 的情况下不起作用。它只有在已经输入了一些数字时才有效,也就
我正在尝试在 python 中构建一个具有闭包属性的计数器。以下工作中的代码: def generate_counter(): CNT = [0] def add_one():
我使用 CSS 来计算 HTML 文档中的部分: body {counter-reset: sect;} section:before { counter-increment: sect;
我是一名优秀的程序员,十分优秀!