網(wǎng)站首頁 編程語言 正文
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
相關(guān)推薦
- 2024-03-22 【IDEA】maven項(xiàng)目刷新依賴的兩種方式
- 2023-11-16 python list 刪除列表中某個(gè)元素的3種方法;附加刪除numpy數(shù)組中的指定索引元素的方法
- 2022-02-22 Linux系統(tǒng)下根目錄擴(kuò)容介紹_Linux
- 2023-05-20 React組件的用法概述_React
- 2023-02-02 C語言多文件編程問題解析_C 語言
- 2024-03-13 TypeError: Cannot read property ‘get‘ of undefined
- 2022-06-24 淺析C++?atomic?和?memory?ordering_C 語言
- 2022-08-20 Python中range()與np.arange()的具體使用_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支