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

This forum is currently in read-only mode.
From the Asset Store
Template for a basketball game with a 3D aspect (illusion of 3D)
  • super simple python example with 3d rotation along the y axis

    to be honest this code could be cleaned up, and made much more flexible, so it doesn't depend on the 2 sprites for radius, or you could choose any two sprites, I just made it that way to be easy to note the flexibility, and so it wouldn't require any coding knowledge to get a basic grasp

    drag and drop the two small circles to adjust the radius and move your mouse around to rotate the larger sprite around the y axis of the smaller one

    http://dl.dropbox.com/u/1013446/pyrot.cap

    also, if you change the line:

    Run python script - "yrot(Sprite)"

    to "yrot(Sprite4)"

    you'll notice that this function can be applied to any object like a behavior. This is a stripped down example, but in reality you could construct a python object that uses an array of construct sprites, and you treat it all like a single 3d rotatable object.

    not to take away from quazi's uber impressive 3d stuff, and it is impressive, but with python you could design an object that could reduce a huge cap only readable by aliens(that's a compliment quaz), to a normal readable cap, with nothing but easy construct events, and simple python commands that you name yourself. if you didn't need a big object composed of many, you could create a better version of the function in this cap custom tailored to your needs, and you wouldn't need a bunch of private variables to manipulate 3d rotation

    and after adding all the sprites to an array (a process which could also be simplified by python), you could just use commands like

    RotateZ(MyUber3dConstruction,35)

    MyUber3dConstruction being composed of 100 objects

    35 being the number of degrees

    Aside from that, these python objects and functions could be shared with others on the forum who would merely have to copy paste them to a script in start of layout

    I recommend anybody who's capable of creating those WOW!! type event sequences in their caps, and eager to share their abilities, try to learn a little python. It really isn't that different from construct events. you just type stuff instead of choosing it from a list. learn a few rules about when to indent, and when to put a '(' or a ')', and you can share your genius/madness with the world, or at least make your madness clutter up your own caps less, and make them readable when you come back to the cap after a few months

    3d is just one example, you could use this technique for anything from inventory systems, to custom sprite particle systems, anything complex that starts making your cap difficult to read or manage. It's neater than functions, and can everything 's' can do as far as data structure creation, but with greater readability, and less pointing, clicking, and definitely less typing in of long addresses. Not to denounce my own plugin, because it does have it's uses, but learning how to create data structures in python is much more useful in the long run than learning how to do it with 's'

  • I don't understand why this is done in Python, when it looks like it can be done using events (ala Functions, Families and Private Variables) just the same. However, I guess the python script is easier to type in quickly than clicking through all of the windows it takes to make events.

    Thanks for sharing Lucid!

  • Great intro to python lucid! This is exactly what the community needs to get going on python.

    *tips his hat*

    I don't understand why this is done in Python, when it looks like it can be done using events (ala Functions, Families and Private Variables) just the same. However, I guess the python script is easier to type in quickly than clicking through all of the windows it takes to make events.

    Thanks for sharing Lucid!

    Jayjay, No not easier to do in python but lucid was making an intro to spark interest in adding python to projects, so after you lean it you can do things that Construct can't. It is a beautifully simple example for the beginner. So while it is easier to do this in construct; if lucid would have made a huge project then most people would have passed it over.

  • Jayjay, No not easier to do in python but lucid was making an intro to spark interest in adding python to projects, so after you lean it you can do things that Construct can't. It is a beautifully simple example for the beginner. So while it is easier to do this in construct; if lucid would have made a huge project then most people would have passed it over.

    Ahhh, ok. I was leaning more towards Python being easier to code quickly in when you know what you're typing, not that programming in a language in general is easier than using Construct events system.

    I do see what you mean though, I was thinking about the case of smaller projects, where including the python runtime in your executable isn't necessary unless you must do something that Construct events alone can't do well (or even at all).

    The way Python is implemented in Construct is (very) nice however, and I'm not trying to put it (or the given example) down in any way.

  • The way Python is implemented in Construct is (very) nice however, and I'm not trying to put it (or the given example) down in any way.

    I totality got that Jayjay, and no bad karma taken.

    But I really applaud lucid trying to get more interest in python, and simple examples spark complex ideas and projects.

  • Cool example lucid. There is a lot of potential for using python to create reusable bits of code.

    Here's a barebones example of using python to make your own platform behavior:

    http://dl.dropbox.com/u/5426011/examples/pybehavior.cap

    And here's a more complicated example:

    http://dl.dropbox.com/u/5426011/examples/pyramid.cap

    Although there is no need to understand how it works to use it.

  • awesome stuff rojo

    I was actually making a platform behavior example when i got called for dinner, so thanks for saving me the trouble

    if I get a chance in the next few days, I want to make an example with a python class.

    with classes you could make extremely robust solutions as I was describing before.

    being able to instantiate classes of complex multi-construct-object objects has alot of potential I think. you could make a 3d editor in python as easily as you could in s with the difference being that it would be copypastable to multiple caps, and completely readable. your events could end up looking as simple as

    On Sprite (drag-and-drop) Drop-

    run python script "repositionIn3dModel(Sprite)"

    on right mouse button click

    run python script "beginMouseDrag()"

    Right Mouse Button Down

    run python script "rotateModelBy(DistanceDragged())"

    you can create the equivalent of objects and behaviors

    if anyone has any ideas you'd like to see a python example of, post it here, and either I or rojo can probably whip something up. when looking at these examples, if you're considering learning python, focus on the simple actions AFTER you've created your functions.

    notice in rojos pyramid example, there is a single command that is doing everything. One of the rewarding things about python (or any modern programming language) is being able to reduce the most complex of tasks to as few commands as you wish. Once you create your complex script, you don't have to even remember how you did, none of the details need to matter, you don't need to remember what each PV was for nothing. Just one command for anything that can be reduced to one task conceptually. Rojo could have extended his example, so you provide not only the object, but the angle as well, only if he needed that flexibility. Either way, he's done thinking about rotating pyramids, now he can just use it like you can any other object.

    Anyone using 's' should seriously consider experimenting with python, it's much more powerful, and the learning curve is about the same.

  • [quote:18e8d23k]if anyone has any ideas you'd like to see a python example of, post it here, and either I or rojo can probably whip something up.

    Ok I'll bite. How about some vector classes for manipulating distort maps? <joke

    Well there's the dpad fuctions using pygame we were talking about, or a good sorting algorithm.

    Construct could use a plug that does that.

  • lucid, somehow i know this is pointed towards me

    honestly, the re usability sounds beautiful, and everything looks pretty simple

    i already started learning python with some tuts, forgot it all now but it wasn't too hard from what i remember, and this code looks pretty easy to grasp, all i need is to learn a little bit more about the syntax and its implementation in construct, and ill be good to go, i can honestly make alot of my code neater with this, and faster to write too, that alone is great.

    a little example showing something i understand in events through python, seems to be just what i needed to really "get" pythons use.

    Now that schools over (last exam was 5 hours ago!) i can sink my teeth into this.

  • 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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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: http://www.scirra.com/forum/viewtopic.php?f=2&t=6488.

    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.

    http://dl.dropbox.com/u/5868916/SortExample.cap

    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.

  • 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.

  • 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:

    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.
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)