Re: [Tutor] converting processing code to python code

2008-12-01 Thread Alan Gauld


"Erica Osher" <[EMAIL PROTECTED]> wrote


I wrote the original code in a program called Processing. (
http://processing.org/)


Having had a look at the web site it is obvious that the hardest bit
of porting the Processing code to Python is that Python does not
support all the visual drawing functions that Processing does. Most
of them can be replicated by writing equivalent functions in Python
but that is a lot of work - effectively building a python vesion of
Processing! (That would be an exellent project BTW but not
one for a beginner!)

I tried a Google search. The best I could find was:

http://i.document.m05.de/?p=483

which suggests using Jython whicjh allows you to call the Java
processing libraies/classes from within Python code. But to
understand it I think you still need to go through the tutorials 
first.


HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] converting processing code to python code

2008-12-01 Thread Alan Gauld


"Erica Osher" <[EMAIL PROTECTED]> wrote 


I wrote the original code in a program called Processing. (
http://processing.org/)


Aha! The language is actually called Processing. Now that's 
a new one on me. Thanks for the link, it does help to have 
the context. :-)


Now the next question, are you experienced in Processing 
or are you a beginner there too? If you are experienced in 
another language the best tutiorial is the standard Python 
tutorial found on the Python website. If you are a beginner 
in Processing as well stick to the newbies tutors I mentioned 
last time.


Regards,

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] converting processing code to python code

2008-12-01 Thread Erica Osher
I wrote the original code in a program called Processing. (
http://processing.org/)

Thanks for your advice, I definitely need some tutorials.

On Mon, Dec 1, 2008 at 8:23 PM, Alan Gauld <[EMAIL PROTECTED]>wrote:

> "Erica Osher" <[EMAIL PROTECTED]> wrote
>
>  The code was originally created for a Processing Project and I'm just
>> starting to learn python and I'd like to build on this code.
>>
>
> The fact that you still don't tell us what the original language is
> suggests
> you are not a very experienced programmer in any language. Is that
> assumption correct?
>
>  The syntax error I get is
>>
>> *Traceback (most recent call last):
>>  File "nodebox/gui/mac/__init__.pyo", line 332, in _compileScript
>>  File "", line 8
>>int x1 = 5;
>> ^
>> SyntaxError: invalid syntax
>>
>
> In Python variables are just names that refer to objects. The objects
> can be of any type so we don't need to declare them as int, float,
> char etc. You just need
>
> x1 = 5
>
> No semi colons are needed either.
>
> However since this is such a basic Python statement I do think
> you should take an hour or two to go through some of the complete
> beginners tutorials found here:
>
> http://wiki.python.org/moin/BeginnersGuide/NonProgrammers
>
> Try mine if you like but there is a variety of styles for you to choose
> from.
>
> Once you are familiar with the basics try converting your code again
> and ask specific questions here for help. That will be much more
> efficient for all of us than simply randomly trying things then asking
> for help.
>
> HTH,
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Erica Osher
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] converting processing code to python code

2008-12-01 Thread Alan Gauld

"Erica Osher" <[EMAIL PROTECTED]> wrote

The code was originally created for a Processing Project and I'm 
just

starting to learn python and I'd like to build on this code.


The fact that you still don't tell us what the original language is 
suggests

you are not a very experienced programmer in any language. Is that
assumption correct?


The syntax error I get is

*Traceback (most recent call last):
 File "nodebox/gui/mac/__init__.pyo", line 332, in _compileScript
 File "", line 8
int x1 = 5;
 ^
SyntaxError: invalid syntax


In Python variables are just names that refer to objects. The objects
can be of any type so we don't need to declare them as int, float,
char etc. You just need

x1 = 5

No semi colons are needed either.

However since this is such a basic Python statement I do think
you should take an hour or two to go through some of the complete
beginners tutorials found here:

http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

Try mine if you like but there is a variety of styles for you to 
choose from.


Once you are familiar with the basics try converting your code again
and ask specific questions here for help. That will be much more
efficient for all of us than simply randomly trying things then asking
for help.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] converting processing code to python code

2008-12-01 Thread Steve Willoughby
On Mon, Dec 01, 2008 at 06:40:03PM -0500, Erica Osher wrote:
> The code was originally created for a Processing Project and I'm just
> starting to learn python and I'd like to build on this code.
> 
> The syntax error I get is
> 
> *Traceback (most recent call last):
>   File "nodebox/gui/mac/__init__.pyo", line 332, in _compileScript
>   File "", line 8
>  int x1 = 5;

That's not even close to Python syntax.  That looks 
like a C-derived language.  I'd recommend going through
a basic Python tutorial first, so you can see how Python
works in a general sense before trying to convert code.

In Python, you don't need to declare variables like
that, you just assign them values.  So that line would
be simply:

  x1 = 5


>  SyntaxError: invalid syntax
> *
> any suggestions?
> 
> Thanks.
> 
> On Mon, Dec 1, 2008 at 6:30 PM, Alan Gauld <[EMAIL PROTECTED]>wrote:
> 
> >
> > "Erica Osher" <[EMAIL PROTECTED]> wrote
> >
> >  I have a simple processing code that I'm trying to work with in python,
> >> but
> >> I keep getting syntax errors. Any help on changing the code would be
> >> greatly
> >> appreciated. Thanks.
> >>
> >
> > It would help to have some background.
> > What language are you translating from? It could be C/C++/JavaScript or
> > Java.
> > Or possibly other C type languages. It might be significant!
> >
> > Also show us what you tried and the syntax error messages.
> > That way we can figure out what it is you are doing wrong.
> >
> > Otherwise we wind up writing your code for you and you learn nothing.
> > Then we have to do it all o er again next time you get stuck.
> > That's inefficient for both you and us!
> >
> >
> > --
> > Alan Gauld
> > Author of the Learn to Program web site
> > http://www.freenetpages.co.uk/hp/alan.gauld
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> 
> 
> 
> -- 
> Erica Osher

> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor


-- 
Steve Willoughby|  Using billion-dollar satellites
[EMAIL PROTECTED]   |  to hunt for Tupperware.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] converting processing code to python code

2008-12-01 Thread Erica Osher
The code was originally created for a Processing Project and I'm just
starting to learn python and I'd like to build on this code.

The syntax error I get is

*Traceback (most recent call last):
  File "nodebox/gui/mac/__init__.pyo", line 332, in _compileScript
  File "", line 8
 int x1 = 5;
  ^
 SyntaxError: invalid syntax
*
any suggestions?

Thanks.

On Mon, Dec 1, 2008 at 6:30 PM, Alan Gauld <[EMAIL PROTECTED]>wrote:

>
> "Erica Osher" <[EMAIL PROTECTED]> wrote
>
>  I have a simple processing code that I'm trying to work with in python,
>> but
>> I keep getting syntax errors. Any help on changing the code would be
>> greatly
>> appreciated. Thanks.
>>
>
> It would help to have some background.
> What language are you translating from? It could be C/C++/JavaScript or
> Java.
> Or possibly other C type languages. It might be significant!
>
> Also show us what you tried and the syntax error messages.
> That way we can figure out what it is you are doing wrong.
>
> Otherwise we wind up writing your code for you and you learn nothing.
> Then we have to do it all o er again next time you get stuck.
> That's inefficient for both you and us!
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Erica Osher
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] converting processing code to python code

2008-12-01 Thread Alan Gauld


"Erica Osher" <[EMAIL PROTECTED]> wrote

I have a simple processing code that I'm trying to work with in 
python, but
I keep getting syntax errors. Any help on changing the code would be 
greatly

appreciated. Thanks.


It would help to have some background.
What language are you translating from? It could be C/C++/JavaScript 
or Java.

Or possibly other C type languages. It might be significant!

Also show us what you tried and the syntax error messages.
That way we can figure out what it is you are doing wrong.

Otherwise we wind up writing your code for you and you learn nothing.
Then we have to do it all o er again next time you get stuck.
That's inefficient for both you and us!


--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] converting processing code to python code

