Chapter 18. Games
Preview
Games cover a lot of ground, from simple text to multiplayer 3D extravaganzas. This chapter will demonstrate a simple game and how the web endpoint can interact with the user across multiple steps. This process is different from the familiar one-shot request-response web endpoints that you’ve seen so far.
Python Game Packages
If you really want to get into Python for games, here are some useful tools:
-
Text:
-
Graphic:
But I’m not going to use any of these in this chapter. The example code can get so large and complex that it overshadows the goal of this book: creating websites—APIs and traditional content—with FastAPI, as simply as possible.
Splitting Game Logic
There are so many ways to write a game. Who does what, and who keeps what where—the client or the server? The web is stateless, so each time the client calls the server, the server is a total amnesiac and swears it’s never seen this client before. So we need to keep state somewhere: data retained across game steps to thread them all together.
We could write a game completely in JavaScript on the client side, and keep all the state there. If you know JavaScript well, that’s a good solution, but if you don’t (a possibility, because you’re reading a Python book), let’s give Python something to do too.
At the other extreme, we could write a server-heavy application: generate some distinct id for this particular ...
Get FastAPI now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.