Introduction:

Today is Wednesday, another hard day to move bricks ~

Mu Mu zi here to update the game series – adventure island 🪂.

Simple, super Mario bros. type of interface but not as polished

Gu Muzi acridine: 🚠 fast, pay attention to me, play games together oh ~

Fan: Report! Recommended action adventure Python mini-games 🚟

The body of the

This article is the development of a small game, HMM, old rules, or everyone familiar with Pygame ha ~

One, under preparation

1) Picture material preparation (modifiable)

2) Music material preparation (modifiable)

It’s more exciting to write a game with a background

Two, environmental installation

1) Development environment requirements

The software development and operating environment of the system are as follows:

Python version: Python 3.

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 run the following command in the Python command window:

PIP install pygame with mirror image source faster PIP install -i https://pypi.douban.com/simple/ pygameCopy the code

Start typing code

1) Import the module setting window

1.1 Game window size can be adjusted to modify ha.

From Pygame. locals import * # Import a constant from PyGame import sys # Import SCREENWIDTH = 822 # SCREENHEIGHT = 199 # FPS = 30 # Update screen timeCopy the code

1.2 Game Title

FPSCLOCK = pygame.time.clock () SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT)) Facilitate our interaction with the program. Pygame.display. Set_caption (' Mary's Adventure island -- source base: #806965976#'Copy the code

2) Define a moving map class

Moving maps were created because running requires players to move.

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

3) Set the background music button

Class Music_Button(): is_open = True def __init__(self): 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)Define a Mary class

Little Mary is the player’s avatar, defining the player’s various states: move, jump, 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. Y) import random # random numberCopy the code

5)Create obstacles and score Settings

Players gain one point for running and avoiding an obstacle.

Obstacle(): score = 1 # move = 5 # Obstacle_y = 150 # Obstacle y coordinates 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): Self.score (self.score) def getScore(self): self.score = self.score; 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

6)Set crash to end the game

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

Iv. Game effect display

1) Video effect display —

The video will not be uploaded

2) Screenshot effect display —

conclusion

All right, a simple Mary adventure island small game ends here oh, you want action adventure games for you — “pop!! Ugly rejected”

In fact, I also feel pretty ugly this game, 2333~ that don’t, next I fill a repair such as contra how?

​

💖 free source code base —

Private letter xiaobian 06 or click on this line of blue font can be free ha!

Your support is my biggest motivation!! Mua welcomes you to read the previous article

🎉 Previous games popular articles recommended:

Item 1.0 Super Mario

Programmer homemade Game: Super Mario Bros 100% Real, will make you cry

Item 1.1 Mine Clearance

Pygame: It is said that this is the most difficult minesweeper game in history, no one, you feel……

Project 1.2 Contra

Pygame combat: After many years, “Contra” pixel style is more than classic and feelings @ all members

Project 2.0 Networking, man-machine integrated backgammon game

You want to play gobang? Do you believe I can’t beat you in a few steps?

🎄 Article summary —

The article summary | Python 1.0-2021 | has continuously updated, direct look at this article is enough

More content + source code in the article summary oh!! Welcome to read ~)

​