Jump to content

  • Log In with Google      Sign In   
  • Create Account

pulpfist

Member Since 01 Nov 2005
Offline Last Active Jan 29 2013 04:02 AM

#5023904 How to create c# text based game like this

Posted by pulpfist on 21 January 2013 - 08:38 AM

I suggest you read up on classes, loops and lists (including the Dictionary)

 

If you do, you should be able to understand how this skeleton works

 

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace RPG
{
    // A class representing a location in the game world
    class Location
    {
        public Location(string description) // Class constructor
        {
            Description = description;
            Exits = new Dictionary<string, Location>();
        }
 
        // Description of this location
        public string Description;
 
        // Available exits for this location (Could use a List instead, but I prefer a Dictionary)
        public Dictionary<string, Location> Exits;
    }
 
    class Program
    {        
        static void Main(string[] args)
        {
            // A Dictionary of locations, representing the game world
            Dictionary<string, Location> Locations = new Dictionary<string, Location>();                    
 
            // Create the locations and add them to the "world"
            Locations["Island"] = new Location("An island surrounded by turquoise water");
            Locations["Town"] = new Location("A town with shops and houses");
            Locations["Farm"] = new Location("A farm in the forrest");
            Locations["Tavern"] = new Location("A tavern selling food and drinks");
 
            // Connect the locations together
            Locations["Island"].Exits["Town"] = Locations["Town"];
            Locations["Town"].Exits["Island"] = Locations["Island"];
 
            Locations["Town"].Exits["Farm"] = Locations["Farm"];
            Locations["Farm"].Exits["Town"] = Locations["Town"];
 
            Locations["Town"].Exits["Tavern"] = Locations["Tavern"];
            Locations["Tavern"].Exits["Town"] = Locations["Town"];
 
            // A reference to the "current" location
            // This reference always points to the current location
            Location currentLocation = Locations["Island"];
            
            // Game loop
            while (true)
            {
                // Print information
                Console.Clear();
                Console.WriteLine("You are here: " + currentLocation.Description);                
                Console.Write("You can go: ");
                foreach (string exit in currentLocation.Exits.Keys)                
                    Console.Write(exit + " ");                
                Console.WriteLine();
 
                // Take input from player
                string input = Console.ReadLine();
                if (input == "Quit" || input == "quit") // Exit if the player types Quit
                    break;
 
                // If the player typed the name of one of the current locations exits, update the current location
                if (currentLocation.Exits.Keys.Contains(input))
                {
                    currentLocation = currentLocation.Exits[input];
                }
                else
                {
                    Console.WriteLine("Can not go to " + input);
                }
            }
        }
    }
}




#4920622 C books

Posted by pulpfist on 09 March 2012 - 04:12 AM

This is a must have imo

http://net.pku.edu.cn/~course/cs101/2008/resource/The_C_Programming_Language.pdf


#4918843 does sfml 2.0 support circle and rectangle as in ver 1.6?

Posted by pulpfist on 03 March 2012 - 07:29 AM

The documentation for sfml2 has a sheet called "classes"

You can find examples there if you scroll down a little

http://www.sfml-dev....tangleShape.php


To draw points and lines in sfml2 you can look into the Draw function of the RenderWindow that takes an array of vertices

http://www.sfml-dev.org/documentation/2.0/classsf_1_1RenderTarget.php#a033df46bee2a3b39fb0c6a82f2aabcdd

http://www.sfml-dev.org/documentation/2.0/group__graphics.php#ga5ee56ac1339984909610713096283b1b


#4913309 Visual Studio 2003 - Compiling XBOX Source Code "LinksBoks" - (RESOLVED)

Posted by pulpfist on 15 February 2012 - 05:46 AM

One last thing though, does fail mean the same thing as error? Just a little fuzzy on what failed. :/

Not exactly.
Basically, the build system has several tasks it will try to complete, like compiling, linking, copying etc.
For each task that for some reason did not succeed, the build system will report a failure.
So I guess in this case the failure was triggered by the error, but saying that it means the same thing wouldn't be accurate.

Thanks again bro!!


Your welcome Posted Image


#4913281 Visual Studio 2003 - Compiling XBOX Source Code "LinksBoks" - (RESOLVED)

Posted by pulpfist on 15 February 2012 - 03:58 AM

Looks like your good to go so you can forget about my last post. You got the files man Posted Image

Those errors seems to be related to the build system trying to connect and copy the files over to the xbox automagically.

Just try to do it manually...


#4913273 Visual Studio 2003 - Compiling XBOX Source Code "LinksBoks" - (RESOLVED)

