前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >通过python turtle画图模块做

通过python turtle画图模块做

作者头像
py3study
发布2020-01-07 15:54:36
3960
发布2020-01-07 15:54:36
举报
文章被收录于专栏:python3python3

最新再看python3,发现了一个“海龟”画图模块,就上手用了一下,主要用到一些简单的函数和列表适合初学者浏览。windows下写的,可以使用pyinstall生成exe文件。

代码语言:javascript
复制
#Copyright LeoYuan 2017
#mail:centos@126.com
__author__ = 'leoyuan'
import turtle as t
import time
import random
import sys

def screenint():
    t.title("骰子游戏!")
    try:
        t.bgpic("touzi.gif")
    except:
        pass
    t.setup(width=570, height=350, startx=400, starty=300)
    t.screensize(500, 300)
#os->offset

def num(os):
    global one, two, three, four, five, six
    one = (1, (50 + os, -50))
    two = (2, (25 + os, -50), (75 + os, -50))
    three = (3, (50 + os, -25), (25 + os, -75), (75 + os, -75))
    four = (4, (25 + os, -25), (75 + os, -25),
           (25 + os, -75), (75 + os, -75))
    five = (5, (25 + os, -25), (75 + os, -25),
           (25 + os, -75), (75 + os, -75), (50 + os, -50))
    six = (6, (25 + os, -25), (75 + os, -25),
          (25 + os, -75), (75 + os, -75),
          (25 + os, -50), (75 + os, -50))

def user_int():
    global user_count, ai_count, name
    ai_count = random.choice(('one', 'two', 'three', 'four', 'five', 'six'))
    user_count = random.choice(('one', 'two', 'three', 'four', 'five', 'six'))
    name = t.textinput('完善信息', '输入姓名:')
    t.up()
    t.goto(-100, 30)
    try:
        t.write(name + "正在扔出骰子……", align='left', font=('微软雅黑', 14, 'normal'))
    except:
        sys.exit(0)
    time.sleep(2)
    t.clear()

def ai_int():
    t.up()
    t.goto(100, 30)
    t.write("电脑正在扔出骰子……", align='right', font=('微软雅黑', 14, 'normal'))
    time.sleep(2)
    t.undo()

def beauty():
    beauty_c = 0
    t.setx(-200)
    t.pensize(2)
    t.down()
    t.color('red', 'yellow')
    t.speed(8)
    t.begin_fill()
    while True:
        beauty_c += 1
        t.fd(200)
        t.lt(170)
        if beauty_c == 36:
            break
    t.end_fill()
    t.done()

def pk():
    u_count = int(eval(user_count)[0])
    a_count = int(eval(ai_count)[0])
    if u_count == a_count:
        t.write('打成平局!', align='right', font=('微软雅黑', 30, 'normal'))
    elif u_count > a_count:
        t.write('恭喜' + name + '胜利!', align='right', font=('微软雅黑', 30, 'normal'))
        beauty()
    else:
        t.write('好可惜!电脑赢了!', align='right', font=('微软雅黑', 30, 'normal'))
    time.sleep(2)
    t.bye()

def draw_dot(n):
    for d in range(n[0]):
        x = n[d + 1][0]
        y = n[d + 1][1]
        t.goto(x, y)
        t.dot(25, 'red')
        t.up()

def frame(dot, os):
    t.color('black')
    t.pensize(5)
    t.up()
    t.goto(0 + os, 0)
    t.down()
    t.speed(10)
    for i in range(4):
        t.forward(100)
        t.right(90)
    t.up()
    draw_dot(dot)
    time.sleep(1)

screenint()
user_int()
num(-150)
frame(eval(user_count), -150)
ai_int()
num(100)
frame(eval(ai_count),100)
pk()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档