我试图用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 :(得分:0)
我看到的一些事情。您的代码似乎存在一些缩进问题。当我解决此问题时,我认为它应该被修复,它永远不会落入最后elif
,因此elif len(left_tower) in (big, middle, small):
永远不会返回True
。