import pgzrun
WIDTH = 800
HEIGHT = 600
TITLE = '见缝插针'
needles = []
needle = Actor('needle', (200, 300), anchor = (170+50, 'top'))
needles.append(needle)
speed=1 #最开始针旋转的角度设置为1
fen=0 #分数最开始设置为0
def draw():
screen.fill('light yellow')
for needle in needles:
needle.draw()
screen.draw.filled_circle((400, 300), 80, 'dark blue') #画一个深蓝色的圆
screen.draw.text('score:'+str(fen),(10,10),color='red',fontsize=50) #显示分数
if speed==0:
screen.draw.text('Game Over',(220,250),color='red',fontsize=100) #显示游戏失败
def update():
for needle in needles:
if needle.x == 400:
needle.angle += speed #角度每次旋转speed度
# screen.clear()
def on_key_down(key):
global needle,speed,fen #声明speed,fen是全局变量
if key == keys.SPACE:
needle = Actor('needle', (400, 300), anchor = (170+50, 'top'))
for n in needles: #检查圆上的所有针
if n.colliderect(needle): #如果圆上的针碰到了新的针,打印游戏失败
print('游戏失败')
speed=0 #游戏失败时,旋转速度设为0
music.play_once('溜走') #播放溜走
needles.append(needle)
if speed == 1: #如果速度是1,那么分数就加1
fen=fen+1
music.play_once('弹簧') #播放弹簧
pgzrun.go()