Use Pygame to create a PC version of WeChat airplane hitting game

Source: Internet
Author: User

I read an article a while ago:

Download, or extract it as myself. Then, when writing a game, you can access the game. You can find the answer to all the problems you encounter. After the resources are ready, we can start to write the game.

 

     pygame.locals  *  sys   SCREEN_WIDTH = 480 SCREEN_HEIGHT = 800    screen = pygame.display.set_caption(   background = pygame.image.load(                            event           event.type ==              exit()

Let's analyze this Code:

  • Clear Screen
  • Draw background
  • Update Screen
  • Handle game exit events

The effect after running is as follows:

 

We opened resources/image/and found that all planes are in a picture of shoot.png. The elements shown on the background (including planes and bullets) are a surface in Pygame. In this case, you can use the subsurface method provided by pygame to load a large image first, call the subsurface method to select a small part to generate a new surface. The location of this thumbnail in the big image is in the shoot. pack file in the image folder.

What we need to do is to load the above Code into the background image and the airplane image, then generate the surbsurface of the airplane and initialize the starting position of the airplane:

  plane_img = pygame.image.load(   player_rect = pygame.Rect(0, 99, 102, 126 player = player_pos = [200, 600]

Then, draw the background in the main loop and insert the code for drawing the plane:

  screen.blit(player, player_pos)

After running, you will be able to see the players who operate on the planes:

 

When an airplane moves, the game responds to Keyboard Events in the main loop. In the above Code, we know screen. the Code blit (player, player_pos) draws an airplane. This player_pos is the position of the plane drawn in each loop. In response to a keyboard event, we only need to change this position to move the plane. Add the code to respond to Keyboard Events in the main loop:

  key_pressed =      player_pos[1] -= 3      player_pos[1] += 3      player_pos[0] -= 3      player_pos[0] += 3

Every time a direction key is pressed, we only need to change the coordinates of an airplane's position to let the plane move ~

 

In this way, we know the basic principle of creating a 2D game: after the game enters the main loop, each cycle draws an image at the corresponding position, and the animation effect can be displayed when the position or image is changed; then, the collision is detected by judging whether the rectangular areas of the two images are intersecting. Simple?

The subsequent steps include displaying bullets, randomly generating enemy planes, detecting collisions, determining the game's end, scoring, and so on. I want to introduce this. Should you be able to complete the following tasks on your own?

Of course, players, bullets, and enemy planes can all be written into a class that inherits the sprite class of pygame to implement some animation effects and detect collisions, making it easier to use. Let's take a look at the class code I wrote:

Pygame. sprite. sprite. self. image = self. rect = self. rect. midbottom = self. speed = 10 self. rect. top-= pygame. sprite. sprite. self. image = [] I self. rect = player_rect [0] self. rect. topleft = init_pos self. speed = 8 self. bullets = pygame. sprite. group () self. img_index = 0 self. is_hit = False bullet = self. rect. top <= self. rect. top = self. rect. top-= self. rect. top> = SCREEN_HEIGHT-self. rect. top = SCREEN_HEIGHT-self. rect. top + = self. rect. left <= self. rect. left = self. rect. left-= self. rect. left> = SCREEN_WIDTH-self. rect. left = SCREEN_WIDTH-self. rect. left + = pygame. sprite. sprite. self. image = self. rect = self. rect. topleft = self. down_imgs = self. speed = 2 self. down_index = self. rect. top + = self. speedView Code


Currently, I have basically implemented the following functions: players moving and launching bullets, randomly generating small planes, hitting and exploding small planes, players being destroyed, background music and sound effects, and the game ending and displaying scores, it is already a simple and playable game. The entire game implements less than 300 lines of code. It can also be seen that the Python code is concise. I shared the code to GitHub. If you are interested, you can download the code and continue generating medium and large aircraft, changing the aircraft speed, dual-row bullets, bombs, and other functions. GitHub link :.

Games:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.