Some questions for mini game


 
Thread Tools Search this Thread
Top Forums Programming Some questions for mini game
# 1  
Old 11-24-2010
Some questions for mini game

Hello fellow programmers!Smilie
First of all excuse me for my English, i am from abroad...

I am trying to learn the C++ programming language as it is one of the most basic languages.So,instead of just creating small examples for each feature(like class,input/output etc), I decided to create a mini game.Nothing special,it will be a game with just numbers and digits,command-line game with some choices affecting the plot etc.Very simple thing(I have in my mind that once I understand the basics of C++, I will search about OpenGL to add graphics in my program but this seems pretty hard for me rigth now).

So, I have a few questions...

1)When the user gives his first/last name, (along with some other features like hit points,weapons carried etc), I decided to "save them " in a single row in a txt file in order to use them inside the game.Actually the main reason for this is that I want to create something like a save file so when a player that has subscribed before enters,he can continue from where he stopped last time(couldnt think of anything else,if you know other ways please tell me).So for the first user the txt file would be something like:

First_Name Last_Name Hit_points Weapon_name

Now the problem that I have is how to manage this file.
A)When another player subscribes, I want all the columns of the file to have the same distance between them.

So instead of:
First_Name Last_Name Hit_points Weapon_name
Bigger_Fisrt_Name Bigger_Last_Name ....

I would like All first names,Last names,weapons etc to be in their own columns
Is that possible?Or I have to write a script or something?

B)Is it possible to sort all rows(let's say by the First_Name)?
C)Is it possible to retrieve a feature(let's say the weapon's name)in order to use it inside the program(for example in a battle as each weapon will do certain damage)

2)When dialogues occur during the story,it is not pretty at all to be presented all together with cout...Even sleep() can't do much work...Is it possible to present them the way I have seen in many games, like someone is typing them at that time? I hope you understand what i am talking about.

Let's start with these two questions and if something new occurs that I cant figure out, i will re-post here...

I have searced to do the things I described above(I used ofstream in order to create the file because until now I was using pure C)...Any suggestion/answer/advice would be really helpful.
Thank you!
# 2  
Old 11-24-2010
Quote:
Originally Posted by bashuser2
1) When the user gives his first/last name, (along with some other features like hit points,weapons carried etc), I decided to "save them " in a single row in a txt file in order to use them inside the game.Actually the main reason for this is that I want to create something like a save file so when a player that has subscribed before enters,he can continue from where he stopped last time(couldnt think of anything else,if you know other ways please tell me).So for the first user the txt file would be something like:

First_Name Last_Name Hit_points Weapon_name

Now the problem that I have is how to manage this file.
A)When another player subscribes, I want all the columns of the file to have the same distance between them.

So instead of:
First_Name Last_Name Hit_points Weapon_name
Bigger_Fisrt_Name Bigger_Last_Name ....

I would like All first names,Last names,weapons etc to be in their own columns
Is that possible?Or I have to write a script or something?
It's possible, sure:printf("%20s %20s %20s", "a", "b", "c"); ...but it's pretty fiddly having to get all the lines the same length all the time when handling raw text, and seems a lot of work for no point: The program needn't care either way.

I'd create the file sorted in the first place, too, instead of sorting later. When the game loads, it loads the entire file into memory; when the game quits, it overwrites the save file completely.

If you arrange the file like:
Code:
First_Name,Last_Name,Hit_points,Weapon_name
First_Name2,Last_Name2,Hit_points2,Weapon_name2
...

...and give it a CSV extension, you'll even be able to open and edit it in Excel! (Or whatever Excel-equivalent you have).
Quote:
C)Is it possible to retrieve a feature(let's say the weapon's name)in order to use it inside the program(for example in a battle as each weapon will do certain damage)
That's a pretty vague question, but is it possible to load something from a file? Definitely. How? That depends how it's stored.
Quote:
2)When dialogues occur during the story,it is not pretty at all to be presented all together with cout...Even sleep() can't do much work...Is it possible to present them the way I have seen in many games, like someone is typing them at that time? I hope you understand what i am talking about.
Code:
#include <stdio.h>
#include <unistd.h>

