python愤怒的小鸟arcade和pymunk制作by李兴球

以下是部分代码预览:

"""
用arcade+pymunk制作的简单的愤怒的小鸟程序,由于使用了物理引擎并结合arcade,相比用海龟画图模块制作的,它有逼真的物理效果。
由于相对于展示的是基本制作原理,配音封面等都是次要的,所以此版本没有配音,没有设计封面与游戏结束画面,

"""
__author__ = "lixingqiu"
__date__ = "2019/4/6"

import os
import math
import arcade
import pymunk
import timeit
from PIL import Image
 
SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 800
SCREEN_TITLE = "愤怒的小鸟"

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

 
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 = []

    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)

    arcade.run()


if __name__ == "__main__":
    main()

付费查看

已有0人支付

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

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

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