Advertisement

Python + Pygame Threading Sample

Started by October 18, 2019 06:19 AM
3 comments, last by Zakwayda 5 years, 1 month ago

Hello, I am a Japanese, a novice of the python programming.
I am currently trying to program games using pygame.
The progress of the game production can be seen on the following blog and youtube channel.

https://ko-gaku-jiji.hatenablog.jp/
(sorry all of my page is written by Japanese)

I'm trying to make a Tetris-like game right now, but sometimes I'm worried.
In the process for the Tetris game, such as the process of line erasing, I feel that I can write a program more simply by using multithreading.
However, I couldn't find a suitable sample for multithreading with pygame.
If you know, please introduce me a web page with a simple and easy-to-understand sample.

Hey Gomta777,

You may already by aware, but in case not, it bears repeating. When thinking of multithreading in python, tread carefully. Python has a global lock that it uses for reference counting objects. For compute bound workloads/(local, non-IO) this will effectively ensure you never see the benefits of multithreaded performance as the GIL will prevent the lock being shared. What you want for non IO workloads is multiprocessing. Turns out the best way around the GIL is more of them.

https://www.toptal.com/python/beginners-guide-to-concurrency-and-parallelism-in-python

The above example walks through multithreading, and multiprocessing in Python.

A few other thoughts: 

* Also, give your code a few once over with CProfile. You may not even need multithreading/multiprocessing. 

* Other speedups with pygame can be had with being sure not to update the entire screen each iteration of the gameloop, and, instead updating only what sprites has moved, i.e. become dirty.

 

Advertisement

Multi-threading is hardly ever simple.

Maybe you use the wrong way of storage? If you use horizontal rows for display, erasing is as simple as removing one of the rows, and moving the remainder down.


That is, just because the game looks vertically oriented doesn't mean you have to implement it that way.

There are often other options that create the same effect but are easier to work with in an implementation.

11 hours ago, Gomta777 said:

In the process for the Tetris game, such as the process of line erasing, I feel that I can write a program more simply by using multithreading.

This echoes some things already said, but it seems unlikely to me that multithreading would be necessary or advantageous for something like Tetris, or that a multithreaded implementation would be simpler than a single-threaded implementation. Can you elaborate on how you think multithreading would help here?

This topic is closed to new replies.

Advertisement