how do you run more than one function at the same time?
i would have thought if i had a function declared (that did a looping function)
code would carry on running after the function was called and do everything at once
not the case.
i have seen a few examples and tried to run them,but no result
thanks
running more than one function at once
Page 1 of 15 Replies - 937 Views - Last Post: 06 May 2016 - 01:54 PM
Replies To: running more than one function at once
#2
Re: running more than one function at once
Posted 06 May 2016 - 12:27 PM
#3
Re: running more than one function at once
Posted 06 May 2016 - 12:28 PM
Quote
i would have thought if i had a function declared (that did a looping function)
Are you talking about a recursive function? (A function that calls itself repeatedly until some condition is met.)
As a general rule, programs don't run more than one thing at the same time. For that you need threading, which is not something that happens automatically. Takes a lot of effort to get right, in fact.
If you call a function, the program will complete whatever task you place inside that function before moving on. If you put an infinite loop inside it, for example, it will never stop running, and whatever comes next will not happen.
#4
Re: running more than one function at once
Posted 06 May 2016 - 12:38 PM
So you want concurrent execution? There are some libraries about this in python, but you should be warned, threading can lead to weird bugs.
I would start by looking at some tutorials on concurrency in python, such as this one
If you're just wondering why this doesn't happen automatically: well, it doesn't. In standard programming models, programs execute instructions in sequence, from first to last. This doesn't always make the best use of multiple processors, but it's a lot easier to reason about. Basically, if you have two threads that read from the same object, you're probably okay, but if you have a thread that modifies some object, and another one that reads from it, then the latter thread can get two different values depending on how those threads execute (pre- or post-modification). And if you have two threads that modify the same object conditionally, based on its value, then of course you can get all sorts of fun - thread A might read and modify first, or thread B might, or you might get thread A reading, then thread B reading, then A tries to modify and then B tries to modify (based on the wrong data). So this sort of thing can create much confusion if you're not careful, or, frankly, even if you are careful.
I would start by looking at some tutorials on concurrency in python, such as this one
If you're just wondering why this doesn't happen automatically: well, it doesn't. In standard programming models, programs execute instructions in sequence, from first to last. This doesn't always make the best use of multiple processors, but it's a lot easier to reason about. Basically, if you have two threads that read from the same object, you're probably okay, but if you have a thread that modifies some object, and another one that reads from it, then the latter thread can get two different values depending on how those threads execute (pre- or post-modification). And if you have two threads that modify the same object conditionally, based on its value, then of course you can get all sorts of fun - thread A might read and modify first, or thread B might, or you might get thread A reading, then thread B reading, then A tries to modify and then B tries to modify (based on the wrong data). So this sort of thing can create much confusion if you're not careful, or, frankly, even if you are careful.
#5
Re: running more than one function at once
Posted 06 May 2016 - 01:35 PM
i have a ufo moving around the screen
here is the code:
as you can see the ufo movement is a function (which is called right at the end of the function definition)
but no more code runs after ufo(x,y)
so how can i get more code running like screen changes and tanks shooting at me etc etc
i would have thought i would have to run all the functions to get a game to run
what is the procedure to build a game in pygame?
here is the code:
def ufo(x,y):
x_change = 0
y_change = 0
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -2
elif event.key == pygame.K_RIGHT:
x_change = +2
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_change = -2
elif event.key == pygame.K_DOWN:
y_change = 2
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
if x_change + x > boundryLeft and x_change + x < boundryRight - 60:
x += x_change
if y_change + y > boundryTop and y_change + y < boundryBottom - 30:
y += y_change
screen.blit (bground1, (0,0))
screen.blit(ufoImg, (x, y))
pygame.display.update()
clock.tick(60)
ufo(x,y)
as you can see the ufo movement is a function (which is called right at the end of the function definition)
but no more code runs after ufo(x,y)
so how can i get more code running like screen changes and tanks shooting at me etc etc
i would have thought i would have to run all the functions to get a game to run
what is the procedure to build a game in pygame?
#6
Re: running more than one function at once
Posted 06 May 2016 - 01:54 PM
The loop in your ufo function is essentially an infinite loop, which runs continually until the game is ended. It blocks all other execution in the meanwhile, which is why nothing happens after the ufo(x,y) call.
In a simple, single-threaded, game you need a single event loop (also referred to as a game loop) that handles everything, not just the movements and drawing of one piece of the game.
I suggest you research that term. There is much written about it, as it is a fundamental concept of game development.
In a simple, single-threaded, game you need a single event loop (also referred to as a game loop) that handles everything, not just the movements and drawing of one piece of the game.
I suggest you research that term. There is much written about it, as it is a fundamental concept of game development.
Page 1 of 1

New Topic/Question
Reply


MultiQuote






|