4/29/14 A Standard Game Application Controller while-loop Check for user input Process user input Update the models Event Loop Update display/view No change to models View Must We Write this Loop Each Time? while program_is_running: # Get information from mouse/keyboard # Handled by OS/GUI libraries controller.update() controller.draw() Method calls for loop body # Draw stuff on the screen # Handled by OS/GUI libraries • No: make this loop part of the library! • Then we only have to provide the controller class. Controller Subclass of Game Attribute view (inherited) Purple because it’s also partly a View Model Subclasses of GObject View Method Class GView GObject.draw • GEllipse, GImage, … • Handles details of • Comprises many objects drawing • Knows how to draw • Provided in Game CS 1110 Spring 2014: MVC and Games while program_is_running: # Get information from mouse/keyboard Would like to # Handled “plugby in”OS/GUI code libraries # Code to respond to user input Why do we need to write this each time? # Code to draw stuff in the window # Get window onto the screen # Handled by OS/GUI libraries Model-View-Controller Pattern Division can apply to classes or modules Calls the methods or functions of Controller • Updates model in response to events • Updates view with model changes Model • Defines and View • Displays model to manages the data • Responds to the controller requests Model-View-Controller in A5 Other attributes (defined by you) Must We Write this Loop Each Time? the user • Provides interface for the controller Example: Animation class Animation(Game): See animation.py """Application to an ellipse in a circle.""" def init(self): """Special loop initialization method.""" … Loop initialization Do NOT use __init__ def update(self, dt): """Change the ellipse position.""" … Loop body def draw(self): """Draw the ellipse""" Use method draw() … defined in GObject 1 4/29/14 Example: Animation What Attributes to Keep: Touch class Animation(Game): See animation.py """Application to an ellipseParent in a circle.""" class that handles details def init(self): """Special loop initialization method.""" … Loop initialization Do NOT use __init__ def update(self, dt): """Change the ellipse position.""" … Loop body def draw(self): """Draw the ellipse""" Use method draw() … defined in GObject More Attributes: Checking Click Types • Double click = 2 fast clicks • Count number of fast clicks pressed released released Mouse button pressed § last not None, touch None: Mouse button released § last and touch not None: Mouse dragged (button down) time See touch.py § Ball countdown vs. serve State ANIMATE_CIRCLE § Implemented as an int See state.py Types of Models for Assignment 6 • Often subclass of GObject sparks § Has built-in draw method § See documentation in A6 § Ball countdown vs. serve • Includes groups of models § Method update() checks state § Executes correct helper § Implemented as an int State ANIMATE_HORIZONTAL § State is an enumeration; one of several fixed values § Global constants are values • Add an attribute state § State is an enumeration; one of several fixed values State ANIMATE_CIRCLE § Executes correct helper § Playing game vs. pausing • How do we store state? See touch.py • Add an attribute state State: Changing What the Loop Does • State: Current loop activity Previous Touch State: Changing What the Loop Does • How do we store state? § Set to 0 when mouse released § Check time when next pressed § last None, touch not None: Current Touch § Method update() checks state pressed § Add an attribute time § Increment when not pressed (e.g. in loop method update()) • Compare touch, last position Line segment = 2 points § Playing game vs. pausing § Reset to 0 if not fast enough • Time click speed § The mouse press position § Or None if not pressed § Use self.view.touch inside controller (Game) methods • State: Current loop activity Is it fast enough? § Add an attribute clicks • Attribute touch in GView State ANIMATE_HORIZONTAL Importance of class invariants See state.py § Global constants are values CS 1110 Spring 2014: MVC and Games § § § § Example: rockets in pyro.py Each rocket is a model But so is the entire list! update() will change both rocket • A5: Model class § Container, like A4 Dataset § Holds bricks, ball, paddle See pyro.py 2
© Copyright 2025 ExpyDoc