#! /usr/bin/env python # -*- coding: utf-8 -*- #========================================================== # mosca.py - un pequeño divertimento para trabajar con el # módulo turtle. #---------------------------------------------------------- # FJA - fja@neocipres.com Marzo de 2015 (12/12/15) #========================================================== import random import os from turtle import * # Sonidos beepS = " -f 659 -l 460 -n -f 784 -l 340 -n -f 659 -l 230 -n -f 659 -l 110"+ \ " -n -f 880 -l 230 -n -f 659 -l 230 -n -f 587 -l 230 -n -f 659 -l 460"+ \ " -n -f 988 -l 340 -n -f 659 -l 230 -n -f 659 -l 110 -n -f 1047-l 230"+ \ " -n -f 988 -l 230 -n -f 784 -l 230 -n -f 659 -l 230 -n -f 988 -l 230"+ \ " -n -f 1318 -l 230 -n -f 659 -l 110 -n -f 587 -l 230 -n -f 587 -l 110"+ \ " -n -f 494 -l 230 -n -f 740 -l 230 -n -f 659 -l 460" beepP = " -f 50" # Preparamos la ventana v = Screen() v.setup(800, 600) v.title("El revoloteo de una mosca") v.bgcolor('#00FFFF') # Preparamos la tortuga y el entorno t = Turtle() t.pencolor('#00FFFF') t.hideturtle() t.setpos(-350, 200) t.pencolor('#0000FF') t.fd(50) t.lt(90) t.fd(50) t.lt(90) t.fd(50) t.lt(90) t.fd(50) t.pencolor('#00FFFF') t.home() t.pencolor('#000000') t.showturtle() t.penup() # Movemos la mosca npasos = 0 mover = True while mover: pasos = random.randrange(0, 30) giro = random.randrange(0, 90) pog = random.randrange(0, 3) npasos = npasos + pasos if pog == 0: t.fd(pasos+5) elif pog == 1: t.lt(giro) t.fd(pasos) elif pog == 2: t.rt(giro) t.fd(pasos) tx, ty = t.pos() if tx < -400: t.setx(-380) os.system("beep" + beepP) elif tx > 400: t.setx(380) os.system("beep" + beepP) elif ty < -300: t.sety(-280) os.system("beep" + beepP) elif ty > 300: t.sety(280) os.system("beep" + beepP) if tx > -350 and tx < -300 and ty > 200 and ty < 250: mover = False t.write("> "+str(npasos), False, 'right', ("Helvetica", 11, "normal")) t.hideturtle() os.system("beep" + beepS) v.exitonclick()