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

### Python海龟宝典含200多个原创的用turtle模块制作的创意程序，原名《Python趣味编程200例》。准备参加全国创意编程与智能设计大赛的同学们可以用来做参考。

```"""

"""
__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):
"""生成一个受重力的角色"""

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

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

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

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

if __name__ == "__main__":
main()
```

#### 学本领，探索更大的世界！

error: Content is protected !!