程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to gracefully use Python for the next code rain?

編輯:Python

In fact, I have long wanted to write a small program to practice , I always like the style of the matrix , Think simple , But there is no way to write , First, the program must look good , I can't accept things that are too bad , Second, be fluent , I can't accept too laggy either .
The overall logical :
1、 Random letters fall down in the form of rain
2、 Change the color , With gradient
3、 You can customize parameters , Easy to migrate to different screens
4、 At the same time, several raindrops fell down
It's really not difficult to write a line of raindrops , At the same time, I was really confused about writing many lines. After thinking for a long time, I was unable to start ,python Of pygame There are a lot of ,mpy No, , Later, I went to github I found it on the Internet , only one , But the core code is only 20 Multiple lines , grace ! It's really elegant ! But the author didn't give any notes , So I'm going to print it out step by step , Distributed operation , Finally understand the mechanism , The migration was successful , The effect is as follows :

The core principle is to use arrays , According to the row and column, create a... With two more rows than the screen display , These two lines are used to cache the data of simulated raindrops , This array is then dedicated to the processes that store raindrops , That is, the color grade , The higher, the brighter , It seems to fall line by line , In fact, the full screen characters are output every time ! Thanks to me github Big guy's highway st7789 drive , Finally, the silky effect is realized , I learned. I learned , In the future, I have more ideas for doing things .
Finally, paste the core code , Again, the reality is too elegant , Actually, only one character is output , The seemingly silky animation is actually painted with full screen characters , High energy and elegance ahead ! Throwing away the definition of variables is actually while There are only three for Loop's done !

BLACK = st7789.color565(0,0,0)
WHITE = st7789.color565(200,255,200)
FONT = 16
width = 15
height = 15
size = width*height
chars = [random.choice("0123456789abcdef") for _ in range(size+2*width)]
field = [0 for _ in range(size+2*width)]
while 1:
for _ in range(1): #how many rains each time
field[int(random.random()*15)] = 15 #random rain
for i in range(size+width*2-1, width-1, -1): #create matrix rain scale
if field[i-width] == 15: #each line -1 and send to next line
field[i] = 15
if field[i-width] > 0:
field[i-width] -= 1
for i in range(size):
if field[i+width]==0: #decide color
attr = BLACK
elif field[i+width] == 15:
attr = WHITE
else:
attr=st7789.color565(0,43+field[i+width]*13,0)
lcd.text(font1,chars[i+width], FONT * (i % width),FONT * (i//width),attr,BLACK)

Hardware initialization part , According to their own platform

import utime as time
from machine import Pin,SPI
import st7789,os,machine
import random
import font1
#---------------Hardware init----------------
#screen for 01stuido pyclock
spi = machine.SPI(1,baudrate=40000000,mosi=Pin(7),miso=Pin(0),sck=Pin(6))
lcd = st7789.ST7789(
spi, 240,240,
reset=Pin(8, Pin.OUT),
cs=Pin(5, Pin.OUT),
dc=Pin(4, Pin.OUT),
backlight=Pin(0, Pin.OUT),
rotation=0
)
lcd.init()
lcd.inversion_mode(True)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved