令牌 ")" / "(", ; 预期的语法错误。- 游戏开发

Syntax error on token ")" / "(", ; expected. - Game Development

所以我们应该在 java 中传递一个游戏引擎示例,并按照 youtube 上的教程进行操作。 Game Dev 1 --- 但由于某种原因它在我的 eclipse 上不起作用,它还说更新和绘制方法未定义类型为 Core。

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Window;

public abstract class Core {

public static DisplayMode modes[] = {
        //new DisplayMode(1920,1080,64,0),
        //new DisplayMode(1920,1080,32,0),
        //new DisplayMode(1920,1080,24,0),
        //new DisplayMode(1920,1080,16,0),
        new DisplayMode(800,600,32,0),
        new DisplayMode(800,600,24,0),
        new DisplayMode(800,600,16,0),
        new DisplayMode(640,480,32,0),
        new DisplayMode(640,480,24,0),
        new DisplayMode(640,480,16,0),  
};

private boolean running;
protected ScreenManager s;

//stop method
public void stop(){
    running = false;
}

//call init and gameloop
public void run(){
    try{
        init();
        gameLoop();
    }finally{
        s.restoreScreen();
    }
}

//set to fullscreen
public void init(){
    s = new ScreenManager();
    DisplayMode dm = s.findFirstCompatibleMode(modes);
    s.setFullScreen(dm);

    Window w = s.getFullScreenWindow();
    w.setFont(new Font("Arial", Font.PLAIN, 20));
    w.setBackground(Color.GREEN);
    w.setForeground(Color.WHITE);
    running = true;
}

//main gameLoop
public void gameLoop(){
    long startTime = System.currentTimeMillis();
    long cumTime = startTime;

    while(running){
        long timePassed = System.currentTimeMillis() - cumTime;
        cumTime += timePassed;

        update(timePassed);

        Graphics2D g = s.getGraphics();
        draw(g);
        g.dispose();
        s.update();

        try{
            Thread.sleep(20);
        }catch(Exception ex){}
    }

    //update animation
    public void update(long timePassed){
    }

    //draw to screen
    public abstract void draw(Graphics2D g);

}

}

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyTest extends Core implements KeyListener {

public static void main(String args[]){
    new KeyTest().run();
}


private String mess = "";

//init also call init from superclass
public void init(){
    super.init();
    Window w = s.getFullScreenWindow();
    w.setFocusTraversalKeysEnabled(false);
    w.addKeyListener(this);
    mess = "press ESC to exit";
}

//key pressed
public void keyPressed(KeyEvent e){
    int keyCode = e.getKeyCode();
    if(keyCode == KeyEvent.VK_ESCAPE){
        stop();
    }else{
        mess = "Pressed: " + KeyEvent.getKeyText(keyCode);
        e.consume();
    }
}

//key released
public void keyReleased(KeyEvent e){
    int keyCode = e.getKeyCode();
    mess = "Released: " + KeyEvent.getKeyText(keyCode);
    e.consume();
}

//last method from interface
public void keyTyped(KeyEvent e){
    e.consume();
}

//draw
public synchronized void draw(Graphics2D g){
    Window w = s.getFullScreenWindow();
    g.setColor(w.getBackground());
    g.fillRect(0, 0, s.getWidth(), s.getHeight());
    g.setColor(w.getForeground());
    g.drawString(mess, 30, 30);
}

}

看看你的public void gameLoop()方法。它包含 public void update(long timePassed)public abstract void draw(Graphics2D g);。您必须在其实现末尾添加 } 并从中声明这 2 个方法,例如:

//main gameLoop
public void gameLoop(){
    long startTime = System.currentTimeMillis();
    long cumTime = startTime;

    while(running){
        long timePassed = System.currentTimeMillis() - cumTime;
        cumTime += timePassed;

        update(timePassed);

        Graphics2D g = s.getGraphics();
        draw(g);
        g.dispose();
        s.update();

        try{
            Thread.sleep(20);
        }catch(Exception ex){}
    }
} //<-here is one you've lost

//update animation
public void update(long timePassed){
}

//draw to screen
public abstract void draw(Graphics2D g);