I am working on a simulation that represents a production line with moving products.
My calculations of the arrival time from the products at a certain (x,y) on paper does not match the calculations of my simulation.
My iteration time of my pygame while loop takes 0.01 second with a very small uncertainty.
So my pixel movement is 0.5 and the length is, for example, 645 pixels. So I thought every step is 0.5 pixel and this will take 1290 steps in total. With an iteration time of 0.01 second this should have a duration of 12,9 second.
However, my simulation needs 16 seconds for a length of 645 pixels.
Can someone explain the difference ?
I made an piece of code with only one RECT so the iteration time is faster but there is still a difference
EXAMPLE :
import pygame, sys, time, random
init_time = time.time()
from pygame.locals import *
pygame.init()
w_x = 1550
w_y = 800
DISPLAYSURF = pygame.display.set_mode((w_x,w_y))
pygame.display.set_caption('COPAL')
## COLORS USED (SIZE CORRESPONDS TO COLOR)
# background
BLACK = (0,0,0)
# boxes
GREEN = (0,100,0)
PINK = (255,0,255)
BLUE = (0,0,128)
xx = 0
yy = 400
but_on = True
sum_duration = 0
total_iteration = 0
total = []
box = [[xx,yy]]
## Filling background
DISPLAYSURF.fill(BLACK)
start = time.time()
start2 = time.time()
while True:
init_end = time.time()
if but_on == True:
print(init_end-init_time)
but_on = False
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
DISPLAYSURF.fill(BLACK)
pygame.draw.rect(DISPLAYSURF,BLUE,(xx,yy,30,30))
pygame.draw.line(DISPLAYSURF,PINK,(645,0),(645,800))
xx += 0.5
if xx == 645:
timex = time.time()
print(timex - start2)
print(time_iteration_avg)
stop = time.time()
duration = stop - init_end
total.append(duration)
sum_duration += duration
time_iteration_avg = sum_duration / len ( total )
pygame.display.update()
EXAMPLE EDITED
import pygame, sys, time, random
init_time = time.time()
from pygame.locals import *
pygame.init()
# window aize
w_x = 1550
w_y = 800
# time for each step
ite = 0.00387
DISPLAYSURF = pygame.display.set_mode((w_x,w_y))
pygame.display.set_caption('COPAL')
# background
BLACK = (0,0,0)
# boxes
GREEN = (0,100,0)
PINK = (255,0,255)
BLUE = (0,0,128)
# start position
xx = 0
yy = 400
but_on = True
sum_duration = 0
total_iteration = 0
total = []
oo= 0
step = 0.5
duration = ite
box = [[xx,yy]]
## Filling background
DISPLAYSURF.fill(BLACK)
start = time.time()
start2 = time.time()
while True:
init_end = time.time()
if but_on == True:
init_t = (init_end - init_time)
print(init_t)
but_on = False
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
DISPLAYSURF.fill(BLACK)
pygame.draw.rect(DISPLAYSURF,BLUE,(xx,yy,30,30))
pygame.draw.line(DISPLAYSURF,PINK,(645,0),(645,800))
xx += step
if round(xx) == 645:
timex = time.time()
print("elapsed time ")
print(timex - start2)
print(time_iteration_avg)
if duration == ite:
step = 0.5
if duration < ite:
dif_ite = ite - duration
time.sleep(dif_ite)
print("Smaller")
step = 0.5
if duration > ite:
dif_pix = duration - ite
pix_ex = 5 / duration
step = 645 / pix_ex
print("Bigger")
total.append(duration)
sum_duration += duration
time_iteration_avg = sum_duration / len ( total )
print(step)
stop = time.time()
duration = stop - init_end
pygame.display.update()