如何让java游戏在继续代码之前等待__秒或输入?我正在尝试制作一个2d跑步跳投游戏

时间:2017-05-27 00:17:50

标签: java swing

我正在努力制作一个2D赛跑游戏,这最终就像一个跳跃任务,但我找不到让程序等待输入的方法或等待___秒再继续代码。如果玩家在__秒之后没有跳跃,那么如果他在给定的时间段之后没有跳跃,那么该角色应该向右移动。如果玩家确实跳了,他应该在Y中向前移动一个并在X上向右移动一次。

package runalreadypls;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class GameGen extends JFrame implements KeyListener{
    private static final long serialVersionUID = -7177541209377865450L;
    Random rand = new Random();
    //Frame location coordinates
    int [][] world=new int[10][102] ;
    int frameX=0;
    int frameY=0;
    int WIDTH = 720;
    int HEIGHT = 480;
    //Character coordinates
    int charX = 0;
    int charY= 4;
    boolean gameOver = false;
    final double minus = 1000;
    BufferedImage img1;
    static double score = 0;
    GameGen(){
        super("Runner");
        this.setSize(WIDTH,HEIGHT);
        this.setLocationRelativeTo(null);
        this.setUndecorated(true);
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.setResizable(false);
        this.setFocusable(true);
        this.setVisible(true);
        addKeyListener(this); 
        worldGen();
        repaint(); 
        //I don't really know what this does but I was trying to make it 
        display graphics before it started the code first
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                do{
                    if(gameOverCollision()){
                        break;
                    }

                    score = score + 3;
                    charX= charX + 1;
                    if(gameOverCollision()){
                        break;
                    }
                    if (charY > 9){
                        if (world[charY+1][charX] == 0){
                            charY = charY+1;
                            mover();
                        }
                    }
                    mover();
                    if(gameOverCollision()){
                        break;
                    }
                }while(gameOverCollision());
            }
        });
        Game game = new Game();

        if (gameOver){
            System.out.println("Thank you for playing " +game.userName +" your score is: " + score);
        }
    }


    public void mover(){
        world[charY][charX] = 2;
        repaint();
    }
    /*
     1 = Ground
     2 = Player
     3 = Background
    */
    public void worldGen(){
        int xAt = 5;
        world[4][0] = 2;//Character placement
        world[5][0] = 1;//First tile placement
        for (int x = 1; x < 100; x++){  //Starting place
            int X =3;
            X = rand.nextInt(X)+1;
            if (xAt <= 6 && xAt>=3){
                if (X == 1){
                    xAt =xAt+ 1;
                    world[xAt][x] = 1;
                }else if (X == 3){  
                    xAt =xAt - 1;
                    world[xAt][x] = 1;
                }else if (X==2){
                    world[xAt][x]=1;
                }
            }else if(xAt >6 && xAt <9){
                if (X==1 || X==3){
                    xAt =xAt- 1;
                    world[xAt][x] = 1;
                }else if (X==2){
                    world[xAt][x]=1;
                }
            }else if(xAt >1 && xAt <3){
                if (X==1 || X==3){
                    xAt =xAt+ 1;
                    world[xAt][x] = 1;
                }else if (X==2){
                    world[xAt][x] = 1;
                }
            }
        }
        for(int x = 0;x <9;x++){
            for(int y = 0; y < 100; y++){
                if (world[x][y] == 1){
                    world[x+1][y] = 1;
                } 
            }
        }
    }
    public void paint(Graphics g){
        super.paint(g);
            for(int x = 0;x <9;x++){
                for(int y = 0; y < 100; y++){
                    if(world[x][y] == 0){
                        try {
                            img1 = ImageIO.read(new File("src/runalreadypls/Background.png"));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    } else if(world[x][y] == 1){
                        try {
                            img1 = ImageIO.read(new File("src/runalreadypls/Foreground.png"));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }else if (world[x][y] == 2){
                        try {
                            img1 = ImageIO.read(new File("src/runalreadypls/Character.png"));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    g.drawImage(img1,45*y,60*x,null);
                }
            }


    }
    /*
     * 
     * WALL collision
     * If player misses jump
     * Game is over
     * 
     * 
     * */
    public boolean gameOverCollision(){
        if(world[charY][charX] == 1){
            gameOver = true;
            return true;
        }else{
            return false;
        }       
    }
    /*
     * 
     * KeyListener -Y
     * 
     * 
     * */
    @Override
    public void keyPressed(KeyEvent e) {
        if(charY<9 && charY >0){
            if (e.getKeyCode()==KeyEvent.VK_SPACE){
                charY-=1;
                mover();
                repaint();
                if(world[charY][charX+1] != 1){
                    score = score -3;           
                }
            }
        }
    }

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

}

0 个答案:

没有答案