-2

Im trying to make the game Towers of hanoi with pygame. My biggest struggle is now that i have to move the disks, this is my code so far, but it is not moving the disks and i dont know why. I think it is also registering only the first mouseclick and not the second. but it isnt removing the disc either.

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)
Tycho Atsma
  • 61
  • 1
  • 2
  • 10
  • Whoever just added a down vote, any reason? Please do mention one. – Nagaraj Tantri Oct 08 '14 at 15:22
  • possible duplicate of [How can I generate the position of a second click in Pygame?](http://stackoverflow.com/questions/26243177/how-can-i-generate-the-position-of-a-second-click-in-pygame) – sloth Oct 09 '14 at 08:04

1 Answers1

0

A couple things I see. There appears to be some indentation issues with your code. When I fix this how I believe it should be fixed it never falls into the last elif so elif len(left_tower) in (big, middle, small): is never returning True.

Leistungsabfall
  • 6,368
  • 7
  • 33
  • 41
user2097159
  • 862
  • 5
  • 13