Why python is awesome (a simple 3d rotation example)

Post your own tutorials, guides and demos.

Post » Fri Jun 18, 2010 10:21 pm

Hmmm.. I can't seem to open the program directly. It immediately goes to an error.

I tried loading it from Construct and another error occured, this time it says that I'm out of memory.

0.o
B
3
S
1
G
2
Posts: 86
Reputation: 997

Post » Sat Jun 19, 2010 6:55 am

[color=#FF0000:2bckzkiv]namre, it sounds like you may be using a version of Construct before 0.99.84, which is the earliest version that Python works in. You may need to install the latest (unstable) version from here: [url:2bckzkiv]http://www.scirra.com/forum/viewtopic.php?f=2&t=6488[/url:2bckzkiv].[/color:2bckzkiv]

Anyway, I think that sharing Python classes, functions and code snippets is a great idea. Although, the Python integration in Construct is not yet fully implemented, and could use some more general polish, some things can still be done quite nicely with Python.

For instance, on the topic of sorting, Python has a very nice built-in sort using the Timsort algorithm. But, probably one of the most likely things that someone might want to sort is the Array object, and it doesn't seem to be readable from Python. It's not too difficult the make a Construct function that takes care of the bits that don't yet work in Python, and also add the script in there, but it's not so convenient as just a function or class definition in Python. It can, however, be called from Construct or Python, once made.

For example, I made a .cap that will sort an Array of 3,000 random numbers between 0 and 30,000. If I could read the Array from Python, a simple Python function could be made that could accept a reference to the Array object to be sorted. Instead, I had to use a Construct function hard-coded for a specific Array object. Not yet an ideal situation.

I may as well include the .cap here, though. ;)
[url:2bckzkiv]http://dl.dropbox.com/u/5868916/SortExample.cap[/url:2bckzkiv]

The function copies the Array to a python list, sorts the list, and copies the list back to the Array. Seems fairly quick despite the overhead, averaging about 0.034 seconds per 1,000 entries on my machine.
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Post » Sat Jun 19, 2010 6:10 pm

buidling an array using python isnt all that complicated though is it? from what i remember using lists and "for loops" to make arrays is pretty easy.
B
52
S
7
G
6
Posts: 1,945
Reputation: 7,610

Post » Sat Jun 19, 2010 7:03 pm

Yeah, Python has many ways that one can implement an array, or something similar.

In the example above, I built a single-dimensional list to match the single-dimensional array in Construct. The list could have been used instead of the array just as well, but Construct's array object has it's own uses, too.

Python lists can also simulate multi-dimensional arrays by using lists of lists, like so:

[code:3cf635dg]l = [[1,2,3], [4,5,6], [7,8,9]][/code:3cf635dg]

... where l[1][1] would reference the value 5. Initializing a multi-dimensional list can seem a bit odd compared to doing the same with an array, because they work a bit differently. Here's an example that builds a 2-d list of 1s, x by y. This uses a handy list comprehension, which is basically shorthand for a normal loop, and operator overloading with the '[1] * x' to build the inner lists of x number of 1s.

[code:3cf635dg]l = [[1] * x for j in range(y)][/code:3cf635dg]

If x = 4 and y = 6, the above results in l containing:

[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]

There are also the 'array' module and 'NumPy' extension that can be imported for real arrays, and dictionaries with tuples for keys can even be used as a sort of array. ;)
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Previous

Return to Your tutorials & example files

Who is online

Users browsing this forum: No registered users and 0 guests