0

I'm trying to make a 2D runner game for my culminating that is like a jump quest but I can not find a way to make the program wait for input or wait ___ seconds before continuing with the code. If the player were to not jump after __ seconds the character should move to the right one, if he does not jump after the given time slot. If the player does jump he should go one one the Y and move once to the right on the 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) {
    }

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • To have a Swing GUI delay, use a [Swing Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). To wait for input -- Swing is an event-driven GUI, and so simply respond to whatever event needs to be listened to. Change the behavior of the response based on the state of the GUI (the state of the fields it holds). – Hovercraft Full Of Eels May 27 '17 at 00:20
  • 1
    I see that you're calling `Thread.sleep` **on** the Swing event thread, **NEVER** do this. Please read [Lesson: Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) – Hovercraft Full Of Eels May 27 '17 at 00:21
  • ok thanks I will look into it. – David Huynh May 27 '17 at 00:22
  • You're welcome and good luck – Hovercraft Full Of Eels May 27 '17 at 00:23
  • Um is it okay if i ask how i would stop flickering? It flickers pretty badly and I cant seem to find why or should i open another question – David Huynh May 28 '17 at 22:15

0 Answers0