在python的pygame设置彩虹弦:使用汉字变量及代码分析

1.推荐指数:★★★★

不一样的python学习和玩法,提高学习兴趣,加深代码理解。


在python的pygame设置彩虹弦:使用汉字变量及代码分析

2.第1步:

<code>#---第1步---导出模块---
import pygame,sys
from pygame.locals import *
import math
import colorsys/<code>

3.第2步:

<code>#---第2步--初始化游戏和定义宽和高,屏幕挂起,窗口标题名称---
#---小彩蛋---中文变量---
pygame.init()
屏幕宽,屏幕高=2000,1200
#宽-500,减去的数值绝对值越大,越往左边靠
#屏幕高-100,减去的数值越大,往上靠,原因坐标点0,0是左上角顶点
屏幕中心x坐标,屏幕中心y坐标 = 屏幕宽//2 -500 ,屏幕高//2 - 100
screen = pygame.display.set_mode((屏幕宽,屏幕高))
pygame.display.set_caption("多彩正弦线")/<code>

4.第3步:

<code>#---第3步---定义类:彩虹---
class 彩虹():
def __init__(self,radius,color,粗细):
self.color = color
#减去多少自己定义,没意义,主要是位置选好即可
self.x = 屏幕中心x坐标-30
self.y = 屏幕中心y坐标-30
self.angle = 0
#self.radius =radius

#self.粗细=粗细
#定义角度
def setxy(self,angle):
self.x = int(屏幕中心x坐标+angle)
self.y = int(屏幕中心y坐标-100*math.sin(math.radians(angle)))
#画圆点
pygame.draw.circle(screen,self.color,(self.x,self.y),10, 0)
#颜色多变叠加
def coloradd(self):
#下面的中文变量---主要是玩技术---实际变成还是建议英文或者字母---
多,彩,色, = colorsys.rgb_to_hls(self.color[0]/255,self.color[1]/255,self.color[2]/255)
多=多+0.01
r,g,b=colorsys.hls_to_rgb(多,彩,色)
#r,g,b=colorsys.rgb_to_hls(多,彩,色) #这样就是报错,一种天蓝色颜色
self.color=(int(r*255),int(g*255),int(b*255))/<code>

5.第4步:

<code>#---第4步---主函数定义---
def main():
#注意这里有三个对应的值,在__init__定义的:半径,颜色,粗细
#粗细对应2,可是设置起来没变化,所以上面的self.粗细=粗细可以注释掉,也没有影响
pen=彩虹(100,(255,0,0),2)
#引入时钟刷新
clock=pygame.time.Clock()
#角度范围或者说彩虹条向左右延伸的长度,可调节,注意与屏幕关系
for angle in range(-380,880):
pen.setxy(angle)
pen.coloradd()
pygame.display.update()
#数值越大动画越快,调节速度的,刷新速度

clock.tick(600)/<code>

注意:没有while循环对吧?那么动画结束后就自动关闭窗口,没得看了。可以在主函数内加while循环。推荐常规while循环设置,复制进去后注意缩进,因为它在def main()内。

<code>    '''
#花里胡哨设置while循环
#这是一个中文设置,玩技术,纯好玩的,大型游戏不建议,有点啰嗦
#下面可以注释掉:↓
展示多彩弧度 = True
while 展示多彩弧度:
for event in pygame.event.get():
#点击右上角的x则关闭窗口,必不可少的,我觉得,个人比较喜欢
if event.type ==pygame.QUIT:
展示多彩弧度=False
pygame.quit()
sys.exit()
pygame.display.update()
#上面可以注释掉,↑
#注意注释掉的pygame,没有while循环,那么运动停止后就自动退出了
'''
'''
#常规设置while循环
while True:
for event in pygame.event.get():
if event.type ==pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()

'''/<code>

6.第5步:

<code>
#---第5步--程序入口---相当于是 Python 模拟的程序入口
if __name__=="__main__":
main()/<code>

7.效果图:


在python的pygame设置彩虹弦:使用汉字变量及代码分析

在玩中学,在学中玩,喜欢不?


分享到:


相關文章: