pygame封装两个常用控件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | #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

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步