In [ ]:
'''Code5-1-4 第1步 
导入模块
界面初始化
准备颜色元组、变量
填充窗口、画圆、更新画面
防止未响应
'''
# A
import pygame as pg
import random,math
pg.init()
sc=pg.display.set_mode((600,700))
# B
black=(0,0,0)
white=(255,255,255)
red=(255,0,0)
# C
sc.fill(white)
pg.draw.circle(sc,red,(300,300),300,1)
pg.draw.circle(sc,black,(300,300),1,1)
pg.display.update()
# D
while True:
    event = pg.event.poll()
    if event.type == pg.QUIT:
        pg.quit()
        exit()
In [ ]:
'''Code5-1-4 第2步 
部分代码通过变量循环10000次
获取x,y的随机值
绘制随机小圆点
更新画面
'''
import pygame as pg
import random,math
pg.init()
sc=pg.display.set_mode((600,700))
black=(0,0,0)
white=(255,255,255)
red=(255,0,0)
sc.fill(white)
pg.draw.circle(sc,red,(300,300),300,1)
pg.display.update()

# A
times=0
while True:
    if times<10000:
        times+=1
        x=random.randint(0,600)
        y=random.randint(0,600)
        pg.draw.circle(sc,black,(x,y),1,1)
        pg.display.update()

    event = pg.event.poll()
    if event.type == pg.QUIT:
        pg.quit()
        exit()
In [ ]:
'''Code5-1-4 第3步 
记录落在圆内的小点个数
小点落圆内使用红色,落圆外使用黑色
'''
import pygame as pg
import random,math
pg.init()
sc=pg.display.set_mode((600,700))
black=(0,0,0)
white=(255,255,255)
red=(255,0,0)
sc.fill(white)
pg.draw.circle(sc,red,(300,300),300,1)
pg.display.update()

# A
times = 0
inside = 0
while True:
    if times<10000:
        x=random.randint(0,600)
        y=random.randint(0,600)
        pg.draw.circle(sc,black,(x,y),1,1)

        # B
        dis=math.sqrt((x-300)**2+(y-300)**2)
        if dis<=300:
            inside+=1
            pg.draw.circle(sc,red,(x,y),1,1)
        else:
            pg.draw.circle(sc,black,(x,y),1,1)
        times+=1
        pg.display.update()
        
    event = pg.event.poll()
    if event.type == pg.QUIT:
        pg.quit()
        exit()
In [ ]:
'''Code5-1-4 第4步
设置变量,设置字体
白底覆盖之前的文字,渲染、传输新的文字
计算并输出π值
'''
import pygame as pg
import random,math
pg.init()
sc=pg.display.set_mode((600,700))
black=(0,0,0)
white=(255,255,255)
red=(255,0,0)
sc.fill(white)
pg.draw.circle(sc,red,(300,300),300,1)
pg.display.update()

# A
inside=0

times=0
mf = pg.font.Font(r"c:\windows\fonts\msyh.ttc", 50)

while True:
    if times<10000000000:
        x=random.randint(0,600)
        y=random.randint(0,600)
        
        # B
        dis=math.sqrt((x-300)**2+(y-300)**2)
        if dis<=300:
            inside+=1
            pg.draw.circle(sc,red,(x,y),1,1)
        else:
            pg.draw.circle(sc,black,(x,y),1,1)
        times+=1
        pg.draw.rect(sc,white,(0,600,600,100))
        
        # C
        text = mf.render("π≈"+str(inside*4/times), True , (0,0,0))

        sc.blit(text, (0, 600))
        pg.display.update()
    event = pg.event.poll()
    if event.type == pg.QUIT:
        pg.quit()
        exit()
pygame 2.0.0 (SDL 2.0.12, python 3.8.10)
Hello from the pygame community. https://www.pygame.org/contribute.html
In [3]:
'''Code5-1-4 拓展
别的方法也是有的
π/4==1-1/3+1/5-1/7+1/9......
'''
pi=1
for i in range(1,10000000):
    if i%2==1:
        pi-=1/(1+i*2)
    else:
        pi+=1/(1+i*2)
pi*=4
print(pi)
3.1415925535897915