Showing posts with label game. Show all posts
Showing posts with label game. Show all posts

Thursday, 18 May 2017

Text Based Adventure Game

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.
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.

Code:

import cursesfrom time import sleep

#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() return
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

Friday, 21 April 2017

MasterMind

--- MasterMind --- 

Number guessing game

The player enters 4 digit number, and the computer tells the player how many(but not which) of the 4 digits are correct.

Number changes randomly every time, when new game starts.

E.g.

Guess the number in few tries as possible

Enter the number :      1234
Enter the number :      4567
*
Enter the number :      7533
***
Enter the number :      7593
you used 25 chances, to guess.


CODE

#imports required for validation, i was using older python version for print, i used below import

from __future__ import print_function
import re
from random import randint

print ("Guess the number in few tries as possible\n");
number=[]
inp=str(randint(0000,9999))
for x in inp:
        number.append(x)

check = True
count=0

#loop until guess is correct

while check == True:
        guess=[]
        input=raw_input("\nEnter the number :\t");
        count+=1

#validation if the input is numeric 
        if not re.match("^[0-9]*$", input):
                print ("\nError! Only numeric value allowed")
                continue

# validation if the number is 4 digit longs or not
        if len(input) != 4:
                print ("\nError! Must be 4 digit long number")
                continue

        for x in input:
                guess.append(x)

        if guess == number:
                check = False
                break

        for i in range(4):
                if number[i] == guess[i]:
                        print ("*", end='')

print ("\nYou used {0} chances, to guess".format(count))