Lovely people, it's been a minute. I hope everyone has been good, alive, and well.

From my introductory post, I spoke a lot about how I build games with Pygame, and here's a little "gamedev" piece with Pygame.

打开网易新闻 查看精彩图片

Let's start with the finished product. I know it's not exactly Need for Speed, but let's go through my building procedure.

The first thing to do with Pygame is to make your game window. Everything happens inside it, so it makes sense. But you have to think about the kind of game you want. I realized I needed a portrait-style window for my racer, not landscape.

Next, I had to put things in the window. Can't have a blank game, right? Unless you're playing mind games lol. The environment comes first: the road, the road lines, all that.

My initial plan was simple: find a color that looks like a road and add some race lines. I tried to cheat by using the pipe character (|) to fake the lines, but it looked bad, so I changed my mind.

I ended up drawing rectangles to create lane divisions. If this is your first Pygame trip, don't be confused — a lot of stuff under the hood is just shapes. You'd be amazed at what rectangles can do.

Here's what I mean. Define a rectangle like an object:

rect = pygame.Rect(x, y, width, height)

Then fill it with a color and blit it to the screen. That's the core. I used three rectangles for the road boundaries and lanes, and the game already looked like a racer.

Now, this is just the beginning — you still need cars, movement, collisions, and more. But the foundation is as simple as drawing shapes. So if you've been putting off Pygame, grab some rectangles and start coding.