Lecture 22: GUI Applications CS 1110 Introduction to Computing - - PowerPoint PPT Presentation

lecture 22 gui applications
SMART_READER_LITE
LIVE PREVIEW

Lecture 22: GUI Applications CS 1110 Introduction to Computing - - PowerPoint PPT Presentation

http://www.cs.cornell.edu/courses/cs1110/2019sp Lecture 22: GUI Applications CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White] Relevant modules (see schedule page)


slide-1
SLIDE 1

Lecture 22: GUI Applications

CS 1110 Introduction to Computing Using Python

[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]

http://www.cs.cornell.edu/courses/cs1110/2019sp

slide-2
SLIDE 2

Relevant modules (see schedule page)

  • animation.py
  • arrows.py
  • pyro.py
  • state.py
  • subcontroller.py
  • game2d.zip (unzip)

Try them out! >>> python arrows.py

slide-3
SLIDE 3

Update display/view No change to objects Check for user input Process user input Update the objects

A Standard GUI Application

Animates the application, like a movie

slide-4
SLIDE 4

while program_is_running: # Get keyboard info ß Handled by OS/GUI libraries # Your code goes here # Draw stuff to screen ß Handled by OS/GUI libraries application.update() ß Custom application class

slide-5
SLIDE 5

Example: Animation

class Animation(game2d.GameApp): """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""" … See animation.py Loop initialization Do NOT use __init__ Loop body Use method draw() defined in GObject

Parent class that does hard stuff

slide-6
SLIDE 6

Does update() respond to the user?

A) Yes, any key changes the animation B) Yes, certain keys select certain animations C) No D) I don't know

def update(self,dt): """ Animates the ellipse. Parameter dt: The time since the last animation frame. Precondition: dt is a float. """ # Change the angle self.angle = self.angle+ANIMATION_STEP % (2*math.pi) pos=self._polar_to_coord(ANIMATION_RADIUS,self.angle) self.ellipse.x = pos[0] self.ellipse.y = pos[1]

See animation.py

slide-7
SLIDE 7

Does update() respond to the user?

A) Yes, any key changes the animation B) Yes, certain keys select certain animations C) No D) I don't know

See state.py

slide-8
SLIDE 8

Changing the Loop Activity

  • State: Current loop activity

§ Playing game vs. pausing § Ball countdown vs. serve

  • Add an attribute state

§ Method update() checks state § Executes correct helper

  • How do we store state?

§ State is an enumeration;

  • ne of several fixed values

§ Implemented as an int § Global constants are values

State STATE_CIRCLE State STATE_HORIZONTAL See state.py State STATE_VERTICAL

slide-9
SLIDE 9

Need rules for when we switch states

  • Attribute key_count in GInput

§ How many keys are pressed? § 0, 1, 2, … § curr_keys = self.input.key_count

  • Is this a new key press?

§ Need current curr_keys, and key count from last time: lastkeys

change = curr_keys > 0 and self.lastkeys == 0

  • When we're done, curr_keys becomes the new lastkeys

self.lastkeys = curr_keys

See

_determineState(self):

in state.py

slide-10
SLIDE 10

Designing Complex Applications

  • Applications can become

extremely complex

§ Large classes doing a lot § Many states & invariants § Specification unreadable

  • Idea: Break application

up into several classes

§ Start with a “main” class § Other classes have roles § Main class delegates work

MainApp Animation

See subcontroller.py § Processes input § Determines state § Animates (only) Calls the methods of

slide-11
SLIDE 11
  • 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 § Specification for non-hidden methods § Only thing missing is the implementation

How to Break Up: Software Patterns

Just like this course!

slide-12
SLIDE 12

Model

  • Defines and

manages the data

  • Responds to the

controller requests

View

  • Displays the model

to the app user

  • Provides user input

to the controller

Controller

  • Updates model in

response to events

  • Updates view with

model changes

Model-View-Controller Pattern

Calls the methods or functions of Division can apply to classes

  • r modules

Wave & Invaders Classes Ship, Alien & Bolt Classes draw() methods

slide-13
SLIDE 13

Model Subclasses of GObject

  • GEllipse, GImage, …
  • Often more than one

View Class GView, GInput

  • Do not subclass!
  • Part of GameApp

Controller

Subclass of GameApp

Model-View-Controller in CS 1110

Classes in game2d Method draw in GObject Attribute view (inherited) Other attributes (defined by you)

Neglected for most

  • f this lecture
slide-14
SLIDE 14

Models in Assignment 7

  • Often subclass of GObject

§ Has built-in draw method § See documentation in A7

  • Includes groups of models

§ Example: rockets in pyro.py § Each rocket is a model § But so is the entire list! § update() will change both

  • A7: Several model classes

§ Ship to animate the player § Alien to represent an alien

See pyro.py rocket sparks