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

Python實現(xiàn)屏幕代碼雨效果的示例代碼

 更新時間:2022年03月11日 09:18:13   作者:Vertira  
這篇文章主要介紹了如何利用Python中的Pygame模塊實現(xiàn)代碼雨效果,文中通過示例代碼介紹的非常詳細,感興趣的朋友們下面隨著小編來一起學習學習吧

直接上代碼

import pygame
import random
 
 
def main():
    # 初始化pygame
    pygame.init()
 
    # 默認不全屏
    fullscreen = False
    # 窗口未全屏寬和高
    WIDTH, HEIGHT = 1100, 600
 
    init_width, init_height = WIDTH, HEIGHT
 
    # 字塊大小,寬,高
    suface_height = 18
    # 字體大小
    font_size = 20
 
    # 創(chuàng)建一個窗口
    screen = pygame.display.set_mode((init_width, init_height))
 
    # 字體
    font = pygame.font.Font('msyh.ttf', font_size)
 
    # 創(chuàng)建一個圖像對象
    bg_suface = pygame.Surface((init_width, init_height), flags=pygame.SRCALPHA)
    pygame.Surface.convert(bg_suface)
    bg_suface.fill(pygame.Color(0, 0, 0, 28))
 
    # 用純色填充背景
    screen.fill((0, 0, 0))
 
    # 顯示的字符
    letter = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x',
              'c',
              'v', 'b', 'n', 'm']
    texts = [
        font.render(str(letter[i]), True, (0, 255, 0)) for i in range(26)
    ]
 
    # 也可以替換成0 1 顯示
    # texts = [
    #     font.render('0',True,(0,255,0)),font.render('1',True,(0,255,0))
    # ]
 
    # 生成的列數(shù)
    column = int(init_width / suface_height)
    drops = [0 for i in range(column)]
 
    while True:
        # 按鍵檢測
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                # 接受到退出事件后退出
                exit()
            elif event.type == pygame.KEYDOWN:
                # 按F11切換全屏,或窗口
                if event.key == pygame.K_F11:
                    print("檢測到按鍵F11")
                    fullscreen = not fullscreen
                    if fullscreen:
                        # 全屏效果,參數(shù)重設(shè)
                        size = init_width, init_height = pygame.display.list_modes()[0]
                        screen = pygame.display.set_mode(size, pygame.FULLSCREEN | pygame.HWSURFACE)
 
                    else:
                        init_width, init_height = WIDTH, HEIGHT
                        screen = pygame.display.set_mode((WIDTH, HEIGHT))
 
                    # 圖像對象重新創(chuàng)建
                    bg_suface = pygame.Surface((init_width, init_height), flags=pygame.SRCALPHA)
                    pygame.Surface.convert(bg_suface)
                    bg_suface.fill(pygame.Color(0, 0, 0, 28))
                    column = int(init_width / suface_height)
                    drops = [0 for i in range(column)]
                elif event.key == pygame.K_ESCAPE:
                    # 按ESC退出
                    exit()
        # 延時
        pygame.time.delay(30)
 
        # 圖像對象放到窗口的原點坐標上
        screen.blit(bg_suface, (0, 0))
 
        for i in range(len(drops)):
            # 隨機字符
            text = random.choice(texts)
 
            # 把字符畫到該列的下雨的位置
            screen.blit(text, (i * suface_height, drops[i] * suface_height))
 
            # 更新下雨的坐標
            drops[i] += 1
 
            # 超過界面高度或隨機數(shù),下雨位置置0
            if drops[i] * suface_height > init_height or random.random() > 0.95:
                drops[i] = 0
 
        # 更新畫面
        pygame.display.flip()
 
 
if __name__ == '__main__':
    main()

運行效果:

import pygame的安裝方法

pygame 這個包沒有安裝。python安裝pygame包的方法

很簡單:

使用國內(nèi)源安裝,清華源 中科,阿里都可以。

進入Anaconda3 的虛擬環(huán)境,輸入下面的命令??焖侔惭b

pip install pygame -i https://pypi.tuna.tsinghua.edu.cn/simple

安裝效果

Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pygame
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4c/0d/23f786eb611652b0125fcf334a0c21324922a756e6d954c50ecddfc8d4bb/pygame-2.1.2-cp36-cp36m-win_amd64.whl (8.4 MB)
     |████████████████████████████████| 8.4 MB 119 kB/s
Installing collected packages: pygame
Successfully installed pygame-2.1.2

成功安裝。

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

相關(guān)文章

最新評論