python街机游戏模块arcade制作的重力弹球

python街机游戏模块arcade制作的重力弹球

"""
python街机游戏模块arcade制作的重力弹球
 
"""

import arcade

# ---设置常量

# 屏幕尺寸
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600

# 圆的半径
CIRCLE_RADIUS = 20

# 重力常量
GRAVITY_CONSTANT = 0.3

# 减速用的常量
BOUNCINESS = 0.9


def draw(delta_time):
    """
    用此函数画所有对象
    """

    # 开始渲染,不需要结束渲染
    arcade.start_render()

    # 画球
    arcade.draw_circle_filled(draw.x, draw.y, CIRCLE_RADIUS,
                              arcade.color.RED)

    # 水平和垂直方向每次增中的值
    draw.x += draw.delta_x
    draw.y += draw.delta_y

    draw.delta_y -= GRAVITY_CONSTANT #  由于受到重力,垂直速度不断减小

    # 碰到左右边缘,水平速度不仅取反,还会减小一点
    if draw.x < CIRCLE_RADIUS and draw.delta_x < 0:
        draw.delta_x *= -BOUNCINESS
    elif draw.x > SCREEN_WIDTH - CIRCLE_RADIUS and draw.delta_x > 0:
        draw.delta_x *= -BOUNCINESS

    # 碰到下边缘
    if draw.y < CIRCLE_RADIUS and draw.delta_y < 0:
        # If we bounce with a decent velocity, do a normal bounce.
        # Otherwise we won't have enough time resolution to accurate represent
        # the bounce and it will bounce forever. So we'll divide the bounciness
        # by half to let it settle out.
        # 让垂直速度小到一定的时候让它不断地减半,这样就不会永远反弹。
        if draw.delta_y * -1 > GRAVITY_CONSTANT * 15:
            draw.delta_y *= -BOUNCINESS
        else:
            draw.delta_y *= -BOUNCINESS / 2



draw.x = CIRCLE_RADIUS                # python能给函数增加属性
draw.y = SCREEN_HEIGHT - CIRCLE_RADIUS
draw.delta_x = 2
draw.delta_y = 0


def main():
    # 打开一个窗口
    arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "python街机游戏模块arcade制作的重力弹球,翻译:李兴球")
    arcade.set_background_color(arcade.color.WHITE)

    # 80分之一秒调用一次draw函数。这个有点像海龟画图模块里屏幕对象的ontimer定时器功能。
    arcade.schedule(draw, 1 / 80)

    # 运行程序
    arcade.run()

    # 关闭窗口
    arcade.close_window()


if __name__ == "__main__":
    main()

 

李兴球

李兴球的博客是Python创意编程原创博客