python draw.text颜色_python wand:用draw.text()改变文本样式

使用wand.drawing.Drawing,您需要构建绘图对象的“上下文”.可以通过直接在绘图对象实例上设置属性来定义字体样式,族,系列,颜色等等.

from wand.image import Image

from wand.color import Color

from wand.drawing import Drawing

from wand.display import display

with Image(width=200, height=150, background=Color('lightblue')) as canvas:

with Drawing() as context:

context.fill_color = Color('orange')

context.stroke_color = Color('brown')

context.font_style = 'italic'

context.font_size = 24

context.text(x=25,

y=75,

body="Hello World!")

context(canvas)

canvas.format = "png"

display(canvas)

但是如果你的绘图对象已经有了矢量属性呢?

这是Drawing.push()& Drawing.pop()可用于管理您的图纸堆栈.

# Default attributes for drawing circles

context.fill_color = Color('lime')

context.stroke_color = Color('green')

context.arc((75, 75), (25, 25), (0, 360))

# Grow context stack for text style attributes

context.push()

context.fill_color = Color('orange')

context.stroke_color = Color('brown')

context.font_style = 'italic'

context.font_size = 24

context.text(x=25,

y=75,

body="Hello World!")

# Return to previous style attributes

context.pop()

context.arc((175, 125), (150, 100), (0, 360))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码可以进行以下优化: 1. 使用函数封装代码块,提高代码的可读性和可维护性。 2. 使用变量名更加可读性强的名称。 3. 将常量提取出来,定义为全局变量,方便后续修改。 4. 优化代码结构,提高代码的可读性。 5. 添加游戏结束的条件,当生命值为0时游戏结束。 以下是优化后的代码: ```python import pygame import time import random # 常量定义 SCREEN_WIDTH = 600 SCREEN_HEIGHT = 500 BALL_RADIUS = 20 RECT_WIDTH = 120 RECT_HEIGHT = 40 BALL_COLOR = (255., 25, 52) RECT_COLOR = (100, 200, 30) BACKGROUND_COLOR = (34, 177, 135) TEXT_COLOR = (0, 0, 0) # 初始化游戏 pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("接小球游戏") font = pygame.font.Font(None, 24) score = 0 lives = 3 # 定义函数 def draw_ball(ball_x, ball_y): pygame.draw.circle(screen, BALL_COLOR, (ball_x, ball_y), BALL_RADIUS, 0) def draw_rect(rect_x, rect_y): pygame.draw.rect(screen, RECT_COLOR, (rect_x, rect_y, RECT_WIDTH, RECT_HEIGHT), 0) def draw_text(score, lives): text_score = font.render('score:%d' % score, True, TEXT_COLOR) text_lives = font.render('lives:%d' % lives, True, TEXT_COLOR) screen.blit(text_score, (0, 0)) screen.blit(text_lives, (SCREEN_WIDTH - 80, 0)) # 游戏循环 while lives > 0: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() elif event.type == pygame.MOUSEMOTION: rect_x, _ = event.pos screen.fill(BACKGROUND_COLOR) ball_y = ball_y + 1 if ball_y > SCREEN_HEIGHT: ball_y = 0 ball_x = random.randint(0, SCREEN_WIDTH) lives -= 1 draw_ball(ball_x, ball_y) draw_rect(rect_x, SCREEN_HEIGHT - RECT_HEIGHT) if rect_x < ball_x < rect_x + RECT_WIDTH and SCREEN_HEIGHT - RECT_HEIGHT < ball_y < SCREEN_HEIGHT: score += 1 ball_y = 0 ball_x = random.randint(0, SCREEN_WIDTH) draw_text(score, lives) pygame.display.update() pygame.quit() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值