请问如何把这样动态的方块改成动态text字
想把这三个移动的方块改成两个分别移动的英文,比如HELLO和WORLD,请问怎么改?
编码是这样的:
import pygame, sys, timefrom pygame.locals import *# set up pygamepygame.init()# set up the windowWINDOWWIDTH = 400WINDOWHEIGHT = 400windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)pygame.display.set_caption('Animation')# set up direction variablesDOWNLEFT = 1DOWNRIGHT = 3UPLEFT = 7UPRIGHT = 9MOVESPEED = 4# set up the colorsBLACK = (0, 0, 0)RED = (255, 0, 0)GREEN = (0, 255, 0)BLUE = (0, 0, 255)# set up the block data structureb1 = {'rect':pygame.Rect(300, 80, 50, 100), 'color':RED, 'dir':UPRIGHT}b2 = {'rect':pygame.Rect(200, 200, 20, 20), 'color':GREEN, 'dir':UPLEFT}b3 = {'rect':pygame.Rect(100, 150, 60, 60), 'color':BLUE, 'dir':DOWNLEFT}blocks = [b1, b2, b3]# run the game loopwhile True: # check for the QUIT event for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() # draw the black background onto the surface windowSurface.fill(BLACK) for b in blocks: # move the block data structure if b['dir'] == DOWNLEFT: b['rect'].left -= MOVESPEED b['rect'].top += MOVESPEED if b['dir'] == DOWNRIGHT: b['rect'].left += MOVESPEED b['rect'].top += MOVESPEED if b['dir'] == UPLEFT: b['rect'].left -= MOVESPEED b['rect'].top -= MOVESPEED if b['dir'] == UPRIGHT: b['rect'].left += MOVESPEED b['rect'].top -= MOVESPEED # check if the block has move out of the window if b['rect'].top < 0: # block has moved past the top if b['dir'] == UPLEFT: b['dir'] = DOWNLEFT if b['dir'] == UPRIGHT: b['dir'] = DOWNRIGHT if b['rect'].bottom > WINDOWHEIGHT: # block has moved past the bottom if b['dir'] == DOWNLEFT: b['dir'] = UPLEFT if b['dir'] == DOWNRIGHT: b['dir'] = UPRIGHT if b['rect'].left < 0: # block has moved past the left side if b['dir'] == DOWNLEFT: b['dir'] = DOWNRIGHT if b['dir'] == UPLEFT: b['dir'] = UPRIGHT if b['rect'].right > WINDOWWIDTH: # block has moved past the right side if b['dir'] == DOWNRIGHT: b['dir'] = DOWNLEFT if b['dir'] == UPRIGHT: b['dir'] = UPLEFT # draw the block onto the surface pygame.draw.rect(windowSurface, b['color'], b['rect']) # draw the window onto the screen pygame.display.update() time.sleep(0.02)
# set up the block data structureb1 = {'rect':pygame.Rect(300, 80, 50, 100), 'color':RED, 'dir':UPRIGHT}b2 = {'rect':pygame.Rect(200, 200, 20, 20), 'color':GREEN, 'dir':UPLEFT}b3 = {'rect':pygame.Rect(100, 150, 60, 60), 'color':BLUE, 'dir':DOWNLEFT}blocks = [b1, b2, b3]
# set up the block data structurefont = pygame.font.SysFont(pygame.font.get_default_font(), 32)b1 = {'text':font.render('A', 1, RED), 'rect':pygame.Rect(300, 80, 50, 100), 'dir':UPRIGHT}b2 = {'text':font.render('B', 1, GREEN), 'rect':pygame.Rect(200, 200, 20, 20), 'dir':UPLEFT}b3 = {'text':font.render('C', 1, BLUE), 'rect':pygame.Rect(100, 150, 60, 60), 'dir':DOWNLEFT}blocks = [b1, b2, b3]for b in blocks: b['rect'].width = b['text'].get_width() b['rect'].height = b['text'].get_height()