我正在阅读pygame 1.9.6的文档,我已经点击了第29页,说实话,我只是无法理解为什么我不能按照它的意思去做(用红色的绿色矩形表示)点和黑色文本定义4个角,4个中点和中心点。
文档代码可在第29页上找到,网址为:https : //buildmedia.readthedocs.org/media/pdf/pygame/latest/pygame.pdf
我觉得他们提供的示例中遗漏了很多东西,因此我进行了一些尝试,但是碰到了我只想看看如何使它按其应有的方式工作的问题。
我的变化:
import pygame
from pygame.locals import *
from pygame.rect import *
from pygame.font import *
def draw_point(text, pos):
img = font.render(text, True, Black)
pygame.draw.circle(screen, RED, pos, 3)
screen.blit(img, pos)
SIZE = 500, 200
RED = (255, 0, 0)
GRAY = (150, 150, 150)
GREEN = (255, 0, 0)
BLACK = (255, 255, 255)
pygame.init()
screen = pygame.display.set_mode(SIZE)
rect = Rect(50, 40, 250, 80)
##print(f'x={rect.x}, y={rect.y}, w={rect.w}, h={rect.h}')
##print(f'left={rect.left}, top={rect.top}, right={rect.right}, bottom={rect.bottom}')
##print(f'center={rect.center}')
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
screen.fill(GRAY)
pygame.draw.rect(screen, GREEN, rect, 4)
for pt in pts:
draw_point(pt, eval('rect.'+pt))
pygame.display.flip()
pygame.quit()
任何帮助表示赞赏!
阅读完整的文档。
变量pts
在第25页
pts = ('topleft', 'topright', 'bottomleft', 'bottomright',
'midtop', 'midright', 'midbottom', 'midleft', 'center')
并且该变量font
在第36页上:
font = pygame.font.Font(None, 24)
完整的例子:
import pygame
from pygame.locals import *
from pygame.rect import *
from pygame.font import *
def draw_point(text, pos):
img = font.render(text, True, BLACK)
pygame.draw.circle(screen, RED, pos, 3)
screen.blit(img, pos)
SIZE = 500, 200
RED = (255, 0, 0)
GRAY = (150, 150, 150)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
pygame.init()
screen = pygame.display.set_mode(SIZE)
font = pygame.font.Font(None, 24)
rect = Rect(50, 40, 250, 80)
pts = ('topleft', 'topright', 'bottomleft', 'bottomright',
'midtop', 'midright', 'midbottom', 'midleft', 'center')
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
screen.fill(GRAY)
pygame.draw.rect(screen, GREEN, rect, 4)
for pt in pts:
draw_point(pt, eval('rect.'+pt))
pygame.display.flip()
pygame.quit()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句