简体   繁体   中英

Python and Pygame inspiration and best practice

Sorry for not being very specific in this question, but I don't know where else to ask.

I have a Raspberry Pi which I will try to use in a car computer project. I have got the tip to use Pygame to write the interface.

I'm not really familiar to python or pygame but i have quite experience in PHP, HTML and C# and VB against winforms.

In this case I will have a menu to the right with lets say 5 "tabs". How would I think when doing this? Should I think PHP/HTML and have a file for every tab page and here then load header and background and stuff, and some how link to every *.py file (if that's even possible)? Should i think more of JS and ajax to use a surface (a "div") to display my pages in, depending on the selected menu?

I've spent an evening playing around and what I suppose i have to do is something like this:

//Set up the layout
//Add a surface for Tab 1 (tab1Layout.py)
//Add a surface for Tab 2 (tab2Layout.py) - set invisible
//Add a surface for Tab 3 (tab3Layout.py) - set invisible
//Add a surface for Tab 4 (tab4Layout.py) - set invisible
//Add a surface for Tab 5 (tab5Layout.py) - set invisible

//Add the menu to the right (menu.py)

//Start the pygame loop
//Listen for events in the menu (menuEvents.py)
//Listen for events in tab 1 (tab1Events.py)
//Listen for events in tab 2 (tab2Events.py)
//Listen for events in tab 3 (tab3Events.py)
//Listen for events in tab 4 (tab4Events.py)
//Listen for events in tab 5 (tab5Events.py)

Am i on the right track here?

Do you guys know any good tutorials or other libraries for doing this kind of stuff? I do not wanna load X or something like that.

Any good tips of tutorials or know any similar projects where I can study the code for this?

So, slightly hard to know exactly what you want. However I would like to champion simplicity here, if you make yourself this many files you'll just confused, and have a lot of redundant code.

From my person game making perspective, my screens are all methods of a single class. There are a few instance variables like the screen, but each method contain the main pygame loop, ie

while True:
    for event in pygame.event.get():
        #whatever
        pass

and on button press one can move on to a separate method in your class. I would recommend creating either a class for each tab button or a class for the whole set of tab buttons, this can be updated, drawn to screen and used in all separate methods for each of your tabs

The way this varies from things like php & html is the lack of predefined structure, as you will have to make all of this from scratch. I have a few code snippets available on my website that you can use for making buttons etc if that would help. Here is a link to it.

Also, If you choose the seperate file method, python's import works very intutively, you could import a class (say Layout3) from your file tabLayout3.py simply using:

from tabLayout3 import Layout3

(provided the path of tabLayout3.py is in my python path)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM