为了账号安全,请及时绑定邮箱和手机立即绑定

尝试让 pygame 文档代码正常工作

尝试让 pygame 文档代码正常工作

偶然的你 2023-07-27 13:57:41
我正在浏览 pygame 1.9.6 的文档,已经读到了第 29 页,老实说,我只是无法理解为什么我不能让它按照它所说的去做(用红色制作一个绿色矩形)点和黑色文本定义 4 个角、4 个中点和中心点。文档代码可在第 29 页查看: https://buildmedia.readthedocs.org/media/pdf/pygame/latest/pygame.pdf我觉得他们提供的示例遗漏了很多东西,所以我稍微尝试了一下,但已经达到了我只想看看如何让它按照它所说的去做的地步。我的变化:import pygamefrom 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, 200RED = (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 = Truewhile 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()任何帮助表示赞赏!
查看完整描述

1 回答

?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

阅读完整的文档。


该变量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()



查看完整回答
反对 回复 2023-07-27
  • 1 回答
  • 0 关注
  • 59 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信