pygame也能实现好看的雷达图,不信可以往下看看。
这篇是我一边敲代码,一边写博客的文章,想到什么就写什么,所以思路比较直接,望理解啊。
好的,马上开始。
import pygame,sys
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption('pygame和列表元素有趣的碰撞V1.0')
clock=pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((0,0,0))
clock.tick(300)
pygame.display.update()
黑黑的框框,熟悉的味道
pygame.draw.line(screen,(0,255,0),(0,150),(300,150),1)
pygame.draw.line(screen,(0,255,0),(150,0),(150,300),1)
# 画同心圆
pygame.draw.circle(bg,(255,255,0,60),(150,150),150)
pygame.draw.circle(bg,(0,255,0,255),(150,150),150,2)
pygame.draw.circle(bg,(255,255,0,60),(150,150),100)
pygame.draw.circle(bg,(0,255,0,255),(150,150),100,2)
pygame.draw.circle(bg,(255,255,0,60),(150,150),50)
pygame.draw.circle(bg,(0,255,0,255),(150,150),50,2)
import pygame,sys
pygame.init()
screen=pygame.display.set_mode((300,300))
bg = screen.copy().convert_alpha()
pygame.display.set_caption('pygame画雷达图V1.0')
clock=pygame.time.Clock()
i = 0
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((0,0,0))
# 画同心圆
pygame.draw.circle(bg,(255,255,0,60),(150,150),150)
pygame.draw.circle(bg,(0,255,0,255),(150,150),150,2)
pygame.draw.circle(bg,(255,255,0,60),(150,150),100)
pygame.draw.circle(bg,(0,255,0,255),(150,150),100,2)
pygame.draw.circle(bg,(255,255,0,60),(150,150),50)
pygame.draw.circle(bg,(0,255,0,255),(150,150),50,2)
# 画十字线
pygame.draw.line(bg,(0,255,0,255),(0,150),(300,150),1)
pygame.draw.line(bg,(0,255,0,255),(150,0),(150,300),1)
screen.blit(bg,(0,0))
i += 1
clock.tick(30)
pygame.display.update()
还行吧,过得去。
angle = 0
posx = 150 + int(150 * math.sin(angle * math.pi / 180))
posy = 150 - int(150 * math.cos(angle * math.pi / 180))
pygame.draw.line(bg,(0,255,0,255),(150,150),(posx,posy),1)
import pygame,sys
import math
pygame.init()
screen=pygame.display.set_mode((300,300))
bg = screen.copy().convert_alpha()
pygame.display.set_caption('pygame画雷达图V1.0')
clock=pygame.time.Clock()
i = 0
angle = 0
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((0,0,0))
# 画同心圆
pygame.draw.circle(bg,(255,255,0,60),(150,150),150)
pygame.draw.circle(bg,(0,255,0,255),(150,150),150,2)
pygame.draw.circle(bg,(255,255,0,60),(150,150),100)
pygame.draw.circle(bg,(0,255,0,255),(150,150),100,2)
pygame.draw.circle(bg,(255,255,0,60),(150,150),50)
pygame.draw.circle(bg,(0,255,0,255),(150,150),50,2)
# 画十字线
pygame.draw.line(bg,(0,255,0,255),(0,150),(300,150),1)
pygame.draw.line(bg,(0,255,0,255),(150,0),(150,300),1)
# 画指针
posx = 150 + int(150 * math.sin(angle * math.pi / 180))
posy = 150 - int(150 * math.cos(angle * math.pi / 180))
pygame.draw.line(bg,(0,255,0,255),(150,150),(posx,posy),1)
angle += 1
i += 1
screen.blit(bg,(0,0))
clock.tick(60)
pygame.display.update()
发现没有那种拖着尾巴的效果,因为还差最后一步。
好的,见证奇迹的代码就要出现了。
# 增加蒙版层
bgSurface = pygame.Surface((300, 300), flags=pygame.SRCALPHA)
pygame.Surface.convert_alpha(bgSurface)
bgSurface.fill(pygame.Color(0, 0, 0, 25))
screen.blit(bgSurface,(0,0))
import pygame,sys
import math
pygame.init()
screen=pygame.display.set_mode((300,300))
bg = screen.copy().convert_alpha()
# 增加蒙版层
bgSurface = pygame.Surface((300, 300), flags=pygame.SRCALPHA)
pygame.Surface.convert_alpha(bgSurface)
bgSurface.fill(pygame.Color(0, 0, 0, 25))
pygame.display.set_caption('pygame画雷达图V1.0')
clock=pygame.time.Clock()
i = 0
angle = 0
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
# screen.fill((0,0,0))
screen.blit(bgSurface,(0,0))
# 画同心圆
pygame.draw.circle(bg,(255,255,0,5),(150,150),150)
pygame.draw.circle(bg,(0,255,0,255),(150,150),150,2)
pygame.draw.circle(bg,(255,255,0,5),(150,150),100)
pygame.draw.circle(bg,(0,255,0,255),(150,150),100,2)
pygame.draw.circle(bg,(255,255,0,5),(150,150),50)
pygame.draw.circle(bg,(0,255,0,255),(150,150),50,2)
# 画十字线
pygame.draw.line(bg,(0,255,0,255),(0,150),(300,150),1)
pygame.draw.line(bg,(0,255,0,255),(150,0),(150,300),1)
# 画指针
posx = 150 + int(150 * math.sin(angle * math.pi / 180))
posy = 150 - int(150 * math.cos(angle * math.pi / 180))
pygame.draw.line(bg,(0,255,0,255),(150,150),(posx,posy),1)
angle += 1
i += 1
screen.blit(bg,(0,0))
clock.tick(60)
pygame.display.update()
OK,搞定,分享完毕,其实还是挺好玩的,有兴趣的朋友可以自行改改,运行一下代码。也许会有更好的收获。
感谢,比心。