Introduction:

Hello, hello! I’m Mumuzi.

A new day begins again

Yes, today is a big project,It’s time for the daily update. What are you looking forward to?

Thought for a long time, this game is probably you have not played, so direct point not to start the popular science story. All the imaginary girlfriends in recent stories have disappeared.

Let’s go straight to making a Python Island of Mary adventures game

The body of the

(1) Environmental installation:

Python version: Python 3.6

Development tool: PyCharm. Built-in Python modules: IterTools, random. Third party module: PyGame.

Note: When using a third-party module, you first need to install the module using the PIP install command. You can execute the following command in a command window:

pip install pygame
Copy the code

(2) Picture material: self-made.

​​

(3) Almost all game software on mobile phones have their own background music, of course adventure Island is no exception.

​​

Attached code: the game background music can be suspended can also play, music more fun game.

​​​​​​

Class Music_Button(): is_open = True self.open_img = pygame.image.load('image/btn_open.png').convert_alpha() self.close_img = pygame.image.load('image/btn_close.png').convert_alpha() self.bg_music = pygame.mixer.Sound('audio/bg_music.wav') # Def is_select(self): # get mouse, Point_x, point_y = pygame.mouse. Get_pos () w, In_x = point_x > 20 and point_x < 20 + win_y = point_y > 20 and  point_y < 20 + h return in_x and in_yCopy the code

(4) For adventure islands, the player will always move forward so define a moving map.

Class MyMap(): def __init__(self, x, y): Self.x = x self.y = y def map_rolling(self): self.x = x self.y = y def map_rolling(self): If self.x < -790: # < -790, the map is completely moved. Self. x = 800 Def map_update(self): screen.blit (self.bg, (self.x, self.y))Copy the code

(5) Define the classes of adventure island players, jumping up, avoiding obstacles, etc.

​​

class Marie(): def __init__(self): # initialization little Mary rectangular self. The rect = pygame. The rect (0, 0, 0, 0) self.jumpState = False # jumpState self.jumpHeight = 130 # jumpHeight self.lowest_y = 140 # lowest coordinate self.jumpValue = 0 # jump increment # Little Mary dynamic figure index self. Self marieIndex = 0. MarieIndexGen = cycle ([0, 1, Self.adventure_img = (pygame.image.load("image/adventure1.png").convert_alpha(), pygame.image.load("image/adventure2.png").convert_alpha(), pygame.image.load("image/adventure3.png").convert_alpha(), ) self. Jump_audio = pygame. Mixer. Sound (' audio/jump. Wav) # jump Sound self. The rect. Size = self. Adventure_img [0]. Get_size () the self. X = 50; Self. y = self.lowest_y; Def jump(self): self. JumpState = True # def move(self): self. If self. JumpState: # if self. Rect. y >= self. # if you stand on the ground self.jumpValue = 5 # Move up by 5 pixels if self.rect.y <= self.lowest_y - self.jumpheight: # Little Mary reaches the top and falls back self.jumpValue = 5 # Move down by 5 pixels self.rect.y += self.jumpValue # change Mary's y coordinate through a loop if self.rect.y >= Def draw_marie(self): self.lowest_y: self.jumpState = False def draw_marie(self): Blit (self. Adventure_img [marieIndex], (self.x, self. self.rect.y))Copy the code

(6) Set obstacles to appear randomly.

Import random # random number # Obstacle(): Score = 1 # move = 5 # obstacle_y = 150 # obstacle_y def __init__(self): # rectangular self initialization obstacles. The rect = pygame. The rect (0, 0, 0, Self.missile = pygame.image.load("image/missile.png").convert_alpha() self.pipe = Pygame.image.load ("image/pipe.png").convert_alpha() # (pygame.image.load('image/0.png').convert_alpha(), pygame.image.load('image/1.png').convert_alpha(), pygame.image.load('image/2.png').convert_alpha(), pygame.image.load('image/3.png').convert_alpha(), pygame.image.load('image/4.png').convert_alpha(), pygame.image.load('image/5.png').convert_alpha(), pygame.image.load('image/6.png').convert_alpha(), pygame.image.load('image/7.png').convert_alpha(), pygame.image.load('image/8.png').convert_alpha(), Self.score_audio = Pygame.mixer.sound ('audio/score.wav') # bonus # 0 and 1 random number r = random. Randint (0, 1) if r == 0: Self. Image = self. Missile # Display missile obstacles if random number is 0 instead display pipe self.image = self.missile # Display missile obstacles self.move = 15 # Speed up self.obstACLE_Y = 100 # Missile coordinates in the sky else: Self.rect. size = self.image.get_size() # self.width, Self. x = 800 self.y = self.obstacle_y self.rect.center = (self.x, Self.y) def obstacle_move(self): self.rect.x -= self.move def obstacle(self): SCREEN.blit(self.image, (self.rect.x, self.rect.y))Copy the code

(7) Avoid obstacles, one at a time. Plot the score.

​​

Def getScore(self): self.score = self.score; If TMP == 1: self.score_audio. Play () # self.score = 0; return tmp; Def showScore(self, score): Self. scoreDigits = [int(x) for x in list(STR (score))] totalWidth = 0 self.scoreDigits = [int(x) for x in list(STR (score))] totalWidth = 0 self.scoreDigits: TotalWidth += self.numbers[digit].get_width() # offset = (SCREENWIDTH - (totalWidth+30)) for digit in self.scoreDigits: Blit (self. Numbers [digit], (Xoffset, # self.numbers[digit].get_width() # self.numbers[digit].get_width()Copy the code

(8) The end of the game hit the obstacle.

​​

Def game_over(): Bump_audio = pygame.mixer.sound ('audio/bump.wav') # bump bump_audio. Play () # Bump screen_w = Pygame.display.info ().current_w screen_h = pygame.display.info ().current_h # Loads the end of the game picture over_img = Pygame.image.load ('image/gameover. PNG ').convert_alpha() # Draw the game ending image in the middle of the form screen.blit (over_img, ((screen_w - over_img.get_width()) / 2, (screen_h - over_img.get_height()) / 2))Copy the code

Effect:

​​​​​

conclusion

Hey hey! Follow xiaobian together to adventure ~ production is not easy, remember one key three even oh!!

If you need this article complete code + image material. Python novice installation packages, free activation codes, and more Python information

Source base: [private letter xiaobian 06] can be free oh!!

​​