Posted by pulpfist on 15 February 2012 - 03:29 AM

Build the linkboks project first, then the DefaultHost project.

The result of building the DefaultHost project should be the browser file.


#4913153 Visual Studio 2003 - Compiling XBOX Source Code "LinksBoks" - (RESOLVED)

Posted by pulpfist on 14 February 2012 - 04:43 PM

Ok I'm not into xbox but here is what I would try:

1. Set up a fresh copy of the linksboks code
2. Double click the linksboks.sln to have the project opened in visual studio
3. Choose "build" form the menus
4. Look for a file called LinksBoks.xbe

If that doesn't work I wouldn't really know what's wrong, but any error messages could be interesting to see...


#4912965 Visual Studio 2003 - Compiling XBOX Source Code "LinksBoks" - (RESOLVED)

Posted by pulpfist on 14 February 2012 - 08:19 AM

Assuming you have built a static library (not a dll), the lib file (The lib file is basically just an archive containing the obj files) along with any header files is the complete library.

You should be able to link this library to your own projects.

How to do this is a question on its own and probably differ between different versions of Visual Studio (VS2003 is getting pretty old btw)


#4891852 c++ error: warning C4700: uninitialized local variable 'k' used

Posted by pulpfist on 08 December 2011 - 11:41 AM

You are using the variable k without giving it a sane value first.
The compiler you are using consider this to be worth a warning.

Basically, after declaring and defining the variable k:

int k;

its contents is undefined and can be any value.


#4883331 Advancing beyond a (advanced)beginner

Posted by pulpfist on 12 November 2011 - 06:50 PM

Would I become more proficient at programming simply by picking up a lower level language?

More than anything, you would become less efficient.
The only viable reason for picking a low level language like C++ today is if you are writing low level stuff like operating systems, device drivers, web/database servers or the like.
That's what C/C++ was designed for.
Game engines still use C++ today but that's mostly because C++ was the only serious alternative 20+ years ago.

If you really want to learn about hardware by forcing yourself through the quirks of a low level language, I would go for assembly language.
If you want to learn C++, I would go for C++

I find it hard to say either yes or no to your question though. It depends on several factors.
Studying distributed systems at university requires very different tools than writing apps for mobiles, as you can probably imagine


#4878245 Seeking Beginner Advice for Text-Only Dungeon Game in C# and Python

Posted by pulpfist on 29 October 2011 - 07:49 AM

A few minor suggestions:

Change 1:



public void AliveOrDead()
{
    if (HitPoints <= 0)
    {
        Alive = false;
        Console.WriteLine("\n{0} was slain!\n", Name);
    }
}

Then, the last lines in SwingDamageRoll and ThrustDamageRoll becomes
target.AliveOrDead();

It may seem a bit odd but this is typical of OO programming. The number of function arguments needed is drastically reduced.





Change 2:


public void SwingDamageRoll(Character target)
{
    // Create a array containing the damage modifiers
    int[] damageModifierLookup = { -5, -5, -5, -4, -4, -3, -3, -2, -2, -1, 0, 1, 2, -1, 0, 1 };

    int damageRoll, basicDamage, injury;            

    if (Strength < 13) // less than 13, we roll 1 -> 6
        damageRoll = random.Next(1, 6);
    else // bigger than 12, we roll 2 -> 12
        damageRoll = random.Next(2, 12);


    if (Strength < 0) // less than 0, the damage modifier is always -5
        damageRoll -= 5;
    else if(Strength > 15) // bigger than 15, the damage modifier is always +1
        damageRoll += 1;
    else // between 0 and 15, the damage modifier is found in the lookup array
        damageRoll += damageModifierLookup[Strength];


    // Make sure damageRoll is not negative
    damageRoll = Math.Max(damageRoll, 0);            


    basicDamage = Math.Max(((damageRoll + WeaponSwingDamage) - target.Armor), 0);
    injury = (int)(basicDamage * SwingDamageType);
    target.HitPoints -= injury;
    Console.WriteLine("\nDebug: Rolling for damage... {0} rolled {1} and did {2} damage.\n", Name, damageRoll, injury);

    target.AliveOrDead();

} // End Swing Damage Roll function.

This SwingDamageRoll function should do exactly the same as the old one, but with slightly less code and repetitions. The same could be done to the ThrustDamageRoll function of course.





Change 3:

In the Character class the equipped weapon should probably be a reference to a weapon, not a list of weapons.
public Weapon equippedWeapon = null;
equippedWeapon is now a reference to a weapon, currently referencing nothing (null)
Once the player chooses to equip a weapon in the weaponList, we can set this reference to the selected weapon:
equippedWeapon = weaponList[ selectedWeaponIndex ];

