Java help with Text-based adventure game

Started by
1 comment, last by Matthewj234 12 years, 3 months ago
I'm very new to game development so i thought I would start out with a text-based adventure game. I know how to set up displaying the text and giving you options and things like that. But i wish to display it in a frame. I believe it's a Jframe i'm supposed to use? I also wish to maybe have like a title screen and a "game over" screen and a "you win" screen.

Can anyone help?
Advertisement
Hey, I started out writing a text based adventure game like yourself smile.png

Anyway, to make a JFrame is very simple:


class whatever{
public static void main(String[] args){
JFrame frame = new JFrame("Name of your window");
frame.setSize(width,Height);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE)
//stops the program when the jframe is closed
frame.setLocation(x,y);
frame.setVisible(true)
}
}



but from there you have to populate the window like so:

Add this line of code to your main method
frame.add(new update());

Your update class could be this:
(btw Im using a thread for this class. you can use a Timer if you want just search it in google or something)

public class Update extends JPanel implements Runnable, KeyListener{
//The key listener so your program can take key input
Thread thread;
boolean gameover;
public Update(){
addKeyListener(this);
setFocusable(true);
thread = new Thread(this);
thread.start();
}
public void update(){}
public void paint(Graphics g){}
public void run(){
while(!gameover){
//game loop
update();
repaint();

thread.sleep(insert sleep time in millis here);

}
//display game over screen
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}

}



This just sets up a nice framework for you text adventure game..
But I would recommend just having a console so you wont have to bother having to do switches for the key inputs etc...
Although you could probably make the whole adventure game a lot better with JFrame because you could have puzzles and stuff that would involve colour.

Hope it helped.
Gen..
Dont look into JFrames yet. To start with make use of you IDE's cosole, then look into a JFrame based console. purely because as far as i am aware, there isnt a built in console for JFrames.

- Numprt

This topic is closed to new replies.

Advertisement