请问如何把这样动态的方块改成动态text字
时间:2011-09-29
来源:互联网

想把这三个移动的方块改成两个分别移动的英文,比如HELLO和WORLD,请问怎么改?
编码是这样的:
Python code
import pygame, sys, time from pygame.locals import * # set up pygame pygame.init() # set up the window WINDOWWIDTH = 400 WINDOWHEIGHT = 400 windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32) pygame.display.set_caption('Animation') # set up direction variables DOWNLEFT = 1 DOWNRIGHT = 3 UPLEFT = 7 UPRIGHT = 9 MOVESPEED = 4 # set up the colors BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # set up the block data structure b1 = {'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 loop while 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)
是不是要改这一部分呢?
Python code
# set up the block data structure b1 = {'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]
可是要怎么改呢?
我乱写了一通,都不行,麻烦高手帮忙啦~~~
作者: hakunac 发布时间: 2011-09-29
改了两个地方。
其一:
Python code
其二:
Python code
完整代码:
Python code
其一:
Python code
# set up the block data structure font = 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]
其二:
Python code
# draw the block onto the surface windowSurface.blit(b['text'], b['rect'])
完整代码:
Python code
import pygame, sys, time from pygame.locals import * # set up pygame pygame.init() # set up the window WINDOWWIDTH = 400 WINDOWHEIGHT = 400 windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32) pygame.display.set_caption('Animation') # set up direction variables DOWNLEFT = 1 DOWNRIGHT = 3 UPLEFT = 7 UPRIGHT = 9 MOVESPEED = 4 # set up the colors BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # set up the block data structure font = 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] # run the game loop while 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 windowSurface.blit(b['text'], b['rect']) # draw the window onto the screen pygame.display.update() time.sleep(0.02)
作者: iambic 发布时间: 2011-09-29
不过前面那个碰撞效果不太好,因为是按照rect碰撞的,但是rect和文字大小不一致。
可以把rect修正为和文字一样大小:
Python code
可以把rect修正为和文字一样大小:
Python code
# set up the block data structure font = 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()
作者: iambic 发布时间: 2011-09-29
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28