

Create background class class BackGround(GameSprite):ĩ. Create refresh screen refresh sprite group (including background picture, hero picture, bullet picture, zombie picture refresh and draw to the screen) def _update_sprites(self):ħ. If (, self.enemy_group, True, True):Įlif (, self.enemy_group, True, True):Įnemies = (self.hero, self.enemy_group, True)Įnemies2 = (self.eero, self.enemy_group, True)Ħ. Create a collision detection event (when a monster touches a character and exits the game) def _check_collide(self): Create a listening event (including the setting of the keyboard key to exit the window and launch bullets) def _handler_event(self):ĥ. Create a privatized spirit family def _create_sprites(self): Self.screen = _mode((SCREEN_RECT.width, SCREEN_RECT.height))
Create the PlaneGame class of the main game for initialization def _init_(self): Prepare the required picture material through ps processing.ġ. Define function (control player movement, bullet launch, monster random appearance)Ħ. Define the first and second heroes, bullets and monsters.ĥ. Define the PlaneGame class, Sprite class and background class of the main gameĤ.
PYGAME IMAGE CONVERT ALPHA CODE
Write the code and make the general windowģ.

If one of the two players touches the zombie, it will be judged as a failure of the game. The number of zombies defeated by players is shown on the top of the game Player 2 controls the up and down left and right movement of characters by ↑ ↓ ←→ key, and the space bar is to launch bullets. I know very little about how it works or it's performance though.Player 1 controls the up, down, left and right movement of the characters through the wa s d key, and the Q key is to fire bullets. The _polygon function caught my attention. If you're feeling adventurous, you can look into the gfxdraw libraries for pygame. Also, copying a surface may not necessarily preserve the alphas, but like the last point, I've found that it does when the alphas are already defined in the original image. The StackOverflow question that I referenced details the two ways to ensure a surface has alphas which are unaffected by surfarray operations. So, the last few steps are actually taking your original surface and coloring it the way you need: origSurface = ('images/blue_circle.png')Ĭolor_surface(coloredSurface, 120, 78, 240)Ī couple of notes about this code: the call to convert_alpha on the surface seems to be unnecessary if the image already has per-pixel alphas defined. It might be necessary if you try to do more complicated things with the surface. Which I also left out because I've found this to be a startlingly slow call in practice. The tutorial I posted also uses an additional instruction, something like: iarray = numpy.array(arr) In that function, I use 3d because it references the pixels into an array, rather than copy them, making it a faster operation. def color_surface(surface, red, green, blue): Some credit goes to the poster who answered this question.

Below is a code example of what you're looking to do. I did a bit of toying around to make sure that this works as you need it. I should still note that in practice, I've found it not to be fast enough for some applications, such as operating on large images, but if you're only operating on small particle sprites, you should be fine. Ordinarily, this would be a very slow operation, but surfarrays use numpy/numeric as their core, so the operation is quite fast.
PYGAME IMAGE CONVERT ALPHA HOW TO
A simple tutorial on how to get started with them can be found here.Įssentially what a surfarray does is directly modify the pixel values of pygame surfaces, and can operate on each of the R, G, and B channels for every pixel "simultaneously," which I put in quotes because I just mean you can change all the pixels with just one line of code.
