Eu estou aqui com um problema. Fiz uma simulação em Vpython, a simular um lançamento horizontal em que uma bola supostamente desce um escorrega(não está visível) e depois voa no ar(exemplo clássico). Recebe como Input a altura do chão à mesa, e a altura da mesa ao cimo do escorrega de onde a bola é lançada. Estava tudo bem até que quis por a simulação mais formal para poder apresentar na minha turma. Para tal inseri uma janela de controlos, onde posso clicar para repetir, repetir apenas numa dimensão... Até aqui estava tudo bem. Quando inseri sliders e um botão para que possa alterar os valores o programa deixou de funcionar. Aqui está o código:
- Código: Seleccionar Todos
from visual.controls import *
import math
# dados e calculos
def valores(a,b):
global h0,h1,g,vx,t_max,dx,t,vx,ox,oy
h0=a
h1=b
g = 10
vx = math.sqrt(2*g*h0)
t_max = math.sqrt((2*h1)/g)
dx = vx * t_max
t = 0.00
vf = math.sqrt(vx**2+(-g*t_max)**2)
# Janela principal
screen = display(center=(dx/2+9,h1/2), width = 900, height = 900)
screen.range = (dx+10,h0,1)
screen.autoscale = 0
# objectos
bola = sphere(pos=(-0.5,h1+0.5), color = color.red, radius = 0.5, make_trail = True)
ox = bola.pos.x
oy = bola.pos.y
mesa = box(pos=(-0.5,h1/2), color=color.green, widht = 0.4, height = h1)
chao = box(pos =(dx/2+9,0),color = color.blue, length = dx + 20 ,height = 1)
label(pos=(5,-2-0.05*h1),text = '5')
label(pos=(10,-2-0.05*h1),text = '10')
label(pos=(15,-2-0.05*h1),text = '15')
label(pos=(20,-2-0.05*h1),text = '20')
label(pos=(25,-2-0.05*h1),text = '25')
label(pos=(30,-2-0.05*h1),text = '30')
label(pos=(35,-2-0.05*h1),text = '35')
label(pos=(40,-2-0.05*h1),text = '40')
label(pos=(45,-2-0.05*h1),text = '45')
label(pos=(50,-2-0.05*h1),text = '50')
label(pos=(70,-2-0.05*h1),text = '70')
label(pos=(90,-2-0.05*h1),text = '90')
label(pos=(110,-2-0.05*h1),text = '110')
label(pos=(130,-2-0.05*h1),text = '130')
label(pos=(150,-2-0.05*h1),text = '150')
label(pos=(170,-2-0.05*h1),text = '170')
def xy(t,t_max,vx,g):
while t < t_max:
rate(100000)
t += 0.00001
bola.pos.x = vx * t
bola.pos.y = 0.5*-g*(t**2)+h1+0.5
k = 0
while k < 2:
rate(10)
k+=1
#Resultados
print "O alcance da bola foi:" , bola.pos.x
print "O tempo de queda foi:", t_max
print "A velocidade final da bola foi:", vf
bola.pos.x = ox
bola.pos.y = oy
def x(t,t_max,vx,g):
bola.pos.x = ox
bola.pos.y = oy
while t < t_max:
rate(100000)
t += 0.00001
bola.pos.x = vx * t
k = 0
while k < 2:
rate(10)
k+=1
#Resultados
print "O alcance da bola foi:" , bola.pos.x
print "O tempo de queda foi:", t_max
print "A velocidade final da bola foi:", vf
bola.pos.x = ox
def y(t,t_max,vx,g):
bola.pos.x = ox+2
bola.pos.y = oy
while t < t_max:
rate(100000)
t += 0.00001
bola.pos.y = 0.5*-g*(t**2)+h1
k = 0
while k < 2:
rate(10)
k+=1
#Resultados
print "O alcance da bola foi:" , bola.pos.x
print "O tempo de queda foi:", t_max
print "A velocidade final da bola foi:", g*t_max
bola.pos.y = oy
bola.pos.x = ox
#Janela de controlos
c = controls()
start = button(pos=(0,0), width=60,height=60, text='Comecar', action=lambda: xy(0.00,t_max,vx,g))
butao_x = button(pos=(-60,-60),width=30,height=30, text='x', action=lambda: x(0.00,t_max,vx,g))
butao_y = button(pos=(60,-60),width=30,height=30, text='y', action=lambda: y(0.00,t_max,vx,g))
h0_s = slider(pos=(-60,40), width=5, height=5, text="Altura da mesa ao ponto de lançamento")
h1_s = slider(pos=(-60,80), width=5, height=5, text="Altura da mesa ao chao")
passar = button(pos=(-50,0), width=30,height=15, text='Inico', action=lambda: valores(h0_s.value,h1_s.value))
O que alterei quando deixou de funcionar, foi por o código que actualmente está na função valores, nessa função. Quando estava livre(em nenhuma função) estava a funcionar bem, mas como queria fazer a implementação para poder mudar os valores das alturas, a única solução que arranjei foi inserir nessa função de modo a que quando clicasse no botão iniciar, ele abria uma nova janela com as alturas alteradas , e depois supostamente quando clicasse em começar, a bola deveria mover-se, o que não acontece. Se alguém me conseguir ajudar, agradecia
