0

I am making a physics related program in pygame for my coursework. The topic is simple harmonic motion and my program will (hopefully) show a simple pendulum in motion alongside all the relative values of its speed, acceleration, displacement etc. To begin the simulation I require the user to enter some values about the motion of this pendulum, such as its amplitude, time period and the length of its string.

My first question is how would I get this input, rather than writing code for every scenario (i.e if keydown = 1:, if keydown = 2:, if keydown = 3: etc...) is there anyway of generalizing the whole list of integars so I can say, if the keydown is an integer then the time period = input.

Secondly I want this to happen during real time so that once they enter the number they can see it and amend it before pressing enter to confirm it, like you would on a normal form, how would I go about blitting the values in real time?

I apologize if the questions seem really naive but the only computer programming I have done so far have been very specific to the exam board syllabus which doesn't allow for a very wide scope.

2 Answers2

0

You could assign to flip through editing different properties. Typing something would invoke a restart method on the pendulum.

As for numbers, the keycodes are in order, so you can have an if statement to see if its between 0 and 9. Something like this:

for event in pygame.event.get():
        if event.type == KEYDOWN and event.key >= K_0 and event.key <= K_9:
            key = event.key - K_0
Bartlomiej Lewandowski
  • 10,771
  • 14
  • 44
  • 75
0

You could have them enter those values into the terminal at start up, then have a button which freezes your window and returns to the terminal to ask input when they click it, like this:

int amplitude = raw_input("enter the amplitude")
int length = raw_input("enter the length of the string")
...
while(true) //main loop
    if changevariables:
        int amplitude = raw_input("enter the new amplitude")
        ...
Chachmu
  • 7,725
  • 6
  • 30
  • 35