Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Uhh.. Yeah.. i am making this text based game.. simulator.. thing and i need YOU  (Read 4160 times)

DrPoo

  • Bay Watcher
  • In Russia Putin strikes meteor
    • View Profile

I just love programming, but actual graphics programming tends to fuck me over, i love ASCII games, but because there is no function for putting characters at different places on the terminal, in python, i can only make such games on my TI-84+ :(

So i am making this text game, no graphics or anything. Infact, a simulator, but one thing i hate, is typing in all kinds of data, but i really want a extensive list of materials, and such, wich i can generate random items from, i could find public lists, but i do not know how i can make them a python list. Python 2.5 is indeed, a crazy language. If any Grand Master pythonworker could tell me, a dabbling pythonworker, how to craft such fine mechanics to turn regular TXT files into huge lists, or if you bother, actually typing a syntax correct list of materials and intrinsics and such?
Logged
Would the owner of an ounce of dignity please contact the mall security?

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile

Something like this I think:

Code: [Select]
list = []
f = open('./somefile.txt')
for line in f:
  list.append(line.strip())

Also, libtcod is geared towards putting characters where ever you want them.  There is even a tutorial:   http://roguebasin.roguelikedevelopment.org/index.php?title=Complete_Roguelike_Tutorial,_using_python%2Blibtcod
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Eagleon

  • Bay Watcher
    • View Profile
    • Soundcloud

Even using OGL or similar, it's not hard to make a text tiler with a fixed-width font. For a TTF-based display, you need A: Screen width, B: Screen height, C: Width and D: Height of your font. For a console display, all you need are the row and column count for your screenmode.

Divide A by C, B by D. That's how many characters you can display per row and column. Then create an array of text objects (can be as simple as a 'char' or as complex as an object with a color, display priority, animation state, etc.), and call a drawing function for each position by multiplying array pos by the relevant dimensions to get where the text should be. Since it's fixed width, your empty positions will just be spaces. Useful functions to make after this are movement between rows, swapping characters, shifting rows and columns of characters in different directions, making your text objects use an event system instead of redrawing the whole screen every update etc., but that's basically all libtcod is doing with the data.

The other... It depends on your formatting and how user-friendly you want it to be - something as intuitive as DF's raws or XML will be much more involved, but you can shove a bunch of data into a CSV file, and there's probably a built-in function to read and separate a single comma-separated string into an array in Python already. If not, it's not hard to make - what you're doing then is finding any commas in the string, storing its position, and cutting out whatever is before it into a new variable.

If you do want to make something like DF's parser, the best way to figure out what's required is to make one yourself for one of the raw files. Read a single file in as one big string, and start dissecting it into individual tags, and then grouping those tags together into associated objects. There's no magic one-function solution for something like this. Well ok, there might be, but you'll probably want something it can't do, so it's best to learn what's needed to make it from scratch :)
Logged
Agora: open-source, next-gen online discussions with formal outcomes!
Music, Ballpoint
Support 100% Emigration, Everyone Walking Around Confused Forever 2044

DrPoo

  • Bay Watcher
  • In Russia Putin strikes meteor
    • View Profile

Thanks, ill try out that libcod and all that when i come home, thanks for help man :)
I might be able to port my TI-Roguelike that never actually quite was finished due to memory limitations, and INDEED the speed of it, to python with Libcod. I will spit out a game in notime, well notime being a week or something, due to that i only use the computer 1 hour a day.

Something like this I think:

Code: [Select]
list = []
f = open('./somefile.txt')
for line in f:
  list.append(line.strip())

Also, libtcod is geared towards putting characters where ever you want them.  There is even a tutorial:   http://roguebasin.roguelikedevelopment.org/index.php?title=Complete_Roguelike_Tutorial,_using_python%2Blibtcod

Thanks for the code man! I would wish i knew all this list handling witchcraft so i didnt have to do clumsy loops and sloppily handled data structures all the time, but this is a step forward, thanks!

Even using OGL or similar, it's not hard to make a text tiler with a fixed-width font. For a TTF-based display, you need A: Screen width, B: Screen height, C: Width and D: Height of your font. For a console display, all you need are the row and column count for your screenmode.

Divide A by C, B by D. That's how many characters you can display per row and column. Then create an array of text objects (can be as simple as a 'char' or as complex as an object with a color, display priority, animation state, etc.), and call a drawing function for each position by multiplying array pos by the relevant dimensions to get where the text should be. Since it's fixed width, your empty positions will just be spaces. Useful functions to make after this are movement between rows, swapping characters, shifting rows and columns of characters in different directions, making your text objects use an event system instead of redrawing the whole screen every update etc., but that's basically all libtcod is doing with the data.

The other... It depends on your formatting and how user-friendly you want it to be - something as intuitive as DF's raws or XML will be much more involved, but you can shove a bunch of data into a CSV file, and there's probably a built-in function to read and separate a single comma-separated string into an array in Python already. If not, it's not hard to make - what you're doing then is finding any commas in the string, storing its position, and cutting out whatever is before it into a new variable.

