前言

《愤怒的小鸟》其实活得还不错,尽管我们一直在嘲笑它的IP帝国梦做得太大。

但要知道,把休闲益智游戏的生意做到这个份上的,恐怕也就独此一家了。尤其还是这样的一

款古早、过时、难让人相信还能翻出什么花样的游戏。继前两期的效果来看,大家还是依旧挺

喜欢这款游戏的啦~嘿!我是栗子,今天终于迎来了最终版本啦~

这一期给大家写完《愤怒的小鸟最终版》三期完结撒花!

后续再想想给大家更新一些什么内容,爬虫的内容一般过不了,这就没办法!还有其他的游

戏、opencv方面的可以给大家继续更新研究一下啦!大家如果有想学的也可以评论区评论,或

许下一次更新的内容就是你的评论哦~

正文

一)运行环境

本文用到的环境:Python3.6、Pycharm社区版、Pygame游戏模块、pymunk模块自带的就不展示。

pip install -i https://pypi.douban.com/simple/ +模块名

图片素材:

(还有很多素材音频、字体、图片等就不展示啦,比较多,啊随机展示了一点点)

二)代码展示

代码分为四大块,内容有很多代码,这里直接展示主程序,其他的直接找我拿,就直接给你们

看下效果展示一下哈!

主程序:

import os
import sys
import math
import time
import pygame
current_path = os.getcwd()
import pymunk as pm
from characters import Bird
from level import Levelpygame.init()
screen = pygame.display.set_mode((1200, 650))
redbird = pygame.image.load("../resources/images/red-bird3.png").convert_alpha()
background2 = pygame.image.load("../resources/images/background3.png").convert_alpha()
sling_image = pygame.image.load("../resources/images/sling-3.png").convert_alpha()
full_sprite = pygame.image.load("../resources/images/full-sprite.png").convert_alpha()
rect = pygame.Rect(181, 1050, 50, 50)
cropped = full_sprite.subsurface(rect).copy()
pig_image = pygame.transform.scale(cropped, (30, 30))
buttons = pygame.image.load("../resources/images/selected-buttons.png").convert_alpha()
pig_happy = pygame.image.load("../resources/images/pig_failed.png").convert_alpha()
stars = pygame.image.load("../resources/images/stars-edited.png").convert_alpha()
rect = pygame.Rect(0, 0, 200, 200)
star1 = stars.subsurface(rect).copy()
rect = pygame.Rect(204, 0, 200, 200)
star2 = stars.subsurface(rect).copy()
rect = pygame.Rect(426, 0, 200, 200)
star3 = stars.subsurface(rect).copy()
rect = pygame.Rect(164, 10, 60, 60)
pause_button = buttons.subsurface(rect).copy()
rect = pygame.Rect(24, 4, 100, 100)
replay_button = buttons.subsurface(rect).copy()
rect = pygame.Rect(142, 365, 130, 100)
next_button = buttons.subsurface(rect).copy()
clock = pygame.time.Clock()
rect = pygame.Rect(18, 212, 100, 100)
play_button = buttons.subsurface(rect).copy()
clock = pygame.time.Clock()
running = True
# the base of the physics
space = pm.Space()
space.gravity = (0.0, -700.0)
pigs = []
birds = []
balls = []
polys = []
beams = []
columns = []
poly_points = []
ball_number = 0
polys_dict = {}
mouse_distance = 0
rope_lenght = 90
angle = 0
x_mouse = 0
y_mouse = 0
count = 0
mouse_pressed = False
t1 = 0
tick_to_next_circle = 10
RED = (255, 0, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
sling_x, sling_y = 135, 450
sling2_x, sling2_y = 160, 450
score = 0
game_state = 0
bird_path = []
counter = 0
restart_counter = False
bonus_score_once = True
bold_font = pygame.font.SysFont("arial", 30, bold=True)
bold_font2 = pygame.font.SysFont("arial", 40, bold=True)
bold_font3 = pygame.font.SysFont("arial", 50, bold=True)
wall = False# Static floor
static_body = pm.Body(body_type=pm.Body.STATIC)
static_lines = [pm.Segment(static_body, (0.0, 060.0), (1200.0, 060.0), 0.0)]
static_lines1 = [pm.Segment(static_body, (1200.0, 060.0), (1200.0, 800.0), 0.0)]
for line in static_lines:line.elasticity = 0.95line.friction = 1line.collision_type = 3
for line in static_lines1:line.elasticity = 0.95line.friction = 1line.collision_type = 3
space.add(static_body)
for line in static_lines:space.add(line)def to_pygame(p):"""Convert pymunk to pygame coordinates"""return int(p.x), int(-p.y+600)def vector(p0, p1):"""Return the vector of the pointsp0 = (xo,yo), p1 = (x1,y1)"""a = p1[0] - p0[0]b = p1[1] - p0[1]return (a, b)def unit_vector(v):"""Return the unit vector of the pointsv = (a,b)"""h = ((v[0]**2)+(v[1]**2))**0.5if h == 0:h = 0.000000000000001ua = v[0] / hub = v[1] / hreturn (ua, ub)def distance(xo, yo, x, y):"""distance between points"""dx = x - xody = y - yod = ((dx ** 2) + (dy ** 2)) ** 0.5return ddef load_music():"""Load the music"""song1 = '../resources/sounds/angry-birds.ogg'pygame.mixer.music.load(song1)pygame.mixer.music.play(-1)def sling_action():"""Set up sling behavior"""global mouse_distanceglobal rope_lenghtglobal angleglobal x_mouseglobal y_mouse# Fixing bird to the sling ropev = vector((sling_x, sling_y), (x_mouse, y_mouse))uv = unit_vector(v)uv1 = uv[0]uv2 = uv[1]mouse_distance = distance(sling_x, sling_y, x_mouse, y_mouse)pu = (uv1*rope_lenght+sling_x, uv2*rope_lenght+sling_y)bigger_rope = 102x_redbird = x_mouse - 20y_redbird = y_mouse - 20if mouse_distance > rope_lenght:pux, puy = pupux -= 20puy -= 20pul = pux, puyscreen.blit(redbird, pul)pu2 = (uv1*bigger_rope+sling_x, uv2*bigger_rope+sling_y)pygame.draw.line(screen, (0, 0, 0), (sling2_x, sling2_y), pu2, 5)screen.blit(redbird, pul)pygame.draw.line(screen, (0, 0, 0), (sling_x, sling_y), pu2, 5)else:mouse_distance += 10pu3 = (uv1*mouse_distance+sling_x, uv2*mouse_distance+sling_y)pygame.draw.line(screen, (0, 0, 0), (sling2_x, sling2_y), pu3, 5)screen.blit(redbird, (x_redbird, y_redbird))pygame.draw.line(screen, (0, 0, 0), (sling_x, sling_y), pu3, 5)# Angle of impulsedy = y_mouse - sling_ydx = x_mouse - sling_xif dx == 0:dx = 0.00000000000001angle = math.atan((float(dy))/dx)def draw_level_cleared():"""Draw level cleared"""global game_stateglobal bonus_score_onceglobal scorelevel_cleared = bold_font3.render("Level Cleared!", 1, WHITE)score_level_cleared = bold_font2.render(str(score), 1, WHITE)if level.number_of_birds >= 0 and len(pigs) == 0:if bonus_score_once:score += (level.number_of_birds-1) * 10000bonus_score_once = Falsegame_state = 4rect = pygame.Rect(300, 0, 600, 800)pygame.draw.rect(screen, BLACK, rect)screen.blit(level_cleared, (450, 90))if score >= level.one_star and score <= level.two_star:screen.blit(star1, (310, 190))if score >= level.two_star and score <= level.three_star:screen.blit(star1, (310, 190))screen.blit(star2, (500, 170))if score >= level.three_star:screen.blit(star1, (310, 190))screen.blit(star2, (500, 170))screen.blit(star3, (700, 200))screen.blit(score_level_cleared, (550, 400))screen.blit(replay_button, (510, 480))screen.blit(next_button, (620, 480))def draw_level_failed():"""Draw level failed"""global game_statefailed = bold_font3.render("Level Failed", 1, WHITE)if level.number_of_birds <= 0 and time.time() - t2 > 5 and len(pigs) > 0:game_state = 3rect = pygame.Rect(300, 0, 600, 800)pygame.draw.rect(screen, BLACK, rect)screen.blit(failed, (450, 90))screen.blit(pig_happy, (380, 120))screen.blit(replay_button, (520, 460))def restart():"""Delete all objects of the level"""pigs_to_remove = []birds_to_remove = []columns_to_remove = []beams_to_remove = []for pig in pigs:pigs_to_remove.append(pig)for pig in pigs_to_remove:space.remove(pig.shape, pig.shape.body)pigs.remove(pig)for bird in birds:birds_to_remove.append(bird)for bird in birds_to_remove:space.remove(bird.shape, bird.shape.body)birds.remove(bird)for column in columns:columns_to_remove.append(column)for column in columns_to_remove:space.remove(column.shape, column.shape.body)columns.remove(column)for beam in beams:beams_to_remove.append(beam)for beam in beams_to_remove:space.remove(beam.shape, beam.shape.body)beams.remove(beam)def post_solve_bird_pig(arbiter, space, _):"""Collision between bird and pig"""surface=screena, b = arbiter.shapesbird_body = a.bodypig_body = b.bodyp = to_pygame(bird_body.position)p2 = to_pygame(pig_body.position)r = 30pygame.draw.circle(surface, BLACK, p, r, 4)pygame.draw.circle(surface, RED, p2, r, 4)pigs_to_remove = []for pig in pigs:if pig_body == pig.body:pig.life -= 20pigs_to_remove.append(pig)global scorescore += 10000for pig in pigs_to_remove:space.remove(pig.shape, pig.shape.body)pigs.remove(pig)def post_solve_bird_wood(arbiter, space, _):"""Collision between bird and wood"""poly_to_remove = []if arbiter.total_impulse.length > 1100:a, b = arbiter.shapesfor column in columns:if b == column.shape:poly_to_remove.append(column)for beam in beams:if b == beam.shape:poly_to_remove.append(beam)for poly in poly_to_remove:if poly in columns:columns.remove(poly)if poly in beams:beams.remove(poly)space.remove(b, b.body)global scorescore += 5000def post_solve_pig_wood(arbiter, space, _):"""Collision between pig and wood"""pigs_to_remove = []if arbiter.total_impulse.length > 700:pig_shape, wood_shape = arbiter.shapesfor pig in pigs:if pig_shape == pig.shape:pig.life -= 20global scorescore += 10000if pig.life <= 0:pigs_to_remove.append(pig)for pig in pigs_to_remove:space.remove(pig.shape, pig.shape.body)pigs.remove(pig)# bird and pigs
space.add_collision_handler(0, 1).post_solve=post_solve_bird_pig
# bird and wood
space.add_collision_handler(0, 2).post_solve=post_solve_bird_wood
# pig and wood
space.add_collision_handler(1, 2).post_solve=post_solve_pig_wood
load_music()
level = Level(pigs, columns, beams, space)
level.number = 0
level.load_level()while running:# Input handlingfor event in pygame.event.get():if event.type == pygame.QUIT:running = Falseelif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:running = Falseelif event.type == pygame.KEYDOWN and event.key == pygame.K_w:# Toggle wallif wall:for line in static_lines1:space.remove(line)wall = Falseelse:for line in static_lines1:space.add(line)wall = Trueelif event.type == pygame.KEYDOWN and event.key == pygame.K_s:space.gravity = (0.0, -10.0)level.bool_space = Trueelif event.type == pygame.KEYDOWN and event.key == pygame.K_n:space.gravity = (0.0, -700.0)level.bool_space = Falseif (pygame.mouse.get_pressed()[0] and x_mouse > 100 andx_mouse < 250 and y_mouse > 370 and y_mouse < 550):mouse_pressed = Trueif (event.type == pygame.MOUSEBUTTONUP andevent.button == 1 and mouse_pressed):# Release new birdmouse_pressed = Falseif level.number_of_birds > 0:level.number_of_birds -= 1t1 = time.time()*1000xo = 154yo = 156if mouse_distance > rope_lenght:mouse_distance = rope_lenghtif x_mouse < sling_x+5:bird = Bird(mouse_distance, angle, xo, yo, space)birds.append(bird)else:bird = Bird(-mouse_distance, angle, xo, yo, space)birds.append(bird)if level.number_of_birds == 0:t2 = time.time()if event.type == pygame.MOUSEBUTTONUP and event.button == 1:if (x_mouse < 60 and y_mouse < 155 and y_mouse > 90):game_state = 1if game_state == 1:if x_mouse > 500 and y_mouse > 200 and y_mouse < 300:# Resume in the paused screengame_state = 0if x_mouse > 500 and y_mouse > 300:# Restart in the paused screenrestart()level.load_level()game_state = 0bird_path = []if game_state == 3:# Restart in the failed level screenif x_mouse > 500 and x_mouse < 620 and y_mouse > 450:restart()level.load_level()game_state = 0bird_path = []score = 0if game_state == 4:# Build next levelif x_mouse > 610 and y_mouse > 450:restart()level.number += 1game_state = 0level.load_level()score = 0bird_path = []bonus_score_once = Trueif x_mouse < 610 and x_mouse > 500 and y_mouse > 450:# Restart in the level cleared screenrestart()level.load_level()game_state = 0bird_path = []score = 0x_mouse, y_mouse = pygame.mouse.get_pos()# Draw backgroundscreen.fill((130, 200, 100))screen.blit(background2, (0, -50))# Draw first part of the slingrect = pygame.Rect(50, 0, 70, 220)screen.blit(sling_image, (138, 420), rect)# Draw the trail left behindfor point in bird_path:pygame.draw.circle(screen, WHITE, point, 5, 0)# Draw the birds in the wait lineif level.number_of_birds > 0:for i in range(level.number_of_birds-1):x = 100 - (i*35)screen.blit(redbird, (x, 508))# Draw sling behaviorif mouse_pressed and level.number_of_birds > 0:sling_action()else:if time.time()*1000 - t1 > 300 and level.number_of_birds > 0:screen.blit(redbird, (130, 426))else:pygame.draw.line(screen, (0, 0, 0), (sling_x, sling_y-8),(sling2_x, sling2_y-7), 5)birds_to_remove = []pigs_to_remove = []counter += 1# Draw birdsfor bird in birds:if bird.shape.body.position.y < 0:birds_to_remove.append(bird)p = to_pygame(bird.shape.body.position)x, y = px -= 22y -= 20screen.blit(redbird, (x, y))pygame.draw.circle(screen, BLUE,p, int(bird.shape.radius), 2)if counter >= 3 and time.time() - t1 < 5:bird_path.append(p)restart_counter = Trueif restart_counter:counter = 0restart_counter = False# Remove birds and pigsfor bird in birds_to_remove:space.remove(bird.shape, bird.shape.body)birds.remove(bird)for pig in pigs_to_remove:space.remove(pig.shape, pig.shape.body)pigs.remove(pig)# Draw static linesfor line in static_lines:body = line.bodypv1 = body.position + line.a.rotated(body.angle)pv2 = body.position + line.b.rotated(body.angle)p1 = to_pygame(pv1)p2 = to_pygame(pv2)pygame.draw.lines(screen, (150, 150, 150), False, [p1, p2])i = 0# Draw pigsfor pig in pigs:i += 1# print (i,pig.life)pig = pig.shapeif pig.body.position.y < 0:pigs_to_remove.append(pig)p = to_pygame(pig.body.position)x, y = pangle_degrees = math.degrees(pig.body.angle)img = pygame.transform.rotate(pig_image, angle_degrees)w,h = img.get_size()x -= w*0.5y -= h*0.5screen.blit(img, (x, y))pygame.draw.circle(screen, BLUE, p, int(pig.radius), 2)# Draw columns and Beamsfor column in columns:column.draw_poly('columns', screen)for beam in beams:beam.draw_poly('beams', screen)# Update physicsdt = 1.0/50.0/2.for x in range(2):space.step(dt) # make two updates per frame for better stability# Drawing second part of the slingrect = pygame.Rect(0, 0, 60, 200)screen.blit(sling_image, (120, 420), rect)# Draw scorescore_font = bold_font.render("SCORE", 1, WHITE)number_font = bold_font.render(str(score), 1, WHITE)screen.blit(score_font, (1060, 90))if score == 0:screen.blit(number_font, (1100, 130))else:screen.blit(number_font, (1060, 130))screen.blit(pause_button, (10, 90))# Pause optionif game_state == 1:screen.blit(play_button, (500, 200))screen.blit(replay_button, (500, 300))draw_level_cleared()draw_level_failed()pygame.display.flip()clock.tick(50)pygame.display.set_caption("fps: " + str(clock.get_fps()))

三)效果展示

