炮打小球
想了半天 我想实现点击屏幕出现一个小球后 再点击屏幕还会出现别的小球,
想用列表存储小球 不知道为什么加入第二个小球后屏幕卡住了
"""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()