Python beginners question


#1

Hello,

I am trying to start writing own modes. I am used to program shell scripts, and I can mostly understand and sometimes modify Perl, Java, Groovy, or even C++ code. But I don’t have any Python experience until now.

So I tried a small example: let a line move from left to right. The color and the position are changed by the same variable. That works fine with this script:
-------------snip------------
import os
import pygame
import math

    count1 = 0

    def setup(screen, etc):
        pass

    def draw(screen, etc):
        global count1

        if count1 > 254 :
           count1 = 0
        count1 = int(count1 + 1)
        
        color = count1,0,0
        xpos = int(count1*5)
        thick = 100
        pygame.draw.line(screen, color, [xpos, 1], [xpos, 720], thick)
-------------snip------------

Now I wanted to move forward and backward. I use this code:

-------------snip------------
 import os
 import pygame
 import math

 count1 = 0
 direction = 0

 def setup(screen, etc):
    pass

 def draw(screen, etc):
    global count1
    global direction
    
    
    if count1 > 254 :
        direction = 1
    if count1 < 1 :
        direction = 0

    if direction = 0 :
        count1 = int(count1 + 1)
    if direction = 1 :
        count1 = int(count1 - 1)
    

    color = count1,0,0
    xpos = int(count1*5)
    thick = 100
    pygame.draw.line(screen, color, [xpos, 1], [xpos, 720], thick)

-------------snip------------

But this time the mode won’t load at all.

What am I doing wrong here?

Any help is appreciated.


#2

its == , not = (like c++) for equals.
see
https://www.tutorialspoint.com/python/python_basic_operators.htm

tip: if you press the OSD button, often it will show the error , including line number


#3

Oh, yes. Silly me. Thanks!

The OSD view didn’t help in this case as the Mode didn’t load at all (that was what the OSD view did tell…)


#4

Hello again,

now some other stupid question which a real programmer would never ask :slight_smile:

I use a lot of code, which is repeating in each mode. I’d like to have this in central library file and include it in my different modes main.py. For example I have a simple “toggle switch” that switches at each trigger, I have a kind of LFO, which counts from 0 to 255 and back. Also I have a monitoring output, which displays the values of some variables as numbers on the screen.

Until now I hold those in a text file and copy paste them. But each time I improve such a standard routine, I have to add this improvement to all my modes. That is annoying.

So is there some way to use something like MyLIbrary.LFO similar like the etc.knobX?

Example:
MyLibrary.LFO(number,speed) gets an integer parameter for the selection of LFO 1 to 4 and gets an integer for the speed value (not discussing here how this speed setting is achieved). It returns an integer which changes at the given speed
So I could set
color = ( MyLibrary.LFO(1,10), MyLibrary.LFO(2,11),MyLibrary.LFO(3,12) )

Some of these functions might be tricky as they depend on global variables; can this be handled?

Am I missing other pit falls?

Best regards
Florian


#5

lookup python modules, variables and functions are scoped to the module.

then for more structure you could look at classes.


#6

Thank you again!