弘扬奥运精神,我们49行画个奥运五环
Posted JAVA炭烧
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了弘扬奥运精神,我们49行画个奥运五环相关的知识,希望对你有一定的参考价值。
虽然这届奥运遇到重重阻碍,疫情影响而延后,延后了要举办民众反对,再然后彩排后还爆出xq案件,更有小道爆出黑幕。这些种种我们就忽略掉吧~
截止本文发表,我们看看战况:
弘扬奥运精神,今天我们用49行写歌奥运五环~
本文效果预览:
奥林匹克标志
奥林匹克 标志 (Olympic Logo /Symbole Olympique/Olympic Rings)是由皮埃尔·德·顾拜旦先生于1913年构思设计的,是由《奥林匹克宪章》确定的,也被称为奥运五环标志,它是世界范围内最为人们广泛认知的奥林匹克运动会标志。它由5个奥林匹克环套接组成,有蓝、黄、黑、绿、红5种颜色。环从左到右互相套接,上面是蓝、黑、红环,下面是是黄、绿环。整个造形为一个底部小的规则梯形。
初始化
我们使用的是PyQt5
先搭个框架:
import sys
from PyQt5.QtGui import QPainter, QPen, QBrush
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtCore import Qt
# 定义Olympic类
class Olympic(QWidget):
def __init__(self):
# 父类初始化
super().__init__()
# 设置宽高
self.setGeometry(300, 300, 550, 600)
self.setWindowTitle('奥运五环')
# 初始化变量
self.isBlue = False
self.isYellow = False
self.isBlack = False
self.isGreen = False
self.isRed = False
self.brushWidth = 10
self.show()
# 定义粉刷事件
def paintEvent(self, event):
qp = QPainter()
qp.begin(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Olympic()
app.exec_()
开始
先粉刷第一个蓝色圈圈
# 定义粉刷事件
def paintEvent(self, event):
qp = QPainter()
qp.begin(self)
bluePen = QPen(QBrush(Qt.blue), self.brushWidth)
qp.setPen(bluePen)
qp.drawEllipse(99, 150, 100, 100)
qp.end()
依次画出其他颜色的圈圈
# 定义粉刷事件
def paintEvent(self, event):
qp = QPainter()
qp.begin(self)
bluePen = QPen(QBrush(Qt.blue), self.brushWidth)
yellowPen = QPen(QBrush(Qt.yellow), self.brushWidth)
blackPen = QPen(QBrush(Qt.black), self.brushWidth)
greenPen = QPen(QBrush(Qt.green), self.brushWidth)
redPen = QPen(QBrush(Qt.red), self.brushWidth)
qp.setPen(bluePen)
qp.drawEllipse(99, 150, 100, 100)
qp.setPen(yellowPen)
qp.drawEllipse(161.5, 200, 100, 100)
qp.setPen(blackPen)
qp.drawEllipse(224, 150, 100, 100)
qp.setPen(greenPen)
qp.drawEllipse(286.5, 200, 100, 100)
qp.setPen(redPen)
qp.drawEllipse(349, 150, 100, 100)
qp.end()
画完是这样婶的~
优化环环相扣
你以为画完了?
nono~
奥运五环时这样婶的~
伦家是环环相扣的
所以我们要处理下,先画个试试:
# 画个圆弧覆盖下
qp.setPen(bluePen)
qp.drawArc(99, 150, 100, 100, 345*16, 30*16)
看下效果
黄色和蓝色是对了,我们再用同样的方式处理下
# 画个圆弧覆盖下
qp.setPen(bluePen)
qp.drawArc(99, 150, 100, 100, 345*16, 30*16)
qp.setPen(yellowPen)
qp.drawArc(161.5, 200, 100, 100, 70*16, 15*16)
qp.setPen(greenPen)
qp.drawArc(286.5, 200, 100, 100, 160*16, 30*16)
qp.setPen(blackPen)
qp.drawArc(224, 150, 100, 100, 345*16, 30*16)
qp.setPen(greenPen)
qp.drawArc(286.5, 200, 100, 100, 70*16, 15*16)
qp.end()
看下效果
这样我们的奥运五坏就画完了~
完整代码:
import sys
from PyQt5.QtGui import QPainter, QPen, QBrush
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtCore import Qt
# 定义Olympic类
class Olympic(QWidget):
def __init__(self):
# 父类初始化
super().__init__()
# 设置宽高
self.setGeometry(300, 300, 550, 600)
self.setWindowTitle('奥运五环')
# 初始化变量
self.isBlue = False
self.isYellow = False
self.isBlack = False
self.isGreen = False
self.isRed = False
self.brushWidth = 10
self.show()
# 定义粉刷事件
def paintEvent(self, event):
qp = QPainter()
qp.begin(self)
bluePen = QPen(QBrush(Qt.blue), self.brushWidth)
yellowPen = QPen(QBrush(Qt.yellow), self.brushWidth)
blackPen = QPen(QBrush(Qt.black), self.brushWidth)
greenPen = QPen(QBrush(Qt.green), self.brushWidth)
redPen = QPen(QBrush(Qt.red), self.brushWidth)
qp.setPen(bluePen)
qp.drawEllipse(99, 150, 100, 100)
qp.setPen(yellowPen)
qp.drawEllipse(161.5, 200, 100, 100)
qp.setPen(blackPen)
qp.drawEllipse(224, 150, 100, 100)
qp.setPen(greenPen)
qp.drawEllipse(286.5, 200, 100, 100)
qp.setPen(redPen)
qp.drawEllipse(349, 150, 100, 100)
# 画个圆弧覆盖下
qp.setPen(bluePen)
qp.drawArc(99, 150, 100, 100, 345*16, 30*16)
qp.setPen(yellowPen)
qp.drawArc(161.5, 200, 100, 100, 70*16, 15*16)
qp.setPen(greenPen)
qp.drawArc(286.5, 200, 100, 100, 160*16, 30*16)
qp.setPen(blackPen)
qp.drawArc(224, 150, 100, 100, 345*16, 30*16)
qp.setPen(greenPen)
qp.drawArc(286.5, 200, 100, 100, 70*16, 15*16)
qp.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Olympic()
app.exec_()
去掉注释和空行,49行
不过这样好像太简单了,我们加个鼠标点击圈圈来取色,如果两个颜色的圈圈交叉我们要取出两种颜色,预备备,开始~
取色
Olympic类添加定义鼠标点击事件
# 定义鼠标点击事件
def mousePressEvent(self, event):
self.x = event.x()
self.y = event.y()
# 根据鼠标位置计算是否点击在圈圈内
def point_is_in_the_circle(origin_x, origin_y, radius, x, y):
d = ((x - origin_x)**2 + (y - origin_y)**2)**(1/2)
if (d < radius):
return True
return False
self.isBlue = False
if(point_is_in_the_circle(149, 200, 50, self.x, self.y)):
self.isBlue = True
# 更新下视图
self.update()
paintEvent添加
if((self.isBlue)):
if(self.isBlue):
qp.fillRect(100, 450, 350, 100, Qt.blue)
看下效果
补气所有取色效果
mousePressEvent方法
# 定义鼠标点击事件
def mousePressEvent(self, event):
self.x = event.x()
self.y = event.y()
# 根据鼠标位置计算是否点击在圈圈内
def point_is_in_the_circle(origin_x, origin_y, radius, x, y):
d = ((x - origin_x)**2 + (y - origin_y)**2)**(1/2)
if (d < radius):
return True
return False
self.isBlue = False
self.isYellow = False
self.isBlack = False
self.isGreen = False
self.isRed = False
if(point_is_in_the_circle(149, 200, 50, self.x, self.y)):
self.isBlue = True
if(point_is_in_the_circle(211.5, 250, 50, self.x, self.y)):
self.isYellow = True
if(point_is_in_the_circle(274, 200, 50, self.x, self.y)):
self.isBlack = True
if(point_is_in_the_circle(336.5, 250, 50, self.x, self.y)):
self.isGreen = True
if(point_is_in_the_circle(399, 200, 50, self.x, self.y)):
self.isRed = True
# 更新下视图
self.update()
paintEvent方法,需要注意的是俩个圈圈并集的把两种颜色都取出来,如下:
if((self.isBlue) or (self.isYellow) or (self.isBlack) or (self.isGreen) or (self.isRed)):
if(self.isBlue):
qp.fillRect(100, 450, 350, 100, Qt.blue)
if(self.isYellow):
qp.fillRect(100, 450, 350, 100, Qt.yellow)
if((self.isBlue) and (self.isYellow)):
qp.fillRect(100, 450, 175, 100, Qt.blue)
qp.fillRect(275, 450, 175, 100, Qt.yellow)
if(self.isBlack):
qp.fillRect(100, 450, 350, 100, Qt.black)
if((self.isBlack) and (self.isYellow)):
qp.fillRect(100, 450, 175, 100, Qt.yellow)
qp.fillRect(275, 450, 175, 100, Qt.black)
if(self.isGreen):
qp.fillRect(100, 450, 350, 100, Qt.green)
if((self.isBlack) and (self.isGreen)):
qp.fillRect(100, 450, 175, 100, Qt.black)
qp.fillRect(275, 450, 175, 100, Qt.green)
if(self.isRed):
qp.fillRect(100, 450, 350, 100, Qt.red)
if((self.isGreen) and (self.isRed)):
qp.fillRect(100, 450, 175, 100, Qt.green)
qp.fillRect(275, 450, 175, 100, Qt.red)
修复bug
# 修复 command+w 关闭窗口
sys.exit(app.exec_())
最终效果
完整代码如下:
import sys
from PyQt5.QtGui import QPainter, QPen, QBrush
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtCore import Qt
# 定义Olympic类
class Olympic(QWidget):
def __init__(self):
# 父类初始化
super().__init__()
# 设置宽高
self.setGeometry(300, 300, 550, 600)
self.setWindowTitle('奥运五环')
# 初始化变量
self.isBlue = False
self.isYellow = False
self.isBlack = False
self.isGreen = False
self.isRed = False
self.brushWidth = 10
self.show()
# 定义鼠标点击事件
def mousePressEvent(self, event):
self.x = event.x()
self.y = event.y()
# 根据鼠标位置计算是否点击在圈圈内
def point_is_in_the_circle(origin_x, origin_y, radius, x, y):
d = ((x - origin_x)**2 + (y - origin_y)**2)**(1/2)
if (d < radius):
return True
return False
self.isBlue = False
self.isYellow = False
self.isBlack = False
self.isGreen = False
self.isRed = False
if(point_is_in_the_circle(149, 200, 50, self.x, self.y)):
self.isBlue = True
if(point_is_in_the_circle(211.5, 250, 50, self.x, self.y)):
self.isYellow = True
if(point_is_in_the_circle(274, 200, 50, self.x, self.y)):
self.isBlack = True
if(point_is_in_the_circle(336.5, 250, 50, self.x, self.y)):
self.isGreen = True
if(point_is_in_the_circle(399, 200, 50, self.x, self.y)):
self.isRed = True
# 更新下视图
self.update()
# 定义粉刷事件
def paintEvent(self, event):
qp = QPainter()
qp.begin(self)
bluePen = QPen(QBrush(Qt.blue), self.brushWidth)
yellowPen = QPen(QBrush(Qt.yellow), self.brushWidth)
blackPen = QPen(QBrush(Qt.black), self.brushWidth)
greenPen = QPen(QBrush(Qt.green), self.brushWidth)
redPen = QPen(QBrush(Qt.red), self.brushWidth)
qp.setPen(bluePen)
qp.drawEllipse(99, 150, 100, 100)
qp.setPen(yellowPen)
qp.drawEllipse(161.5, 200, 100, 100)
qp.setPen(blackPen)
qp.drawEllipse(224, 150, 100, 100)
qp.setPen(greenPen)
qp.drawEllipse(286.5, 200, 100, 100)
qp.setPen(redPen)
qp.drawEllipse(349, 150, 100, 100)
# 画个圆弧覆盖下
qp.setPen(bluePen)
qp.drawArc(99, 150, 100, 100, 345*16, 30*16)
qp.setPen(yellowPen)
qp.drawArc(161.5, 200, 100, 100, 70*16, 15*16)
qp.setPen(greenPen)
qp.drawArc(286.5, 200, 100, 100, 160*16, 30*16)
qp.setPen(blackPen)
qp.drawArc(224, 150, 100, 100, 345*16, 30*16)
qp.setPen(greenPen)
qp.drawArc(286.5, 200, 100, 100, 70*16, 15*16)
if((self.isBlue) or (self.isYellow) or (self.isBlack) or (self.isGreen) or (self.isRed)):
if(self.isBlue):
qp.fillRect(100, 450, 350, 100, Qt.blue)
if(self.isYellow):
qp.fillRect(100, 450, 350, 100, Qt.yellow)
if((self.isBlue) and (self.isYellow)):
qp.fillRect(100, 450, 175, 100, Qt.blue)
qp.fillRect(275, 450, 175, 100, Qt.yellow)
if(self.isBlack):
qp.fillRect(100, 450, 350, 100, Qt.black)
if((self.isBlack) and (self.isYellow)):
qp.fillRect(100, 450, 175, 100, Qt.yellow)
qp.fillRect(275, 450, 175, 100, Qt.black)
if(self.isGreen):
qp.fillRect(100, 450, 350, 100, Qt.green)
if((self.isBlack) and (self.isGreen)):
qp.fillRect(100, 450, 175, 100, Qt.black)
qp.fillRect(275, 450, 175, 100, Qt.green)
if(self.isRed):
qp.fillRect(100, 450, 350, 100, Qt.red)
if((self.isGreen) and (self.isRed)):
qp.fillRect(100, 450, 175, 100, Qt.green)
qp.fillRect(275, 450, 175, 100, Qt.red)
qp.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Olympic()
# 修复 command+w 关闭窗口
sys.exit(app.exec_())
作业
用今天所学,实现如下每个圈圈相交部分有一点点白边:
以上是关于弘扬奥运精神,我们49行画个奥运五环的主要内容,如果未能解决你的问题,请参考以下文章