Pygame simulation of a production line












0












$begingroup$


So my code works as intended, but suffers major performance issues every time I add a new object to the screen. The speed of my objects basically gets cut in half.



All you have to do to observe this is run the code and hit spacebar to create a new object. it is suppose to represent a production line. the product is the red square and the workstation is the green square.



The first station should take 5 seconds,
The second station should take 8, and the third should be 3.



I'm guessing there is something fundamentally wrong with the way I am creating objects into the system that is leading to a slow run speed. Thoughts?



import pygame
import time


pygame.init()

screenx = 1200
screeny = 600

win = pygame.display.set_mode((screenx, screeny))

pygame.display.set_caption("simulation testing")

clock = pygame.time.Clock()


class Product(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.color = (255, 0, 0)
self.count = 0

def draw(self, win):
pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)

def move(self):
self.x += self.vel

def wait(self):
self.vel = 0

def count_and_finish(self):
global count
count += 1


class Workstation(object):
def __init__(self, name, x, y, width, height, cycletime):
self.name = name
self.x = x
self.y = y
self.width = width
self.height = height
self.cycletime = cycletime
self.color = (0, 255, 0)
self.complete = False

def draw(self, win):
pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)

def do_work(self):
global seconds
if seconds <= self.cycletime:
seconds += .1
time.sleep(.1)
else:
self.complete = True
seconds = 0


