当前位置: 首页 > news >正文

python代码案例分享,python实现代码雨,动画显示,pygame使用教程

python代码案例分享,python实现代码雨,动画显示

代码实现:


# 代码雨列表
class CodeRain:def __init__(self):self.column_width = 20self.columns = WIDTH // self.column_widthself.drops = []self.texts = []self.colors = []self.speeds = []self.trail_colors = []  # 轨迹颜色for i in range(self.columns):self.drops.append(random.randint(-100, 0))self.texts.append(randomCode())self.colors.append(randomColor())self.speeds.append(randomSpeed() / 10)self.trail_colors.append((0, random.randint(100, 255), 0))  # 绿色轨迹def update(self):for i in range(self.columns):self.drops[i] += self.speeds[i]if self.drops[i] > HEIGHT:self.drops[i] = random.randint(-100, 0)self.texts[i] = randomCode()self.colors[i] = randomColor()self.speeds[i] = randomSpeed() / 10def draw(self, surface):for i in range(self.columns):if self.drops[i] > 0:# 绘制轨迹效果for j in range(10):if self.drops[i] - j * 15 > 0:trail_text = randomCode()trail_color = (0, max(0, 255 - j * 25), 0)  # 渐变绿色trail_surface = font.render(trail_text, True, trail_color)surface.blit(trail_surface, (i * self.column_width, self.drops[i] - j * 15))# 绘制主字符text_surface = font.render(self.texts[i], True, self.colors[i])surface.blit(text_surface, (i * self.column_width, self.drops[i]))

定义代码精力类:


class Code(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self)self.font = pygame.font.Font('./font.ttf', randomSize())	# 随机字体大小self.speed = randomSpeed()			# 随机速度self.code = self.getCode()			# 随机长度self.image = self.font.render(self.code, True, randomColor())	# 使用已有的文本创建一个位图image,返回值为一个image  随机颜色self.image = pygame.transform.rotate(self.image, random.randint(87, 93))	# 讲图像随机旋转角度self.rect = self.image.get_rect()self.rect.topleft = randomPos()		# 随机位置def getCode(self):length = randomLen()code = ''for i in range(length):code += randomCode()return codedef update(self):self.rect = self.rect.move(0, self.speed)if self.rect.top > HEIGHT:self.kill()

初始化pygame

pygame.init()			# 初始函数,使用pygame的第一步
screen = pygame.display.set_mode((WIDTH, HEIGHT))	#生成主屏幕screen;第一个参数是屏幕大小
pygame.display.set_caption('Code Rain-居然')	# 窗口命名clock = pygame.time.Clock()					# 初始化一个clock对象
codesGroup = pygame.sprite.Group()			# 精灵组,一个简单的实体容器
while True:clock.tick(24)							# 控制游戏绘制的最大帧率为30for event in pygame.event.get():if event.type == QUIT:pygame.quit()sys.exit(0)# screen.fill((1, 1, 1))					# 填充screen.fill((0, 0, 0))						# 填充背景颜色codeobject = Code()codesGroup.add(codeobject)				# 添加精灵对象codesGroup.update()codesGroup.draw(screen)pygame.display.update()

最终执行demo类

我整理了一些python代码案例源码以及教程,包含基础教程,自动化,算法,实例案例有几百个案例,python学习佳品

「python源码案例」
https://drive.uc.cn/s/f661de583ee34

http://www.dtcms.com/a/408784.html

相关文章:

  • 上海智能网站建设公司哪里有网站制作服务
  • 网络公司网站设计多少钱广告机信息发布系统软件
  • 工业信息化部网站备案系统小型企业网站建设报告模拟组建过程
  • 建网站选服务器wordpress页头登录
  • 深圳网站开发设计学习做网页的网站
  • 婚纱摄影网站的设计怎么创立一个自己的品牌
  • 合肥官方网站优化费用网络推广引流软件
  • 企业网站重要性蓝色商务网站模板
  • 移动端手机网站制作电商网站设计推荐亿企邦
  • openEuler2403安装Dify
  • 武昌做网站的公司网站建设竣工验收报告
  • 哪个网站做任务给东西icp备案通过了 怎么修改我的网站
  • 网站后台附件无法上传网站建设公司销售
  • 网站开发常用小图片ps做网站需注意
  • 中国建设银行有哪些招聘网站旅游网站建设要求
  • 长清网站建设价格公司网站建设怎么选择
  • 苏州无锡市住房和城乡建设局网站网页设计风格
  • 苏州高端网站建设机构网站建设先有域名然后呢
  • 如何注册网站免费注册网络安全十大公司
  • 大同市建设工程招标投标网站有什么公司做网站好
  • 企业站官方网站常州手机网站开发
  • 郑州高端网站建设团队centos 部署wordpress
  • 网页设计网站方案网店装修教程免费模板
  • 湘潭市建设网站wordpress先页面再首页
  • 网站站内logo怎么做长沙哪家网站公司
  • 电子商务网站建设考试简答题平台网站怎么做seo
  • 网站页面设计具体步骤wordpress媒体库在哪个文件夹
  • 济源网站维护百度账号登录官网
  • 网站编辑前端可以做吗兰州最好的互联网公司
  • 网站开发和浏览器兼容问题wordpress 免费摄影主题