python愤怒的小鸟,撞击单摆练习程序_pymunk_pendulum

```"""

"""
__author__ = "lixingqiu"
__date__ = "2019/5/3"

import os
import math
import pymunk
import timeit
from PIL import Image

SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 800
SCREEN_TITLE = "愤怒的小鸟,撞击单摆练习程序_by_李兴球"

def __init__(self, pymunk_shape, filename):
super().__init__(filename, center_x=pymunk_shape.body.position.x, center_y=pymunk_shape.body.position.y)
self.pymunk_shape = pymunk_shape

class CircleSprite(PhysicsSprite):
def __init__(self, pymunk_shape, filename):
super().__init__(pymunk_shape, filename)
self.width = pymunk_shape.radius * 2
self.height = pymunk_shape.radius * 2

class BoxSprite(PhysicsSprite):
def __init__(self, pymunk_shape, filename, width, height):
super().__init__(pymunk_shape, filename)
self.width = width
self.height = height

def make_sprite(mass,image,position,space):
"""生成一个受重力的角色"""
width,height = Image.open(image).size  # 获取图像的宽度和高度
mass = mass
moment = pymunk.moment_for_box(mass, (width,height))
body = pymunk.Body(mass, moment)
body.position = pymunk.Vec2d(position)
shape = pymunk.Poly.create_box(body, (width, height))
shape.friction = 0.3
sprite = BoxSprite(shape, image, width=width, height=height)
return sprite

""" 最顶层的游戏类，继承自窗口 """

def __init__(self, width, height,title):
super().__init__(width, height,title)

# -- Pymunk的重力空间
self.space = pymunk.Space()
self.space.gravity = (0.0, -900.0)

# 所有角色列表
self.background.left = self.background.bottom = 0
self.static_lines = []

# 用鼠标拖曳的角色相关变量
self.shape_being_dragged = None
self.last_mouse_position = 0, 0

self.draw_time = 0
self.processing_time = 0

# 创建地板
floor_height = 80
body = pymunk.Body(body_type=pymunk.Body.STATIC)
shape = pymunk.Segment(body, [0, floor_height], [SCREEN_WIDTH, floor_height], 0.0)
shape.friction = 10
self.static_lines.append(shape)

# 创建小鸟的平台
sprite = make_sprite(33,"images/platform.png",(160, 160),self.space)
self.sprite_list.append(sprite)

# 创建物理小鸟
self.shoot_position = pymunk.Vec2d(160 , 290)
self.physic_bird = make_sprite(33,"images/bird.png",self.shoot_position,self.space)
self.sprite_list.append(self.physic_bird)

# 创建virtual小鸟，它是用来被拖曳的，它并不加入到所有角色列表，需要单独渲染
self.virtual_bird = arcade.Sprite("images/bird.png")#  被拖曳的小鸟
self.virtual_bird.center_x = self.shoot_position[0]
self.virtual_bird.center_y = self.shoot_position[1]

# 创建单摆球
ball_x = SCREEN_WIDTH // 2
ball_y = SCREEN_HEIGHT //2
self.physic_ball = make_sprite(33,"images/绿球.png",(ball_x,ball_y),self.space) # 球在下面
self.sprite_list.append(self.physic_ball)
# 建立针关节,第一个参数表示它是静态body,
self.pin_x = ball_x                                # 为了画线,所以建这两个变量
self.pin_y = ball_y +250
pj = pymunk.PinJoint(self.space.static_body, self.physic_ball.pymunk_shape.body, (self.pin_x,self.pin_y), (0,0))#关节点在上面250距离处
self.physic_ball.force=(10000,0)

self.reset_shoot = True

def reset_shoot_bird(self):
"""重新发射"""
self.virtual_bird.center_x = self.shoot_position[0]
self.virtual_bird.center_y = self.shoot_position[1]
self.physic_bird.pymunk_shape.body.velocity = (0,0)
self.physic_bird.pymunk_shape.body.position = self.shoot_position
self.physic_bird.pymunk_shape.body.angle = 0

self.reset_shoot = True
self.shape_being_dragged = None

def on_key_press(self, key, modifiers):
"""
当按键时调用此方法
"""
if key == arcade.key.SPACE:
self.reset_shoot_bird()

def on_draw(self):
"""
渲染屏幕
"""

# 开始渲染屏幕

# 开始计时
draw_start_time = timeit.default_timer()

# 画背景图片
self.background.draw()

# 画所有的角色
self.sprite_list.draw()

# 画静止的线条
for line in self.static_lines:
body = line.body
pv1 = body.position + line.a.rotated(body.angle)
pv2 = body.position + line.b.rotated(body.angle)

if self.reset_shoot :  self.virtual_bird.draw()

# 画皮筋
x1,y1 = self.shoot_position
x2,y2 = self.virtual_bird.position
dd = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)

if dd>1 and self.reset_shoot:

# 画单摆线
ball_x = self.physic_ball.pymunk_shape.body.position[0]
ball_y = self.physic_ball.pymunk_shape.body.position[1] +  22

# 画时间
output = f"处理时间: {self.processing_time:.3f}"
arcade.draw_text(output, 20, SCREEN_HEIGHT - 20, arcade.color.WHITE, 12,font_name='simhei')

output = f"绘画时间: {self.draw_time:.3f}"
arcade.draw_text(output, 20, SCREEN_HEIGHT - 40, arcade.color.WHITE, 12,font_name='simhei')

self.draw_time = timeit.default_timer() - draw_start_time

def on_mouse_press(self, x, y, button, modifiers):

if button == arcade.MOUSE_BUTTON_LEFT :
self.shape_being_dragged = self.virtual_bird

def on_mouse_release(self, x, y, button, modifiers):

physic_bird_position = self.physic_bird.pymunk_shape.body.position
distance = (physic_bird_position.get_distance(self.shoot_position))
if distance > 1 :return

if button == arcade.MOUSE_BUTTON_LEFT:
# 松开
self.shape_being_dragged = None
dx = x - self.shoot_position[0]
dy = y - self.shoot_position[1]
self.physic_bird.pymunk_shape.body.force = -dx*10000, -dy*10000
self.reset_shoot = False

def on_mouse_motion(self, x, y, dx, dy):
if self.shape_being_dragged is not None:
# 抓住了某个物体，让它跟随鼠标指针
self.virtual_bird.center_x = x
self.virtual_bird.center_y = y

def update(self, delta_time):

start_time = timeit.default_timer()
self.virtual_bird.update()

# 检测每一个角色，移除y坐标小于0的
for sprite in self.sprite_list:
if self.physic_bird == sprite:continue         # 排除物理小鸟
if sprite.pymunk_shape.body.position.y < 0:
# 从重力空间中移除
self.space.remove(sprite.pymunk_shape, sprite.pymunk_shape.body)
# 从列表中删除
sprite.kill()

self.space.step(1 / 60.0)

# 把角色统统移到它的物理坐标
for sprite in self.sprite_list:
sprite.center_x = sprite.pymunk_shape.body.position.x
sprite.center_y = sprite.pymunk_shape.body.position.y
sprite.angle = math.degrees(sprite.pymunk_shape.body.angle)

# 保存时间
self.processing_time = timeit.default_timer() - start_time

def main():
MyGame(SCREEN_WIDTH, SCREEN_HEIGHT,SCREEN_TITLE)