2008-12-01 Thread Steve Willoughby
On Mon, Dec 01, 2008 at 12:59:23PM -0800, Steve Willoughby wrote:
> > void drawSquare1() {
> >  if(x1<0 || x1>width-size) {
> >   x1Speed = -x1Speed;
> >  }
> > 
> >  if(y1<0 || y1>height-size) {
> >   y1Speed = -y1Speed;
> >  }
> 
> def drawSquare1():
>   if (x1 < 0 or x1 > width-size):
> x1 += x1Speed
> y1 += y1Speed
> rect(x1, y1, size, size)

I think my eyes skipped somewhere while copying that
over.  The exact translation of that code snippet would
of course have been:

def drawSquare1():
  if x1 < 0 or x1 > width-size:
x1Speed = -x1Speed
  if y1 < 0 or y1 > height-size:
y1Speed = -y1Speed

Sorry 'bout that.

-- 
Steve Willoughby|  Using billion-dollar satellites
[EMAIL PROTECTED]   |  to hunt for Tupperware.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] converting processing code to python code

2008-12-01 Thread Steve Willoughby
On Mon, Dec 01, 2008 at 03:48:59PM -0500, Erica Osher wrote:
> I have a simple processing code that I'm trying to work with in python, but
> I keep getting syntax errors. Any help on changing the code would be greatly
> appreciated. Thanks.

Could you show us what you have so far in Python and what syntax errors
you get?

Just as one example, this would be
pretty straightforward to convert straight
to Python, syntactically.  Whether you'd 
want to restructure the application to be
more optimally "Pythonic" is another topic
depending on what else is going on (or is 
this the entire program)?

> void drawSquare1() {
>  if(x1<0 || x1>width-size) {
>   x1Speed = -x1Speed;
>  }
> 
>  if(y1<0 || y1>height-size) {
>   y1Speed = -y1Speed;
>  }

def drawSquare1():
  if (x1 < 0 or x1 > width-size):
x1 += x1Speed
y1 += y1Speed
rect(x1, y1, size, size)

Is that the sort of code you're coming up with?

One thing that strikes me off the top here is that
(in either language) you'd be better off not using
all those global variables.  Make your functions take
parameters and use them in your calculations.


-- 
Steve Willoughby|  Using billion-dollar satellites
[EMAIL PROTECTED]   |  to hunt for Tupperware.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] converting processing code to python code

2008-12-01 Thread Erica Osher
I have a simple processing code that I'm trying to work with in python, but
I keep getting syntax errors. Any help on changing the code would be greatly
appreciated. Thanks.

void setup() {
 size(550, 500);
 noStroke();
 smooth();
 fill(255, 255, 255, 150);
}


//Square 1 vars
int x1 = 5;
int y1 = 10;

int x1Speed = 2;
int y1Speed = 2;

//Square 2 Vars
int x2 = 150;
int y2 = 100;

int x2Speed = 4;
int y2Speed = 4;

int size = 100;

void draw() {
 background(180, 0, 0);
 drawSquare1();
 drawSquare2();

 checkCollision();
}



void drawSquare1() {
 if(x1<0 || x1>width-size) {
  x1Speed = -x1Speed;
 }

 if(y1<0 || y1>height-size) {
  y1Speed = -y1Speed;
 }

 x1+= x1Speed;
 y1+= y1Speed;
 rect(x1, y1, size, size);
}

void drawSquare2() {
 if(x2<0 || x2>width-size) {
  x2Speed = -x2Speed;
 }

 if(y2<0 || y2>height-size) {
  y2Speed = -y2Speed;
 }

 x2+= x2Speed;
 y2+= y2Speed;
 rect(x2, y2, size, size);
}

void checkCollision() {
 if(abs(x1-x2) < size && abs(y1-y2) < size) {
  println("Collision");
  //fill(255, 255, 255, 200);
  x1Speed=-x1Speed;
  x2Speed=-x2Speed;

  y1Speed=-y1Speed;
  y2Speed=-y2Speed;
 } else {
  fill(255, 255, 255, 100);
 };
}


-- 
Erica Osher
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor