Python Pygame将文本与其他文本交换

Python Pygame将文本与其他文本交换,python,pygame,Python,Pygame,我有一段代码: 导入pygame 定义颜色(): 随机输入 clock=pygame.time.clock() pygame.init() pygame.font.init pygame.display.set_标题('颜色') 屏幕=宽度,高度=900600 screen=pygame.display.set_模式(屏幕,0,32) 粒子=[] 运行=真 单击=False 运行时: 屏幕填充((0,0,0)) 按钮1=pygame.Rect(宽度/2,(高度/10),宽度/4,60) 按钮1.

我有一段代码:

导入pygame
定义颜色():
随机输入
clock=pygame.time.clock()
pygame.init()
pygame.font.init
pygame.display.set_标题('颜色')
屏幕=宽度,高度=900600
screen=pygame.display.set_模式(屏幕,0,32)
粒子=[]
运行=真
单击=False
运行时:
屏幕填充((0,0,0))
按钮1=pygame.Rect(宽度/2,(高度/10),宽度/4,60)
按钮1.x=宽度/2
颜色=(255255)
pygame.draw.rect(屏幕,(135206235),按钮1)
文字语音('pixel.ttf',30',Particle:',(255255),(100),(300),False)
颜色=白色
font=pygame.font.font('pixel.ttf',30)
text=font.render(颜色、真、彩色)
textRect=text.get_rect()
textRect.center=(180),(300))
screen.blit(text,textRect)
文本语音('pixel.ttf',50,'Colors',(255255),(button_1.centerx),(button_1.centery),False)
particles.append([[150200],[random.randint(0,20)/10-1,1],random.randint(4,6)])
对于粒子中的粒子:
粒子[0][0]+=粒子[1][0]
粒子[0][1]+=粒子[1][1]
粒子[2]=0.1
粒子[1][1]+=0.03
pygame.draw.circle(屏幕、颜色、[int(粒子[0][0])、int(粒子[0][1])、int(粒子[2]))

如果粒子[2]创建一个函数,该函数渲染文本,但不将其渲染到窗口表面:

def render_text_speech(字体名称、大小、文本、颜色、x、y、粗体):
屏幕=宽度,高度=900600
font=pygame.font.font(字体名称、大小)
字体设置为粗体(粗体)
text=font.render(文本、真、彩色)
textRect=text.get_rect()
textRect.center=(x,y)
返回文本,textRect
在主应用程序循环之前渲染文本,并将文本表面连续渲染到循环中的窗口表面:

def colors():
# [...]
按钮1=pygame.Rect(宽度/2,(高度/10),宽度/4,60)
按钮1.x=宽度/2
颜色=(255255)
颜色=白色
text_particle=render_text_speech('pixel.ttf',30,'particle:',(255255),100300,False)
text\u colors=render\u text\u speech('pixel.ttf',50,'colors',(255255255),button\u 1.centerx,button\u 1.centery,False)
text\u color=render\u text\u speech('pixel.ttf',30,color\t,color,180300,False)
# [...]
运行时:
# [...]
屏幕。blit(*文本\粒子)
屏幕。blit(*文本颜色)
屏幕。blit(*文本颜色)
按下文本鼠标按钮时,只需重新渲染文本曲面即可:

def colors():
# [...]
运行时:
# [...]
对于pygame.event.get()中的事件:
# [...]
如果event.type==pygame.MOUSEBUTTONDOWN:
如果event.button==1且textRect.collidepoint(event.pos):
颜色=红色
颜色=(255,0,0)
text\u color=render\u text\u speech('pixel.ttf',30,color\t,color,180300,False)

请参见示例:

导入pygame
随机输入
pygame.init()
pygame.font.init()
pygame.display.set_标题('颜色')
屏幕=宽度,高度=900600
screen=pygame.display.set_模式(屏幕,0,32)
def render_text_speech(字体名称、大小、文本、颜色、x、y、粗体):
屏幕=宽度,高度=900600
font=pygame.font.SysFont(无,大小)
#font=pygame.font.font(字体名称、大小)
字体设置为粗体(粗体)
text=font.render(文本、真、彩色)
textRect=text.get_rect()
textRect.center=(x,y)
返回文本,textRect
定义颜色():
clock=pygame.time.clock()
按钮1=pygame.Rect(宽度/2,(高度/10),宽度/4,60)
按钮1.x=宽度/2
颜色=[(“白色”、(255、255、255))、(“红色”、(255、0、0))、(“绿色”、(0、255、0))、(“蓝色”、(0、0、255))]
当前颜色=0
颜色,颜色=颜色[当前颜色]
text_particle=render_text_speech('pixel.ttf',30,'particle:',(255255),100300,False)
text\u colors=render\u text\u speech('pixel.ttf',50,'colors',(255255255),button\u 1.centerx,button\u 1.centery,False)
text\u color=render\u text\u speech('pixel.ttf',30,color\t,color,180300,False)
粒子=[]
运行=真
运行时:
屏幕填充((0,0,0))
pygame.draw.rect(屏幕,(135206235),按钮1)
屏幕。blit(*文本\粒子)
屏幕。blit(*文本颜色)
屏幕。blit(*文本颜色)
textRect=文本颜色[1]
particles.append([[150200],[random.randint(0,20)/10-1,1],random.randint(4,6)])
对于粒子中的粒子:
粒子[0][0]+=粒子[1][0]
粒子[0][1]+=粒子[1][1]
粒子[2]=0.1
粒子[1][1]+=0.03
pygame.draw.circle(屏幕、颜色、[int(粒子[0][0])、int(粒子[0][1])、int(粒子[2]))
如果粒子[2]=len(颜色):
当前颜色=0
颜色,颜色=颜色[当前颜色]
text\u color=render\u text\u speech('pixel.ttf',30,color\t,color,180300,False)
pygame.display.update()
时钟滴答(60)
颜色()

创建一个函数,该函数呈现文本,但不会将其显示到窗口表面:

def render_text_speech(字体名称、大小、文本、颜色、x、y、粗体):
屏幕=宽度,高度=900600
font=pygame.font.font(字体名称、大小)
字体设置为粗体(粗体)
text=font.render(文本、真、彩色)
textRect=text.get_rect()
textRect.center=(x,y)
返回文本,textRect
在主应用程序循环之前渲染文本,并将文本表面连续渲染到循环中的窗口表面:

def colors():
# [...]
按钮1=pygame