I'm implementing a modular AI testing engine using the MVC pattern in python.
So far everything's ok: The AIs, mouse, keyboard are controllers, the model is a physic engine, the view is a pygame instance that renders everything. I have an event handler to handle most communication.
Now, I want to implement a menu system (imagine a game menu where you can choose Load, Save, Quit, etc). But 开发者_Python百科I'm blocked, I can't figure out how should I implement it. The problem is I'm thinking it should be part controller and part view but I'm not sure.
Any ideas? Thank you
First, a few points too often forgotten when talking about MVC:
No pattern is a dogma. If the best solution is not MVC, that probably means MVC wasn't appropriate for the problem.
MVC, the OOD style for GUI applications is very different from MVC, the layered architecture for web applications. In this case you need the first one, so many docs and articles about the second might not be good advice.
In 'GUI-MVC', the Model-View-Controller triad is for each application module; there's no 'model layer', 'view layer' nor 'controller layer'.
now, for your specific case
You could see the menu system as an extra module, independent from your existing engine. This module can (if you find it convenient) have it's own MVC structure. The model can be the list (or tree) of commands, the views are the visible menus, the controller could be a dispatcher that gets the menu event and executes the commands.
Or, the whole menu can be just a GUI control; one of the many existing controllers. Like any GUI control, it has a visible part. If your base libraries are simple enough, the code to instatiate and mange the menus can be too simple to be worth of a complex internal structure.
精彩评论