How do I go about doing this?

This forum is currently in read-only mode.
  • I want to use txt file to make my game level with python

    but I don't now the best way of doing it. can anybody help me out.

  • I want to use txt file to make my game level with python

    but I don't now the best way of doing it. can anybody help me out.

    I can't quite figure out what it is that you're wanting from that. Can you be more specific?

    Python has really nice text processing abilities, so it sounds quite feasible.

    Do you want to design levels using a text file, then read it into variables using Python? If so, what format would you use? Graphical representation (like arranging characters in a grid pattern to represent the layout), or descriptive (like an .ini file?)

    What type of level? Top-down (like a dungeon level), or side-view (like a platformer?)

    Or, do you want to use Python to procedurally generate the level, then store it in a text file? The generation would be the hard part there. Storing string variables into text files is pretty easy, though. I have an example of procedural generation using python, here. It would be easy to store the results as a text file in that example.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Sorry for my lack of information I was sleepy and, I wanted to write it down before I forget it.

    anyway, I would like to have characters arrange in a grid pattern to represent the layout.

    Tt is a Top-down game.

    it would look like this

    4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4

    4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4

    4 4 4 4 0 0 0 0 0 0 0 0 0 4 4 4 4

    4 4 4 0 0 0 0 0 0 0 0 0 0 0 4 4 4

    4 4 0 0 6 8 8 8 6 8 8 8 6 0 0 4 4

    4 4 0 0 8 4 4 4 4 4 4 4 8 0 0 4 4

    4 4 0 0 8 4 4 4 4 4 4 4 8 0 0 4 4

    4 4 0 0 6 4 4 4 4 4 4 4 6 0 0 4 4

    4 4 0 0 8 4 4 4 4 4 4 4 8 0 0 4 4

    4 4 0 0 8 4 4 4 4 4 4 4 8 0 0 4 4

    4 4 0 0 6 8 9 7 4 7 9 8 6 0 0 4 4

    4 4 0 0 0 0 0 0 4 0 0 0 0 0 0 4 4

    4 4 4 0 0 0 0 0 4 0 0 0 0 0 4 4 4

    4 4 4 4 0 0 0 0 4 0 0 0 0 4 4 4 4

    4 4 4 4 4 4 0 0 4 0 0 4 4 4 4 4 4

    4 4 4 4 4 4 0 0 4 0 0 4 4 4 4 4 4

    4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4

    4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4

    4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4

    4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4

    4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4

    4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4

  • You don't necessarily need python to do that. In fact the array object works quite well for this.

    You do know the object can save, and load an external file don't you?

  • You don't necessarily need python to do that. In fact the array object works quite well for this.

    You do know the object can save, and load an external file don't you?

    Yes I know that and I us mostly use it for that.

    but I am trying to learn more about python in construct.

  • Ahh, well in that case, I'd say you don't have to load an external file.

  • So, Silent Cacophony can you post a cap showing how to do it.

  • I'm not sure what you want to do with the file, still, but I did a bit of experimenting in my PyShell:

    >>> with open("grid.txt") as f:
    	s = f.read()
    	
    
    

    s

    '4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4\n4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4\n4 4 4 4 0 0 0 0 0 0 0 0 0 4 4 4 4\n4 4 4 0 0 0 0 0 0 0 0 0 0 0 4 4 4\n4 4 0 0 6 8 8 8 6 8 8 8 6 0 0 4 4\n4 4 0 0 8 4 4 4 4 4 4 4 8 0 0 4 4\n4 4 0 0 8 4 4 4 4 4 4 4 8 0 0 4 4\n4 4 0 0 6 4 4 4 4 4 4 4 6 0 0 4 4\n4 4 0 0 8 4 4 4 4 4 4 4 8 0 0 4 4\n4 4 0 0 8 4 4 4 4 4 4 4 8 0 0 4 4\n4 4 0 0 6 8 9 7 4 7 9 8 6 0 0 4 4\n4 4 0 0 0 0 0 0 4 0 0 0 0 0 0 4 4\n4 4 4 0 0 0 0 0 4 0 0 0 0 0 4 4 4\n4 4 4 4 0 0 0 0 4 0 0 0 0 4 4 4 4\n4 4 4 4 4 4 0 0 4 0 0 4 4 4 4 4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 4 4 4 4 4 4 4 4 4 4 4 4 4 4\n', '4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4']

    l1 = []

    > for j in s:

    l1.append([i for i in j if not i.isspace()])

    l1

    [

    ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4'], ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4'], ['4', '4', '4', '4', '0', '0', '0', '0', '0', '0', '0', '0', '0', '4', '4', '4', '4'], ['4', '4', '4', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '4', '4', '4'], ['4', '4', '0', '0', '6', '8', '8', '8', '6', '8', '8', '8', '6', '0', '0', '4', '4'], ['4', '4', '0', '0', '8', '4', '4', '4', '4', '4', '4', '4', '8', '0', '0', '4', '4'], ['4', '4', '0', '0', '8', '4', '4', '4', '4', '4', '4', '4', '8', '0', '0', '4', '4'], ['4', '4', '0', '0', '6', '4', '4', '4', '4', '4', '4', '4', '6', '0', '0', '4', '4'], ['4', '4', '0', '0', '8', '4', '4', '4', '4', '4', '4', '4', '8', '0', '0', '4', '4'], ['4', '4', '0', '0', '8', '4', '4', '4', '4', '4', '4', '4', '8', '0', '0', '4', '4'], ['4', '4', '0', '0', '6', '8', '9', '7', '4', '7', '9', '8', '6', '0', '0', '4', '4'], ['4', '4', '0', '0', '0', '0', '0', '0', '4', '0', '0', '0', '0', '0', '0', '4', '4'], ['4', '4', '4', '0', '0', '0', '0', '0', '4', '0', '0', '0', '0', '0', '4', '4', '4'], ['4', '4', '4', '4', '0', '0', '0', '0', '4', '0', '0', '0', '0', '4', '4', '4', '4'], ['4', '4', '4', '4', '4', '4', '0', '0', '4', '0', '0', '4', '4', '4', '4', '4', '4'], ['4', '4', '4', '4', '4', '4', '0', '0', '4', '0', '0', '4', '4', '4', '4', '4', '4'], ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4'], ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4'], ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4'], ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4'], ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4'], ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4']]

    l2 = []

    > for j in s:

    l2.append(j.split())

    l2

    [

    ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4'], ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4'], ['4', '4', '4', '4', '0', '0', '0', '0', '0', '0', '0', '0', '0', '4', '4', '4', '4'], ['4', '4', '4', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '4', '4', '4'], ['4', '4', '0', '0', '6', '8', '8', '8', '6', '8', '8', '8', '6', '0', '0', '4', '4'], ['4', '4', '0', '0', '8', '4', '4', '4', '4', '4', '4', '4', '8', '0', '0', '4', '4'], ['4', '4', '0', '0', '8', '4', '4', '4', '4', '4', '4', '4', '8', '0', '0', '4', '4'], ['4', '4', '0', '0', '6', '4', '4', '4', '4', '4', '4', '4', '6', '0', '0', '4', '4'], ['4', '4', '0', '0', '8', '4', '4', '4', '4', '4', '4', '4', '8', '0', '0', '4', '4'], ['4', '4', '0', '0', '8', '4', '4', '4', '4', '4', '4', '4', '8', '0', '0', '4', '4'], ['4', '4', '0', '0', '6', '8', '9', '7', '4', '7', '9', '8', '6', '0', '0', '4', '4'], ['4', '4', '0', '0', '0', '0', '0', '0', '4', '0', '0', '0', '0', '0', '0', '4', '4'], ['4', '4', '4', '0', '0', '0', '0', '0', '4', '0', '0', '0', '0', '0', '4', '4', '4'], ['4', '4', '4', '4', '0', '0', '0', '0', '4', '0', '0', '0', '0', '4', '4', '4', '4'], ['4', '4', '4', '4', '4', '4', '0', '0', '4', '0', '0', '4', '4', '4', '4', '4', '4'], ['4', '4', '4', '4', '4', '4', '0', '0', '4', '0', '0', '4', '4', '4', '4', '4', '4'], ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4'], ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4'], ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4'], ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4'], ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4'], ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4']]

    l3 = []

    > for j in s:

    l3.append([int(i) for i in j if not i.isspace()])

    l3

    [

    [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4], [4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4], [4, 4, 0, 0, 6, 8, 8, 8, 6, 8, 8, 8, 6, 0, 0, 4, 4], [4, 4, 0, 0, 8, 4, 4, 4, 4, 4, 4, 4, 8, 0, 0, 4, 4], [4, 4, 0, 0, 8, 4, 4, 4, 4, 4, 4, 4, 8, 0, 0, 4, 4], [4, 4, 0, 0, 6, 4, 4, 4, 4, 4, 4, 4, 6, 0, 0, 4, 4], [4, 4, 0, 0, 8, 4, 4, 4, 4, 4, 4, 4, 8, 0, 0, 4, 4], [4, 4, 0, 0, 8, 4, 4, 4, 4, 4, 4, 4, 8, 0, 0, 4, 4], [4, 4, 0, 0, 6, 8, 9, 7, 4, 7, 9, 8, 6, 0, 0, 4, 4], [4, 4, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 4, 4], [4, 4, 4, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 4, 4, 4], [4, 4, 4, 4, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4, 0, 0, 4, 0, 0, 4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4, 0, 0, 4, 0, 0, 4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]][/code:1l52u2pk]

    Assuming that you don't want the spaces in the final data structure, and you want numerical values instead of characters, and that you may want to use Construct's array object, I made a simple .cap that demonstrates how you can load the data file into both a 2D python list and a 2D Construct array.

    http://dl.dropbox.com/u/5868916/grid.zip

    The text file should be in the same folder as the .cap in it's current form, is all.

  • Thanks for the example, I was looking for something like it.

    Can you show me how to change the script to use double digit numbers?

    Something like this

    15 15 15 15 15 15 15 15 15 15
    15 15 15 15 15 15 15 15 15 15
    15 15 15 15 02 02 02 02 02 02
    15 15 15 02 02 02 02 02 02 02
    15 15 02 02 03 03 03 03 03 03
    15 15 02 02 03 15 15 15 15 15
    15 15 02 02 03 15 15 15 15 15
    15 15 02 02 03 15 15 15 15 15
    15 15 02 02 03 15 15 15 15 15
    15 15 02 02 03 15 15 15 15 15
    [/code:1r0lhh1w]
    I have 17 tiles for background(maybe more later) and I don't wanna use letters.
    Do you know how to make it with events? I've tried it with TextManipulator and from start it looked promising, but after 2 hours trying I got massive brain pain and I had to quit
  • 17 tiles? Well, you could use the alphabet as well.

  • It's pretty simple to read text without python. Each line separates rows and each space separates columns so each number can be any number of digits.

    http://dl.dropbox.com/u/5426011/examples3/textdata.cap

  • Thanks for the example, I was looking for something like it.

    Can you show me how to change the script to use double digit numbers?

    That's actually pretty simple. The .split() method does that nicely. Then I just convert the string values to integers.

    Change this:

    l.append([int(i) for i in j if not i.isspace()])

    to this:

    l.append([int(i) for i in j.split()])

    But, as R0J0hound pointed out with his example, parsing the text in Construct can be quite simple. It works much the same way, as well.

    Reading it in from a file should be pretty simple, though I've never done it. It looks as if an EditBox, or the Binary object can do it. Probably other ways, too.

  • Thanks guys, all that could've been done with 4 lines of code, what a shame

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)