1)第一关

2)闯关成功

​3)其他关卡(随机截图)

4)闯关失败

其实还有很多关卡,还有只有小猪在自己往上跳,需要你命中的......很有趣很好玩,想自己试试

后面的关卡就找我拿源码吧!

总结

嘻嘻,这个第三版本可真好玩儿,喜欢的小可爱记得点点关注的啦!

✨完整的素材等:滴滴我吖!或者点击文末公众号自取免费拿的哈~

【Pygame实战】第三版本最终确认——《愤怒的小鸟梦幻爆破》上线回归,爆赞~相关推荐

  1. RabbitMQ(三)发布确认

    4.1 发布确认原理 生产者将信道设置成 confirm 模式,一旦信道进入 confirm 模式,所有在该信道上面发布的消息都将会被指派一个唯一的 ID(从 1 开始),一旦消息被投递到所有匹配的队 ...

  2. 基于 abp vNext 和 .NET Core 开发博客项目 - 定时任务最佳实战(三)

    基于 abp vNext 和 .NET Core 开发博客项目 - 定时任务最佳实战(三) 转载于:https://github.com/Meowv/Blog 本篇继续围绕抓取完成后的操作做一个提醒. ...

  3. Android项目实战(三十八):2017最新 将AndroidLibrary提交到JCenter仓库(图文教程)...

    Android项目实战(三十八):2017最新 将AndroidLibrary提交到JCenter仓库(图文教程) 原文:Android项目实战(三十八):2017最新 将AndroidLibrary ...

  4. 日志服务Python消费组实战(三):实时跨域监测多日志库数据

    解决问题 使用日志服务进行数据处理与传递的过程中,你是否遇到如下监测场景不能很好的解决: 特定数据上传到日志服务中需要检查数据内的异常情况,而没有现成监控工具? 需要检索数据里面的关键字,但数据没有建 ...

  5. Pygame 实战(行动代号(单机版)):(二). 游戏编程

    当前系列历史文章: Pygame 实战(行动代号(单机版)):(一). 游戏简介 这一部分就开始讲实现的代码啦!因为之前写代码的时候没什么经验,所以可能有不少冗余的部分和可以简化的地方命名也不是很规范 ...

  6. Django打造大型企业官网-项目实战(三)

    Django打造大型企业官网-项目实战(三) 一.CRM 后台管理系统 前面我们使用的是 xadmin 后台管理系统,在使用中发现,在权限限制中,我们能实现不同等级的用户/管理(超级管理员/管理员/用 ...

  7. Pygame实战:利用Python实现智能五子棋,实现之后发现我玩不赢它。

    导语 前段时间不是制作了一款升级版本五子棋的嘛! 但是居然有粉丝私信我说: "准备拿到代码玩一下ok过去了!太难了准备放收藏夹落灰q@q~" 所噶,今天先放一个简易版本的五子棋给大 ...

  8. 虫师selenium3+python自动化测试电子版_Selenium3 Java自动化测试实战 第三版.pdf

    <Selenium2Java 自动化测试实战(修正版)> Selenium3 Java 自动化测试项目实战 第三版( ) 作者:虫师 1 <Selenium2Java 自动化测试实战 ...

  9. MSP430-GRACE 实战(三):定时器中断

    文章目录 MSP430-GRACE 实战(三):定时器中断 一.开发平台 1.1 硬件平台 1.2 软件平台 二.原理分析 三.GRACE 配置 3.1 新建工程(通用步骤) 3.2 配置时钟(通用步 ...

  10. 【Pygame实战】全新模式飞机大战:空中追逐战,讲个笑话,飞机大战是休闲手游。

    导语 三月疫情原因,很多地方都封闭式管理了! 在回家无聊的打酱油,小编今天给大伙带来了一波小游戏--全民左右飞机大战!在这个快熬 不下去的日子里,打打飞机消遣闲暇时间,也是蛮惬意的,这几天小编必须全身 ...

最新文章

  1. 算法 - 最好、最坏、平均复杂度
  2. 内容激活码jsp发送email
  3. Storm中并行度原来是这样计算的(1.0.1版本)
  4. Spring Boot整合Spring Data Redis-提取Redis的链接参数
  5. CSAPP:Attack lab
  6. java+fseek+函数_函数fseek() 用法(转)
  7. 东方通没有创造中间件 却在定义中间件的“化蝶”
  8. 【ElasticSearch】Es 源码之 IndicesModule 源码解读
  9. Spring Boot学习——统一异常处理
  10. 程序运行中(BSS段、数据段、代码段、堆栈)
  11. Handler看这一篇就够了
  12. Cesium图形绘制
  13. 运维工程师主要做什么_弱电工程IT运维工作到底是做什么的?
  14. Expected response code 250 but got code “501“, with messa php laravel 发邮件 smtp qq邮箱 阿里云
  15. Win10文件夹中图片不显示预览图解决方法
  16. 增长黑客AB-Test系统(三)——AB-Test Hash分流
  17. js前端面试题总结及答案
  18. putty使用方法,中文教程
  19. C语言刷题(9):判断一个数是否为质数(素数)
  20. 双向可控硅的三个引脚和单向可控硅的三个引脚在正向导通时作用是一样的吗?

热门文章

  1. 自定义组件时 Binary XML file line Error inflating class 异常
  2. [机缘参悟-64]:《兵者,诡道也》-5-三十六计解读-混战计
  3. 简明c语言,简明易懂的C语言俄罗斯方块
  4. Android 手机设置中铃声设置里的通知音量与铃声音量的分离
  5. docker 小结
  6. 开课吧java广告,开课吧Java面试题:虚引用与软引用和弱引用的区别
  7. Dfam:真核生物转座元件数据库
  8. 怎样设定目标(三)—— 目标设定前的准备
  9. 建设银行上海住房公积金业务网点
  10. 一种绘制有向图的方法<TSE93> - 2. 最优层级分配