2014-10-08 57 views
-2

我試圖製作遊戲與pygame的河內塔。 我最大的困難是現在我必須移動磁盤,這是我的代碼到目前爲止,但它不移動磁盤,我不知道爲什麼。我認爲它也只註冊第一個鼠標點擊而不是第二個。但它也不會刪除光盤。河內的塔(移動光盤)

import pygame, sys 
from pygame.locals import* 


pygame.init() 
screen = pygame.display.set_mode((500, 300)) 
pygame.display.set_caption("Towers of Hanoi") 

# Colours 
BLACK = ( 0, 0, 0) 
WHITE = (255, 255, 255) 
RED = (255, 0, 0) 
GREEN = ( 0, 255, 0) 
BLUE = ( 0, 0, 255) 
SILVER = (192, 192, 192) 
AQUA = (0, 255, 255) 
PURPLE = (128, 0, 128) 

# Background 
background = screen.fill(WHITE) 


# Platform 
pygame.draw.rect(screen, BLUE,(50, 250, 400, 20)) 


# Towers 
tower_left = pygame.draw.line(screen, SILVER,(125, 100), (125, 249), 20) 
tower_middle = pygame.draw.line(screen, SILVER,(250, 100), (250, 249), 20) 
tower_right = pygame.draw.line(screen, SILVER,(375, 100), (375, 249), 20) 


# Discs 
big = pygame.draw.ellipse(screen, RED,(70, 235, 115, 15)) 

middle = pygame.draw.ellipse(screen, GREEN,(75, 220, 105, 15)) 

small = pygame.draw.ellipse(screen, BLACK,(85, 205, 85, 15)) 


# List of towers (3 being the biggest) 
left_tower = [big, middle, small] 
middle_tower = [] 
right_tower = [] 

count = 0 

# Click locations 
clicks = [] 
for c in range(0, 1): 
    for event in pygame.event.get(): 
     if event.type == MOUSEBUTTONDOWN: 
      x, y = pygame.mouse.get_pos() 
      clicks.append([x, y]) 


# Main game loop 
while True: 

    for event in pygame.event.get(): 

     if event.type == QUIT: 
     pygame.quit() 
     sys.quit() 

     # Getting the mouseposition and moving the disc 
     elif event.type == MOUSEBUTTONDOWN: 
      x, y = pygame.mouse.get_pos() 
      first_click = x, y 

      # Remove the disc if it is in the range of the first tower 
      if (x > 100) and (x < 140) and (y > 150) and (y < 250): 
       if len(left_tower) == 0: 
        print "That's not valid" 

      elif len(left_tower) in (big, middle, small): 
       upper_disc = left_tower[-1] 
       left_tower.remove(upper_disc) 

       if event.type == MOUSEBUTTONDOWN: 
        x, y = pygame.mouse.get_pos() 
        second_click = x, y 

        # Add the upper disc to the tower where the second click was 
        if (x > 140) and (x < 220) and (y > 150) and (y < 250): 
         middle_tower.append(upper_disc) 
        elif (x > 350) and (x < 450) and (y > 150) and (y < 250): 
         right_tower.append(upper_disc) 
        else: 
         left_tower.append(upper_disc) 



    pygame.display.update() 

pygame.time.wait(10) 
+0

誰剛剛添加了投票,任何原因?請提一個。 – 2014-10-08 15:22:53

+0

可能重複的[我怎樣才能在Pygame中產生第二次點擊的位置?](http://stackoverflow.com/questions/26243177/how-can-i-generate-the-position-of-a-second-點擊功能於pygame的) – sloth 2014-10-09 08:04:50

回答

0

我看到一些東西。您的代碼似乎存在一些縮進問題。當我解決這個問題時,我相信它應該是固定的,它永遠不會落入最後elif因此elif len(left_tower) in (big, middle, small):永遠不會返回True