In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to add a player to your Python game". The content is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to add a player to your Python game".
In Pygame, the icon or avatar that the player controls is called the goblin sprite. If you don't have any images available for goblins yet, you can use Krita or Inkscape to create some of your own images. If you lack confidence in your artistic skills, you can also search for ready-made images on OpenClipArt.org or OpenGameArt.org. If you haven't created an images folder as described in the previous article, you need to create it in your Python project directory. Put all the pictures you want to use in the game in the images folder.
To make your game really exciting, you should use a dynamic leprechaun picture for your hero. This means that you need to draw more material, and they have to be very different. The most common animation is the walking cycle, through a series of images to make your leprechaun look like walking. The fastest and roughest version of the walking cycle requires four images.
Note: the code examples in this article are compatible with both static and dynamic player goblins.
Name your player goblin hero.png. If you are creating a dynamic leprechaun, you need to add a number to the name, starting with hero1.png.
Create a Python class
In Python, when you are creating an object that you want to display on the screen, you need to create a class.
Near the top of your Python script, add the following code to create a player. In the following code example, the first three lines are already in the Python script you are working on:
Import pygameimport sysimport os # is the new code class Player (pygame.sprite.Sprite):''generate a player' 'def _ _ init__ (self): pygame.sprite.Sprite.__init__ (self) self.images = [] img = pygame.image.load (os.path.join (' images') 'hero.png'). Convert () self.images.append (img) self.image = self.images [0] self.rect = self.image.get_rect ()
If your controllable character has a walking loop, save the corresponding image as a separate file from hero1.png to hero4.png in the images folder.
Use a loop to tell Python to traverse each file.
'' object 'class Player (pygame.sprite.Sprite):' 'generate a player' 'def _ _ init__ (self): pygame.sprite.Sprite.__init__ (self) self.images = [] for i in range (1Magne5): img = pygame.image.load (os.path.join (' images') 'hero' + str (I) +' .png'). Convert () self.images.append (img) self.image = self.images [0] self.rect = self.image.get_rect () brings players into the game world
Now that you have created a Player class, you need to use it to generate a player leprechaun in your game world. If you don't call the Player class, it will never work, and there will be no players. You can verify it by running your game right away. The game will run as seen at the end of the last article, with a clear result: an empty game world.
In order to bring a player leprechaun to your game world, you must generate a leprechaun by calling the Player class and add it to the goblin group of Pygame. In the following code example, the first three lines are already existing, and you need to add code after that:
World = pygame.display.set_mode ([worldx,worldy]) backdrop = pygame.image.load (os.path.join ('images','stage.png')). Convert () backdropbox = screen.get_rect () # the following is the new code player = Player () # generate player player.rect.x = 0 # Mobile x coordinates player.rect.y = 0 # Mobile y coordinates player_list = pygame.sprite.Group () player_list.add (player)
Try to start your game and see what happens. High-energy warning: it won't work as you expected, and when you start your project, player goblins don't show up. In fact, it was generated, but only for a millisecond. How do you fix something that has only appeared for a millisecond? You may recall that in the last article, you need to add something to the main loop. To keep the player alive for more than a millisecond, you need to tell Python to draw once in each loop.
Change the statement at the bottom of your loop as follows:
World.blit (backdrop, backdropbox) player_list.draw (screen) # drawing player pygame.display.flip () clock.tick (fps)
Now start your game and your player appears!
Set alpha channel
Depending on how you create your player leprechaun, there may be a color block around it. What you see is the space that the alpha channel should occupy. It used to be an invisible "color", but Python doesn't know how to make it invisible yet. So what you see is the space around the boundary area around the leprechaun (or "hit box" in modern gaming terminology).
You can tell Python which color to make invisible by setting an alpha channel and a RGB value. If you don't know the RGB value of the image you are using the alpha channel, you can use Krita or Inkscape to open it and use a unique color, such as # 00ff00 (almost "green screen green") to fill the white space around the image. Write down the hexadecimal value of the color (in this case, # 00ff00, green screen green) and use it as an alpha channel for your Python script.
To use the alpha channel, you need to add the following two lines to your goblin generation code. Code like * lines already exists in your script, you only need to add two more lines:
Img = pygame.image.load (os.path.join ('images','hero' + str (I) +' .png'). Convert () img.convert_alpha () # optimize alpha img.set_colorkey (ALPHA) # set alpha
Unless you tell it, Python doesn't know which color to use as the alpha channel. Add some color definitions in the settings-related area of your code. Add the following variable definition anywhere in the relevant area of your settings:
ALPHA = (0,255,0)
In the sample code above, we use 0 00ff00 255, which represents the same value in RGB as the value represented in hexadecimal. You can get all these color values through a good graphics application, such as GIMP, Krita, or Inkscape. Alternatively, you can use an excellent system-level color picker, such as KColorChooser, to detect colors.
If your graphics application renders your leprechaun background to other values, you can adjust the value of the ALPHA variable as needed. No matter how much you set alpha, it will be "invisible". The RGB color value is very strict, so if you need to set the alpha to 000, but you want to use 000 for the black lines in your image, you only need to set the color of the center line of the image to 111. In this way, (the black line in the image) is close enough to black, but no one can tell the difference except the computer.
Run your game to see the results.
The above is all the content of the article "how to add a player to your Python game". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.