我正在为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()
答案 0 :(得分:1)
方法main的第二个参数必须是鼠标位置,而不是鼠标按钮:
run = True
while run:
# [...]
mouse_pos = pygame.mouse.get_pos()
a.main(events, mouse_pos, 1)
# [...]
当pygame.mouse.get_pressed()
表示所有鼠标按钮状态的布尔值序列时,pygame.mouse.get_pos()
返回鼠标光标的X和Y位置。