Skip to main content
 首页 » 编程设计

python之如何在 Pygame 中生成第二次点击的位置

2024年10月25日9虾米姐

我正在制作汉诺塔。

它应该是这样工作的:你点击第一个塔,从你想要一个圆盘移动的地方,然后点击第二个你想要圆盘移动到的地方。磁盘应该从第一个塔(一个列表)移动到第二个塔(另一个列表)。

我的问题是,当您第一次点击时,代码会生成位置,不久之后您应该再次点击以决定磁盘的位置,但代码会自动采用第一次点击的位置。

这是我的代码示例:

import pygame, sys 
from pygame.locals import * 
 
pygame.init() 
 
DISPLAYSURF = pygame.display.set_mode((500, 400)) 
pygame.display.set_caption("Tower of Hanoi") 
 
block_red = pygame.image.load('red.png') 
block_blue = pygame.image.load('blue.png') 
block_green = pygame.image.load('green.png') 
 
rod1 = [block_red, block_blue, block_green] 
rod2 = [] 
rod3 = [] 
 
WHITE = (255, 255, 255) 
 
while True: 
 
    DISPLAYSURF.fill(WHITE) 
 
    # get the position of the mouse click 
    for event in pygame.event.get(): 
        if event.type == MOUSEBUTTONDOWN: 
            mousex, mousey = pygame.mouse.get_pos() 
            click1 = mousex, mousey 
 
            # first in the left part of the screen 
            if (mousex > 0) and (mousex < 166) and (mousey > 0) and (mousey < 400): 
 
                if len(rod1) == 0: 
                    print "not valid" 
 
                elif len(rod1) == 1 or 2 or 3: 
                    disk1 = rod1[-1] 
                    rod1.remove(disk1) 
 
                    # click again in a other part of the screen 
                    if event.type == MOUSEBUTTONDOWN: 
                        mousex, mousey = pygame.mouse.get_pos()    
                        click2 = mousex, mousey 
 
                        if (mousex > 166) and (mousex < 333) and (mousey > 0) and (mousey < 400): 
                            rod2.append(disk1) 
                        elif (mousex > 333) and (mousex < 500) and (mousey > 0) and (mousey < 400): 
                            rod3.append(disk1) 
                        else:  
                            rod1.append(disk1) 
 
    # if statement fot the middle part 
    # if statement for the right part                         
 
    for event in pygame.event.get(): 
        if event.type == QUIT: 
            pygame.quit() 
            sys.exit() 
    pygame.display.update() 

请您参考如下方法:

请记住,您的代码在循环中运行,因此您必须跟踪游戏的状态。

点击第一个塔改变了游戏的状态:现在选择了一个塔,现在点击一个塔会做一些不同的事情(它将一个方 block 从第一个塔移动到第二个塔) .

在您的示例中,您只需要跟踪当前是否选择了一座塔(当然还有每个塔拥有的方 block )这一事实。不要害怕 state 这个词,一个简单的变量就足够了。

看看下面的代码(注意注释)。它只是在变量 selected 中跟踪选定的杆,然后检查它是否设置为决定是否需要移动 block 。

import pygame 
from collections import namedtuple 
 
pygame.init() 
screen = pygame.display.set_mode((500, 400)) 
 
# create a named tuple to keep track of the size/location of the rods and their blocks 
Rod = namedtuple('Rod', ['rect', 'items']) 
 
# first rod has 4 items. The just use a number to keep track of the size of the blocks  
rods = (Rod(pygame.rect.Rect((100, 150, 25, 250)), [6, 5, 4, 3, 2, 1]), 
        Rod(pygame.rect.Rect((225, 150, 25, 250)), []), 
        Rod(pygame.rect.Rect((350, 150, 25, 250)), [])) 
 
# keep track of the currently selected rod 
selected = None 
 
while True: 
    if pygame.event.get(pygame.QUIT): break 
 
    screen.fill(pygame.color.Color('white')) 
 
    # draw the rods. It's easy since every rod has a rect which we can use with pygame.draw.rect 
    for rod in rods: 
        # if a rod is selected, we draw it yellow instead of black 
        pygame.draw.rect(screen, pygame.color.Color('yellow' if selected == rod else 'black'), rod.rect)     
        # draw each block of each rod 
        for i, item in enumerate(rod.items): 
            r = pygame.rect.Rect(rod.rect.x - item * 8, 375 - 25 * i, 25 + item * 16, 25)  
            pygame.draw.rect(screen, pygame.color.Color('green' if selected == rod else 'darkgreen'), r)     
 
    for e in pygame.event.get(): 
        if e.type == pygame.MOUSEBUTTONDOWN: 
            # check if we clicked a rod. It's easy since every rod has a rect 
            rod = next((r for r in rods if r.rect.collidepoint(pygame.mouse.get_pos())), None) 
            if rod: 
                if selected: 
                    # if there's already a rod selected, move block from one the selected 
                    # rod to the clicked rod 
                    rod.items.append(selected.items.pop()) 
                    selected = None 
                elif rod.items: 
                    # if no rod is selected, selected the currently clicked one (if it has blocks) 
                    selected = rod 
            else: 
                selected = None 
 
    pygame.display.flip()    

结果: