Moving the Paddles - Keyboard Input - Demo 04

Objective

Add movement to the paddles using keyboard input.

Demo 01

Demo 04

How to Execute

Load src/modelviewprojection/demo04.py in Spyder and hit the play button.

Move the Paddles using the Keyboard

Keyboard Input

Action

w

Move Left Paddle Up

s

Move Left Paddle Down

k

Move Right Paddle Down

i

Move Right Paddle Up

Paddles which don’t move are quite boring. Let’s make them move up or down by getting keyboard input.

And while we are at it, let’s go ahead and create data structures for a Vector, and for the collection of vertices that make up a Paddle.

Code

Data Structures

Here we use dataclasses, which automatically creates on the class a constructor, accessor methods, and pretty-printer. This saves a lot of boiler plate code.

src/modelviewprojection/demo04.py
83@dataclasses.dataclass
84class Vector:
85    x: float
86    y: float
src/modelviewprojection/demo04.py
91@dataclasses.dataclass
92class Paddle:
93    vertices: list[Vector]
94    color: colorutils.Color3

Although Python is a dynamically-typed language, we can add type information as helpful hints to the reader, and for use with static type-checking tools for Python, such as mypy.

src/modelviewprojection/demo04.py
 99paddle1 = Paddle(
100    vertices=[
101        Vector(x=-1.0, y=-0.3),
102        Vector(x=-0.8, y=-0.3),
103        Vector(x=-0.8, y=0.3),
104        Vector(x=-1.0, y=0.3),
105    ],
106    color=colorutils.Color3(r=0.578123, g=0.0, b=1.0),
107)
108
109paddle2 = Paddle(
110    vertices=[
111        Vector(0.8, -0.3),
112        Vector(1.0, -0.3),
113        Vector(1.0, 0.3),
114        Vector(0.8, 0.3),
115    ],
116    color=colorutils.Color3(r=1.0, g=1.0, b=0.0),
117)
  • Create two instances of a Paddle.

I make heavy use of keyword arguments in Python.

Notice that I am nesting the constructors. I could have instead have written the construction of paddle1 like this:

x = -0.8
y = 0.3
vector_a = Vector(x, y)
x = -1.0
y = 0.3
vector_b = Vector(x, y)
x = -1.0
y = -0.3
vector_c = Vector(x, y)
x = -0.8
y = -0.3
vector_d = Vector(x, y)
vector_list = list(vector_a, vector_b, vector_c, vector_d)
r = 0.57
g = 0.0
b = 1.0
paddle1 = Paddle(vector_list, r, g, b)

But then I would have many local variables, some of whose values change frequently over time, and most of which are single use variables. By nesting the constructors as the author has done above, the author minimizes those issues at the expense of requiring a degree on non-linear reading of the code, which gets easy with practice.

Query User Input and Use It To Animate

\[\vec{x'} = \vec{t}(\vec{x}; \vec{c}) = \vec{x} + \vec{c}\]
\[\begin{split}\begin{bmatrix} x_{1} \\ x_{2} \end{bmatrix} = \vec{t}( \begin{bmatrix} x_{1} \\ x_{2} \end{bmatrix}; \begin{bmatrix} c_{1} \\ c_{2} \end{bmatrix} ) = \begin{bmatrix} x_{1} \\ x_{2} \end{bmatrix} + \begin{bmatrix} {c}_{1} \\ {c}_{2} \end{bmatrix}\end{split}\]
src/modelviewprojection/demo04.py
122def handle_movement_of_paddles() -> None:
123    global paddle1, paddle2
124    if glfw.get_key(window, glfw.KEY_S) == glfw.PRESS:
125        for v in paddle1.vertices:
126            v.x += 0.0
127            v.y -= 0.1
128    if glfw.get_key(window, glfw.KEY_W) == glfw.PRESS:
129        for v in paddle1.vertices:
130            v.x += 0.0
131            v.y += 0.1
132    if glfw.get_key(window, glfw.KEY_K) == glfw.PRESS:
133        for v in paddle2.vertices:
134            v.x += 0.0
135            v.y -= 0.1
136    if glfw.get_key(window, glfw.KEY_I) == glfw.PRESS:
137        for v in paddle2.vertices:
138            v.x += 0.0
139            v.y += 0.1
140
141
  • If the user presses ‘s’ this frame, subtract 0.1 from the y component of each of the vertices in the paddle. If the key continues to be held down over time, this value will continue to decrease.

  • If the user presses ‘w’ this frame, add 0.1 more to the y component of each of the vertices in the paddle

  • If the user presses ‘k’ this frame, subtract .1.

  • If the user presses ‘i’ this frame, add .1 more.

  • when writing to global variables within a nested scope, you need to declare their scope as global at the top of the nested scope. (technically it is not a global variable, it is local to the current python module, but the point remains)

The Event Loop

Monitors can have variable frame-rates, and in order to ensure that movement is consistent across different monitors, we choose to only flush the screen at 60 hertz (frames per second).

src/modelviewprojection/demo04.py
145TARGET_FRAMERATE: int = 60
146
147time_at_beginning_of_previous_frame: float = glfw.get_time()
src/modelviewprojection/demo04.py
151while not glfw.window_should_close(window):
152    while (
153        glfw.get_time()
154        < time_at_beginning_of_previous_frame + 1.0 / TARGET_FRAMERATE
155    ):
156        pass
157
158    time_at_beginning_of_previous_frame = glfw.get_time()
src/modelviewprojection/demo04.py
162    glfw.poll_events()
163
164    width, height = glfw.get_framebuffer_size(window)
165    GL.glViewport(0, 0, width, height)
166    GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
src/modelviewprojection/demo04.py
170    draw_in_square_viewport()
src/modelviewprojection/demo04.py
174    handle_movement_of_paddles()
  • We’re still near the beginning of the event loop, and we haven’t drawn the paddles yet. So we call the function to query the user input, which will also modify the vertices’ values if there was input.

src/modelviewprojection/demo04.py
178    GL.glColor3f(*iter(paddle1.color))
179
180    GL.glBegin(GL.GL_QUADS)
181    for vector in paddle1.vertices:
182        GL.glVertex2f(vector.x, vector.y)
183    GL.glEnd()
  • While rendering, we now loop over the vertices of the paddle. The paddles may be displaced from their original position that was hard-coded, as the callback may have updated the values based off of the user input.

  • When glVertex is now called, we are not directly passing numbers into it, but instead we are getting the numbers from the data structures of Paddle and its associated vertices.

Adding input offset

Adding input offset to Paddle 1

src/modelviewprojection/demo04.py
187    GL.glColor3f(*iter(paddle2.color))
188
189    GL.glBegin(GL.GL_QUADS)
190    for vector in paddle2.vertices:
191        GL.glVertex2f(vector.x, vector.y)
192    GL.glEnd()
Adding input offset to Paddle 1

Adding input offset to Paddle 2

src/modelviewprojection/demo04.py
196    glfw.swap_buffers(window)