【问题标题】:pygame code organizationpygame 代码组织
【发布时间】:2011-06-04 11:12:14
【问题描述】:

我已经开始学习使用 python/pygame 制作游戏,虽然在 pygame 中快速制作游戏很容易,但没有关于如何以合理的方式组织代码的真正教程。

在 pygame 教程页面上,我找到了 3 种方法。

1- 不使用类,用于小型项目

2- MVC ruby​​-on-rails 类型的结构,但没有 rails 框架,这会导致某些东西过于复杂和晦涩(即使具有 OO 编程和 Rails 知识)

3- 类似 C++ 的结构如下:(简洁直观,但可能不太像 python?)

import pygame
from pygame.locals import *

class MyGame:
    def __init__(self):
        self._running = True
        self._surf_display = None
        self.size = self.width, self.height = 150, 150
    def on_init(self):
        pygame.init()
        self._display_surf = pygame.display.set_mode(self.size)
        pygame.display.set_caption('MyGame')
        #some more actions
        pygame.display.flip()
        self._running = True
    def on_event(self, event):
        if event.type == pygame.QUIT:
            self._running = False
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                self._running = False
        elif event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                print event.pos
    def on_loop(self):
        pass
    def on_render(self):
        pass
    def on_cleanup(self):
        pygame.quit()
    def on_execute(self):
        if self.on_init() == False:
            self._running = False
        while( self._running ):
            for event in pygame.event.get():
                self.on_event(event)
            self.on_loop()
            self.on_render()
        self.on_cleanup()

if __name__ == "__main__" :
    mygame = MyGame()
    mygame.on_execute()

我习惯用 C++ 制作 SDL 游戏,我使用这种精确的结构,但我想知道它是否适用于小型和大型项目,或者在 pygame 中是否有更简洁的方式。

例如,我发现一个游戏是这样组织的:

imports

def functionx
def functiony

class MyGameObject:
class AnotherObject:
class Game: #pygame init, event handler, win, lose...etc
while True: #event loop
display update

它看起来也很有条理,易于理解。

我应该在我的所有项目中始终使用哪种结构,以便在小型和大型游戏中使用干净的代码?

【问题讨论】:

  • 只要有 GUI,MVC 就以某种形式存在。您的“C++ 结构”实际上是 MVC 的控制器部分。每当您使用on_foo 方法等待事件时,您可能会使用某种形式的 MVC。 martinfowler.com/eaaDev/uiArchs.html
  • 我正在尝试使用 pygame 编写一个简单的游戏,我发现您的方法非常有趣,但是您能解释一下我们需要在 on_loop()on_render() 方法中做什么吗?正如我在on_render 中了解的那样,我们制作了一些图纸,对吗?在on_loop() 中我们执行一些逻辑。对吗?

标签: python pygame


【解决方案1】:

我还建议您可以使用 cmets(就像第一次看起来一样沉闷)来划分您的工作。举个例子:

import pygame, random, math

## CLASSES ----------------------------------------------

class Ball():
    def __init__(self, (x,y), size):
        """Setting up the new instance"""
        self.x = x
        self.y = y
        self.size = size

## FUNCTIONS --------------------------------------------
def addVectors((angle1, length1), (angle2, length2)):
    """Take two vectors and find the resultant"""
    x = math.sin(angle1) * length1 + math.sin(angle2) * length2
    y = math.cos(angle1) * length1 + math.cos(angle2) * length2

## INIT -------------------------------------------------

width = 600
height = 400
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("S-kuru")

等等。

作为另一个需要考虑的选项,您是否考虑过使用子模块?它们只是您放置常用函数的其他 Python 文件 (.py)。

def showText(passedVariable):
    print passedVariable
    return

这个新文件是导入的,就像 math 或 random 一样,函数也是这样:

import mySubModule

mySubModule.showText("Hello!")

但这就是我的工作方式。绝对遵循你能理解的,不仅是现在,下周或下一年也一样。

【讨论】:

  • 我会将您的 INIT 部分放入 if __name__ == "__main__": 块中
【解决方案2】:

做你可以遵循的。如果您可以理解您发布的代码,那么这就是您应该使用的。如果不同的结构感觉更自然,请改用它。

【讨论】:

  • 对,所以您确认除了以可读的方式创建类和轮询事件之外,没有标准的 pygame/python 方式来组织项目。只要上面的 2 个示例对 Python 程序员来说看起来不可怕,那就没问题了。
猜你喜欢
  • 2018-10-12
  • 2011-08-08
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多