【发布时间】:2020-06-29 04:48:46
【问题描述】:
请有人告诉我如何更改代码,以使屏幕文本和编码按钮文本保持到下一次单击鼠标按钮并且在释放鼠标按钮后不会消失? 现在,当我单击并按住鼠标按钮时,屏幕文本正在更改,但编码按钮没有更新。而且在我释放鼠标按钮后,文本又回到了初始状态。 我需要文本(在屏幕上和按钮上)进行更改并永远在屏幕上保持更新,或者直到再次单击鼠标按钮。 我制作了一个用于在屏幕上显示文本的函数和一个用于在屏幕上绘制按钮的类。他们都工作。也许问题出在“if”语句的主要逻辑中?
pygame.init()
# setting the window size and FPS
SIZE = WIDTH, HEIGHT = (640, 640)
FPS = 30
screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()
bgcolor = (54, 54, 54)
# defining the function to draw text on the screen
def draw_text(surface, text, pos):
text_color = (255, 255, 255)
text_font = pygame.font.SysFont('Times New Roman', 24)
x, y = pos
word_surface = text_font.render(text, 1, text_color)
surface.blit(word_surface, (x, y))
# defining a class for buttons
class DrawButton():
def __init__(self, button_text, button_x, button_y):
self.button_text = button_text
self.button_x = button_x
self.button_y = button_y
self.button_color_passive = (89, 89, 89)
self.button_color_active = (212, 200, 235)
self.button_width = 400
self.button_height = 100
self.cur = pygame.mouse.get_pos()
self.click = pygame.mouse.get_pressed()
def text_to_button(self): # defining the method for drawing a text on buttons
bf_passive = pygame.font.SysFont('Times New Roman', 24)
bf_passive_rend = bf_passive.render(self.button_text, 1, self.button_color_active)
bf_passive_rect = bf_passive_rend.get_rect()
bf_active = pygame.font.SysFont('Times New Roman', 28)
bf_active_rend = bf_active.render(self.button_text, 1, self.button_color_passive)
bf_active_rect = bf_active_rend.get_rect()
if self.button_x + self.button_width > self.cur[0] > self.button_x \
and self.button_y + self.button_height > self.cur[1] > self.button_y:
bf_active_rect.center = (int(self.button_x + (self.button_width / 2)), int(self.button_y + (self.button_height / 2)))
screen.blit(bf_active_rend, bf_active_rect)
else:
bf_passive_rect.center = (int(self.button_x + (self.button_width / 2)), int(self.button_y + (self.button_height / 2)))
screen.blit(bf_passive_rend, bf_passive_rect)
def draw_button(self): # defining the method for drawing button rectangle
if self.button_x + self.button_width > self.cur[0] > self.button_x \
and self.button_y + self.button_height > self.cur[1] > self.button_y:
pygame.draw.rect(screen, self.button_color_passive, (self.button_x + 5, self.button_y + 5, self.button_width, self.button_height))
pygame.draw.rect(screen, self.button_color_active, (self.button_x, self.button_y, self.button_width, self.button_height))
if self.click[0] == 1:
pygame.draw.rect(screen, self.button_color_passive, (self.button_x, self.button_y, self.button_width, self.button_height))
else:
pygame.draw.rect(screen, self.button_color_passive, (self.button_x, self.button_y, self.button_width, self.button_height))
text1 = "Text 1"
text2 = "Text 2"
while True:
dt = clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
screen.fill(bgcolor)
text = text1
if text == text1:
button1 = DrawButton('Next', WIDTH - 435, HEIGHT - 150)
button1.draw_button()
button1.text_to_button()
if text == text1 and button1.click[0] == 1:
text = text2
button2 = DrawButton('Next again', WIDTH - 435, HEIGHT - 150)
button2.draw_button()
button2.text_to_button()
draw_text(screen, text, (40, 20))
pygame.display.update()```
【问题讨论】:
-
首先你应该只创建一次按钮 - 在
while-loop 之前 - 如果你将在每个循环中创建新按钮,那么你不能保留它之前的阶段。并且您应该将新文本作为参数发送到按钮。 -
顺便说一句:您可以将按钮大小和位置保持为 pygame.Rect() ,然后您可以使用
rect.collide(mouse_position)检查鼠标是否在按钮区域内 -
你应该在
while-loop之前设置text = text1,然后只在if/else中更改它 -
顺便说一句:类中的变量不需要使用前缀/后缀
button。为不同的对象使用相同的名称更容易 -button.draw()、player.draw()、enemy.draw()- 因为您可以将它们保留在列表中并使用for item in list: item.draw()