Introduction:

“10 years ago, I did not know you, you do not belong to me.” 10 years later, I have forgotten who you are.

This song of love is probably the best way to describe the voice of the past.

Ten years ago, you must have played this classic game.

A young man found an outlet for his emotions with a red bird. They are high-spirited and ambitious, looking for joy and sustenance in the addictive world of the game

More than a decade has passed and the game has faded into oblivion. Has-been games? Birds are not angry. Do you really think I am “Hello bird”?

Now the high temperature weather, in addition to sunscreen clothes, may rely on this hot bird to cool down the summer!



The body of the

First of all, the environment installation part:

Ready – Python3.6, Pycharm2021, the main game module Pygame, and some built-in modules are available directly.

Game source can be inconsistent version can also run, the corresponding module can be installed, their own arrangements!

pip install -i https://pypi.douban.com/simple/  pygame
Copy the code

Preparation – corresponding material pictures, music background, etc. :

The definition of Sprite class is the pig thrown by the opponent.

class Pig(pygame.sprite.Sprite): def __init__(self, screen, imagepaths, loc_info, velocity=None, **kwargs): Pygame.sprite.__init__ (self) Assert Len (loc_info) == 3 assert Len (imagepaths) == 3 # Set the necessary attribute constant self.screen = screen self.loc_info = list(loc_info) self.imagepaths = imagepaths self.velocity = VelocityVector() if velocity is None Self.freq = 0 self.freq = 0 self.freq = 0 self.animate_count = 0 Self. inverse_friction = 0.99 self.gravity = VelocityVector(0.2, Math.pi) # screen size self.screen_size = screen.get_rect().size self.screen_size = (self.screen_size[0], Self.screen_size [1] -50) # self.pig_images = [] for imagepath in imagepaths: Self. Image = random. Choice (self.pig_images[:2]) Def draw(self): self.animate_count += 1 if (self.animate_count % self.switch_freq == 0) and (not self.is_dead): self.animate_count = 0 self.image = random.choice(self.pig_images[:2]) position = self.loc_info[0] - self.loc_info[2], Self.loc_info [1] - self.loc_info[2] self.screen.blit(self. Image, position) "" def move(self): Self. Velocity = VectorAddition(self. Velocity, self.gravity) self.loc_info[0] += self.velocity.magnitude * math.sin(self.velocity.angle) self.loc_info[1] -= Self.velocity.magnitude * math. Cos (self.velocity.angle) self.velocity.magnitude *= self self.loc_info[0] > self.screen_size[0] - self.loc_info[2]: self.loc_info[0] = 2 * (self.screen_size[0] - self.loc_info[2]) - self.loc_info[0] self.velocity.angle *= -1 self.velocity.magnitude *= self.elasticity elif self.loc_info[0] < self.loc_info[2]: self.loc_info[0] = 2 * self.loc_info[2] - self.loc_info[0] self.velocity.angle *= -1 self.velocity.magnitude *= If self.loc_info[1] > self.screen_size[1] - self.loc_info[2]: self.loc_info[1] = 2 * (self.screen_size[1] - self.loc_info[2]) - self.loc_info[1] self.velocity.angle = math.pi - self.velocity.angle self.velocity.magnitude *= self.elasticity elif self.loc_info[1] < self.loc_info[2]: self.loc_info[1] = 2 * self.loc_info[2] - self.loc_info[1] self.velocity.angle = math.pi - self.velocity.angle Def setdead(self): self.velocity. Magnitude *= self.elasticity. self.is_dead = True self.image = self.pig_images[-1]Copy the code

And then the sprites defined here are the birds, the players:

