15
2024
11

python编程学习-小学数学加减分解题

学习python编程,在新东方少儿编程官网的自由创作平台做的一些作品。

可以在线运行,效果如下(在新页面中打开链接):

代码如下(用到平台自定义的xdf库):

from xdf import *

p0 = [200, 512+100]
p1 = [768-200, 512+100]
p2 = [768/2, 512-200]

#line0 = line(p0[0], p0[1], p1[0], p1[1])
line1 = line(p1[0], p1[1], p2[0], p2[1])
line2 = line(p2[0], p2[1], p0[0], p0[1])

c0 = circle(p0[0], p0[1], 100, 'blue')
c1 = circle(p1[0], p1[1], 100, 'blue')
c2 = circle(p2[0], p2[1], 100, 'blue')

t0 = text('', p0[0], p0[1] + 40, 100, 'center', 'black')
t1 = text('', p1[0], p1[1] + 40, 100, 'center', 'black')
t2 = text('', p2[0], p2[1] + 40, 100, 'center', 'black')

resText = text('', 768 / 2, 1024-200, 50, 'red', 'center')
scoreText = text('得分:0', 10, 50, 40, 'red')
isRunning = False
score = 0

a = [0, 0, 0]
n = 0
ts = [t0, t1, t2]
cs = [c0, c1, c2]
tp = False

def reset():
    global n,isRunning
    a[2] = random(2, 9)
    a[0] = random(1, a[2] - 1)
    a[1] = a[2] - a[0]
    n = random(3) - 1
    isRunning = True
    refresh()
    resText.change('')

def updateScore():
    global score
    scoreText.change("得分:" + str(score))
    
def refresh():
    for i in range(3):
        ts[i].change(str(a[i]))
        if i == n:
            ts[i].hide()
            cs[i].change('red')
        else:
            ts[i].show()
            cs[i].change('blue')

def loop():
    global tp,score,isRunning
    if tp:
        tp = False
        x = input("答案:")
        if not x:
            return
        try:
            b = int(x)
            if b == a[n]:
                ts[n].show()
                score = score + 1
                updateScore()
                isRunning = False
                resText.change('正确')
                delay(reset, 3000)
            else:
                resText.change('错误')
        except e:
            pass
            
def taped(m):
    global n,tp
    if m == n:
        tp = True
        
def tap0():
    taped(0)
    
def tap1():
    taped(1)

def tap2():
    taped(2)

c0.tap = tap0
c1.tap = tap1
c2.tap = tap2
reset()


也可以下载代码,本地python环境运行(用pygame封装了xdf库)。



« 上一篇下一篇 »

相关文章:

python编程学习-扫雷  (2024-11-15 11:32:58)

python编程学习-对角棋  (2024-11-15 11:32:8)

python编程学习-密室逃生  (2024-11-15 11:31:35)

python编程学习-围住神经猫  (2024-11-15 11:31:6)

python编程学习-四子棋  (2024-11-15 11:30:32)

python编程学习-华容道  (2024-11-14 16:43:25)

python编程学习-动画  (2024-11-14 16:39:37)

python编程学习-别踩白块  (2024-11-14 16:38:59)

python编程学习-俄罗斯方块  (2024-11-14 16:38:19)

python编程学习-井字棋  (2024-11-14 16:34:9)

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。