亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試讓 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 關注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號