用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - python代码库

打乒乓

2020-03-09 作者:scp-173举报

[python]代码库

import turtle as t

#2. 创建背景
game = t.Screen()
game.title("打乒乓")
game.bgcolor("black")
game.setup(800,600)
game.tracer(0)

#3. 创建球拍
xm = t.Turtle()
xm.ht() #先隐藏
xm.up()
xm.speed(0)
xm.color('yellow')
xm.shape('square')
xm.shapesize(5, 1)
xm.goto(-350, 0)
xm.st() #再显示

#5. 创建如花
ruhua = t.Turtle()
ruhua.ht() #先隐藏
ruhua.up()
ruhua.speed(0)
ruhua.color('white')
ruhua.shape('square')
ruhua.shapesize(5, 1)
ruhua.goto(350, 0)
ruhua.st() #再显示


#6. 创建乒乓球
pp = t.Turtle()
pp.up()
pp.speed(0)
pp.color('white')
pp.shape('circle')
pp.st() #再显示
pp.dx = 2
pp.dy = 2

player_speed = 10
xm_score = 0
ruhua_score = 0

def write_score():
    pen.clear()
    score_text = "小明:{}  如花:{}".format(xm_score, ruhua_score)
    pen.write(score_text, align="center", font=("Arial", 20, 'bold'))

pen = t.Turtle()
pen.ht()
pen.up()
pen.color('white')
pen.goto(-30, 250)
write_score()

    

def xm_up():
    y = xm.ycor()
    y = y + player_speed
    xm.sety(y)

def xm_down():
    y = xm.ycor()
    y = y - player_speed
    xm.sety(y)

def ruhua_up():
    y = ruhua.ycor()
    y = y + player_speed
    ruhua.sety(y)

def ruhua_down():
    y = ruhua.ycor()
    y = y - player_speed
    ruhua.sety(y)

game.listen()
game.onkey(xm_up, 's')
game.onkey(xm_down, 'x')
game.onkey(ruhua_up, 'Up')
game.onkey(ruhua_down, 'Down')

#判定是否要退出
running = True 
def stop_loop():
    global running
    running = False

#获得窗口的Tk对象,并注册关闭事件
root = game.getcanvas().winfo_toplevel()
root.protocol('WM_DELETE_WINDOW', stop_loop)

#主循环
while running:
    game.update()
    pp.setx(pp.xcor() + pp.dx)
    pp.sety(pp.ycor() + pp.dy)
    
    if(pp.ycor() > 290) or (pp.ycor() < -290):
        pp.dy *= -1
    
    #9 接球
    y_up = ruhua.ycor()+50
    y_down = ruhua.ycor()-50
    if(pp.ycor() < y_up and pp.ycor() > y_down and pp.xcor() > 340):
        pp.dx *= -1
        pp.setx(339)

    if(pp.ycor() < xm.ycor() + 50 and pp.ycor() > xm.ycor() - 50 and pp.xcor() < -340):
        pp.dx *= -1
        pp.setx(-339)
    
    #10 球出界
    if (pp.xcor() > 380):
        pp.goto(0,0)
        xm_score += 1
        print("小明得分!")
        write_score()
        
    if (pp.xcor() < -380):
        pp.goto(0,0)
        ruhua_score += 1
        print("如花得分!")
        write_score()

#game.mainloop()


分享到:
更多

网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。