pygame中绘制线条的方法

pygame初学篇四: 绘制线条的方法

绘制线条方法:pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle,width), pygame 官网介绍说明方法:http://www.pygame.org/docs/ref/draw.html#pygame.draw.circle
lines(Surface, color, closed, pointlist, width=1) -> Rect lines方法介绍: Surfuce参数: 传入需要在该Surface对象上绘制圆形的Surface对象; color参数: 需要绘制圆形的线的颜色,传入一个rgb三原色元组 . closed参数: 表示是否需要把线条闭合,pointlist 参数: 需要绘制线条各个折点的坐标,width参数:表示绘制线的宽度. 注意此参数为0,则不显示线条。
line(Surface, color, start_pos, end_pos, width=1) -> Rect line方法介绍: Surfuce参数: 传入需要在该Surface对象上绘制圆形的Surface对象;
color参数: 需要绘制圆形的线的颜色,传入一个rgb三原色元组 . closed参数: 表示是否需要把线条闭合,start_pos 参数: 需要绘制线条的起始坐标,
end_pos 参数: 需要绘制线条的终点坐标,width参数:表示绘制线的宽度. 注意此参数为0,则不显示线条。
aaline(Surface, color, startpos, endpos, blend=1) -> Rect aaline方法介绍: Surfuce参数: 传入需要在该Surface对象上绘制圆形的Surface对象;
color参数: 需要绘制圆形的线的颜色,传入一个rgb三原色元组 . start_pos 参数: 需要绘制线条的起始坐标,end_pos 参数: 需要绘制线条的终点坐标blend参数:表示是否开启抗锯齿,值为1时开启,值为0时不开启。

示例程序:
# -*- coding: utf-8 -*-
# @Author: 四叶草
# @Date:   2017-11-04 19:15:46
# @Last Modified by:   Administrator
# @Last Modified time: 2017-11-08 17:03:48

import pygame
import sys
import math 
from pygame.locals import *

# pygame 初始化
pygame.init()

# 设置背景颜色和线条颜色
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

# 设置直线的坐标
points = [(200, 75), (300, 25), (400, 75)]

# 设置背景框大小
size = width, height = 600, 600
#position = width // 2, height // 2

# 设置帧率,返回clock 类
clock = pygame.time.Clock()

screen = pygame.display.set_mode(size)
pygame.display.set_caption("llls make")

while True:
	for event in pygame.event.get():
		# 查找关闭窗口事件
		if event.type == QUIT:
			sys.exit()
	
	# 填充背景色
	screen.fill(WHITE)

	# 画不封闭的两条直线
	pygame.draw.lines(screen, GREEN, 0, points, 1)

	# 画不抗锯齿的一条直线
	pygame.draw.line(screen, BLUE, (100, 200), (540, 250), 1)

	# 画抗锯齿的一条直线
	pygame.draw.aaline(screen, BLUE, (100, 250), (540, 300), 1)
	
	# 刷新图s
	pygame.display.flip()

	clock.tick(60)

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用 Pygame 绘制走势图,您需要考虑以下步骤: 1. 准备数据集:首先,您需要准备一组数据,这些数据将在走势图绘制。您可以使用 NumPy 等 Python 库生成随机数据或从文件读取数据。 2. 设计窗口:使用 Pygame 库创建一个窗口,该窗口将显示您的走势图。您可以根据需要设置窗口的大小和颜色。 3. 绘制坐标轴:在窗口,您需要绘制一个坐标轴,该坐标轴将显示您的数据。您可以使用 Pygame 提供的绘制线和文本功能来绘制坐标轴。 4. 绘制数据:使用绘制线功能,您可以将数据绘制在坐标轴上。您可以使用不同的颜色和线条样式来表示不同的数据。 5. 显示图形:将所有元素绘制到窗口上后,您需要使用 Pygame 的显示函数来显示图形。 下面是一个简单的示例代码,可以帮助您开始绘制走势图: ```python import pygame import numpy as np # 准备数据 data = np.random.randint(0, 100, size=(50)) # 设置窗口大小 width, height = 800, 600 # 初始化 Pygame pygame.init() # 创建窗口 screen = pygame.display.set_mode((width, height)) # 设置窗口标题 pygame.display.set_caption("Trend Chart") # 设置颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) GRAY = (128, 128, 128) # 绘制坐标轴 def draw_axis(): pygame.draw.line(screen, BLACK, (50, height-50), (width-50, height-50), 2) pygame.draw.line(screen, BLACK, (50, height-50), (50, 50), 2) for i in range(10): x = i * (width-100) / 9 + 50 pygame.draw.line(screen, GRAY, (x, height-50), (x, 50), 1) text = str(i * 10) font = pygame.font.Font(None, 20) text = font.render(text, True, BLACK) text_rect = text.get_rect(center=(x, height-30)) screen.blit(text, text_rect) # 绘制数据 def draw_data(): for i in range(len(data)-1): x1 = i * (width-100) / (len(data)-1) + 50 y1 = height - data[i] * (height-100) / 100 - 50 x2 = (i+1) * (width-100) / (len(data)-1) + 50 y2 = height - data[i+1] * (height-100) / 100 - 50 pygame.draw.line(screen, WHITE, (x1, y1), (x2, y2), 2) # 主循环 running = True while running: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 填充背景色 screen.fill(WHITE) # 绘制坐标轴和数据 draw_axis() draw_data() # 显示图形 pygame.display.flip() # 退出 Pygame pygame.quit() ``` 该示例代码使用 Pygame 库创建一个窗口,并绘制随机数据的走势图。您可以根据需要修改代码以适应您的数据集和样式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

零涂

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值