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

StillLearnin

我正在阅读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()

任何帮助表示赞赏!

拉比德76

阅读完整的文档

变量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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法从Android文档获取相机代码以正常工作

来自分类Dev

试图使代码在类上工作

来自分类Dev

pygame碰撞无法正常工作

来自分类Dev

试图使Laravel Valet 1.1.2正常工作

来自分类Dev

试图使我的部署脚本正常工作

来自分类Dev

骨干/木偶试图使attachView正常工作

来自分类Dev

我在游戏结束代码中的冲突在pygame中无法正常工作

来自分类Dev

我在游戏结束代码中的冲突在pygame中无法正常工作

来自分类Dev

使用官方文档中的PHP代码进行HTTP身份验证无法正常工作

来自分类Dev

样式化xml返回相同的xml文档,一种使代码正常工作的方法

来自分类Dev

使用官方文档中的PHP代码进行HTTP身份验证无法正常工作

来自分类Dev

试图弄清楚我需要进行哪些更改才能使此代码正常工作。使用Visual Studio

来自分类Dev

我的代码可以通过两个选择正常工作,但是我试图只使用一个

来自分类Dev

Nusap代码无法正常工作

来自分类Dev

PHP代码无法正常工作

来自分类Dev

如何使示例代码正常工作?

来自分类Dev

JavaScript代码无法正常工作

来自分类Dev

Ajax代码无法正常工作

来自分类Dev

JavaScript代码无法正常工作

来自分类Dev

如何使此代码正常工作?

来自分类Dev

jQuery代码无法正常工作

来自分类Dev

相同的代码无法正常工作

来自分类Dev

jQuery 代码无法正常工作

来自分类Dev

pygame键盘动画无法正常工作

来自分类Dev

Pygame碰撞点功能无法正常工作

来自分类Dev

退出算法pygame无法正常工作

来自分类Dev

pygame键盘动画无法正常工作

来自分类Dev

pygame 退出按钮无法正常工作

来自分类Dev

MAMP - 文档根目录无法正常工作

Related 相关文章

热门标签

归档