def redraw_screen():
win.fill((0, 0, 0))
pygame.draw.line(win, (255, 0, 0), (0, screeny // 2 + 155), (screenx, screeny // 2 + 155), 5)
pygame.draw.line(win, (255, 0, 0), (0, screeny // 2 - 155), (screenx, screeny // 2 - 155), 5)
ws1.draw(win)
ws2.draw(win)
ws3.draw(win)
for product in products:
product.draw(win)
pygame.display.update()


seconds = 0
count = 0
products =
ws1 = Workstation("ws1", 200, 165, 160, 165, 5)
ws2 = Workstation("ws2", 500, 165, 160, 165, 8)
ws3 = Workstation("ws3", 800, 165, 160, 165, 3)

run = True
while run:
clock.tick(30)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

keys = pygame.key.get_pressed()

if keys[pygame.K_SPACE]:
time.sleep(.25)
products.append(Product(0, round(screeny // 2 - 75), 150, 150))

for product in products:
if product.x >= 0 and product.x + product.width + 15 <= screenx:
if products.index(product) != 0:
if product.x >= products[products.index(product) - 1].x - product.width - 10:
product.wait()
elif product.x == ws1.x + 5:
product.wait()
ws1.do_work()
if ws1.complete:
product.vel = 5
ws1.complete = False
product.move()
elif product.x == ws2.x + 5:
product.wait()
ws2.do_work()
if ws2.complete:
product.vel = 5
ws2.complete = False
product.move()
elif product.x == ws3.x + 5:
product.wait()
ws3.do_work()
if ws3.complete:
product.vel = 5
ws3.complete = False
product.move()
else:
product.vel = 5
product.move()
else:
if product.x == ws1.x + 5:
product.wait()
ws1.do_work()
if ws1.complete:
product.vel = 5
ws1.complete = False
product.move()
elif product.x == ws2.x + 5:
product.wait()
ws2.do_work()
if ws2.complete:
product.vel = 5
ws2.complete = False
product.move()
elif product.x == ws3.x + 5:
product.wait()
ws3.do_work()
if ws3.complete:
product.vel = 5
ws3.complete = False
product.move()
else:
product.vel = 5
product.move()
else:
products.pop(products.index(product))
product.count_and_finish()

print(str(count))

redraw_screen()

pygame.quit()









share|improve this question











$endgroup$

















    0












    $begingroup$


    So my code works as intended, but suffers major performance issues every time I add a new object to the screen. The speed of my objects basically gets cut in half.



    All you have to do to observe this is run the code and hit spacebar to create a new object. it is suppose to represent a production line. the product is the red square and the workstation is the green square.



    The first station should take 5 seconds,
    The second station should take 8, and the third should be 3.



    I'm guessing there is something fundamentally wrong with the way I am creating objects into the system that is leading to a slow run speed. Thoughts?



    import pygame
    import time


    pygame.init()

    screenx = 1200
    screeny = 600

    win = pygame.display.set_mode((screenx, screeny))

    pygame.display.set_caption("simulation testing")

    clock = pygame.time.Clock()


    class Product(object):
    def __init__(self, x, y, width, height):
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.vel = 5
    self.color = (255, 0, 0)
    self.count = 0

    def draw(self, win):
    pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)

    def move(self):
    self.x += self.vel

    def wait(self):
    self.vel = 0

    def count_and_finish(self):
    global count
    count += 1


    class Workstation(object):
    def __init__(self, name, x, y, width, height, cycletime):
    self.name = name
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.cycletime = cycletime
    self.color = (0, 255, 0)
    self.complete = False

    def draw(self, win):
    pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)

    def do_work(self):
    global seconds
    if seconds <= self.cycletime:
    seconds += .1
    time.sleep(.1)
    else:
    self.complete = True
    seconds = 0


    def redraw_screen():
    win.fill((0, 0, 0))
    pygame.draw.line(win, (255, 0, 0), (0, screeny // 2 + 155), (screenx, screeny // 2 + 155), 5)
    pygame.draw.line(win, (255, 0, 0), (0, screeny // 2 - 155), (screenx, screeny // 2 - 155), 5)
    ws1.draw(win)
    ws2.draw(win)
    ws3.draw(win)
    for product in products:
    product.draw(win)
    pygame.display.update()


    seconds = 0
    count = 0
    products =
    ws1 = Workstation("ws1", 200, 165, 160, 165, 5)
    ws2 = Workstation("ws2", 500, 165, 160, 165, 8)
    ws3 = Workstation("ws3", 800, 165, 160, 165, 3)

    run = True
    while run:
    clock.tick(30)

    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    run = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE]:
    time.sleep(.25)
    products.append(Product(0, round(screeny // 2 - 75), 150, 150))

    for product in products:
    if product.x >= 0 and product.x + product.width + 15 <= screenx:
    if products.index(product) != 0:
    if product.x >= products[products.index(product) - 1].x - product.width - 10:
    product.wait()
    elif product.x == ws1.x + 5:
    product.wait()
    ws1.do_work()
    if ws1.complete:
    product.vel = 5
    ws1.complete = False
    product.move()
    elif product.x == ws2.x + 5:
    product.wait()
    ws2.do_work()
    if ws2.complete:
    product.vel = 5
    ws2.complete = False
    product.move()
    elif product.x == ws3.x + 5:
    product.wait()
    ws3.do_work()
    if ws3.complete:
    product.vel = 5
    ws3.complete = False
    product.move()
    else:
    product.vel = 5
    product.move()
    else:
    if product.x == ws1.x + 5:
    product.wait()
    ws1.do_work()
    if ws1.complete:
    product.vel = 5
    ws1.complete = False
    product.move()
    elif product.x == ws2.x + 5:
    product.wait()
    ws2.do_work()
    if ws2.complete:
    product.vel = 5
    ws2.complete = False
    product.move()
    elif product.x == ws3.x + 5:
    product.wait()
    ws3.do_work()
    if ws3.complete:
    product.vel = 5
    ws3.complete = False
    product.move()
    else:
    product.vel = 5
    product.move()
    else:
    products.pop(products.index(product))
    product.count_and_finish()

    print(str(count))

    redraw_screen()

    pygame.quit()









    share|improve this question











    $endgroup$















      0












      0








      0





      $begingroup$


      So my code works as intended, but suffers major performance issues every time I add a new object to the screen. The speed of my objects basically gets cut in half.



      All you have to do to observe this is run the code and hit spacebar to create a new object. it is suppose to represent a production line. the product is the red square and the workstation is the green square.



      The first station should take 5 seconds,
      The second station should take 8, and the third should be 3.



      I'm guessing there is something fundamentally wrong with the way I am creating objects into the system that is leading to a slow run speed. Thoughts?



      import pygame
      import time


      pygame.init()

      screenx = 1200
      screeny = 600

      win = pygame.display.set_mode((screenx, screeny))

      pygame.display.set_caption("simulation testing")

      clock = pygame.time.Clock()


      class Product(object):
      def __init__(self, x, y, width, height):
      self.x = x
      self.y = y
      self.width = width
      self.height = height
      self.vel = 5
      self.color = (255, 0, 0)
      self.count = 0

      def draw(self, win):
      pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)

      def move(self):
      self.x += self.vel

      def wait(self):
      self.vel = 0

      def count_and_finish(self):
      global count
      count += 1


      class Workstation(object):
      def __init__(self, name, x, y, width, height, cycletime):
      self.name = name
      self.x = x
      self.y = y
      self.width = width
      self.height = height
      self.cycletime = cycletime
      self.color = (0, 255, 0)
      self.complete = False

      def draw(self, win):
      pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)

      def do_work(self):
      global seconds
      if seconds <= self.cycletime:
      seconds += .1
      time.sleep(.1)
      else:
      self.complete = True
      seconds = 0


      def redraw_screen():
      win.fill((0, 0, 0))
      pygame.draw.line(win, (255, 0, 0), (0, screeny // 2 + 155), (screenx, screeny // 2 + 155), 5)
      pygame.draw.line(win, (255, 0, 0), (0, screeny // 2 - 155), (screenx, screeny // 2 - 155), 5)
      ws1.draw(win)
      ws2.draw(win)
      ws3.draw(win)
      for product in products:
      product.draw(win)
      pygame.display.update()


      seconds = 0
      count = 0
      products =
      ws1 = Workstation("ws1", 200, 165, 160, 165, 5)
      ws2 = Workstation("ws2", 500, 165, 160, 165, 8)
      ws3 = Workstation("ws3", 800, 165, 160, 165, 3)

      run = True
      while run:
      clock.tick(30)

      for event in pygame.event.get():
      if event.type == pygame.QUIT:
      run = False

      keys = pygame.key.get_pressed()

      if keys[pygame.K_SPACE]:
      time.sleep(.25)
      products.append(Product(0, round(screeny // 2 - 75), 150, 150))

      for product in products:
      if product.x >= 0 and product.x + product.width + 15 <= screenx:
      if products.index(product) != 0:
      if product.x >= products[products.index(product) - 1].x - product.width - 10:
      product.wait()
      elif product.x == ws1.x + 5:
      product.wait()
      ws1.do_work()
      if ws1.complete:
      product.vel = 5
      ws1.complete = False
      product.move()
      elif product.x == ws2.x + 5:
      product.wait()
      ws2.do_work()
      if ws2.complete:
      product.vel = 5
      ws2.complete = False
      product.move()
      elif product.x == ws3.x + 5:
      product.wait()
      ws3.do_work()
      if ws3.complete:
      product.vel = 5
      ws3.complete = False
      product.move()
      else:
      product.vel = 5
      product.move()
      else:
      if product.x == ws1.x + 5:
      product.wait()
      ws1.do_work()
      if ws1.complete:
      product.vel = 5
      ws1.complete = False
      product.move()
      elif product.x == ws2.x + 5:
      product.wait()
      ws2.do_work()
      if ws2.complete:
      product.vel = 5
      ws2.complete = False
      product.move()
      elif product.x == ws3.x + 5:
      product.wait()
      ws3.do_work()
      if ws3.complete:
      product.vel = 5
      ws3.complete = False
      product.move()
      else:
      product.vel = 5
      product.move()
      else:
      products.pop(products.index(product))
      product.count_and_finish()

      print(str(count))

      redraw_screen()

      pygame.quit()









      share|improve this question











      $endgroup$




      So my code works as intended, but suffers major performance issues every time I add a new object to the screen. The speed of my objects basically gets cut in half.



      All you have to do to observe this is run the code and hit spacebar to create a new object. it is suppose to represent a production line. the product is the red square and the workstation is the green square.



      The first station should take 5 seconds,
      The second station should take 8, and the third should be 3.



      I'm guessing there is something fundamentally wrong with the way I am creating objects into the system that is leading to a slow run speed. Thoughts?



      import pygame
      import time


      pygame.init()

      screenx = 1200
      screeny = 600

      win = pygame.display.set_mode((screenx, screeny))

      pygame.display.set_caption("simulation testing")

      clock = pygame.time.Clock()


      class Product(object):
      def __init__(self, x, y, width, height):
      self.x = x
      self.y = y
      self.width = width
      self.height = height
      self.vel = 5
      self.color = (255, 0, 0)
      self.count = 0

      def draw(self, win):
      pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)

      def move(self):
      self.x += self.vel

      def wait(self):
      self.vel = 0

      def count_and_finish(self):
      global count
      count += 1


      class Workstation(object):
      def __init__(self, name, x, y, width, height, cycletime):
      self.name = name
      self.x = x
      self.y = y
      self.width = width
      self.height = height
      self.cycletime = cycletime
      self.color = (0, 255, 0)
      self.complete = False

      def draw(self, win):
      pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)

      def do_work(self):
      global seconds
      if seconds <= self.cycletime:
      seconds += .1
      time.sleep(.1)
      else:
      self.complete = True
      seconds = 0


      def redraw_screen():
      win.fill((0, 0, 0))
      pygame.draw.line(win, (255, 0, 0), (0, screeny // 2 + 155), (screenx, screeny // 2 + 155), 5)
      pygame.draw.line(win, (255, 0, 0), (0, screeny // 2 - 155), (screenx, screeny // 2 - 155), 5)
      ws1.draw(win)
      ws2.draw(win)
      ws3.draw(win)
      for product in products:
      product.draw(win)
      pygame.display.update()


      seconds = 0
      count = 0
      products =
      ws1 = Workstation("ws1", 200, 165, 160, 165, 5)
      ws2 = Workstation("ws2", 500, 165, 160, 165, 8)
      ws3 = Workstation("ws3", 800, 165, 160, 165, 3)

      run = True
      while run:
      clock.tick(30)

      for event in pygame.event.get():
      if event.type == pygame.QUIT:
      run = False

      keys = pygame.key.get_pressed()

      if keys[pygame.K_SPACE]:
      time.sleep(.25)
      products.append(Product(0, round(screeny // 2 - 75), 150, 150))

      for product in products:
      if product.x >= 0 and product.x + product.width + 15 <= screenx:
      if products.index(product) != 0:
      if product.x >= products[products.index(product) - 1].x - product.width - 10:
      product.wait()
      elif product.x == ws1.x + 5:
      product.wait()
      ws1.do_work()
      if ws1.complete:
      product.vel = 5
      ws1.complete = False
      product.move()
      elif product.x == ws2.x + 5:
      product.wait()
      ws2.do_work()
      if ws2.complete:
      product.vel = 5
      ws2.complete = False
      product.move()
      elif product.x == ws3.x + 5:
      product.wait()
      ws3.do_work()
      if ws3.complete:
      product.vel = 5
      ws3.complete = False
      product.move()
      else:
      product.vel = 5
      product.move()
      else:
      if product.x == ws1.x + 5:
      product.wait()
      ws1.do_work()
      if ws1.complete:
      product.vel = 5
      ws1.complete = False
      product.move()
      elif product.x == ws2.x + 5:
      product.wait()
      ws2.do_work()
      if ws2.complete:
      product.vel = 5
      ws2.complete = False
      product.move()
      elif product.x == ws3.x + 5:
      product.wait()
      ws3.do_work()
      if ws3.complete:
      product.vel = 5
      ws3.complete = False
      product.move()
      else:
      product.vel = 5
      product.move()
      else:
      products.pop(products.index(product))
      product.count_and_finish()

      print(str(count))

      redraw_screen()

      pygame.quit()






      python performance simulation pygame






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 19 mins ago









      200_success

      129k15153415




      129k15153415










      asked 3 hours ago









      KeizzerweissKeizzerweiss

      304




      304






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212175%2fpygame-simulation-of-a-production-line%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212175%2fpygame-simulation-of-a-production-line%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to make a Squid Proxy server?

          Is this a new Fibonacci Identity?

          19世紀