pygame封装两个常用控件
#coding=utf-8 import os,sys,re,time import pygame import random from win32api import GetSystemMetrics from tkinter import messagebox pygame.init() pygame.display.set_caption("我的控件") percent = 0.6 screen_width = GetSystemMetrics(0) screen_height = GetSystemMetrics(1) window_width = int(screen_width*percent) window_height = int(screen_height*percent) dt = 0 clock = pygame.time.Clock() screen = pygame.display.set_mode((window_width, window_height)) #停止处理输入法事件 pygame.key.stop_text_input() def showMsg(msg): messagebox.showinfo('提示信息', msg) class Button: def __init__(self, x, y, w, h): self.x = x self.y = y self.w = w self.h = h self.color = 'gray' self.text = '按钮' self.text_color = 'black' self.text_size = 12 self.border_width = 1 self.border_color = 'black' self.font_path = os.path.join(os.path.dirname(sys.argv[0]), 'simsun.ttc') self.my_font = pygame.font.Font(self.font_path, self.text_size) def setColor(self, color): self.color = color def setText(self, text): self.text = text def getText(self): return self.text def setTextColor(self, text_color): self.text_color = text_color def setTextSize(self, text_size): self.text_size = text_size self.my_font = pygame.font.Font(self.font_path, self.text_size) def setBorderWidth(self, border_width): self.border_width = border_width def setBorderColor(self, border_color): self.border_color = border_color def draw(self, win): pygame.draw.rect(win, self.color, (self.x, self.y, self.w, self.h)) if self.border_width > 0: pygame.draw.rect(win, self.border_color, (self.x, self.y, self.w, self.h), self.border_width) text = self.my_font.render(self.text, True, self.text_color) myx = self.x + (self.w - text.get_width()) / 2 myy = self.y + (self.h - text.get_height()) / 2 win.blit(text, (myx, myy)) def click(self, event): if self.x + self.w > event.pos[0] > self.x and self.y + self.h > event.pos[1] > self.y: return True return False class Label: def __init__(self, x, y, w, h): self.x = x self.y = y self.w = w self.h = h self.color = 'white' self.text = '' self.text_color = 'black' self.text_size = 12 self.border_width = 0 self.border_color = '' self.font_path = os.path.join(os.path.dirname(sys.argv[0]), 'simsun.ttc') self.my_font = pygame.font.Font(self.font_path, self.text_size) def setColor(self, color): self.color = color def setText(self, text): self.text = text def getText(self): return self.text def setTextColor(self, text_color): self.text_color = text_color def setTextSize(self, text_size): self.text_size = text_size self.my_font = pygame.font.Font(self.font_path, self.text_size) def setBorderWidth(self, border_width): self.border_width = border_width def setBorderColor(self, border_color): self.border_color = border_color def getCharWH(self): padding_percent_width = 0.3 padding_percent_height = 0.3 test_text1 = '测试字符串' test_text2 = self.my_font.render(test_text1, True, self.text_color) char_width = test_text2.get_width() / len(test_text1) char_height = test_text2.get_height() padding_width = char_width * padding_percent_width padding_height = char_height * padding_percent_height line_max_char = int((self.w - padding_width * 2) / char_width) return (char_height, padding_width, padding_height, line_max_char) def getTrueLines(self, char_height, padding_width, padding_height, line_max_char): texts = self.text.split("\n") k = 0 for i,mytext in enumerate(texts): while len(mytext) > line_max_char: submytext = mytext[0:line_max_char] mytext = mytext[line_max_char:] k += 1 k += 1 return k+1 def draw(self, win): (char_height, padding_width, padding_height, line_max_char) = self.getCharWH() lineNum = self.getTrueLines(char_height, padding_width, padding_height, line_max_char) if lineNum * char_height > self.h: self.h = lineNum * char_height pygame.draw.rect(win, self.color, (self.x, self.y, self.w, self.h)) if self.border_width > 0: pygame.draw.rect(win, self.border_color, (self.x, self.y, self.w, self.h), self.border_width) texts = self.text.split("\n") k = 0 for i,mytext in enumerate(texts): while len(mytext) > line_max_char: submytext = mytext[0:line_max_char] subtext = self.my_font.render(submytext, True, self.text_color) submyx = self.x + padding_width submyy = self.y + padding_height + char_height * k win.blit(subtext, (submyx, submyy)) mytext = mytext[line_max_char:] k += 1 text = self.my_font.render(mytext, True, self.text_color) myx = self.x + padding_width myy = self.y + padding_height + char_height * k win.blit(text, (myx, myy)) k += 1 bt = Button(5, 5, 80, 25) bt.setText('测试按钮') bt.setColor('Brown') bt.setTextColor('Gold') bt.setBorderColor('Lime') bt.setBorderWidth(1) label_text = ''' 我我我我我我我我我我我我我我我我我我我我我我我我 111111111111111111111111111111111111111111111 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx .。。。。。。。。。。。。。。。。。。。。。。。。 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% '''.strip() label = Label(115, 5, 250, 250) label.setColor('Maroon') label.setText(label_text) label.setTextSize(18) label.setBorderColor('Lime') label.setBorderWidth(1) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.MOUSEBUTTONDOWN: if bt.click(event): showMsg("%s被点击了" % bt.getText()) keys_pressed = pygame.key.get_pressed() #ESC键 if keys_pressed[pygame.K_ESCAPE]: running = False screen.fill("purple") bt.draw(screen) label.draw(screen) #更新显示 pygame.display.flip() #pygame.display.update() dt = clock.tick(60) / 600 pygame.quit()
效果:
本文来自博客园,作者:河北大学-徐小波,转载请注明原文链接:https://www.cnblogs.com/xuxiaobo/p/18384105