0

我刚开始尝试制作游戏,我做了一个 HelloWorld Applet,然后测试了我在上面滚动的想法,最终它开始变成了“直升机”风格的游戏。现在一切正常,直到我决定放入一堆 switch 语句来处理状态(标题屏幕、运行和游戏结束)。之前运行的代码没有改变,我的新“介绍屏幕”工作正常,但是当您切换到游戏状态时,双缓冲似乎失控了。游戏前景快速闪烁,并切掉了三角形,而背景几乎没有渲染。这只是我探索游戏编码的基本原则,所以它不是优雅的或模块化的或任何东西,但它应该可以工作......

[编辑] 我知道 Applets 和 AWT 通常可能是一个不好的方法,但我是这样开始的,我只是想了解它是如何工作的以及我做错了什么,这样我才能感到满意并继续前进。

package testStuff;


import java.awt.*;
import java.applet.*;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class HelloWorld extends Applet implements Runnable{

//game time counter
int count = 0;

//controller object
Controller control;

//accesses images and creates image variables
File wavesource = new File("C:\\sourceimages\\waves.jpg");
File playerSource = new File("C:\\sourceimages\\plane3.png");
Image player = null;
Image waves = null;

//font for score
Font myFont;

//double buffer objects
Graphics bground;
Image bgImage = null;
private int bgx = 0;

//player position
private int xPos=0;
private int yPos=50;

//arrays for tunnel locations
private int[] topTunnel = new int[200];
private int[] botTunnel = new int[200];

//size of tunnel
private int tunnelSize;

//boolean determines direction of tunnel movement
private boolean tunUp;

//state
private int state;


//"constructor"
public void init(){

    //set state
    state = 0;
    //instantiates controller adds it to the applet
    control = new Controller();
    this.addKeyListener(control);
    //instantiates images
    try {
        waves = ImageIO.read(wavesource);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        player = ImageIO.read(playerSource);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //instantiates arrays and tunnel size
    for(int i=0;i<199;i++){
        topTunnel[i]=-1;
        botTunnel[i]=-1;
    }
    topTunnel[199]=20;
    botTunnel[199]=179;
    tunnelSize = botTunnel[199]-topTunnel[199];
    tunUp = false;
}
public void paint(Graphics g){
    switch(state){
    case 0:
        g.setColor(Color.black);
        myFont = new Font("Courier", Font.BOLD+Font.ITALIC, 12);
        g.setFont(myFont);
        g.drawString("DON'T CRASH THE PLANE BRO", 10, 100);
        myFont = new Font("Courier", Font.PLAIN, 8);
        g.setFont(myFont);
        g.drawString("Press Spacebar to Play", 40, 150);
        break;
    case 1:
        g.drawImage(player, xPos, yPos, null);
        g.setColor(Color.red);
        for(int i=0;i<200;i++){
            g.fillRect(i, 0, 1, topTunnel[i]);
            g.fillRect(i, botTunnel[i], 1, botTunnel[i]);
        }
        g.setColor(Color.cyan);
        myFont = new Font("Helvetica", Font.PLAIN, 12);
        setFont(myFont);
        if(count<170)
            g.drawString("SCORE: " + 0, 0, 12);
        else
            g.drawString("SCORE: " + (this.count-170), 0, 12);
        break;
    }
}

public void start(){
    Thread thread = new Thread(this);
    thread.start();
}

@Override
public void run(){
    while(true){
        switch(state){
        case 0:
            //increases count
            count++;

            //paints
            this.repaint();

            try {
                    Thread.sleep(1000/30);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            if(control.spaceDown()){
                state = 1;
                count = 0;
            }
            break;

        case 1:
            //increases count
            count++;

            //handles scrolling
            if(xPos<75){
                xPos++;
            }
            else{
                if(bgx>-600){
                    bgx--;
                }
                else{
                    bgx=0;
                }
            }
            //handles input
            if(control.spaceDown()==true&&yPos>=0){
                yPos--;
            }
            else if(yPos<180){
                yPos++;
            }
            //repositions tunnel
            if(xPos>=75){
                for(int i=1;i<200;i++){
                    topTunnel[i-1]=topTunnel[i];
                    botTunnel[i-1]=botTunnel[i];
                }
            }
            //defines new tunnel space
            if(topTunnel[199]<=0 || !tunUp)
                topTunnel[199]++;
            if(botTunnel[199]>=200 || tunUp)
                topTunnel[199]--;
            botTunnel[199] = topTunnel[199]+tunnelSize;

            //randomly chooses direction to move tunnel
            double randomNum = Math.random();
            if(randomNum>.5)
                tunUp = true;
            else
                tunUp = false;

            //narrows tunnel
            if(count%20 == 0)
                tunnelSize--;

            //calls update
            this.repaint();
            //handles framerate
            try {
                Thread.sleep(1000/30);
            } catch (InterruptedException e) {
                 //TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
    }
}

public void update(Graphics g){

    //instantiates image and graphics on first tick
    if(bgImage == null){
        bgImage = createImage(this.getSize().width, this.getSize().height);
        bground = bgImage.getGraphics();
    }
    //create background based on state
    switch(state){
    case 0:
        //flashing colors!
        if(count%20<10){
            bground.setColor(Color.yellow);
            bground.fillRect(0, 0, 200, 200);
        }
        else{
            bground.setColor(Color.orange);
            bground.fillRect(0, 0, 200, 200);
        }
        break;
    case 1:
        //draws background image(s)
        bground.drawImage(waves,bgx,0,this);
        if(bgx<-399)
            bground.drawImage(waves,bgx+600,0,this);
        break;
    }
    //paint over the background then draw it to screen
    paint(bground);
    g.drawImage(bgImage, 0, 0, this);
}
 }
4

2 回答 2

1

您需要“清除”每一帧上的图形,否则您正在绘制之前绘制的内容......

在下面的示例中,我在“播放”时填充了图形上下文,但在暂停时保持原样,您应该能够看到区别......

public class HelloWorld extends Applet implements Runnable {

    private int direction = 4;
    private int state = 0;
    private Image bgImage;
    private Graphics bground;
    private int count;

    private int x = 0;

//"constructor"
    public void init() {
        addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                    if (state == 0) {
                        state = 1;
                    } else if (state == 1) {
                        state = 0;
                    }
                }
            }

        });
    }

    public void paint(Graphics g) {
        switch (state) {
            case 0:
                g.setColor(Color.black);
                g.drawString("DON'T CRASH THE PLANE BRO", 10, 100);
                g.drawString("Press Spacebar to Play", 40, 150);
                break;
            case 1:
                break;
        }
    }

    public void start() {
        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void run() {
        while (true) {
            switch (state) {
                case 0:
                    count++;
                    this.repaint();
                    break;
                case 1:

                    x += direction;
                    if (x < 0) {
                        x = 0;
                        direction *= -1;
                    } else if (x > getWidth()) {
                        x = getWidth();
                        direction *= -1;
                    }

                    //calls update
                    this.repaint();
                    //handles framerate
                    try {
                        Thread.sleep(1000 / 30);
                    } catch (InterruptedException e) {
                        //TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    break;
            }
        }
    }

    public void update(Graphics g) {

        //instantiates image and graphics on first tick
        if (bgImage == null) {
            bgImage = createImage(this.getSize().width, this.getSize().height);
            bground = bgImage.getGraphics();
        }
        //create background based on state
        switch (state) {
            case 0:
                //flashing colors!
                if (count % 20 < 10) {
                    bground.setColor(Color.yellow);
                    bground.fillRect(0, 0, 200, 200);
                } else {
                    bground.setColor(Color.orange);
                    bground.fillRect(0, 0, 200, 200);
                }
                break;
            case 1:
                bground.setColor(Color.WHITE);
                bground.fillRect(0, 0, getWidth(), getHeight());
                bground.setColor(Color.RED);
                int y = (getHeight() / 2) - 4;
                bground.fillOval(x, y, 8, 8);
                break;
        }
        //paint over the background then draw it to screen
        paint(bground);
        g.drawImage(bgImage, 0, 0, this);
    }

}
于 2013-01-24T09:23:30.433 回答
0

我认为你应该开始但添加更多的大括号。我是编码的初学者,但从我一直在阅读的内容来看,在代码中的某些语句上编写没有大括号的冗长语句会导致一些错误。

这里有很多过程,我觉得大括号有助于

 //defines new tunnel space
        if(topTunnel[199]<=0 || !tunUp)
            topTunnel[199]++;
        if(botTunnel[199]>=200 || tunUp)
            topTunnel[199]--;
        botTunnel[199] = topTunnel[199]+tunnelSize;
        //randomly chooses direction to move tunnel
        double randomNum = Math.random();
        if(randomNum>.5)
            tunUp = true;
        else
            tunUp = false;

        //narrows tunnel
        if(count%20 == 0)
            tunnelSize--;

        //calls update
        this.repaint();

如果我错了,请纠正我,我也想学习!

于 2013-01-24T06:55:05.290 回答