欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2022年11月03日 16:34:10   作者:showswoller  
這篇文章主要為大家詳細(xì)介紹了python中的一個小項目:利用pygame實現(xiàn)代碼雨動畫效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下

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

如視頻所示 利用pygame庫實現(xiàn)了一個代碼呈雨狀下落的視覺效果

部分代碼如下

import sys
import random
import pygame
from pygame.locals import *
 
 
# 屏幕大小
WIDTH = 800
HEIGHT = 600
# 下落速度范圍
SPEED = [15, 30]
# 字母大小范圍
SIZE = [5, 30]
# CODE長度范圍
LEN = [1, 8]
 
 
# 隨機生成一個顏色
def randomColor():
	return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
 
 
# 隨機生成一個速度
def randomSpeed():
	return random.randint(SPEED[0], SPEED[1])
 
 
# 隨機生成一個大小
def randomSize():
	return random.randint(SIZE[0], SIZE[1])
 
 
# 隨機生成一個長度
def randomLen():
	return random.randint(LEN[0], LEN[1])
 
 
# 隨機生成一個位置
def randomPos():
	return (random.randint(0, WIDTH), -20)
 
 
# 隨機生成一個字符串
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())	# 隨機字體大小
		self.speed = randomSpeed()			# 隨機速度
		self.code = self.getCode()			# 隨機長度
		self.image = self.font.render(self.code, True, randomColor())	# 使用已有的文本創(chuàng)建一個位圖image,返回值為一個image  隨機顏色
		self.image = pygame.transform.rotate(self.image, random.randint(87, 93))	# 講圖像隨機旋轉(zhuǎn)角度
		self.rect = self.image.get_rect()
		self.rect.topleft = randomPos()		# 隨機位置
 
	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;第一個參數(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()

到此這篇關(guān)于Python+Pygame實現(xiàn)代碼雨動畫效果的文章就介紹到這了,更多相關(guān)Python Pygame代碼雨內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論