Python ile Matrix Rain Etkisi Yaratmak
İncelediğimiz kod ile Matrix filmlerindeki, düşen harflerle yaratılan etkiyi yeniden oluşturuyoruz.
Orijinal kodları: https://gist.github.com/MrKioZ/c07b9377d20bab53af6ebcdfbdeabb64 adresinde bulabilirsiniz.
Aşağıdaki kodlar kısaltılmış ve bazı eklentiler yapılmış halidir.
Kaynak kodlar:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# Orijinal kod: https://gist.github.com/MrKioZ/c07b9377d20bab53af6ebcdfbdeabb64 import pygame, pygame.font import random import os import sys # COLOR = (0, 200, 200) #The Color of the Matrix COLOR = (0, 200, 0) #The Color of the Matrix TICK = 30 # ekran yenileme hızı CARPAN = 2.0 # harf yoğunluğu IZBIRAK = True # arkaplanın dolu veya boş olması # def IsWritten(): # defTemp = True # for x in range((lettersOnScreen[0] * CARPAN) , (lettersOnScreen[0] * CARPAN) + 1): # if xHeads[x] == -1: # defTemp = False # return defTemp def getColor(fx,fy): defTemp=xHeads[fx]-fy if (maxCol>defTemp>0): return defTemp else: return maxCol-1 # Pygame init pygame.init() temp = pygame.display.Info() # displLength = (temp.current_w, temp.current_h) # surface = pygame.display.set_mode(displLength, pygame.FULLSCREEN) # dikkat! tam ekran sistemde kilitlenme yapabiliyor displLength = (temp.current_w-100, temp.current_h-50) surface = pygame.display.set_mode(displLength, pygame.DOUBLEBUF) # Font init pygame.font.init() # fontObj = pygame.font.Font(pygame.font.get_default_font(), 14) # varsayılan yerine bu fontu indirip kullanabilirsiniz fontObj = pygame.font.Font(os.path.join("matrix_fonts","matrix code nfi.ttf"), 14) sampleLetter = fontObj.render('_', False, (0, 111, 0)) letterSize = (sampleLetter.get_width(), sampleLetter.get_height()) lettersOnScreen = (int(displLength[0] / letterSize[0]), int(displLength[1] / letterSize[1])) # color init colorList = [(255, 255, 255)] print(colorList) primeColors = len(colorList)+1 R,G,B = COLOR colorList += [(R+10, G+10, B+10)] * ((lettersOnScreen[1] - 10)) print(colorList) endColors = len(colorList) # colorList += [(R-50 if R else 0, B-50 if B else 0, G-50 if G else 0),(R-100 if R else 0, B-100 if B else 0, G-100 if G else 0),(0, 0, 0)] if IZBIRAK: kuyruk = (0,50,0) # izi kalsın else: kuyruk = (0, 0, 0) # izi temizle colorList += [(20, 20, 20),(10, 10, 10),(4, 4, 4),kuyruk] endColors = len(colorList) - endColors+1 print(colorList) maxCol = len(colorList) print(primeColors, endColors, maxCol); # exit() # char generator letters = [[0 for _ in range(lettersOnScreen[1] + 1)] for _ in range(lettersOnScreen[0])] char = chr(random.randint(32, 126)) for y in range(lettersOnScreen[1] + 1): for x in range(lettersOnScreen[0]): letters[x][y] = [fontObj.render(char, False, colorList[col]) for col in range(maxCol)] char = chr(random.randint(32, 126)) xHeads = [0 for _ in range(lettersOnScreen[0] + 1)] # 1st loop - word write, no char switch notDone = True ticksLeft = lettersOnScreen[1] + maxCol strLen = int((lettersOnScreen[0] * CARPAN)) # main matrix, has char switch while notDone: for event in pygame.event.get(): if event.type == pygame.QUIT: notDone = False if event.type == pygame.KEYDOWN: notDone = False if random.randint(1, 2) == 1: randomInt = random.randint(0, lettersOnScreen[0]) if xHeads[randomInt] <= 0: xHeads[randomInt] = 1 for x in range(lettersOnScreen[0]): col = 0 counter = xHeads[x] # main loop for redraw while (counter > 0) and (col < maxCol): if (counter < lettersOnScreen[1] + 2) and (col < primeColors or col > (maxCol - endColors)): surface.blit(letters[x][counter - 1][col], (x * letterSize[0], (counter - 1) * letterSize[1])) col += 1 counter -= 1 # charswitch randomInt = random.randint(1, maxCol - 1) charPosY = xHeads[x] - randomInt if (lettersOnScreen[1] - 1 > charPosY > 0): temp = letters[x][charPosY] randomX = random.randint(1, lettersOnScreen[0] - 1) randomY = random.randint(1,lettersOnScreen[1] - 1) surface.blit(letters[x][charPosY][maxCol - 1], (x * letterSize[0], charPosY * letterSize[1])) surface.blit(letters[randomX][randomY][maxCol - 1], (randomX * letterSize[0], randomY * letterSize[1])) # char swap letters[x][charPosY] = letters[randomX][randomY] letters[randomX][randomY] = temp surface.blit(letters[x][charPosY][randomInt], (x * letterSize[0], charPosY * letterSize[1])) surface.blit(letters[randomX][randomY][getColor(randomX,randomY)], (randomX * letterSize[0], randomY * letterSize[1])) # check if is out of screen if xHeads[x] > 0: xHeads[x] += 1 if xHeads[x] - maxCol > lettersOnScreen[1]: xHeads[x] = 0 pygame.display.update() clock = pygame.time.Clock() clock.tick(TICK) sys.exit() |
Ahmet Aksoy