日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Python+Pygame實(shí)現(xiàn)代碼雨動畫效果_python

作者:showswoller ? 更新時(shí)間: 2022-12-06 編程語言

pygame實(shí)現(xiàn)代碼雨動畫

如視頻所示 利用pygame庫實(shí)現(xiàn)了一個(gè)代碼呈雨?duì)钕侣涞囊曈X效果

部分代碼如下

import sys
import random
import pygame
from pygame.locals import *
 
 
# 屏幕大小
WIDTH = 800
HEIGHT = 600
# 下落速度范圍
SPEED = [15, 30]
# 字母大小范圍
SIZE = [5, 30]
# CODE長度范圍
LEN = [1, 8]
 
 
# 隨機(jī)生成一個(gè)顏色
def randomColor():
	return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
 
 
# 隨機(jī)生成一個(gè)速度
def randomSpeed():
	return random.randint(SPEED[0], SPEED[1])
 
 
# 隨機(jī)生成一個(gè)大小
def randomSize():
	return random.randint(SIZE[0], SIZE[1])
 
 
# 隨機(jī)生成一個(gè)長度
def randomLen():
	return random.randint(LEN[0], LEN[1])
 
 
# 隨機(jī)生成一個(gè)位置
def randomPos():
	return (random.randint(0, WIDTH), -20)
 
 
# 隨機(jī)生成一個(gè)字符串
def randomCode():
	return random.choice('qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890')
 
 
# 定義代碼精靈類
class Code(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.font = pygame.font.Font('./font.ttf', randomSize())	# 隨機(jī)字體大小
		self.speed = randomSpeed()			# 隨機(jī)速度
		self.code = self.getCode()			# 隨機(jī)長度
		self.image = self.font.render(self.code, True, randomColor())	# 使用已有的文本創(chuàng)建一個(gè)位圖image,返回值為一個(gè)image  隨機(jī)顏色
		self.image = pygame.transform.rotate(self.image, random.randint(87, 93))	# 講圖像隨機(jī)旋轉(zhuǎn)角度
		self.rect = self.image.get_rect()
		self.rect.topleft = randomPos()		# 隨機(jī)位置
 
	def getCode(self):
		length = randomLen()
		code = ''
		for i in range(length):
			code += randomCode()
		return code
	def update(self):
		self.rect = self.rect.move(0, self.speed)
		if self.rect.top > HEIGHT:
			self.kill()
 
 
pygame.init()			# 初始函數(shù),使用pygame的第一步
screen = pygame.display.set_mode((WIDTH, HEIGHT))	#生成主屏幕screen;第一個(gè)參數(shù)是屏幕大小
pygame.display.set_caption('Code Rain-居然')	# 窗口命名
 
	# 控制游戲繪制的最大幀率為30
	for 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()

原文鏈接:https://blog.csdn.net/jiebaoshayebuhui/article/details/127584110

欄目分類
最近更新