class Bird(pygame.sprite.Sprite): def __init__(self, screen, imagepaths, loc_info, velocity=None, color=(255, 255, 255), **kwargs): Pygame.sprite.__init__ (self) Assert Len (loc_info) == 3 Assert Len (imagepaths) == 1 # Set the necessary attribute constant self.color = color  self.screen = screen self.loc_info = list(loc_info) self.imagepaths = imagepaths self.velocity = VelocityVector() if Self.fly_path = [] self.is_dead = False self.dead = 0.8 Self. is_loaded = False self.is_selected = False self.gravity = VelocityVector(0.1, 0) self.is_selected = False self.gravity = VelocityVector(0.1, 0) Math.pi) # screen size self.screen_size = screen.get_rect().size self.screen_size = (self.screen_size[0], Self.screen_size [1] -50) # self.image = Pygame.image.load (imagepaths[0]) def draw(self): self.screen_size[1] -50) # self.image = Pygame.image.load (imagepaths[0]) if not self.is_loaded: for point in self.fly_path: pygame.draw.ellipse(self.screen, self.color, (point[0], point[1], 3, 3), 1) position = self.loc_info[0] - self.loc_info[2], self.loc_info[1] - self.loc_info[2] self.screen.blit(self.image, "Def selected(self): pos = pygame.mouse.get_pos() dx, dy = pos[0] - self.loc_info[0], pos[1] - self.loc_info[1] dist = math.hypot(dy, Dx) if dist < self.loc_info[2]: return True return False "" def load(self, slingshot): Self. loc_info[0], self.loc_info[1] = slingshot.x, slingshot.y self.is_loaded = True slingshot): pos = pygame.mouse.get_pos() if self.selected: self.loc_info[0], self.loc_info[1] = pos[0], pos[1] dx, dy = slingshot.x - self.loc_info[0], slingshot.y - self.loc_info[1] self.velocity.magnitude = min(int(math.hypot(dx, dy) / 2), Self.velocity. Angle = math.pi / 2 + math.atan2(dy, dx) "" def projectPath (self): if self.is_loaded: path = [] bird = Bird(self.screen, self.imagepaths, self.loc_info, velocity=self.velocity) for i in range(30): bird.move() if i % 5 == 0: path.append((bird.loc_info[0], bird.loc_info[1])) for point in path: Self.screen, self.color, (point[0], point[1], 2, 2)) def move(self): Self. Velocity = VectorAddition(self. Velocity, self.gravity) self.loc_info[0] += self.velocity.magnitude * math.sin(self.velocity.angle) self.loc_info[1] -= Self.velocity.magnitude * math. Cos (self.velocity.angle) self.velocity.magnitude *= self self.loc_info[0] > self.screen_size[0] - self.loc_info[2]: self.loc_info[0] = 2 * (self.screen_size[0] - self.loc_info[2]) - self.loc_info[0] self.velocity.angle *= -1 self.velocity.magnitude *= self.elasticity elif self.loc_info[0] < self.loc_info[2]: self.loc_info[0] = 2 * self.loc_info[2] - self.loc_info[0] self.velocity.angle *= -1 self.velocity.magnitude *= If self.loc_info[1] > self.screen_size[1] - self.loc_info[2]: self.loc_info[1] = 2 * (self.screen_size[1] - self.loc_info[2]) - self.loc_info[1] self.velocity.angle = math.pi - self.velocity.angle self.velocity.magnitude *= self.elasticity elif self.loc_info[1] < self.loc_info[2]: self.loc_info[1] = 2 * self.loc_info[2] - self.loc_info[1] self.velocity.angle = math.pi - self.velocity.angle self.velocity.magnitude *= self.elasticityCopy the code

Define the stakes in the map:

