package com.Multiply.com;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Core extends JPanel {
public static void main(String[] args){
Core game = new Core();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setTitle("Multiply");
game.frame.setIconImage(testIcon);
game.frame.add(game);
game.frame.pack();
game.frame.setLocationRelativeTo(null);
game.frame.setResizable(false);
game.frame.setVisible(true);
t1.start();
running = true;
Collision.charCollision();
}
//start of variables
private static final long serialVersionUID = 1L;
public static int width = 1280; //width and height of the screen
public static int height = 720;
public static boolean running = false;
public static int charX = width /2 - 50;
public static int charY = height /2 - 50;
public static int charSizeX = 50;
public static int charSizeY = 50;
public static double charSpeed = 10;
public static Image testIcon = Toolkit.getDefaultToolkit().getImage("Multiply_Icon.png"); //Icon variables, locates the icon in the folder
JFrame frame = new JFrame(); //the frame variable
static Thread t1 = new Thread();
public Core(){
KeyListener listener = new MyKeyListener();
addKeyListener(listener);
setFocusable(true);
Dimension size = new Dimension(width, height); //variable that sets the size of the window to 1280x720p
setPreferredSize(size); //Sets the size of the window to "size"
frame = new JFrame(); //makes frame into the JFrame window
}
public class MyKeyListener implements KeyListener {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
//System.out.println("keyPressed="+KeyEvent.getKeyText(e.getKeyCode()));
if(keyCode == KeyEvent.VK_W){
charY-= charSpeed;
repaint();
}
else if(keyCode == KeyEvent.VK_A){
charX-= charSpeed;;
repaint();
}
else if(keyCode == KeyEvent.VK_S){
charY+= charSpeed;
repaint();
}
else if(keyCode == KeyEvent.VK_D){
charX+= charSpeed;
repaint();
}
}
@Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
//System.out.println("keyReleased="+KeyEvent.getKeyText(e.getKeyCode()));
if(keyCode == KeyEvent.VK_ESCAPE){
ConfigFileWriter cfw = new ConfigFileWriter();
if(cfw.wroteFile == false){
cfw.openFile();
cfw.writeFile();
cfw.closeFile();
cfw.wroteFile = true;
System.out.println("wroteFile = true");
}
running = false;
System.out.println("Escape");
try {
t1.join();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.exit(0);
}
else if(keyCode == KeyEvent.VK_OPEN_BRACKET){ //To be removed, testing purposes
charSpeed-= 1;
}
else if(keyCode == KeyEvent.VK_CLOSE_BRACKET){
charSpeed+= 1;
}
else if(keyCode == KeyEvent.VK_L){
ConfigFileReader cfr = new ConfigFileReader();
cfr.openFile();
cfr.readFile();
cfr.closeFile();
charSpeed = Double.parseDouble(cfr.speed);
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
public void paint(Graphics g) { // Draws top first to bottom last
Graphics2D g2 = (Graphics2D) g;
Image background = Toolkit.getDefaultToolkit().getImage("C:/Users/Kanin/Desktop/Multiply_Game_Engine/Images/Black_Background.png");
Image character = Toolkit.getDefaultToolkit().getImage("C:/Users/Kanin/Desktop/Multiply_Game_Engine/Images/Character.png"); // C:/Users/Kanin/Desktop/Multiply_Game_Engine/
g2.drawImage(background, 0, 0, this); //draws the black background
g2.drawImage(character, charX, charY, charSizeX, charSizeY, this);
}
}
package com.Multiply.com;
public class Collision extends Core {
private static final long serialVersionUID = 1L;
public static void charCollision(){
while(running){
System.out.println("Running"); //Won't detect unless this is here for some reason.
if(charX < -1){ //Character can't go past the left side of the screen.
charX = 0;
}
else if(charX > 1240){ //Character can't go past the right side of the screen.
charX = 1240;
}
else if(charY < -1){ //Character can't go above the top side of the screen.
charY = 0;
}
else if(charY > 680){ //Character can't go below the bottom side of the screen.
charY = 680;
}
}
}
}
在最顶端附近说 的System.out.println( “暗战”); 如果我没有那条线,它就不会注册我的区块已超出界限而且它会继续下去。当我运行游戏时,如果我删除了system.out代码块,它甚至不会在if语句中运行任何东西
答案 0 :(得分:1)
我的猜测是你注册的KeyListener
是在与主线程(GUI框架产生的线程之一)不同的线程中执行的,但是Collision.charCollision
方法是在主线程。
当省略System.out.println
语句时,方法charCollision
中的变量假定为VM自包含,但只要您插入与控制台的交互(从而与其他线程交互) )变量running
与GUI线程保持同步。
您可以尝试声明GUI中以及主线程中使用的所有变量volatile
,看看是否已经解决了您的问题。
有关多线程的详细信息,请参阅Loop doesn't see changed value without a print statement。