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

python愤怒的小鸟,撞击单摆练习程序_by_李兴球_arcade_pymunk_angry_bird__pendulum
以下是部分代码预览:

"""
练习做单摆程序,把它加到愤怒的小鸟程序,拉小鸟,去撞击单摆球,然后球就会在重力的作用下晃荡,本程序用arcade模块和pymunk模块实现,运行之要用pip先安装.

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

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

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

class PhysicsSprite(arcade.Sprite):
    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):      
    """生成一个受重力的角色"""
   

class MyGame(arcade.Window):
    """ 最顶层的游戏类,继承自窗口 """

    def __init__(self, width, height,title):
        super().__init__(width, height,title)
        
        arcade.set_background_color(arcade.color.DARK_SLATE_GRAY)

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

        # 所有角色列表
        self.background = arcade.Sprite("images/background.png")
        self.background.left = self.background.bottom = 0
        self.sprite_list = arcade.SpriteList()
        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):
        """
        渲染屏幕
        """

        # 开始渲染屏幕
        arcade.start_render()

        # 开始计时
        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)
            arcade.draw_line(pv1.x, pv1.y, pv2.x, pv2.y, arcade.color.WHITE, 2)

        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:
          arcade.draw_line(x1, y1, x2, y2, arcade.color.ORANGE, 4)


        # 画单摆线
        ball_x = self.physic_ball.pymunk_shape.body.position[0]
        ball_y = self.physic_ball.pymunk_shape.body.position[1] +  22
        arcade.draw_line(self.pin_x, self.pin_y, ball_x, ball_y, arcade.color.ORANGE, 4)
 


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

    arcade.run()

if __name__ == "__main__":
    main()

 

如需要查看完整代码,请扫码付款:

VIP免费查看

已有0人支付

本站所有作品,教程等皆为原创,盗版必究。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。扫码付款后的朋友可以加QQ: 406273900,提供技术支持及发送作品图片与配音素材。亦可直接向微信号scratch8付款购买。
李兴球的博客_Python创意编程技术前沿 » python愤怒的小鸟,撞击单摆练习程序_pymunk_pendulum

学本领,探索更大的世界!

李兴球博客 风火轮编程主页