我已经开始学习使用python/pygame制作游戏,并且好像很容易在pygame中快速制作一个有效的游戏,但是没有关于如何以合理的方式组织代码的真正教程.
在pygame教程页面上,我发现了3种方法.
1-对于小型项目,不使用课程
2 MVC ruby-on-rails类型的结构,但没有导轨框架,导致过于复杂和模糊的东西(即使有OO编程和轨道知识)
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()
Run Code Online (Sandbox Code Playgroud)
我习惯用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
Run Code Online (Sandbox Code Playgroud)
它看起来非常有条理,易于遵循.
我应该在所有项目中使用哪种结构,以便在小型和大型游戏中使用干净的代码?
| 归档时间: |
|
| 查看次数: |
1952 次 |
| 最近记录: |