gpt4 book ai didi

java - 当两个方 block 碰撞时 g.drawString 没有绘制游戏结束

转载 作者:行者123 更新时间:2023-11-30 05:34:17 25 4
gpt4 key购买 nike

我正在尝试用方 block 制作一个虚拟标签。目前,我的重点是让游戏在玩家捕获另一个玩家时宣布游戏结束并在一秒钟后关闭。但是,当两个方 block 碰撞时,程序不会向屏幕写入任何内容。

我知道调用 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");
}
}
}
}

最佳答案

更改打印坐标以向右和向下移动字符串。尝试 200,200

此外,您应该执行以下操作:

  • 去掉静态语句。
  • 然后将 main 中的代码移至 start() 方法中。然后您可以调用您的通过执行以下操作来进行编程:
  new VirtualTag().start();

通常您应该在 main 中启动程序,如下所示:

 SwingUtilities.invokeLater(()->new Virtual_Tag().start());

但在这种情况下,这不起作用,因为您将在事件调度线程中连续循环,并且不会绘制任何内容。

关于java - 当两个方 block 碰撞时 g.drawString 没有绘制游戏结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56915841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com