栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

移动的挡板1

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

移动的挡板1

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()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/732533.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号