Deck 13: Video Games: Multithreading, Event Handlers, Static Variables

ملء الشاشة (f)
exit full mode
سؤال
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
استخدم زر المسافة أو
up arrow
down arrow
لقلب البطاقة.
سؤال
What is the main structure of an event-driven program?

A) Event loop
B) Queue
C) Callback function
D) Event processing
سؤال
A ____ is a data structure that works like a to-do list.

A) list
B) set
C) queue
D) dictionary
سؤال
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
سؤال
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')
سؤال
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
سؤال
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().
سؤال
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
سؤال
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
سؤال
The turtle module's ____ method is used to register callbacks for mouse events.

A) mouse
B) onmouse
C) mouseclick
D) onclick
سؤال
Before using the screen to respond to keyboard events, call the ____ method of Screen.

A) listen
B) onkey
C) main
D) start
سؤال
What is the name of the event loop for a turtle?

A) run
B) main
C) mainloop
D) eventloop
سؤال
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
سؤال
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(n) ____ variable is shared by all instances of the class.

A) instance
B) multithreaded
C) event
D) static
سؤال
A queue enforces a first-come first-served strategy for handling events.
سؤال
Typically, desktop applications are single-threaded.
سؤال
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.
سؤال
The main difference between keyboard callbacks and mouse callbacks is that the function we write to accept mouse callbacks must accept two parameters.
سؤال
A Python-mangled name that is hidden outside its class starts with a @.
سؤال
Match each definition with its term.
-First-come first-served data structure.

A) Queue
B) Static variable
C) Decorator
D) Name mangling
سؤال
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
سؤال
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
سؤال
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
سؤال
Describe the overall structure of an event-driven program.
سؤال
Explain the difference between a single thread of execution and multithreading.
سؤال
What are some of the benefits of extending a class from Thread?
سؤال
What callback registration functions does the turtle module provide?
سؤال
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.
سؤال
Explain how to set up a callback for mouse events on the turtle.
سؤال
Describe the __moveOneStep method of the AnimatedTurtle class presented in your text. What is the significance of the two underscores in the method name?
سؤال
How would you detect when two turtles collide?
سؤال
Describe the static method in the video game presented in your text. What is special about static methods?
فتح الحزمة
قم بالتسجيل لفتح البطاقات في هذه المجموعة!
Unlock Deck
Unlock Deck
1/33
auto play flashcards
العب
simple tutorial
ملء الشاشة (f)
exit full mode
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
D
2
What is the main structure of an event-driven program?

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
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
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')
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
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().
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
12
What is the name of the event loop for a turtle?

A) run
B) main
C) mainloop
D) eventloop
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
15
A(n) ____ variable is shared by all instances of the class.

A) instance
B) multithreaded
C) event
D) static
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
16
A queue enforces a first-come first-served strategy for handling events.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
17
Typically, desktop applications are single-threaded.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
20
A Python-mangled name that is hidden outside its class starts with a @.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
25
Describe the overall structure of an event-driven program.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
26
Explain the difference between a single thread of execution and multithreading.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
27
What are some of the benefits of extending a class from Thread?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
28
What callback registration functions does the turtle module provide?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
30
Explain how to set up a callback for mouse events on the turtle.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
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?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
32
How would you detect when two turtles collide?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
33
Describe the static method in the video game presented in your text. What is special about static methods?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
locked card icon
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.