int main(void)
{
        const char *str="Hello World!\n";
        int n;

        for(n=0; str[n] != '\0'; n++)
        {
                fputc(str[n], stdout); // Write one character
                fflush(stdout); // Don't wait for a newline before really printing it
                usleep(100000L);
        }
        return(0);
}

Quote:
I have searced to do the things I described above(I used ofstream in order to create the file because until now I was using pure C)...
Unless you have a really, really good reason to use iostream? Don't. It's tolerable for structured data, but for text? Blech. Formatting text and numbers in pure C++ is especially painful, there's not good tools like sprintf, just a lot of fiddly, global options...

Last edited by Corona688; 11-24-2010 at 05:22 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 11-25-2010
first of all thank you very much for your interest and your answer.

you are right,using iostream for text file IS very painful,especially for someone like me who doesnt have any previous experience.The only reason to do this is that I want to oblige myself study many aspects of C++ language(from iostream to inheritance and templates...).
But as soon as I study some things,I will choose and use the easier option like you said...
btw the small program you wrote rocks,it is exactly what i wanted.
Just a question in that though: Is there any C++(or even C) way of not calling this function all the time? (as the game will have many dialogues)Is there any way to tell the program that for everything printed in the program,you will use this function? A little bit difficult I think...

A last question if you have any tips :

I want to create a countdown timer in order to obligr the character answer questions in a specific amount of time(seconds).Okay,countdown is easy.But what I cant find till now,is how the seconds will change in the same spot.See below:

NOT what i want:

Time to answer:
10
9
8
....
1

What I want:


Time to answer : 10-->9-->....-->1

but numbers 10,9,8... will replace each other(and will not be presented as a sequence).

I hope you understand what i am asking...

Thanks again for your interest,you have already helped me a lot!

Last edited by bashuser2; 11-25-2010 at 06:48 AM..
# 4  
Old 11-25-2010
There's a simple trick to printing on the same line: Don't print newlines, print carriage returns instead. That will return it to the beginning of the line, instead of moving to the next line, without resorting to things like ANSI escape sequences.

Of course you have to be careful to always overwrite everything that was printed last time, otherwise you'd get a countdown like 10,90,80,70,60,50,40,30,20,10 -- not what you wanted at all Smilie

And since you're not printing a newline, you either need to flush the output all the time, or print to the unbuffered-by-default stderr (or cerr) stream.

Code:
int n;

for(n=10; n>0; n--)
{
       // %2d pads with spaces to two characters
       fprintf(stderr, "\r%2d", n);
       sleep(1);
}

---------- Post updated at 09:38 AM ---------- Previous update was at 09:29 AM ----------

Quote:
Originally Posted by bashuser2
Just a question in that though: Is there any C++(or even C) way of not calling this function all the time? (as the game will have many dialogues)Is there any way to tell the program that for everything printed in the program,you will use this function? A little bit difficult I think...
You could try overloading some functions in ostream for that, I suppose. I'm not sure you're given enough access to its internals to really affect the way the buffer is flushed, though. Theoretically these things are flexible like that. In practice...they're not.

I don't see what's so bad about calling a function. Are you really going to want to print everything like that, all the time? Or just dialogues? If you're thinking you want to avoid a million function calls like
print_dialogue("line1");
print_dialogue("line2");
print_dialogue("line3");
...

then it's time to abstract that some more, looping it somehow, or having the loop happen inside print_dialogue itself. An array of strings would work, and could be replaced by another backend like reading it from a file later.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 11-26-2010
I have no problem to call the function many times, I was just trying to think for a more tricky way but maybe it would be much more complex...I will try a little bit more and if I can't find something,I will do it the usual way Smilie
Again, thank you very much for your answer,you really helped me out here!

---------- Post updated 11-26-10 at 04:02 AM ---------- Previous update was 11-25-10 at 11:22 AM ----------

hello again...

in order to do the following I need threads?

While I am waiting for the player to choose an answer (cin>>answer; )
I want to use the coutdown function to oblige him answer within let's say 10 seconds.Then,if he doesn't answer,the first choice is automatically picked up as the answer...
Something like:

1.Do this
2.Do that
3.Blah blah
Time to answer : 10->9....->1

any good tutorials for threads?
# 6  
Old 11-26-2010
Threading is a whole new kettle of fish and wouldn't completely fix the problem anyway -- you'd still need to mess with the terminal settings. You can do reads without blocking anyway, though it means changing your terminal settings and using the unix read() or select() call instead of stdio calls or iostream objects. Letting you do what you want in a loop.

Code:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

int main(void)
{
    int fd=STDIN_FILENO;	/* STDIN=0, STDOUT=1, STDERR=2 */
    int running=1;

    system("stty -icanon min 0 -echo"); // So we can read one char at a time.
    // -echo means it doesn't autorepeat what you type, either.

    while(running)
    {
        char buf[512];
        ssize_t bytes;

        bytes=read(fd, &buf, 511);
        if(bytes > 0)
        {
          running=0;
          buf[bytes]='\0'; // NULL-terminate it
          fprintf(stderr, "Read %d bytes: %s\n", (int)bytes, buf);
          running=0;
        }
        else
        {
          fprintf(stderr, "not ready\n");
          usleep(100000L);
        }
    }

    system("stty sane"); // Return terminal to normal

    return(0);
}

Using system("stty") is a bit of a hack but works fine. It alters terminal settings for stdin, which are global to a terminal, i.e. if you don't reset your terminal after your program quits you'll still have weird settings! If you want to take a look at how stty actually works, you can see my examples tcgets.c and tcgets.h. That's code for using a serial port but as far as UNIX is concerned, all terminals are serial ports...

Last edited by Corona688; 11-26-2010 at 12:38 PM..
# 7  
Old 11-29-2010
sorry I didn't answer before,I was absent.Thank you very much,I will test this,it surely is interestingSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in a mini project

Hi All, I want to make something like described below - "Double click on an executable file that will check the health status and other things of various linux servers and send an email to a list of people." I can make shell scripts for individual servers but how to make a script that will check... (1 Reply)
Discussion started by: csrohit
1 Replies

2. Shell Programming and Scripting

Mini Project

Hi Experts, I'm a newbie.....just now i started to write some simple scripts on my own. Can anyone suggest me any simple project kind of stuff to hone my SHELL SCRIPTING skills....which involves database connection and more than that.....bcoz i already tried to write a script which connects to... (0 Replies)
Discussion started by: kritibalu
0 Replies

3. Homework & Coursework Questions

Print questions from a questions folder in a sequential order

1.) I am to write scripts that will be phasetest folder in the home directory. 2.) The folder should have a set-up,phase and display files I have written a small script which i used to check for the existing users and their password. What I need help with: I have a set of questions in a... (19 Replies)
Discussion started by: moraks007
19 Replies

4. OS X (Apple)

Mac mini to Sony TV

Hi All, I have Mac mini, I bought DVI to HDMI cable and connected this to TV and sounds didn't come. On the Sony TV, right below VGA, I see mini-port plug in. I then connected VGA cable from Mac to Sony TV, I can see every thing. but for the sound, should I buy mini-port to mini-port cable.... (1 Reply)
Discussion started by: samnyc
1 Replies

5. UNIX for Dummies Questions & Answers

mini shell programming (help)

Hi All, Well i m a taking an operating system course (newbie to unix) we have studied till now: the fork () execv() the teacher asked us to create a mini shell that execute a user command: cmd1 he said everything in is optional we can use any combination Well dudes , i m really... (2 Replies)
Discussion started by: ELECTRO
2 Replies

6. UNIX for Dummies Questions & Answers

mini distribution

Maybe someone knows floppy distribution with which I could connect to the internet and browse sites. THanks for answers (1 Reply)
Discussion started by: Vilmis
1 Replies

7. Programming

Mini Shell in C

Hi Everyone, I am a student learning C and Unix. I want to create a shell in C which accepts command line arguments and executes them. I am not sure how to do this. Any help would be greatly appreciated. Thanks (5 Replies)
Discussion started by: passat
5 Replies

8. UNIX Desktop Questions & Answers

mini digi-cam

hey guys! I am on a fedora core2 i686 with gui and I have a miniture cool-cam which is digital.. it connects through regular usb.. the system recognizes it and the when I go into the desktop peripheral and go to camera it gives name and tells test was successful.. it also says that the camera has... (0 Replies)
Discussion started by: moxxx68
0 Replies
Login or Register to Ask a Question