gui applications a standard gui application
play

GUI Applications A Standard GUI Application Animates the - PowerPoint PPT Presentation

Module 27 GUI Applications A Standard GUI Application Animates the application, like a movie A Standard GUI Application Check for user input Process user input Animates the Update the objects application, like a movie A Standard GUI


  1. Module 27 GUI Applications

  2. A Standard GUI Application Animates the application, like a movie

  3. A Standard GUI Application Check for user input Process user input Animates the Update the objects application, like a movie

  4. A Standard GUI Application Check for user input Process user input Animates the Update the objects application, like a movie Update display/view No change to objects Restriction set by graphics cards

  5. Basic Application Loop while program_is_running: # Get user input # Custom Application Code # Draw stuff on the screen

  6. Do We Need to Write All This? while program_is_running: # Get user input Can we get this handled for us? Code you must write yourself. # Custom Application Code Can we get this handled for us? # Draw stuff on the screen

  7. Idea: Use a Class/Object application = AppClass() while application.isRunning(): application.getInput() application.update() application.drawToScreen()

  8. Leverage Subclassing application = AppClass() Overridden while application.isRunning(): application.getInput() Inherited application.update() Overridden Inherited application.drawToScreen()

  9. Programming Animation Intra-Frame • Computation within frame § Only need current frame • Example: Collisions § Need current position § Use to check for overlap • Can use local variables § All lost at update() end § But no longer need them

  10. Programming Animation Inter-Frame Current frame • Computation across frames § Use values from last frame • Example: Movement § Need old position/velocity § Compute next position • Requires attributes Previous § Attributes never deleted frame § Remain after update() ends

  11. Idea: Use a Class/Object application = AppClass() while application.isRunning(): application.getInput() Local variables erased. But attributes persist. application.update() application.drawToScreen()

  12. Programming Animation Intra-Frame Inter-Frame • Computation within frame • Computation across frames § Only need current frame § Use values from last frame • Example: Collisions • Example: Movement § Need current position § Need old position/velocity § Use to check for overlap § Compute next position • Can use local variables • Requires attributes § All lost at update() end § Attributes never deleted § But no longer need them § Remain after update() ends

  13. Attributes = Loop Variables Normal Loops Application Variables “external” Attributes are the x = 0 to the loop body “external” variables i = 2 while app.isRunning(): app.getInput() while i <= 5: # Your code called here x = x + i*i application.update() i = i +1 app.drawToScreen()

  14. The Actual Game Loop # Constructor To early to initialize everything game = GameApp(…) … Actual loop game.start() #Loop initialization initialization while game.isRunning(): # Get input Inherited # Your code goes here Separate update() game.update(time_elapsed) and draw() methods game.draw()

  15. Designing a Game Class: Animation class Animation(game2d.GameApp): See animation.py """App to animate an ellipse in a circle.""" def start(self): """Initializes the game loop.""" … def update(self,dt): """Changes the ellipse position.""" … def draw(self): """Draws the ellipse""" …

  16. Designing a Game Class: Animation class Animation(game2d.GameApp): See animation.py """App to animate an ellipse in a circle.""" Parent class that does hard stuff def start(self): """Initializes the game loop.""" … def update(self,dt): """Changes the ellipse position.""" … def draw(self): """Draws the ellipse""" …

  17. Designing a Game Class: Animation class Animation(game2d.GameApp): See animation.py """App to animate an ellipse in a circle.""" Parent class that does hard stuff def start(self): """Initializes the game loop.""" … Loop initialization Do NOT use __init__ def update(self,dt): """Changes the ellipse position.""" … Loop body def draw(self): """Draws the ellipse""" Use method draw() … defined in GObject

  18. Drawing to The Screen • All GameApp objects have a view attribute § Instance of GView (similar to Turtle Window ) § Represents the window to draw to • Create objects to draw § Subclasses of GObject § Special cases, GLabel , GImage , GSprite § All inherit a method draw(view) • Just like our lessons on subclasses!

  19. The GInput Class • All GameApp objects have an input attribute § Contains input for current animation frame § Support for Keyboard and Mouse (Touch) • Class GInput defines attributes, methods § is_key_down(key) : Returns True if key held § is_touch_down() : Returns True if mouse pressed § keys : List of all keys currently pressed § touch : Point2 of (pressed) mouse screen location

  20. The GInput Class • All GameApp objects have an input attribute § Contains input for current animation frame § Support for Keyboard and Mouse (Touch) • Class GInput defines attributes, methods § is_key_down(key) : Returns True if key held Simple Example: § is_touch_down() : Returns True if mouse pressed Pausing animation § keys : List of all keys currently pressed § touch : Point2 of (pressed) mouse screen location

  21. Recall: Programming Animation Inter-Frame Current frame • Computation across frames § Use values from last frame • Example: Movement § Need old position/velocity § Compute next position • Requires attributes Previous § Attributes never deleted frame § Remain after update() ends

  22. Inter-Frame Comparisons • Attribute touch in GInput Line segment = 2 points § The mouse press position § Or None if not pressed § Access with self.input.touch Current • Compare touch , last position Touch § Mouse button pressed: Previous last None, touch not None Touch § Mouse button released: last not None, touch None § Mouse dragged : See touch.py last and touch not None

  23. State: Changing What the Loop Does State ANIMATE_CIRCLE • State : Current loop activity § Playing game vs. pausing § Ball countdown vs. serve • Add an attribute state § Method update() checks state § Executes correct helper State ANIMATE_HORIZONTAL • How do we store state? § State is an enumeration ; one of several fixed values See state.py § Implemented as an int

  24. Designing States • Each state has its own set of invariants. § Drawing? Then touch and last are not None § Erasing? Then touch is None, but last is not § Erasing? Then touch and last are both None • Need to make clear in class specification § What are the application states ? § What are the invariants for each state ? § What are the rules to switch to a new state?

  25. State Triggers • Need a rule for switching between states § Look for some event to happen, and change state § Example: press space to change state in state.py § Example: double clicking to erase in touch.py • Complex apps also limit state transitions § ANIMATE_CIRCLE => ANIMATE_HORIZONTAL OK! § ANIMATE_HORIZONTAL => ANIMATE_CIRCLE BAD! • Again, make clear in specification

  26. Example: Checking Click Types • Double click = 2 fast clicks Is it fast enough? • Count number of fast clicks § Add an attribute clicks pressed released § Reset to 0 if not fast enough • Time click speed released pressed § Add an attribute time § Set to 0 when mouse released time § Increment when not pressed (e.g. in loop method update() ) See touch.py § Check time when next pressed

  27. Designing Complex Applications • Applications can become § Processes input extremely complex MainApp § Determines state § Large classes doing a lot § Many states & invariants Calls the methods of § Specification unreadable • Idea : Break application Animation § Animates (only) up into several classes § Start with a “main” class § Other classes have roles See subcontroller.py § Main class delegates work

  28. How to Break Up: Software Patterns • Pattern : reusable solution to a common problem § Template, not a single program § Tells you how to design your code § Made by someone who ran into problem first • In many cases, a pattern gives you the interface § List of headers for non-hidden methods Just like § Specification for non-hidden methods this course! § Only thing missing is the implementation

  29. Model-View-Controller Pattern Calls the Division Controller methods or can apply • functions of Updates model in to classes response to events or modules • Updates view with model changes Model View • • Defines and Displays the model manages the data to the app user • • Responds to the Provides user input controller get/set to the controller

  30. MVC in this Course Model Controller • A3 : a3app.py • A3 : Color classes § Hidden classes § RGB , CMYK & HSV • A4 : Turtle , Pen • A4 : Funcs in a4.py § Window is View § No need for classes • A7 : Frog , Car , etc.. • A7 : Froggit , Level § All shapes/geometry § The actual assignment!

Recommend


More recommend