pygame也能实现好看的雷达图,不信可以进来看看。

pygame也能实现好看的雷达图,不信可以往下看看。

这篇是我一边敲代码,一边写博客的文章,想到什么就写什么,所以思路比较直接,望理解啊。

好的,马上开始。

一、先把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,搞定,分享完毕,其实还是挺好玩的,有兴趣的朋友可以自行改改,运行一下代码。也许会有更好的收获。

感谢,比心。

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
使用`pygame.sprite.Sprite`类可以更方便地管理和绘制多个图片对象。下面是一个使用Sprite类实现图片动的示例代码: ```python import pygame pygame.init() # 窗口大小 win_width = 500 win_height = 500 # 创建窗口 win = pygame.display.set_mode((win_width, win_height)) pygame.display.set_caption("Sprite Animation") # 加载图片 img1 = pygame.image.load("img1.png") img2 = pygame.image.load("img2.png") img3 = pygame.image.load("img3.png") # 设置动帧数 fps = 10 # 创建时钟 clock = pygame.time.Clock() # 创建精灵组 sprites = pygame.sprite.Group() # 定义精灵类 class MySprite(pygame.sprite.Sprite): def __init__(self, img, x, y): super().__init__() self.image = img self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y # 创建精灵对象 sprite1 = MySprite(img1, 50, 50) sprite2 = MySprite(img2, 150, 50) sprite3 = MySprite(img3, 250, 50) sprites.add(sprite1, sprite2, sprite3) # 运行游戏 run = True while run: # 控制帧数 clock.tick(fps) # 监听事件 for event in pygame.event.get(): if event.type == pygame.QUIT: run = False # 重绘窗口 win.fill((255, 255, 255)) sprites.draw(win) pygame.display.update() # 退出pygame pygame.quit() ``` 在上述代码中,我们首先加载了三张图片,并设置了动帧数。然后创建了一个精灵组,并定义了一个精灵类,该类继承自`pygame.sprite.Sprite`类,并重写了`__init__`方法,用于初始化精灵对象的属性。然后创建了三个精灵对象,将它们添加到精灵组中。在每一帧重新绘制窗口时,我们只需要调用`sprites.draw(win)`方法即可将精灵组中的所有精灵对象绘制到窗口上。 使用Sprite类实现图片动时,我们可以更方便地管理和操作多个图片对象,而且代码也更加简洁和易读。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值