如何在 Pygame 中生成第二次点击的位置? [英] How can I generate the position of a second click in Pygame?

查看:61
本文介绍了如何在 Pygame 中生成第二次点击的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作河内塔.

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

It should work like this: you click on the first tower, from where you want a disk to move, and then on the second where you want the disk to move to. The disk should move from the first tower (a list) to the second tower (another list).

我的问题是,当你第一次点击时,代码会生成位置,之后你应该再次点击来决定磁盘应该去哪里,但代码会自动获取第一次点击的位置.

My problem is that when you first click, the code generates the position, and soon after that you should click again to decide where the disk should go, but the code automatically takes the position of the first click.

这是我的代码示例:

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()

推荐答案

我认为这条线不会达到您的预期:

I think this line will not do what you expect it to:

elif len(rod1) == 1 or 2 or 3:

在解释器中测试它的计算结果不是 True,而是始终为 2.

Test in the interpreter that it does not evaluate to True but rather always to the value of 2.

您可能打算执行以下操作:

You probably meant to do something like:

elif len(rod1) in (1, 2, 3):

甚至:

elif len(rod1) > 0:

此外,您仍然可以使用一系列或"语句来满足您的需求:

Additionally, you could still go for a series of "or" statements to cover your needs:

[不推荐]

elif len(rod1) == 1 or len(rod1) == 2 or len(rod1) == 3:

如果任何一条语句的计算结果为 True,则条件语句也将为 True.

If any one of the statements evaluates to True, the conditional statement will also be True.

这篇关于如何在 Pygame 中生成第二次点击的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