简体   繁体   English

Python和Pygame的灵感与最佳实践

[英]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. 我有一个Raspberry Pi,将尝试在汽车计算机项目中使用。 I have got the tip to use Pygame to write the interface. 我有使用Pygame编写界面的技巧。

I'm not really familiar to python or pygame but i have quite experience in PHP, HTML and C# and VB against winforms. 我对python或pygame并不是很熟悉,但是我在PHP,HTML和C#以及VB对Winforms方面有丰富的经验。

In this case I will have a menu to the right with lets say 5 "tabs". 在这种情况下,我将在右侧显示一个菜单,其中包含5个“标签”。 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)? 我是否应该考虑使用PHP / HTML,并在每个选项卡页面上都有一个文件,然后在此处加载标头,背景和内容,以及如何链接到每个* .py文件(如果可能的话)? Should i think more of JS and ajax to use a surface (a "div") to display my pages in, depending on the selected menu? 根据所选菜单,我是否应该更多地考虑使用JS和Ajax使用表面(“ div”)来显示我的页面?

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. 我不想加载X或类似的东西。

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 屏幕上有一些实例变量,但是每种方法都包含pygame主循环,即

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. 与php和html之类的方法不同的是缺少预定义的结构,因为您必须从头开始进行所有这些操作。 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: 另外,如果选择单独的文件方法,则python的导入非常直观,可以使用以下命令从文件tabLayout3.py中导入一个类(例如Layout3):

from tabLayout3 import Layout3

(provided the path of tabLayout3.py is in my python path) (假设tabLayout3.py的路径在我的python路径中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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