Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我的pygame.MOUSEBUTTONDOWN不工作?_Python_Python 3.x_Pygame - Fatal编程技术网

Python 为什么我的pygame.MOUSEBUTTONDOWN不工作?

Python 为什么我的pygame.MOUSEBUTTONDOWN不工作?,python,python-3.x,pygame,Python,Python 3.x,Pygame,我正在为pygame创建一个类,它允许用户为他们的游戏创建文本框。但是由于某种原因,我的代码没有到达mousebuttondown部分。我附加了我的全部代码以及我面临问题的部分 它没有打印完成 def main(self, events, mousepos, id): for event in events: if event.type == pygame.QUIT: exit() if event.type == pygame.M

我正在为pygame创建一个类,它允许用户为他们的游戏创建文本框。但是由于某种原因,我的代码没有到达
mousebuttondown
部分。我附加了我的全部代码以及我面临问题的部分

它没有打印完成

def main(self, events, mousepos, id):
    for event in events:
        if event.type == pygame.QUIT:
            exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if self.rect(id, mousepos):
                print("done")
继续打印否

def rect(self, text_id, mousepos):
    x, y, width, height = self.dict_all[text_id]
    if ((x + width) > mousepos[0] > x) and ((y + height) > mousepos[1] > y):
        print("yes")
        return True
    else:
        print("no")
        return False
下面的全部代码,更新是我试图实现的一种方法,但由于某些原因无法实现。

import pygame

pygame.font.init()


class textBox:
    def __init__(self, surface, id, color, width, height, x, y, antialias, maxtextlen):
        self.surface = surface
        self.id = id
        self.color = color
        self.width = width
        self.height = height
        self.x = x
        self.y = y
        self.antialias = antialias
        self.maxtextlen = maxtextlen

        self.text_list = []
        self.text_list_keys = []
        self.currentId = 0
        self.click_check = False
        self.font = pygame.font.SysFont('comicsans', 20)
        self.dict_all = {}

        pygame.draw.rect(self.surface, (self.color), (self.x, self.y, self.width, self.height))
        # for i in self.text_list_keys:
        #     if self.id not in i:
        #         self.text_list_keys.append(self.id)
        #         self.text_list.append(tuple(self.id))
        #     else:
        #         self.nothing()
        self.dict_all[self.id] = tuple((self.x, self.y, self.width, self.height))

    def update(self, events, mousepos):
        for event in events:
            if event.type == pygame.QUIT:
                exit()
            if event.type == pygame.MOUSEBUTTONDOWN and ((self.x + self.width) > mousepos[0] > self.x) \
                    and ((self.y + self.height) > mousepos[1] > self.y):
                print("reached: " + mousepos)
                self.click_check = True
            else:
                self.click_check = False

            if self.click_check:
                print("1")
                if event.type == pygame.KEYDOWN:
                    print("@")
                    if event.key == pygame.K_a:
                        print("reached")
                        new_t = ""
                        for j in range(len(self.text_list)):
                            t = (self.text_list[j][0]).index(self.getId(self.currentId))
                            new_t = t
                        self.text_list[new_t].append("a")
                        self.surface.blit(self.font.render(f'{self.text_list[new_t]}', self.antialias, (0, 0, 0)),
                                          (self.x, self.y))

                    else:
                        print("this")

            else:
                pass

    def rect(self, text_id, mousepos):
        x, y, width, height = self.dict_all[text_id]
        if ((x + width) > mousepos[0] > x) and ((y + height) > mousepos[1] > y):
            print("yes")
            return True
        else:
            print("no")
            return False

    def getId(self, text_id):
        self.currentId = text_id

    def nothing(self):
        return False

    def main(self, events, mousepos, id):
        for event in events:
            if event.type == pygame.QUIT:
                exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if self.rect(id, mousepos):
                    print("done")
test.py

import pygame
from pygame_textbox import textBox

pygame.init()

win_width = 500
win_height = 500

screen = pygame.display.set_mode((win_width, win_height))
pygame.display.set_caption("test")

run = True
while run:
    mouse = pygame.mouse.get_pressed()
    screen.fill((0, 0, 0))
    events = pygame.event.get()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            pygame.quit()
            exit()

    a = textBox(screen, 1, (255,  255, 255), 100, 30, 100, 100, True, 20)
    # a.getId(1)
    a.rect(1, mouse)
    a.main(events, mouse, 1)
    pygame.display.update()

main方法的第二个参数必须是鼠标位置,而不是鼠标按钮:

run=True
运行时:
# [...]
mouse\u pos=pygame.mouse.get\u pos()
a、 主(事件,鼠标位置,1)
# [...]
一系列布尔值表示所有鼠标按钮的状态,返回鼠标光标的X和Y位置