Text Based Adventure Game
Hi All, this one is a text based adventure game, and the best thing is you can write the story as you like and the same code will work for it.
This one consist of 3 files.
1) Code
2) Story line input file in text format
3) Text file than contains all the information about story.
I'll come to code later, but first information text file.
This file contains one line for every scenario for you story.
This one consist of 3 files.
1) Code
2) Story line input file in text format
3) Text file than contains all the information about story.
I'll come to code later, but first information text file.
This file contains one line for every scenario for you story.
Eg.
story.txt
Hi, I am Naruto. I want to be Hokage(Village leader).
Day 1, It's finally the day i'll become Genin. I need to take the test first.
(At Examination Hall) Oh no ! , I can't do this jutsu, do you know which jutsu is this.
1) Shadow Clone jutsu.
2) Fire ball jutsu.
lines.txt
3 2 1
lines.txt first line is 3 2 1, it provide information how to read story.txt. 3 means we have 3 line for story setup, like in above example. it can be any number as long as you take care of the screen size. 2 means we are going to provide 2 options for the user. And last 1 means that choice one is correct.
so you can create you own story, you just have to follow the rule as above.
so you can create you own story, you just have to follow the rule as above.
Code:
import cursesfrom time import sleep
def main(): screen = curses.initscr() screen.border() screen.keypad(1) curses.noecho() curses.cbreak() curses.curs_set(0) story(screen) curses.endwin()
if __name__ == "__main__":
main()
Screenshot 1: This is how story looks.
Screenshot 2: Example of how the options can be.
Screenshot 3:Scene 2 starts, if you choose correct answer.
Screenshot 4: Just in case you loose.
Side note : Yes i like Naruto way too much.
Author Shanky.Rawat
#function to read story and check if user is given the correct answers
def story(scr): scr.refresh()#reads both the file in read only mode.
strText=open('/path/to/file/story.txt','r') strLine=open('/path/to/file/lines.txt','r')#reads line by line from lines.txt
for x in strLine:x=x.rstrip('\n') #removes \n from end of line
x=x.split(' ') #split all elements line by line, on the basis of space
#read line by line from story.txt on the basic of number of lines from lines.txt
for y in range(len(x)):#checks if the element is last one, also checks, if your input is correct
if y == len(x)-1: if chr(usr_input) == x[y]: continue else:#in case user input is wrong, game end.
story_draw(15,scr, "Nope, wrong choice, you have to watch Naruto Again.") scr.getch() curses.endwin() exit() else: for y1 in range(int(x[y])): a=strText.readline().rstrip('\n') story_draw(y1+3,scr,a) usr_input=scr.getch() scr.clear() scr.border() scr.refresh() scr.refresh() scr.getch()# function to print string for output on curses.
def story_draw(y,win,a): for x in range(len(a)): win.addstr(y,x+5,a[x])#just to add little animation 😆
sleep(.01) win.refresh() returndef main(): screen = curses.initscr() screen.border() screen.keypad(1) curses.noecho() curses.cbreak() curses.curs_set(0) story(screen) curses.endwin()
if __name__ == "__main__":
main()
Screenshot 1: This is how story looks.
Screenshot 2: Example of how the options can be.
Screenshot 3:Scene 2 starts, if you choose correct answer.
Screenshot 4: Just in case you loose.
Side note : Yes i like Naruto way too much.
Author Shanky.Rawat
No comments:
Post a Comment