1. The player controls Bird
The player only has to press the space button or the up button to control the input, and each time the player presses, the bird flies up a short distance, which is also a feature of the game
(1) Change the game’s main loop
We added a judgment program to the main loop that receives player input events as follows:
Def run_game(self): while True: is_flap = False # For event in pygame.event.get(): if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): Quit () sys.exit() if event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_UP): # press actions = np.array([0, 1]) # flapping = True # flapping = True # flapping = True # flapping = True Actions = np.array([1, 0]) # Let the bird land self.frame_step(input_actions=actions)Copy the code
As we can see from the program, we enter an array of the form OneHot to distinguish between two different actions:
Input_actions [0] == 1: Birds fall
Input_actions [1] == 1: Bird ascending
(2) Change the game frame rendering
We then modify the frame rendering function in the Bird class as follows:
Def frame_step(self, input_actions,BASEY): # if sum(input_actions) = 1: raise ValueError('Multiple input actions! ') if input_actions[1] == 1: if self.PlayerInfo['y'] > -2 * self.PlayerInfo['h']: Self.playervely = self.playerflapacc # apped = True # SOUNDS['wing']. Play () # self.playerVelY < self.playerMaxVelY and not self.playerFlapped: Self. playerVelY += self.playerAccY () (0) self.playerFlapped = False self.PlayerInfo['y'] += min(self.playerVelY, Basey-self.playerinfo ['y'] -self.playerinfo ['h'])# if self.playerinfo ['y'] < 0: basey-self.playerinfo ['y'] -self.playerinfo ['h']) # count the player sequence frame 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
At this point we can judge the user’s input and control the bird’s actions
2. Collision detection of Bird
(1) Change the game frame rendering
In the above code, we have implemented the Bird movement, but we have not implemented the Bird collision detection, the Bird can pass through the Pipe, so we will continue to add the collision detection code in the frame rendering function of the game.
# Draw player image self.bird.frame_step(input_actions, Self. BASEY) # check whether a collision isCrash = self. CheckCrash (self. Bird. PlayerInfo, self. UpperPipes, self. LowerPipes) if isCrash: # self. A contributor [' hit '] play () # self. A contributor [' die '] play () the self. The reset () # resetCopy the code
(2) Collision detection function
def checkCrash(self,player, upperPipes, lowerPipes): If player['y'] + player['h'] >= self. basey-1: return True else: if player['y'] + player['h'] = self. basey-1: return True else: playerRect = pygame.Rect(player['x'], player['y'], player['w'], player['h']) for uPipe, lPipe in zip(upperPipes, lowerPipes): # upper and lower pipe rects uPipeRect = pygame.Rect(uPipe['x'], uPipe['y'], self.PIPE_WIDTH, self.PIPE_HEIGHT) lPipeRect = pygame.Rect(lPipe['x'], lPipe['y'], self.PIPE_WIDTH, self.PIPE_HEIGHT) # player and upper/lower pipe hitmasks pHitMask = self.HITMASKS['player'][pi] uHitmask = self.HITMASKS['pipe'][0] lHitmask = self.HITMASKS['pipe'][1] # if bird collided with upipe or lpipe uCollide = tool.pixelCollision(playerRect, uPipeRect, pHitMask, uHitmask) lCollide = tool.pixelCollision(playerRect, lPipeRect, pHitMask, lHitmask) if uCollide or lCollide: return True return FalseCopy the code
(3) Pixel-level collision check
def pixelCollision(rect1, rect2, hitmask1, hitmask2): """ "Rect = rect1.clip(recT2) # Get the overlap between two rect objects # If the overlap does not exist, there is no collision if rect.width == 0 or rect.height = = 0: X1, y1 = rect.x-rect1. x, rect.y-rect1. y x2, y2 = rect.x-rect2. x, rect.y - rect2.y for x in range(rect.width): for y in range(rect.height): if hitmask1[x1 + x][y1 + y] and hitmask2[x2 + x][y2 + y]: return True return FalseCopy the code
(4) Final effect
Image source: www.walajiao.com/ game franchise