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

时间:2015-10-20 16:46:27

标签: java abstract

所以我们应该在java中传递游戏引擎示例,并在youtube上遵循本教程。 Game Dev 1 ---但由于某种原因它在我的日食上不起作用,它还说更新和绘制方法未定义为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);
}

}

1 个答案:

答案 0 :(得分:2)

查看您的public void gameLoop()方法。它包含public void update(long timePassed)public abstract void draw(Graphics2D g);。你必须在它的实现结束时添加}并声明这两种方法,例如:

//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);