4炮打小球 freepygame

炮打小球
想了半天 我想实现点击屏幕出现一个小球后 再点击屏幕还会出现别的小球,
想用列表存储小球 不知道为什么加入第二个小球后屏幕卡住了

"""Cannon, hitting targets with projectiles.
炮弹打小球
Exercises

1. Keep score by counting target hits.
2. Vary the effect of gravity.
3. Apply gravity to the targets.
4. Change the speed of the ball.

"""

from random import randrange
from turtle import *
from freegames import vector

ball = vector(-200, -200)
speed = vector(0, 0)
targets = []


def tap(x, y):
    """Respond to screen tap."""
    if not inside(ball):
        ball.x = -199
        ball.y = -199
        speed.x = (x + 200) / 25
        speed.y = (y + 200) / 25


def inside(xy):
    """Return True if xy within screen."""
    return -200 < xy.x < 200 and -200 < xy.y < 200


def draw_ball():
    """Draw ball and targets."""
    # clear()

    if inside(ball):
        goto(ball.x, ball.y)
        dot(6, 'red')

    update()


def draw_target():
    """Draw ball and targets."""
    clear()  # 清屏

    for target in targets:
        goto(target.x, target.y)
        dot(20, 'blue')

    # if inside(ball):
    #     goto(ball.x, ball.y)
    #     dot(6, 'red')
    #
    # update()


def move():
    """Move ball and targets."""
    if randrange(40) == 0:  # 添加目标的位置
        y = randrange(-150, 150)
        target = vector(200, y)
        targets.append(target)

    for target in targets:  # 目标移动向左 0.5
        target.x -= 0.5

    if inside(ball):
        # print(ball)
        print(speed)
        print(type(ball))
        # speed.y -= 0.35   # 控制x y方向的速度不同  实现小球抛出的效果
        ball.move(speed)

    dupe = targets.copy()
    targets.clear()

    for target in dupe:   # 实现攻击消除效果
        if abs(target - ball) > 13:
            targets.append(target)

    draw_target()
    draw_ball()

    for target in targets:
        if not inside(target):
            return

    ontimer(move, 50)


setup(420, 420, 370, 0)
hideturtle()
up()
tracer(False)
onscreenclick(tap)  # 点击屏幕 触发tap函数
move()
done()

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值