Python仿黑客帝国代码雨
生活随笔
收集整理的這篇文章主要介紹了
Python仿黑客帝国代码雨
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
# * coding=utf8import sys
import random
import pygame
from pygame.locals import *# 屏幕大小
WIDTH = 800
HEIGHT = 600
# 下落速度范圍
SPEED = [20, 40]
# CODE String列表
LEN = ['PHP','Python','C++','Java','C#','javascript','GoLang','Ruby','Android','Vue','swift','basic','.net']# 隨機生成一個顏色
def randomColor():return (0,238,0)return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))# 隨機生成一個速度
def randomSpeed():return random.randint(SPEED[0], SPEED[1])# 隨機生成一個位置
def randomPos():return (random.randint(0, WIDTH), -20)# 隨機生成一個字符串
def randomCode():return LEN[random.randint(1,len(LEN))-1]# 隨機生成字體大小
def randomSize():return random.randint(12,36)# 定義代碼精靈類
class Code(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self)self.code= randomCode()#self.font = pygame.font.Font('./font.ttf', randomSize())self.font = pygame.font.Font('C:/Windows/Fonts/simhei.ttf', randomSize())self.speed = randomSpeed()self.image = self.font.render(self.code, True, randomColor())self.image = pygame.transform.rotate(self.image, random.randint(90, 90))#以垂直方式下落self.rect = self.image.get_rect()self.rect.topleft = randomPos()def update(self):self.rect = self.rect.move(0, self.speed)if self.rect.top > HEIGHT:#當精靈位置超出屏幕,銷毀self.kill()
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('code_rain')
clock = pygame.time.Clock()
codesGroup = pygame.sprite.Group()
while True:clock.tick(24) #幀數for event in pygame.event.get():#監聽關閉事件if event.type == QUIT:pygame.quit()sys.exit(0)screen.fill((1, 1, 1)) #填充背景色,相當于clear#新建一個精靈codeobject = Code()codesGroup.add(codeobject)# 監控并銷毀codesGroup.update()codesGroup.draw(screen)pygame.display.update()
總結
以上是生活随笔為你收集整理的Python仿黑客帝国代码雨的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu18.04安装COMSOL
- 下一篇: websocket python爬虫_p