Python - A quick look

Post your own tutorials, guides and demos.

Post » Fri Jan 01, 2010 7:25 pm

Okay so first off what is python:

'Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive. Its use of indentation for block delimiters is unusual among popular programming languages.' - wikipedia

Okay so basically, its a programming language, which means that you gotta type stuff.

First, insert a Sprite onto the screen. Then switch to the event sheet editor. To insert a 'script' block, right click and select 'insert script'



Lets start with a really simple script



[code:2dnrrrmr]Sprite.X += 1[/code:2dnrrrmr]

When you run it, that sprite will move to the right 1 pixel per frame. Basically this script block of python is run each frame, just like events are. In fact, you can place a python script anywhere in the event sheet just like a regular event (ie. You can have a python script as a sub event)

Switch back to the layout editor, hold control and make a second instance of 'Sprite'. In fact, make a couple (press enter as you drag instead of constantly dragging and dropping) and now run the application.

You will notice only the first instance moves, none of the others do.

Python only evaluates one instance (in this case, the first).

If you want to move other instances, you need to refer to them in an array notation. Eg:

[code:2dnrrrmr]Sprite[0].X += 1;
Sprite[1].X += 1;[/code:2dnrrrmr]

Or...and this is a better way...take advantage of python for loop:

[code:2dnrrrmr]for s in Sprite:
s.X += 1[/code:2dnrrrmr]

This means it will loop each instance of Sprite, and move it along the x axis by 1 unit per frame.
B
4
S
2
G
5
Posts: 641
Reputation: 3,011

Post » Fri Jan 01, 2010 7:53 pm

Right so we've had a quick look at how to get something up and running in python. Now lets take a close look at the python language itself.

[code:3o8cahs5]awesome = 10[/code:3o8cahs5]
This declares the variable 'awesome' as 10

[code:3o8cahs5]face = "hillarious"[/code:3o8cahs5]
This declares the variable 'face' as a string 'hillarious'

[code:3o8cahs5]
variable = 5
if variable == 5:
Text.text = "cool"
[/code:3o8cahs5]
If you were to run this code, you would need to put a text object on the layout called 'Text'. But basically, if variable == 5 means:

If the variable is equal to 5, run the code which is indented below'

Important things to notice is the indentation, the fact each 'instruction' is on a separate line, and that the if statement has a : at the end... also take note that python uses two equal signs for 'is equal to' like java and C++.

[code:3o8cahs5]for n in [1,2,3,4]:
ListBox.Add(n)[/code:3o8cahs5]
To make this code work, you need to have a list box on the screen. Basically, for loops always loop over something. In C++ you might be familiar with writing: for( int x = 0; x < 10; x++)...but python instead loops over all the elements in a set.

If you need to loop all the numbers from 0 to 100, its a waste of time to write [0,1,2,3,4,5.... so theres a handy function to remember - range:

[code:3o8cahs5]for n in range(0,10,2):
ListBox.Add(n)[/code:3o8cahs5]

This will output 0, 2, 4, 6, 8 ... but not 10...so in C++ its the equivilent of writing:

for (int n = 0; n < 10; n+= 2)

However, the python version is a bit shorter and probably easier to read! However it does take a bit of getting used to.
B
4
S
2
G
5
Posts: 641
Reputation: 3,011

Post » Fri Jan 01, 2010 8:20 pm

Lets now take a look at how Construct handles multiple blocks of script.

Lets say we create two scripts, one that sets a variable, and one that adds that variable to our ListBox:



[code:3tbzidv8]awesome = "Scirra"[/code:3tbzidv8]
[code:3tbzidv8]ListBox.Add( awesome )[/code:3tbzidv8]
Right now what we'll notice happens is that the List box adds the line 'Scirra' when you press the button. This might seem pretty obvious, but for clarification, variables declared inside a script block are 'global' in that they are available to other scripts as well. This doesn't just include variables, it also includes classes and functions.

Okay lets have a look at how to declare a function!



[code:3tbzidv8]def MyFunction(param):
return param + " without my pants"[/code:3tbzidv8]
Basically what we have done is declared a function 'MyFunction' which takes a single parameter called 'param'. Note that we dont declare the type of variable the function takes. It then returns the combination of whatever is in 'param' and the string " without my pants"

[code:3tbzidv8]ListBox.Add( MyFunction("scirra") )[/code:3tbzidv8]
Here we add a line to the list box. Instead of adding "scirra" or a variable, we call the function MyFunction("scirra"), telling python that we want to evalulate 'MyFunction' with param = "scirra". The result is it adds "scirra without my pants" to the list box
B
4
S
2
G
5
Posts: 641
Reputation: 3,011

Post » Fri Jan 01, 2010 8:38 pm

Okay a quick thing to know about functions. Any variable that is declared within a function can only be used in the function. eg:



This script wont work and you'll get an error about an undeclared variable. This is due to a common thing in programming languages called 'variable scope'.

Well this is all I can be bothered writing for now :P
B
4
S
2
G
5
Posts: 641
Reputation: 3,011

Post » Sat Jan 02, 2010 12:50 pm

thanks for this tutorial, i was looking for some python tutorials here....hope enough users bother you to write more :) ...
B
9
S
3
G
3
Posts: 366
Reputation: 2,301

Post » Sat Jan 02, 2010 3:14 pm

Fantastic information. It is much appreciated.. please keep it coming!!
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Mon Jan 04, 2010 1:14 am

Having been using Python for years, I didn't think that this thread was going to be of any interest to me, but showing specifically how Construct handles Python script blocks is very useful.

This is going to be useful for many other people here who probably are fluent in Python, but haven't really looked at it because of the uncertainty of how well it works with Construct.
I look forward to anything else "Pythoners" (!?) should know.

BTW, does this mean Python is 100% bug-free in Construct now? :)

Krush.
B
2
S
2
G
3
Posts: 406
Reputation: 2,062

Post » Mon Jan 04, 2010 3:37 am

to use python with picking:
[url:1balvoay]http://www.scirra.com/forum/viewtopic.php?f=2&t=5305&p=42100#p42100[/url:1balvoay]

it doesn't take much effort to learn python, either, especially if you know c++, or one of the major nonscripting languages.

A few days ago I only knew enough python to be able to test the pyfix plugin., now I'm using almost exclusively python triggered by conditions, and I've grown completely comfortable with python. almost anything you need to know is thoroughly explained by the first google search result on it.

being able to write real functions, to consolidate complicated series of actions, or create expressions, which can then be used in other expressions, enables you to exponentially increase complexity and readability.

Also, being able to simply copy and paste my code to new caps in invaluable.

for the more advanced users, especially, python is definitely worth learning

combined with 's', I was able to rewrite by keyframe based editor I had originally been developing a plugin specifically for. I've gotten further along in less time. And this new version is much more robust and powerful. It's like the speed of event based creation, with the power of c++ plugin development
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Tue Jan 05, 2010 8:46 pm

And now a cool chain/snake example for python

http://dl.dropbox.com/u/939828/Python%20Chain.cap

Tomorrow I will explain how it works.
B
4
S
2
G
5
Posts: 641
Reputation: 3,011

Post » Wed Jan 06, 2010 12:08 am

That's awesome Davo :)
Scirra Founder
B
359
S
214
G
72
Posts: 22,948
Reputation: 178,532

Next

Return to Your tutorials & example files

Who is online

Users browsing this forum: No registered users and 0 guests