2018-04-06 02:06:11

Another term for it is namespace, any variables declared in a function stay in a function, unless explicitly returned to the caller to prevent conflicts with other variables outside the given space. Other than using globals, what you can also do is set it up so X is filled with a returned value the function gives back that updates it with a new value, for example:

def my_fun(a):
    a = a + 1
    x = a
    print(a)
    print(x)
    return x

x = 3
x = my_fun(x)
print x

Keep in mind that if you use a return call in a function, the function will end and anything beyond the return call will be ignored, so be sure to put it at the end or in places where you want the function to end.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

Thumbs up

2018-04-21 23:02:38

that's the one thing that got me while looking at code, globals. Okay. So that's what that does. Also there is a trick to reimport something, like this.
import thing
#do stuff with thing
#thing has been changed...
del(thing)
import thing
#new thing!

----------
An anomaly in the matrix. An error in existence. A being who cannot get inside the goddamn box! A.K.A. Me.

2018-04-23 15:54:30 (edited by cartertemm 2018-04-23 15:55:15)

you can also use reload. I tend to think of del as forgetting the resource exists and just starting over rather than actually destroying it.

Thumbs up

2018-04-24 02:42:56

okay so there is a reload function. Is it reload(module)?

----------
An anomaly in the matrix. An error in existence. A being who cannot get inside the goddamn box! A.K.A. Me.

2018-04-24 20:46:14 (edited by Ethin 2018-04-24 20:46:46)

The reload function is only available in Python 2. As a built-in, that is. In Python3 its in importlib:
Help on function reload in module importlib:

reload(module)
    Reload the module and return it.
The module must have been successfully imported before.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.

Thumbs up

2018-04-24 22:04:42

ivan_soto wrote:

Hi there,
so very recently I decided to just finally move on and try something new. I want to do other things besides games etc, and I need to learn a better programming language than BGT. So...from a bgt user, what are some tips or resources to learn python. BGT is so simple that its hard to move away from, but it needs to be done. It is clear that it's done and it probably won't ever be updated again.

For gamedev on Python I recommend Panda3D. Link below. I worked with it several years ago. Panda is free and supports Python and C++, so you can freely rewrite core to your purposeses.
https://www.panda3d.org/manual/index.php/Features

2018-04-24 23:40:29

I would disagree with post 56 on this matter. I have a group who attempted to write games in Panda3D and everything we tried for our particular game failed -- either Panda3D could not accommodate our needs or it got in the way. Feel free to try it, but most of the documentation is still in Python 2, last time I checked. I'd highly recommend Pygame -- its what our group has stuck with and it has served us well.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.

Thumbs up

2018-05-01 23:09:20

Is there any use for tKinter? I looked at it, and it's mostly dealing with pictures.
On another note, started to learn about classes. Looks cool, but I'm not sure if I declare a variable inside a class, will I be able  to influence it outside of the class it has been declared in? Any help on that would be appreciated.

Thumbs up

2018-05-02 00:09:21

@Ethin
Could you elaborate on what difficulties you ran into with Panda3D?

@amerikranian
Yes you can, though it can also depend on how you declare it in some cases. In most instances you can declare a variable in the class itself, for example:

class example(object):
    number = 5

Or with the "self" prefix:

class example(object):
    def __init__(self):
        self.number = 5

You can then print or access those variables by using the classes container as a prefix, like so:

a = example()
print a.number

a.number = 6
print a.number

But you can also create new variables to a class that way too:

a.new = 20
print a.new

However, when using functions in a class it follows domain specific name spacing. What this means is if you create a variable inside a function within a class without the "self" prefix, that variable will live and die in that function unless returned or passed to a variable in the class. For example:

class example(object):
    def test(self):
        number = 5

a = example()
a.test()
print a.number

This would throw an error that "number" doesn't exist, because variables declared in functions end when the function does. But you can add the "self" prefix to assign the variable to the class itself outside the function, so it can be stored and used elsewhere:

class example(object):
    def test(self):
        self.number = 5

a = example()
a.test()
print a.number

This makes classes really useful, since you can store variables within them, then use functions in those same classes to change, add, or remove data as needed.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

Thumbs up

2018-05-02 00:37:50

@magurp244, mainly that all the documentation (again, last time I checked) was in Python 2 still and some other window 3D handling issues we had. We've switched to Pygame and its been smooth sailing since. I honestly don't remember the exact problem as its been several months since we switched to pygame.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.

Thumbs up

2018-05-08 20:19:56

So I hate to ask the same questions as everyone else, but here we go.
I feel like I know enough to create a small text based rpg. Thing is, python can't play sounds, and while I can do without them, I'd like to add them in. Nothing complicated, mind you, at most it's gonna play 10 things, but still. So, know of any good things to use/where can I get them from/their documentation/installation guides?
Another thing is a window. I can not create one no matter what I do. The turtle graphics window is just not designed for input() method. Also, if you know of a better way of gathering input, do let me know, as the book taught me a lot about loops, if statements, classes, and functions, but only one input method.
Last thing is an inventory. I was thinking of using a dictionary for it, simply because I can just output it on the screen, but feel free to explain why a list or something could be better to use.
Last last thing. The only 2 ways I know of sending info to the user is print() and return, are there other ways to output the information?
I know it may not seem like I know a lot, and I don't claim to be an amazing programmer, but those are the questions that I had when I finished reading the book.

Thumbs up

2018-05-08 20:35:00

Pygame covers most of what you asked about. Accessible_output2 or toke provide tts/screen reader/braille output. Libaudioverse is there if you need 3d sound, but of those I've listed it is probably the most confusing.
There is one thing to be aware of if you use pygame, and that is that it matters how you initialize the sound system. You'd do it one way if you call pygame.init, and another way if you do not. I haven't used it in a while and don't remember which is which.
You can get pygame from pygame.org, IIRC. I don't remember if you have to run setup.py or not (that's how external libraries usually work).

Some of my games
Keep up to date by following @Jeqofire on twitter!
Ear Ninja?

Thumbs up

2018-05-08 23:23:33

Base Python is the core language, but to do more things you'll also need to use third party libraries that add more functions and capabilites. There are a variety of options available for the tasks you have in mind, audio, input, a window, and tts functions, the question is just finding the one that works best for you. Pygame's already been mentioned, and its a fairly solid library with a bunch of tutorials around that can handle most of what you need, and Tolk is also a very solid TTS library, if you don't have any crossplatform needs i'd also recommend it.

Pyglet though is also another alternative to Pygame, its very similar in many respects to Pygame in capability but also has better 3D functionality with OpenGL running under the hood, along with OpenAL bindings that can be tweaked for 3D positional audio, or if you prefer you could use PyAL for just OpenAL audio. I can provide examples for any of those if you'd like.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

Thumbs up