In the previous code, we have implemented a basic framework for building FlappyBird with Pygame. The final result is as follows:

Now let’s add further Bird and Pipe objects to make the game look more complete.

Flappy_Bird_Env init = Flappy_Bird_Env init

# set pipeline images related to the self. PipeVelX = 4 # pipe movement speed self. PIPEGAPSIZE = 100 # pipe spacing between up and down the self. PIPE_WIDTH = self.IMAGES['pipe'][0].get_width() self.PIPE_HEIGHT = self.IMAGES['pipe'][0].get_height()Copy the code

(2) Reset to obtain the location information of a random pipe:

def getRandomPipe(self): GapYs = [20, 30, 40, 50, 60, 70, 80, 90] # index = random. Randint (0, 0, 0) Len (gapYs) -1) gapY = gapYs[index] gapY += int(self.BASEY * 0.2) pipeX = SCREENWIDTH + 10 return [{'x': pipeX, 'y': {'x': pipeX, 'y': gapY + self. Pipeapsize}, #Copy the code

We continue writing the initial information for the pipe in the reset function, which is called by Flappy_Bird_Env’s init:

def reset(self): # Pipe newPipe1 = self.getrandomPipe () # Pipe newPipe2 = self.getrandomPipe () # pipe self.upperPipe = [{'x': {'x': SCREENWIDTH + (SCREENWIDTH / 2), 'y': SCREENWIDTH + (SCREENWIDTH / 2), 'y': NewPipe2 [0] [' y ']}, # second pipeline] # screen display on the screen of the pipeline under the list of the self. The lowerPipes = [{' x ': SCREENWIDTH,' y ': newPipe1 [1] [' y']}, {' x ': SCREENWIDTH + (SCREENWIDTH / 2), 'y': newPipe2[1]['y']}, ]Copy the code

(3) Frame rendering first calculates the position of each pipe:

For uPipe, lPipe in zip(self.upperPipes, self.lowerPipes): Self. pipeVelX lPipe['x'] += self.pipeVelX lPipe['x'] += self.pipeVelX self.upperPipes[0]['x'] < 5: newPipe = self.getRandomPipe() self.upperPipes.append(newPipe[0]) self.lowerPipes.append(newPipe[1]) # If self.pipe_width: self.upperpipes [0]['x'] < -self.pipe_width: self.upperpipes.pop (0) self.lowerpipes.pop (0)Copy the code

Render pipe image, pay attention to render order:

Self.screen. blit(self.images ['background'], (0, 0)) For uPipe, lPipe in zip(self.upperPipes, self.lowerPipes): self.screen.blit(self.IMAGES['pipe'][0], (uPipe['x'], uPipe['y'])) self.screen.blit(self.IMAGES['pipe'][1], Self.screen. blit(self.images ['base'], (self.basex, self.basey)) #Copy the code

(4) Implementation effect Finally our implementation effect is:

Since Bird is complex, we will initialize it as a separate class

(1) Initialization

class Bird(object): def __init__(self,_Player_Sprite,_screen): Self.player_sprite = _Player_Sprite # Load image resources self.playerinfo = {} # create a player information dictionary Self. PlayerInfo [' w '] = self. Player_Sprite [0]. Get_width (#) for the player picture width self. The PlayerInfo = [' h '] Self.player_sprite [0].get_height() # 1]) # loop self.playerMaxvely = 10 # the maximum speed of the player on the y axis self.playerMinvely = -8 # The minimum speed of the player on the y axis self.playerAccy = 1 # Player's acceleration down on y axis self.playerFlapacc = -7 # Player's acceleration up on Y axis, down is positive # Reset the game setting w self.reset()Copy the code

(2) Reset Bird call function when resetting the game:

def reset(self): Self. loopIter = 0 self.PlayerInfo['x'] = int(SCREENWIDTH * 0.2) # self.PlayerInfo['y'] = int(SCREENHEIGHT - Self.playerinfo ['h']) / 2) # PlayerInfo['index'] = 0 self.playerinfo ['index'] = 0 Self. playerFlapped = False #Copy the code

We render the Bird picture differently in each frame, so that we can achieve the animation effect of the Bird:

Def frame_step(self, input_actions,BASEY): if (self.loopiter + 1) % 3 == 0: Self.playerinfo ['index'] = next(self.player_index_gen) self.loopiter = (self.loopiter + 1) % 30 # Self.screen. blit(self.player_sprite [self.playerinfo ['index']], (self.playerinfo ['x'], self.playerinfo ['y'])) #Copy the code

(4) Implementation effect Finally our implementation effect is:

Photo source: www.diuxie.com/ mobile games