If you do want to make something like DF's parser, the best way to figure out what's required is to make one yourself for one of the raw files. Read a single file in as one big string, and start dissecting it into individual tags, and then grouping those tags together into associated objects. There's no magic one-function solution for something like this. Well ok, there might be, but you'll probably want something it can't do, so it's best to learn what's needed to make it from scratch :)

Also thanks! I like this kind of advice :)
« Last Edit: April 07, 2011, 08:58:00 am by DrPoo »
Logged
Would the owner of an ounce of dignity please contact the mall security?

DrPoo

  • Bay Watcher
  • In Russia Putin strikes meteor
    • View Profile

I made it work! a bit.. when it was real time, it just jumped alot of tiles whatever direction you pressed, leaving a long trail of shitty @'s, if it moved outside, the piece of junk crashes, now my attempt to fix it, broke it. And i have no means of debugging due to my enviroment being IDLE on Python 2.5 and libtcod. This is shit.

Code: [Select]
import string
import random
import libtcodpy as libtcod

SCREEN_WIDTH = 80
SCREEN_HEIGHT = 50
LIMIT_FPS = 20

debug = 1
null = 0


#Read material list
mat_list = []
f = open('./mat_list.txt')
for line in f:
    mat_list.append(line.strip())

#Sort list alphabetically, thanks god for this function, otherwise it'd be painful.
mat_list.sort()

#initialise libtcod stuff
libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False)

#SPAGETTHI! Nah its just the key handling function, just as the tut sez..
def handle_keys():

    key = libtcod.console.check_for_keypress(True) #<- This will make it wait for a keypress :)
    if key.vk == libtcod.KEY_ENTER and libtcod.KEY_ALT:
        #Alt+Enter: toggle fullscreen
        libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())

    elif key.vk == libtcod.KEY_ESCAPE:
        return True #poo
   
    global playerx, playery

    #movement keys lol
    if libtcod.console_is_key_pressed(libtcod.KEY_UP):
        playery -= 1

    elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN):
        playery += 1

    elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT):
        playerx -= 1

    elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT):
        playerx += 1



   

#center player on map
playerx = SCREEN_WIDTH/2
playery = SCREEN_HEIGHT/2

#Main loop
while  not libtcod.console_is_window_closed():

    libtcod.console_set_foreground_color(0, libtcod.white)

    libtcod.console_print_left(0, playerx, playery, libtcod.BKGND_NONE, '@')



   
    #update screen
    libtcod.console_flush()

    #handle keys and exit game if needed
    exit = handle_keys()
    if exit:
        break













i think i coded enough for the day..
« Last Edit: April 08, 2011, 08:12:11 am by DrPoo »
Logged
Would the owner of an ounce of dignity please contact the mall security?

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile

Well I can at least tell you why your @ is leaving a trail. You're forgetting to set the tiles it was on but moved away from back to the background.
As for crashing when you go off-screen, that's because you're going outside the map and the interpreter gets invalid coordinates for the console location. If it'd try to access those coordinates it might accidentally override something important, so instead it just informs you (in a rather harsh way) that what you're trying to do is very unwise (even if the interpreter wouldn't do this, the OS would kick it for trying to do an illegal operation)
Logged

DrPoo

  • Bay Watcher
  • In Russia Putin strikes meteor
    • View Profile

I kinda figured out the crashing, happended alot in the days when i was "programming" my TI, i had my little special "domain guards" to keep that from happening, but later i invented walls so that became obsolete.

But i really dont get it, libtcod is for 2.7 according to the tutorial, but i am using 2.5, it says i wont get any trouble, "but the little trouble will smite you" or something.. I dont feel like upgrading again, as i also use pygame, wich only exists for 2.5.

Also what do you mean with setting the tiles? I am quite confused here, realtime it jumps around, fast as shit, turn-based dosent even work.
Logged
Would the owner of an ounce of dignity please contact the mall security?

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile

Well, a console in Libtcod is essential a grid of characters, each with a foreground color and a background color. Now, this console is presistent, that means any changes you make will still be there after you finished the operation. So if you set a certain character of a console to an @, that @ will still be there the next time you flush it, and the time after that, and the time after that...


To prevent that you'll either need to clear the console after each turn and redraw the whole thing, or you build the graphics into the move function, meaning that whenever the player moves, you remove the @ from the tile it's on, change the player's x and y and add the @ to the players new x and y. That'll solve the trail problem.


The speed problem in real-time mode is actually solved rather easily. You can use libtcod.sys_set_fps(20) to set the FPS to a max of 20 (use any other value if you like, you can even set different speeds for different parts of the game this way)
Logged

DrPoo

  • Bay Watcher
  • In Russia Putin strikes meteor
    • View Profile

Thanks, i will do that, when i got motivation.. i hope i wont get another programming blockade due to "LOL I MAKE GAEM, OMG ERROR *delete 200 lines of code"
Logged
Would the owner of an ounce of dignity please contact the mall security?