import turtle as t
#设置窗口大小
t.setup(610,610)
#设置画布大小
t.screensize(600,600)
#左,右挡板移初始的y坐标
state = {1:0,2:0}
#小球的初始坐标
ball = {‘x’:0,‘y’:0}
#小球移动的距离
dis = {‘x’:6,‘y’:3}
t.tracer(False)
#画挡板
def draw1(x,y):
t.up()
t.goto(x,y)
t.down()
t.begin_fill()
t.fillcolor(‘red’)
for i in range(2):
t.forward(30)
t.right(90)
t.forward(100)
t.right(90)
t.end_fill()
#画球
def draw2():
t.up()
t.goto(ball[‘x’],ball[‘y’])
t.down()
t.dot(30,‘yellow’)
#向上移动左挡板
def moveup1():
state[1] += 20
#向下移动左挡板
def movedown1():
state[1] -= 20
#向上移动右挡板
def moveup2():
state[2] += 20
#向下移动右挡板
def movedown2():
state[2] -= 20
#判断小球是否和挡板,边界发生碰撞(分析碰撞的x,y坐标范围)
def isZhuang():
#和左挡板发生碰撞
if abs(-270-ball[‘x’]) <=30 and state[2]-100<=ball[‘y’]<=state[2]:
#x坐标往反方向移动,即向左移动
dis['x'] = -dis['x']
# 和右挡板发生碰撞
if abs(270 - ball['x']) <= 30 and state[1] - 100 < ball['y'] < state[1]:
# x坐标往反方向移动,即向右移动
print(ball['x'])
dis['x'] = -dis['x']
#是否碰到左右边界
if ball['x'] >=300 or ball['x']<=-300:
return 1
#碰到上下边界
if ball['y'] >= 300 or ball['y'] <= -300:
# y坐标往反方向移动
dis['y'] = -dis['y']
#实现游戏效果
def game():
t.clear()
#画右挡板
draw1(270,state[1])
#画左挡板
draw1(-300, state[2])
#画小球
draw2()
print(‘ball’,ball[‘x’],ball[‘y’])
print(‘state’, state[1])
# #判断小球是否和挡板发生碰撞
# isZhuang()
#小球是否越界
if isZhuang()== 1:
return
# 移动小球
ball['x'] += dis['x']
ball['y'] += dis['y']
t.ontimer(game,100)
game()
t.onkey(moveup1,‘w’)
t.onkey(movedown1,‘s’)
t.onkey(moveup2,‘Up’)
t.onkey(movedown2,‘Down’)
t.listen()
t.done()