Doing this, the attack code at the bottom of the SwingDamageRoll and ThrustDamageRoll would look something like this:
...


if (equippedWeapon == null)
{
    Console.WriteLine("\nYou have no weapon equipped");
}
else
{
    basicDamage = Math.Max(((damageRoll + equippedWeapon.SwingDamage) - target.Armor), 0);
    injury = (int)(basicDamage * equippedWeapon.SwingDamageType);
    target.HitPoints -= injury;
    Console.WriteLine("\nDebug: Rolling for damage... {0} rolled {1} and did {2} damage.\n", Name, damageRoll, injury);
}...




edit:
Another little cleanup, I added a static Utils class just after the namespace ConsoleApplication1

static class Utils
{
    public static void ResetScreen()
    {
        Console.WriteLine("Press any key to continue...\n");
        Console.ReadKey();
        Console.Clear();
    }
}

With that in place, you can replace all occurrences of
Console.WriteLine("Press any key to continue...\n");
Console.ReadKey();
Console.Clear();

with

Utils.ResetScreen();
This may not seem as a big deal at a first glance, but the nice thing here is that if you later on want to do more than just clearing the screen, maybe you want to fill the screen with a background color or something, you now have a single place to do it.
Besides, it prevents you from spelling errors now that you only have to spell 'Press any key to continue...' once Posted Image

Speaking of which, to change the window I would make another function in the Utils class:

public static void InitScreen()
{        
    Console.SetWindowSize(120, 50);
    Console.BufferHeight = 500;
    Console.BufferWidth = 120;
    Console.ForegroundColor = ConsoleColor.Green;
    Console.BackgroundColor = ConsoleColor.DarkBlue;
    Console.Title = "The Arena - by Richard D. Sharpe";
    Console.Clear();
}

and call it once at the beginning of the main function:

Utils.InitScreen();





That's just a few ideas, you may find them interesting when the day comes Posted Image


#4877950 Seeking Beginner Advice for Text-Only Dungeon Game in C# and Python

Posted by pulpfist on 28 October 2011 - 01:02 PM

All that seems to work just fine; I just need to get the BuyItem() function to accept all "instances"(?) of Items...

At this point I would make a class called Shop, put the BuyItem function in that class (make it non-static, because we want all shops to have their own inventory) and give that shop an inventory (you already know how to do that)

When the player uses the BuyItem function in the shop, you can list all the items in the shops inventory and ask the player which of the items to purchase.
Once the player has chosen an item he can afford, add the item to the players inventory and remove it from the shops inventory.


This would be a good exercise in using loops in conjunction with arrays/lists Posted Image

edit:

One quick question, in a function, what goes between the parenthesis are called "arguments," right?


Yes, they are usually referred to as function arguments or function parameters


#4876838 Seeking Beginner Advice for Text-Only Dungeon Game in C# and Python

Posted by pulpfist on 25 October 2011 - 11:24 AM

EDIT x 5: Now I'm not sure if the characters are attacking themselves, or if it's just displaying the wrong character.Name...

It seems inconsistent! Sometimes, it's correct an other times, it's not... :blink:


Take a few shots and they will start attacking you!





#4874604 Seeking Beginner Advice for Text-Only Dungeon Game in C# and Python

Posted by pulpfist on 20 October 2011 - 02:13 AM

...

EDIT: No, actually, I don't think I will be using a lot of stuff in Goblin Brawl right now. Turns out, it's too far above me. Though very sleepy, I tried all night (it's 1:50 a.m., now) but I didn't get anything done in that regard. I'm so sleepy now that I cant think straight. I'll try to get something done tomorrow.


No problem.
You did get my brain cooking on how to sort the characters based on speed so I did it my way. You however should do things your own way and in your own pace of course Posted Image


#4874076 Python get_rect() problem

Posted by pulpfist on 18 October 2011 - 05:37 PM

Its because you are accidentally reusing the identifier brain.

First you create a surface called brain:

brain = pygame.image.load("Brain.png")

Then you are creating an instance of the Brain class also called brain:

brain = Brain()




This will rebind the identifier brain, which is probably not what you want.

I would suggest you use different names for the surfaces, like


surface_brain = pygame.image.load("Brain.png")
surface_background = pygame.image.load("background.jpg")
surface_zombie = pygame.image.load("zombie.jpg")
surface_killer = pygame.image.load("killer.png")




PARTNERS