class Block(pygame.sprite.Sprite): def __init__(self, screen, imagepaths, loc_info, velocity=None, **kwargs): Pygame.sprite.__init__ (self) Assert Len (loc_info) == 3 Assert Len (imagepaths) == 2 # Set the necessary attribute constant self.type = 'block' self.screen = screen self.loc_info = list(loc_info) self.imagepaths = imagepaths self.velocity = VelocityVector() if velocity is None else Velocity self.elasticity = 0.7 self.is_destroyed = False self.inverse_friction Self. gravity = VelocityVector(0.2, math.pi) # self.block_images = [] for imagepath in imagepaths: self.block_images.append(pygame.transform.scale(pygame.image.load(imagepath), (100, Self.screen_size = screen.get_rect().size self.screen_size = (self.screen_size[0], Self.image = self.block_images[0] self.rect = self.image.get_rect() self.rotate_angle = math.radians(0) "" def draw(self): pygame.transform.rotate(self.image, self.rotate_angle) self.screen.blit(self.image, (self.loc_info[0] - self.rect.width // 2, self.loc_info[1])) def setDestroy (self): Self.is_destroyed = True self.image = self.block_images[1] def move(self): Self. Velocity = VectorAddition(self. Velocity, self.gravity) self.loc_info[0] += self.velocity.magnitude * math.sin(self.velocity.angle) self.loc_info[1] -= Self.velocity.magnitude * math. Cos (self.velocity.angle) self.velocity.magnitude *= self self.loc_info[0] > self.screen_size[0] - self.rect.width: self.loc_info[0] = 2 * (self.screen_size[0] - self.rect.width) - self.loc_info[0] self.velocity.angle *= -1 self.rotate_angle = -self.velocity.angle self.velocity.magnitude *= self.elasticity elif self.loc_info[0] < self.rect.width: self.loc_info[0] = 2 * self.rect.width - self.loc_info[0] self.velocity.angle *= -1 self.rotate_angle = If self.loc_info[1] > self.screen_size[1] - self.rect.height: self.loc_info[1] = 2 * (self.screen_size[1] - self.rect.height) - self.loc_info[1] self.velocity.angle = math.pi - self.velocity.angle self.rotate_angle = math.pi - self.velocity.angle self.velocity.magnitude *= self.elasticity elif self.loc_info[1] < self.rect.height: self.loc_info[1] = 2 * self.rect.height - self.loc_info[1] self.velocity.angle = math.pi - self.velocity.angle self.rotate_angle = math.pi - self.velocity.angle self.velocity.magnitude *= self.elasticityCopy the code

Main program:

Import sys import CFG import pygame from modules import * "" def main(CFG): Init () PyGame.mixer.music.load (cfg.bgmpath) PyGame.mixer.music.play (-1, 0.0) Screen = Pygame.display.set_mode (cfg.screensize) Pygame.display.set_caption (' Angry Birds -- CSDN Def startGame (): game_levels = GameLevels(CFG, screen) game_levels.start() # def quitgame(): Pygame.quit () sys.exit() # start interface components = Pygame.sprite.group () title_label = Label(screen, 700, 100, 400) 200) title_label.addtext('ANGRY BIRDS', 80, cfg.FONTPATH['arfmoochikncheez'], (236, 240, 241)) components.add(title_label) start_btn = Button(screen, 500, 400, 300, 100, startgame, (244, 208, 63), (247, 220, 111)) start_btn.addtext('START GAME', 60, cfg.FONTPATH['arfmoochikncheez'], cfg.BACKGROUND_COLOR) components.add(start_btn) quit_btn = Button(screen, 1000, 400, 300, 100, quitgame, (241, 148, 138), (245, 183, 177)) quit_btn.addtext('QUIT', 60, cfg.FONTPATH['arfmoochikncheez'], cfg.BACKGROUND_COLOR) components.add(quit_btn) charles_label = Label(screen, cfg.SCREENSIZE[0] - 300, cfg.SCREENSIZE[1] - 80, 300, 100) charles_label.addtext('CHARLES', 60, cfg.FONTPATH['arfmoochikncheez'], (41, 41, 41)) components.add(charles_label) clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: quitgame() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_q: quitgame() elif event.type == pygame.MOUSEBUTTONDOWN: if start_btn.selected(): start_btn.action() elif quit_btn.selected(): quit_btn.action() screen.fill(cfg.BACKGROUND_COLOR) for component in components: component.draw() pygame.display.update() clock.tick(cfg.FPS) '''run''' if __name__ == '__main__': main(cfg)Copy the code

Because the angry Birds code is too much, so all the code is not included! There is code at the end!

Effect:

conclusion

Quickly follow xiaobian up posture first! Is this the game you want to own?

If you need a complete project source code + all material old rules source code base see: # private letter xiaobian 06# or click on the blue font you can get free source package!

Remember the third company oh ~ MUA your support is my biggest motivation!!