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

我正在浏览 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()

任何帮助表示赞赏!


偶然的你
浏览 86回答 1
1回答

慕尼黑8549860

阅读完整的文档。该变量pts位于第 25 页pts = ('topleft', 'topright', 'bottomleft', 'bottomright',        'midtop', 'midright', 'midbottom', 'midleft', 'center')该变量font位于第 36 页:font = pygame.font.Font(None, 24)完整示例: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 = (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 = 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()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python