我正在尝试制作一个带有正方形的虚拟标签。现在,我专注于让游戏结束,然后当第二位玩家赶上另一位玩家时关闭游戏。但是,当两个正方形发生碰撞时,程序不会在屏幕上写入任何内容。
我知道调用g.drawString的if语句是有效的,因为在这里使它两次输出到控制台,所以我对为什么g.drawString不起作用感到困惑。
package com.Arman;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.HashSet;
public class Virtual_Tag {
static int x1 = 0;
static int y1 = 0;
static Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
static int height = (int) dimension.getHeight();
static int width = (int) dimension.getWidth();
static int sWidth = (int) (Math.round(100 * width / 1366.0));
static int sHeight = (int) (Math.round(100 * height / 768.0));
static int x2 = width - sWidth;
static int y2 = height - sHeight;
static myGraphicsPanel graphics = new myGraphicsPanel();
static boolean[] move = new boolean[8];
static int xSpeed1;
static int ySpeed1;
static int xSpeed2;
static int ySpeed2;
static boolean xcol, ycol;
static JFrame frame = new JFrame();
public static void main(String[] args) throws Exception {
frame.setSize(width, height);
JPanel left = new JPanel();
JTextField text1 = new JTextField();
text1.addKeyListener(new keyListener());
left.add(text1);
JTextField text2 = new JTextField();
text2.addKeyListener(new keyListener());
JPanel right = new JPanel();
right.add(text2);
frame.add(left);
frame.add(right);
frame.add(graphics);
frame.setVisible(true);
while (!xcol || !ycol) {
keyListener.move();
x1 += xSpeed1;
x2 += xSpeed2;
y1 += ySpeed1;
y2 += ySpeed2;
int s1 = x1;
int e1 = x1 + sWidth;
int s2 = x2;
int e2 = x2 + sWidth;
xcol = (s1 <= s2 && s2 <= e1) || (s1 <= e2 && e2 <= e1);
s1 = y1;
e1 = y1 + sHeight;
s2 = y2;
e2 = y2 + sHeight;
ycol = (s1 <= s2 && s2 <= e1) || (s1 <= e2 && e2 <= e1);
Thread.sleep(10);
frame.repaint();
}
Thread.sleep(1000);
System.exit(0);
}
static class keyListener extends KeyAdapter {
private HashSet<Integer> pressed = new HashSet<>();
private final int[] KEYS = {KeyEvent.VK_W, KeyEvent.VK_A, KeyEvent.VK_D, KeyEvent.VK_S, KeyEvent.VK_DOWN,
KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, KeyEvent.VK_UP};
public static void move() {
ySpeed1 = 0;
xSpeed1 = 0;
ySpeed2 = 0;
xSpeed2 = 0;
if (move[0]) ySpeed1 -= 10;
if (move[1]) xSpeed1 -= 10;
if (move[2]) xSpeed1 += 10;
if (move[3]) ySpeed1 += 10;
if (move[4]) ySpeed2 += 10;
if (move[5]) xSpeed2 -= 10;
if (move[6]) xSpeed2 += 10;
if (move[7]) ySpeed2 -= 10;
}
public void keyPressed(KeyEvent e) {
pressed.add(e.getKeyCode());
for (int f : pressed) {
boolean found = false;
for (int i = 0; i < 8 && !found; i++) {
if (f == KEYS[i]) {
move[i] = true;
found = true;
}
}
}
}
public void keyReleased(KeyEvent e) {
boolean found = false;
for (int i = 0; i < 8 && !found; i++) {
if (e.getKeyCode() == KEYS[i]) {
move[i] = false;
found = true;
}
}
pressed.remove(e.getKeyCode());
}
public void keyTyped(KeyEvent e) {
// not used
}
}
static class myGraphicsPanel extends JPanel {
public void paintComponent(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(x1, y1, 100, 100);
g.setColor(Color.RED);
g.fillRect(x2, y2, 100, 100);
if (xcol && ycol) {
g.setFont(new Font("Verdana", Font.PLAIN, 36));
g.drawString("Game Over!", 0, 0);
System.out.println("Made it here");
}
}
}
}
答案 0 :(得分:1)
更改打印坐标以将字符串左右移动。尝试200,200
。
此外,您应该执行以下操作:
start()
中。然后您可以调用您的
通过执行以下程序: new VirtualTag().start();
通常,您应按以下步骤在main中启动程序:
SwingUtilities.invokeLater(()->new Virtual_Tag().start());
但是在这种情况下,这是行不通的,因为您将不断循环进入Event Dispatch Thread
,并且不会被涂上任何东西。