def periodicTimer():

  global isQuit
  global food
  global score        # pacman's achieved score
  turtle.tracer(0, 0) # disable screen updates


  p_old = pm.getPosition()
  pm.move()           # move pacman
  p_new = pm.getPosition()
  # Check for illegal moves:
  if (abs(p_old[0] - p_new[0]) > 12) or (abs(p_old[1] - p_new[1]) > 12):
    print('Illegal move, game terminated.')
    isQuit = True
  turtle.update()      # force turtle module's screen update
  turtle.tracer(1, 10) # re-enable periodic screen updates
  pm.decrementSteps()  # pacman made one step, decrement counter
  if pm.getRemainingSteps() == 0:
    print('You ran out of steps!')
    isQuit = True 

  pm_pos = pm.getPosition()

  # check collisions pm versus food:
  eaten_dishes = []
  for dish in food:
    if checkCollision(pm_pos, dish.getPosition()):
      pm.setIsYum()
      dish.setIsEaten()
      eaten_dishes.append(dish)
      score += 1
  # Remove eaten dishes (cannot remove while iterating!):
  for dish in eaten_dishes:
    food.remove(dish)
  # Check if all food has been eaten already:
  if len(food) == 0:
    print('Congratulation, all food collected.')
    isQuit = True

  if isQuit:
    # Game will terminate, put the "Game Over" image:
    game_over.setposition(0, -HEIGHT//2 + 22)
    game_over.shape('game_over.gif')
    game_over.showturtle()
    # Trigger the shutdown handler function to be called in MS_TO_QUIT ms
    # from now:
    window.ontimer(shutDownHander, MS_TO_QUIT)
  else:
    # Trigger the next firing of our timer function, in 90ms from now:
    window.ontimer(periodicTimer, 90)
예제 #2
0
def periodicTimer():
    """Timer function which is called periodically by Python.
     This function implements the game logic:
     - creating all objects
     - arranging objects on the screen
     - calling object's move() methods
     - checking for collisions
     - checking for invalid moves
     - detection program termination conditions (all food eaten, ...)
     - initiate termination
  """
    global phase
    global isQuit
    global food
    global score  # doboggi_man's achieved score
    turtle.tracer(0, 0)  # disable screen updates
    for g in ghosts:
        g.updateShape()  # make ghost change its shape
        g.move()  # move ghost
    if phase % 4 == 0:
        # pm shape is updated every 4th iteration.
        pm.updateShape()  # make doboggi_man change its shape
    p_old = pm.getPosition()
    pm.move()  # move doboggi_man
    p_new = pm.getPosition()
    # Check for illegal moves:
    if (abs(p_old[0] - p_new[0]) > 12) or (abs(p_old[1] - p_new[1]) > 12):
        print('Illegal move, game terminated.')
        isQuit = True
    turtle.update()  # force turtle module's screen update
    turtle.tracer(1, 10)  # re-enable periodic screen updates
    pm.decrementSteps()  # doboggi_man made one step, decrement counter
    if pm.getRemainingSteps() == 0:
        print('You ran out of steps!')
        isQuit = True
    # check collisions pm versus ghosts:
    pm_pos = pm.getPosition()
    for g in ghosts:
        if checkCollision(pm_pos, g.getPosition()):
            isQuit = True
            print('You bumped into a ghost!')
    # check collisions pm versus food:
    eaten_dishes = []
    for dish in food:
        if checkCollision(pm_pos, dish.getPosition()):
            pm.setIsYum()
            dish.setIsEaten()
            eaten_dishes.append(dish)
            score += 1
    # Remove eaten dishes (cannot remove while iterating!):
    for dish in eaten_dishes:
        food.remove(dish)
    # Check if all food has been eaten already:
    if len(food) == 0:
        print('Congratulation, all food collected.')
        isQuit = True

    if phase == 7:
        phase = 0
    else:
        phase += 1
    if isQuit:
        # Game will terminate, put the "Game Over" image:
        game_over.setposition(0, -HEIGHT // 2 + 22)
        game_over.shape('img/game_over.gif')
        game_over.showturtle()
        # Trigger the shutdown handler function to be called in MS_TO_QUIT ms
        # from now:
        window.ontimer(shutDownHander, MS_TO_QUIT)
    else:
        # Trigger the next firing of our timer function, in 90ms from now:
        window.ontimer(periodicTimer, 90)