【发布时间】:2019-08-25 22:08:41
【问题描述】:
我需要在按钮中绘制文本,在我的程序中可以将其视为四个较小的矩形,除此之外,我还需要在标题上绘制文本。我不确定如何做到这一点,因为我的程序结构与我见过的其他程序不同。
关注了其他问题,以及他们收到的试图影响我的答案。
import pygame
import sys
def main():
pygame.init()
clock = pygame.time.Clock()
fps = 60
size = [700, 600]
bg = [255, 255, 255]
font = pygame.font.Font('freesansbold.ttf', 32)
screen = pygame.display.set_mode(size)
black = (0, 0, 0)
button = pygame.Rect(400, 400, 250, 125)
button2 = pygame.Rect(50, 400, 250, 125)
button3 = pygame.Rect(400, 250, 250, 125)
button4 = pygame.Rect(50, 250, 250, 125)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = event.pos # gets mouse position
# checks if mouse position is over the button
if button.collidepoint(mouse_pos):
# prints current location of mouse
print('Instructions'.format(mouse_pos))
if button2.collidepoint(mouse_pos):
# prints current location of mouse
print('Controls'.format(mouse_pos))
if button3.collidepoint(mouse_pos):
# prints current location of mouse
print('Information'.format(mouse_pos))
if button4.collidepoint(mouse_pos):
# prints current location of mouse
print('Start Game'.format(mouse_pos))
screen.fill(bg)
pygame.draw.rect(screen, black, (button)) # draw button
pygame.draw.rect(screen, black, (button2))
pygame.draw.rect(screen, black, (button3))
pygame.draw.rect(screen, black, (button4))
pygame.draw.rect(screen, black, (50, 25, 600, 200))
pygame.display.update()
clock.tick(fps)
pygame.quit()
sys.exit
if __name__ == '__main__':
main()
我希望按钮上有文字,所以以后当我点击它们时,它们会打开一个新窗口。
【问题讨论】:
-
将它们放在单独的函数中然后放在一个类中不是更好吗?
-
我该怎么做?我还没有真正使用过 OOP。
-
既然你也说开一个新窗口,那也看看这个:stackoverflow.com/questions/55149797/…