Don’t say much, first above (see this familiar not, I am also on the network to find tutorials and resources, to learn to write this online game step by step, here I disassemble the project a few steps, the next step by step) :

First prepare resources, the network can also be downloaded, I directly use the tutorial resources here, why (really good, find their own miserable)

And then not to say, the code (this implementation is to put the background picture + aircraft with who keyboard operation movement) main.py, annotations are basically added, not clear can try baidu about the corresponding method

Import Pygame Import sys import Traceback from Pygame.locals import * from random import * import myplane # Initialize Pygame.init () # set window size bg_size = width, height = 400, Screen = Pygame.display. Set_mode (bg_size) # Set window pygame.display. Set_caption (" Plane fight ") # Loading background image, normal image display effect with or without convert is the same, but with convert can convert format, Background = pygame.image.load("images/background.png").convert() def main(): Clock = pygame.time.clock () # Create player plane me = myplane. myplane (bg_size) # create player life_num = 3 # Paused = False # Control player plane picture switch, display tututu effect switch_image = True # Switch delay = 100 running = True while running: For event in Pygame.event.get (): Key_pressed = Pygame.key.get_pressed () if key_pressed[K_w] or key_pressed[K_UP]: me.moveUp() if key_pressed[K_s] or key_pressed[K_DOWN]: me.moveDown() if key_pressed[K_a] or key_pressed[K_LEFT]: me.moveLeft() if key_pressed[K_d] or key_pressed[K_RIGHT]: Me. MoveRight () # Draw the background image above the screen, Screen. blit(background, (0, 0)) # if paused == False and life_num > 0: If me. Active: # Draw the player plane on the screen,switch_image to whether to switch the image if switch_image: Screen.blit (me.image2, me.rect) # indicates that the plane was hit, activating the explosion event else: Print (" aircraft damage ") delay -= 1 if delay == 0: delay = 100 if delay % 5 == 0: Blit (me.image1, me.rect) # Update the entire Surface object to be displayed on the screen. Pygame.display.flip () # Specify loop frequency through clock object, loop 60 times per second # Frame rate refers to program drawing on screen mountain per second clock.tick(60) if __name__ == "__main__": Except SystemExit: print(" except SystemExit ") Traceback.print_exc () pygame.quit() traceback.print_exc() pygame.quit()Copy the code

Myplane. py: Controls the player’s plane, including various attributes of the player’s plane, up, down, left, right movement of the plane, and rebirth of the plane

Import PyGame # Player plane class, PyGame. Sprite module contains a class named Sprite, which is a native of PyGame. class MyPlane(pygame.sprite.Sprite): def __init__(self, bg_size): # convert_alpha() Changes the pixel format of the image, Include alpha for each pixel, equivalent to making the image background transparent self.image1 = pygame.image.load('images/me1.png').convert_alpha() self.image2 = Pygame.image.load ('images/me2.png').convert_alpha() # Plane destroyed image save in digital form self.destory_image = [] self.destory_image.extend([] self.destory_image.extend([]  pygame.image.load('images/me_destroy_1.png').convert_alpha(), pygame.image.load('images/me_destroy_2.png').convert_alpha(), pygame.image.load('images/me_destroy_3.png').convert_alpha(), Load ('images/ me_Destroy_4.png ').convert_alpha()]) # define screen width self.width = bg_size[0] self.height = Bg_size [1] # get_rect() is a method for processing rectangular images, Self.rect = self.image1.get_rect() # The initial position of the plane,// is divisible, Left = (self.width - self.rect.width)//2 self.rect.top = self.height - self.rect.height MyPlaneSpeed = 10 self. Active = True self. Invincible = True Ignores the white background of the image and returns a Mask # from the specified Surface object for quick perfect collision detection. Mask can be accurate to 1 pixel level judgment. Set the transparent part of the Surface object to 1 and the opaque part to 0. Self. mask = pygame.mask.from_surface(self.image1) # def moveUp(self): If self.rect.top > 0: self.rect.top -= self.myplanespeed # else: Self.rect. top = 0 # def moveDown(self): If self.rect.bottom < self.height-60: Self. Rect. bottom += self. MyPlaneSpeed else: Def moveLeft(self): if self. Rect. left > 0: Self.rect. left -= self. MyPlaneSpeed else: self.rect.left = 0 # Def moveRight(self): if self.rect.right < self.width: self.rect.right += self.myPlaneSpeed else: Self.rect. right = self.width # def reset(self): Self. Active = True # Respawns invincible = True # Respawns invincible = True # Left = (self.width - self.rect.width) // 2 self.rect.top = self.height - self.rect.height - 60Copy the code

If you are not clear about one of the steps, you can also mention it. I am also a small chicken, which belongs to the exploratory stage. If you have any questions, please click the spray and run main.py, then you can control the player’s plane to move (various other functions will be added later slowly).