Deck 13: Video Games: Multithreading, Event Handlers, Static Variables
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/33
Play
Full screen (f)
Deck 13: Video Games: Multithreading, Event Handlers, Static Variables
1
Which of the following parts of an event-driven program are managed by a game maker rather than by the Python event handler?
A) Multithreading
B) Queue
C) Event loop
D) Callback function
A) Multithreading
B) Queue
C) Event loop
D) Callback function
D
2
What is the main structure of an event-driven program?
A) Event loop
B) Queue
C) Callback function
D) Event processing
A) Event loop
B) Queue
C) Callback function
D) Event processing
A
3
A ____ is a data structure that works like a to-do list.
A) list
B) set
C) queue
D) dictionary
A) list
B) set
C) queue
D) dictionary
C
4
Case Study 1:
1. class EventHandler:
2. def __init__(self):
3. self.__queue = []
4. self.__eventKeeper = {}
5.
6. def addEvent(self, eventName):
7. self.__queue.append(eventName)
8.
9. def registerCallback(self, event, func):
10. self.__eventKeeper[event] = func
11.
12. def run(self):
13. while(True):
14. if len(self.__queue) > 0:
15. nextEvent = self.__queue.pop(0)
16. self.__eventKeeper[nextEvent]()
17. else:
18. print('queue is empty')
-Refer to the session in the accompanying Case Study 1. What is held in self.__queue?
A) The list of events that need to be processed
B) The dictionary that maps events to their callback functions
C) The list of how many times the simulation should run
D) The number of different types of callback functions
1. class EventHandler:
2. def __init__(self):
3. self.__queue = []
4. self.__eventKeeper = {}
5.
6. def addEvent(self, eventName):
7. self.__queue.append(eventName)
8.
9. def registerCallback(self, event, func):
10. self.__eventKeeper[event] = func
11.
12. def run(self):
13. while(True):
14. if len(self.__queue) > 0:
15. nextEvent = self.__queue.pop(0)
16. self.__eventKeeper[nextEvent]()
17. else:
18. print('queue is empty')
-Refer to the session in the accompanying Case Study 1. What is held in self.__queue?
A) The list of events that need to be processed
B) The dictionary that maps events to their callback functions
C) The list of how many times the simulation should run
D) The number of different types of callback functions
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
5
Case Study 1:
1. class EventHandler:
2. def __init__(self):
3. self.__queue = []
4. self.__eventKeeper = {}
5.
6. def addEvent(self, eventName):
7. self.__queue.append(eventName)
8.
9. def registerCallback(self, event, func):
10. self.__eventKeeper[event] = func
11.
12. def run(self):
13. while(True):
14. if len(self.__queue) > 0:
15. nextEvent = self.__queue.pop(0)
16. self.__eventKeeper[nextEvent]()
17. else:
18. print('queue is empty')
-Refer to the session in the accompanying Case Study 1. How would you call the function associated with the key 'mouse'?
A) self.__queue['mouse']
B) self.__eventKeeper['mouse']()
C) self.__eventKeeper('mouse')
D) self.__queue('mouse')
1. class EventHandler:
2. def __init__(self):
3. self.__queue = []
4. self.__eventKeeper = {}
5.
6. def addEvent(self, eventName):
7. self.__queue.append(eventName)
8.
9. def registerCallback(self, event, func):
10. self.__eventKeeper[event] = func
11.
12. def run(self):
13. while(True):
14. if len(self.__queue) > 0:
15. nextEvent = self.__queue.pop(0)
16. self.__eventKeeper[nextEvent]()
17. else:
18. print('queue is empty')
-Refer to the session in the accompanying Case Study 1. How would you call the function associated with the key 'mouse'?
A) self.__queue['mouse']
B) self.__eventKeeper['mouse']()
C) self.__eventKeeper('mouse')
D) self.__queue('mouse')
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
6
Case Study 1:
1. class EventHandler:
2. def __init__(self):
3. self.__queue = []
4. self.__eventKeeper = {}
5.
6. def addEvent(self, eventName):
7. self.__queue.append(eventName)
8.
9. def registerCallback(self, event, func):
10. self.__eventKeeper[event] = func
11.
12. def run(self):
13. while(True):
14. if len(self.__queue) > 0:
15. nextEvent = self.__queue.pop(0)
16. self.__eventKeeper[nextEvent]()
17. else:
18. print('queue is empty')
-Refer to the session in the accompanying Case Study 1. What kind of loop is shown on lines 13-18?
A) Definite
B) Abstract
C) Multithreaded
D) Infinite
1. class EventHandler:
2. def __init__(self):
3. self.__queue = []
4. self.__eventKeeper = {}
5.
6. def addEvent(self, eventName):
7. self.__queue.append(eventName)
8.
9. def registerCallback(self, event, func):
10. self.__eventKeeper[event] = func
11.
12. def run(self):
13. while(True):
14. if len(self.__queue) > 0:
15. nextEvent = self.__queue.pop(0)
16. self.__eventKeeper[nextEvent]()
17. else:
18. print('queue is empty')
-Refer to the session in the accompanying Case Study 1. What kind of loop is shown on lines 13-18?
A) Definite
B) Abstract
C) Multithreaded
D) Infinite
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
7
How do you make a class multithreaded in Python?
A) Inherit from Thread.
B) Import threading.
C) Include an instance variable named thread.
D) Include a method named threaded().
A) Inherit from Thread.
B) Import threading.
C) Include an instance variable named thread.
D) Include a method named threaded().
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
8
In a multithreaded class, the ____ method creates a new thread and calls the run method of the class inside that new thread.
A) sleep
B) start
C) run
D) thread
A) sleep
B) start
C) run
D) thread
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
9
The ____ function causes the thread to pause for the number of seconds passed as a parameter.
A) thread.pause
B) thread.rest
C) time.sleep
D) time.pause
A) thread.pause
B) thread.rest
C) time.sleep
D) time.pause
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
10
The turtle module's ____ method is used to register callbacks for mouse events.
A) mouse
B) onmouse
C) mouseclick
D) onclick
A) mouse
B) onmouse
C) mouseclick
D) onclick
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
11
Before using the screen to respond to keyboard events, call the ____ method of Screen.
A) listen
B) onkey
C) main
D) start
A) listen
B) onkey
C) main
D) start
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
12
What is the name of the event loop for a turtle?
A) run
B) main
C) mainloop
D) eventloop
A) run
B) main
C) mainloop
D) eventloop
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
13
Passing ____ to a turtle function that is used to register a callback effectively cancels the callback mechanism.
A) stop
B) None
C) 0
D) turtle
A) stop
B) None
C) 0
D) turtle
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
14
To hide a function from use outside of the class, begin the name with:
A) an x.
B) one underscore.
C) two underscores.
D) one "at" symbol.
A) an x.
B) one underscore.
C) two underscores.
D) one "at" symbol.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
15
A(n) ____ variable is shared by all instances of the class.
A) instance
B) multithreaded
C) event
D) static
A) instance
B) multithreaded
C) event
D) static
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
16
A queue enforces a first-come first-served strategy for handling events.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
17
Typically, desktop applications are single-threaded.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
18
Case Study 2:
1. import turtle
2.
3. class Etch:
4. def __init__(self):
5. self.__myT = turtle.Turtle()
6. self.__myScreen = turtle.Screen()
7. self.__myT.color('blue')
8. self.__myT.pensize(2)
9. self.__myT.speed(0)
10. self.__distance = 5
11. self.__turn = 10
...
19. def fwd(self):
20. self.__myT.forward(self.__distance)
-Refer to the session in the accompanying Case Study 2. The Etch class uses the inheritance approach to working with a turtle.
1. import turtle
2.
3. class Etch:
4. def __init__(self):
5. self.__myT = turtle.Turtle()
6. self.__myScreen = turtle.Screen()
7. self.__myT.color('blue')
8. self.__myT.pensize(2)
9. self.__myT.speed(0)
10. self.__distance = 5
11. self.__turn = 10
...
19. def fwd(self):
20. self.__myT.forward(self.__distance)
-Refer to the session in the accompanying Case Study 2. The Etch class uses the inheritance approach to working with a turtle.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
19
The main difference between keyboard callbacks and mouse callbacks is that the function we write to accept mouse callbacks must accept two parameters.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
20
A Python-mangled name that is hidden outside its class starts with a @.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
21
Match each definition with its term.
-First-come first-served data structure.
A) Queue
B) Static variable
C) Decorator
D) Name mangling
-First-come first-served data structure.
A) Queue
B) Static variable
C) Decorator
D) Name mangling
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
22
Match each definition with its term.
-Variable that is shared by all instances of a class and is available to all the methods in the class.
A) Queue
B) Static variable
C) Decorator
D) Name mangling
-Variable that is shared by all instances of a class and is available to all the methods in the class.
A) Queue
B) Static variable
C) Decorator
D) Name mangling
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
23
Match each definition with its term.
-A special instruction to the Python interpreter that starts with @.
A) Queue
B) Static variable
C) Decorator
D) Name mangling
-A special instruction to the Python interpreter that starts with @.
A) Queue
B) Static variable
C) Decorator
D) Name mangling
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
24
Match each definition with its term.
-Hides any functions from outside of the class and starts with __.
A) Queue
B) Static variable
C) Decorator
D) Name mangling
-Hides any functions from outside of the class and starts with __.
A) Queue
B) Static variable
C) Decorator
D) Name mangling
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
25
Describe the overall structure of an event-driven program.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
26
Explain the difference between a single thread of execution and multithreading.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
27
What are some of the benefits of extending a class from Thread?
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
28
What callback registration functions does the turtle module provide?
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
29
Case Study 2:
1. import turtle
2.
3. class Etch:
4. def __init__(self):
5. self.__myT = turtle.Turtle()
6. self.__myScreen = turtle.Screen()
7. self.__myT.color('blue')
8. self.__myT.pensize(2)
9. self.__myT.speed(0)
10. self.__distance = 5
11. self.__turn = 10
...
19. def fwd(self):
20. self.__myT.forward(self.__distance)
-Refer to the session in the accompanying Case Study 2. Explain the composition approach used by the Etch class.
1. import turtle
2.
3. class Etch:
4. def __init__(self):
5. self.__myT = turtle.Turtle()
6. self.__myScreen = turtle.Screen()
7. self.__myT.color('blue')
8. self.__myT.pensize(2)
9. self.__myT.speed(0)
10. self.__distance = 5
11. self.__turn = 10
...
19. def fwd(self):
20. self.__myT.forward(self.__distance)
-Refer to the session in the accompanying Case Study 2. Explain the composition approach used by the Etch class.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
30
Explain how to set up a callback for mouse events on the turtle.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
31
Describe the __moveOneStep method of the AnimatedTurtle class presented in your text. What is the significance of the two underscores in the method name?
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
32
How would you detect when two turtles collide?
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
33
Describe the static method in the video game presented in your text. What is special about static methods?
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck