利用公共变量

  • 本文关键字:变量 java variables
  • 更新时间 :
  • 英文 :


我创造了一款类似于《蛇》的游戏,在这款游戏中,用户首先会看到一个jpanel,询问他们想要的难度,他们选择的JButton会影响地图的大小以及蛇移动之间的延迟。我有地图大小工作得很好,但延迟变量似乎从未改变。我怀疑这与计时器的方式有关,但我不知道如何修复它。我也想知道当程序第一次运行时,似乎有些变量不更新,但第二次运行时,所有变量都更新了。下面是我的类与原始变量和碰撞检测:

import java.util.Random;
import javax.swing.JLabel;

public class GameEngine extends JPanel implements ActionListener{
//creates the size of the panel as well as creating the resolution for all objects, including the players and food.
static final int sWidth = 600;
static final int sHeight = 600;
public static int size = 24;
static int objectSize = (sHeight*sWidth) / size;
public static int delay = 100;
final int playerX[] = new int[objectSize];
final int playerY[] = new int[objectSize];
int bodySize = 4;
int score = 0;
int appleX;
int appleY;
char direction = 'D';
boolean started = false;
Random random;
Timer timer;
boolean easy;
JLabel score1;
public static String difficulty;


GameEngine(){

random = new Random();
this.setPreferredSize(new Dimension(sWidth,sHeight));
this.setBackground(Color.black);
this.setFocusable(true);
this.addKeyListener(new UserMovement());
gameStart();



}
public void gameStart() {

newApple();
started = true;
timer = new Timer(delay,this);
timer.start();


}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);

}

public void drawHead(Graphics g) {
g.setColor(new Color(100,252,0));
g.fillRect(playerX[0], playerY[0], size, size);
}
public void draw(Graphics g) {
if(started) {

//draws the apples
g.setColor(Color.red);
g.fillOval(appleX, appleY, size, size);



for (int i = 0; i < bodySize; i++) {
if(i == 0) {
drawHead(g);
}
else {
g.setColor(new Color(60,180,0));
g.fillRect(playerX[i], playerY[i], size, size);
}
}

g.setColor(Color.white);
g.setFont(new Font("Bold", Font.BOLD, 20));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("Score: " + score,(sWidth - metrics.stringWidth("Score: " + score))/2,g.getFont().getSize());
}
}
public void newApple(){
appleX = random.nextInt((int)(sWidth/size))*size;
appleY = random.nextInt((int)(sHeight/size))*size;
}

//moves the player by using and modifying their coordinates   
public void move() {
for (int i = bodySize; i > 0; i--) {
playerX[i] = playerX[i-1];
playerY[i] = playerY[i-1];

}

switch(direction) {
case 'W':
playerY[0] = playerY[0] - size;
break;
case 'S':
playerY[0] = playerY[0] + size;
break;
case 'A':
playerX[0] = playerX[0] - size;
break;
case 'D':
playerX[0] = playerX[0] + size;
break;


}  
}

public void checkFood() {
if(playerX[0] == appleX && playerY[0] == appleY)
{  
bodySize++;
score++;
newApple();

}
}
public void checkCol() {
//checks for head collision with the body
for(int i = bodySize; i > 0; i--) {
if((playerX[0] == playerX[i]) && (playerY[0] == playerY[i])) {
started = false;
}
}
//checks if head touches any of the walls of the program
if(playerX[0] < 0) {
started = false;
}

if(playerX[0] > sWidth) {
started = false;
}

if(playerY[0] < 0) {
started = false;
}

if(playerY[0] > sHeight) {
started = false;
}

if(started != true) {
timer.stop();
}

}
public void actionPerformed(ActionEvent e){

if(started == true) {
move();
checkFood();
checkCol();
}
repaint();

}
public class UserMovement extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if(direction != 'D') {
direction = 'A';
}
break;
case KeyEvent.VK_RIGHT:
if(direction != 'A') {
direction = 'D';
}
break;
case KeyEvent.VK_UP:
if(direction != 'S') {
direction = 'W';
}
break;
case KeyEvent.VK_DOWN:
if(direction != 'W') {
direction = 'S';
}
break;
}
}

public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
}

,这里是代码调用和改变delaysize变量:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class StartMenu extends JPanel {
StartMenu()
{
JButton easy = new JButton();
JButton hard = new JButton();
this.setPreferredSize(new Dimension(350,240));
this.setLayout(null);
this.setBackground(Color.black);
this.setFocusable(true);
easy.setBounds(75,40,200,40);
hard.setBounds(75,120,200,40);
this.add(easy);
this.add(hard);

easy.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
setVisible(false);
new SnakeStart();
GameEngine.size = 48;
GameEngine.delay = 140;
}
});

hard.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
setVisible(false);
new SnakeStart();
GameEngine.size = 24;
GameEngine.delay = 70;
}
});


}
}

我假设您的GameEngine实例是在StartMenu动作侦听器执行之前创建的。如果这个假设是正确的,这意味着默认值为delayGameEngine.timer是在GameEngine构造函数中创建的,并且在delay更改后不会更新。

你需要确保在StartMenu动作被调用后,用新的delay值显式地更新你的timer

最新更新