Advertisement
jee7

TIA Tetris - Versioon 25

Nov 20th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.90 KB | None | 0 0
  1. import pygame, random
  2.  
  3.  
  4. def joonistaRuut(x, y, värv):
  5.     alumineVärv = (max(0, värv[0] - 10), max(0, värv[1] - 10), max(0, värv[2] - 10))
  6.     pygame.draw.rect(ekraan, alumineVärv, (x * ruuduLaius, y * ruuduLaius, ruuduLaius, ruuduLaius))
  7.  
  8.     ülemineVärv = [min(üksVärv + 10, 255) for üksVärv in värv]
  9.     pygame.draw.rect(ekraan, ülemineVärv, (x * ruuduLaius, y * ruuduLaius, ruuduLaius - 1, ruuduLaius - 1))
  10.  
  11.     pygame.draw.rect(ekraan, värv, (x * ruuduLaius + 1, y * ruuduLaius + 1, ruuduLaius - 2, ruuduLaius - 2))
  12.  
  13.    
  14.  
  15. #See klass hoiab väljaku seisu ja oskab seda joonistada
  16. class Kaart:
  17.     laius = 20  #niipalju ruute laiuses
  18.     kõrgus = 40 #niipalju ruute kõrguses
  19.  
  20.     def __init__(self):
  21.         #Teeme 2-mõõtmelise listi
  22.         self.kaart = []
  23.         for x in range(Kaart.laius):
  24.             self.kaart.append([])
  25.             for y in range(Kaart.kõrgus):
  26.                 self.kaart[x].append(0)
  27.  
  28.     def joonista(self):
  29.         #Joonistame ruudud
  30.         for x, rida in enumerate(self.kaart):
  31.             for y, lahter in enumerate(rida):
  32.                 kasJoonistas = False
  33.                 if lahter != 0:
  34.                     if isinstance(lahter, Kujund):
  35.                         kasJoonistas = lahter.joonista(x, y)
  36.                     else:
  37.                         joonistaRuut(x, y, lahter)
  38.                         kasJoonistas = True
  39.                    
  40.                 if lahter == 0:
  41.                     pygame.draw.rect(ekraan, (20, 20, 20), (x * ruuduLaius, y * ruuduLaius, ruuduLaius, ruuduLaius))
  42.                     pygame.draw.rect(ekraan, (0, 0, 0), (x * ruuduLaius + 1, y * ruuduLaius + 1, ruuduLaius - 2, ruuduLaius - 2))
  43.                 elif not kasJoonistas:
  44.                     pygame.draw.rect(ekraan, (80, 20, 20), (x * ruuduLaius, y * ruuduLaius, ruuduLaius, ruuduLaius))
  45.                     pygame.draw.rect(ekraan, (60, 0, 0), (x * ruuduLaius + 1, y * ruuduLaius + 1, ruuduLaius - 2, ruuduLaius - 2))
  46.  
  47.  
  48.     def lisaKujund(self, kujund):
  49.         for lokaalneX, rida in enumerate(kujund.kujund):
  50.             for lokaalneY, lahter in enumerate(rida):
  51.                 if kujund.kujund[lokaalneX][lokaalneY]:
  52.                     self.kaart[kujund.x + lokaalneX][kujund.y + lokaalneY] = kujund;
  53.  
  54.     def uuenda(self, kujund):
  55.  
  56.         kasSaabLiikuda = kujund.y < len(self.kaart[0]) - len(kujund.kujund[0])
  57.         if kasSaabLiikuda:
  58.             for x in range(len(kujund.kujund)):
  59.                 for y in range(len(kujund.kujund[0])):
  60.                     if (kujund.kujund[x][y] and (y + 1 == len(kujund.kujund[0]) or not kujund.kujund[x][y + 1]) and self.kaart[x + kujund.x][y + kujund.y + 1] != 0):
  61.                         kasSaabLiikuda = False
  62.  
  63.                     if not kasSaabLiikuda:
  64.                         break
  65.  
  66.         #Kui kujundi all on veel tühjasi ridu:
  67.         if kasSaabLiikuda:
  68.             kujund.liigu(self, pygame.K_DOWN)
  69.         else:
  70.             self.lukusta(kujund)
  71.  
  72.  
  73.     def lukusta(self, kujund):
  74.         #Kujund ei saa enam liikuda, lukustame selle kujundi (kirjutame värvid väljakule)
  75.         print("Lukusta")
  76.         kujund.kasElus = False
  77.         for x in range(len(kujund.kujund)):
  78.             for y in range(len(kujund.kujund[0])):
  79.                 if kujund.kujund[x][y]:
  80.                     self.kaart[kujund.x + x][kujund.y + y] = kujund.värv
  81.        
  82. #Klass, mille objektid on erinevad kujundid
  83. class Kujund:
  84.  
  85.     def __init__(self, x, y, kujund, värv):
  86.         self.x = x             #Kujundi asukoht kaardil
  87.         self.y = y          
  88.         self.kujund = kujund[0]   #2-mõõtmeline list, mis hoiab kujundit
  89.         self.pöörded = kujund
  90.         self.pööre = 0
  91.         self.värv = värv
  92.         self.kasElus = True
  93.  
  94.     def joonista(self, kaardiX, kaardiY):
  95.         #Joonistame kujundi vastavatele kordinaatidele, kui kujund on seal
  96.         x = kaardiX - self.x
  97.         y = kaardiY - self.y
  98.         if x >= 0 and y >= 0 and self.kujund[x][y]:
  99.             joonistaRuut(kaardiX, kaardiY, self.värv)
  100.  
  101.             return True
  102.         else:
  103.  
  104.             return False
  105.  
  106.     def liigu(self, kaart, suund):
  107.         deltaX = 0
  108.         deltaY = 0
  109.         if suund == pygame.K_LEFT:
  110.             deltaX = -1
  111.         elif suund == pygame.K_RIGHT:
  112.             deltaX = 1
  113.         elif suund == pygame.K_DOWN:
  114.             deltaY = 1
  115.         else:
  116.             return
  117.  
  118.         vasakulTühjasiTulpi = 0
  119.         for x in range(len(self.kujund)):
  120.             kasTühi = True
  121.             for y in range(len(self.kujund[0])):
  122.                 kasTühi &= not self.kujund[x][y]
  123.  
  124.             if kasTühi:
  125.                 vasakulTühjasiTulpi += 1
  126.             else:
  127.                 break;
  128.  
  129.         kasSaabLiikuda = True
  130.         for x in range(len(self.kujund)):
  131.             for y in range(len(self.kujund[0])):
  132.                 uusX = self.x + deltaX + x
  133.                 uusY = self.y + deltaY + y
  134.                 kasSaabLiikuda &= uusX >= -vasakulTühjasiTulpi
  135.                 kasSaabLiikuda &= uusY < len(kaart.kaart[0])
  136.                 kasSaabLiikuda &= uusX < len(kaart.kaart)
  137.                 if kasSaabLiikuda and self.kujund[x][y]:
  138.                     kasSaabLiikuda &= (kaart.kaart[uusX][uusY] == 0 or kaart.kaart[uusX][uusY] == self)
  139.  
  140.         print(kasSaabLiikuda)
  141.  
  142.         if kasSaabLiikuda:
  143.             self.kustutaKaardilt(kaart)
  144.             self.x += deltaX
  145.             self.y += deltaY
  146.             self.asetaKaardile(kaart)
  147.  
  148.  
  149.     def pööra(self, kaart, kasVastupäeva):
  150.         if kasVastupäeva:
  151.             indeks = self.pööre + 1
  152.         else:
  153.             indeks = self.pööre - 1
  154.  
  155.         indeks %= len(self.pöörded)
  156.         uusKujund = self.pöörded[indeks]
  157.         kasSaabPöörata = True
  158.         for x in range(len(uusKujund)):
  159.             for y in range(len(uusKujund[0])):
  160.                 if self.x + x < 0 or self.x + x >= len(kaart.kaart) or self.y + y < 0 or self.y + y >= len(kaart.kaart[0]):
  161.                     kasSaabPöörata &= False
  162.                     continue
  163.                
  164.                 if uusKujund[x][y] and (kaart.kaart[self.x + x][self.y + y] != 0 and kaart.kaart[self.x + x][self.y + y] != self):
  165.                     kasSaabPöörata &= False
  166.  
  167.         print("Saab pöörata: " + str(kasSaabPöörata))
  168.         if kasSaabPöörata:
  169.             self.kustutaKaardilt(kaart)
  170.             self.pööre = indeks
  171.             self.kujund = uusKujund
  172.             self.asetaKaardile(kaart)
  173.            
  174.  
  175.     def kustutaKaardilt(self, kaart):
  176.         for x in range(len(self.kujund)):
  177.                 for y in range(len(self.kujund[0])):
  178.                     if self.kujund[x][y]:
  179.                         kaart.kaart[self.x + x][self.y + y] = 0
  180.  
  181.  
  182.     def asetaKaardile(self, kaart):
  183.         for x in range(len(self.kujund)):
  184.                 for y in range(len(self.kujund[0])):
  185.                     if self.kujund[x][y]:
  186.                         kaart.kaart[self.x + x][self.y + y] = self
  187.  
  188.        
  189.  
  190. class KujundiGeneraator:
  191.  
  192.     def __init__(self):
  193.         self.kujundid = [
  194.             [
  195.                 [[False, False, False], [True, True, True]],
  196.                 [[False, True], [False, True], [False, True]]
  197.             ],
  198.             [
  199.                 [[False, True, False], [True, True, True]],
  200.                 [[False, True, False], [False, True, True], [False, True, False]],
  201.                 [[False, False, False], [True, True, True], [False, True, False]],
  202.                 [[False, True], [True, True], [False, True]]
  203.             ]
  204.         ]
  205.         self.värvid = [
  206.             (100, 0, 0),
  207.             (0, 100, 0),
  208.             (0, 0, 100)
  209.         ]
  210.  
  211.     def genereeri(self):
  212.  
  213.         return Kujund(10, 10, random.choice(self.kujundid), random.choice(self.värvid))
  214.    
  215.        
  216. #Globaalne ruudu laius/kõrgus
  217. ruuduLaius = 15
  218. #Vastava suurusega aken
  219. ekraan = pygame.display.set_mode([ruuduLaius * Kaart.laius, ruuduLaius * Kaart.kõrgus])
  220. #Meie kaart
  221. kaart = Kaart()
  222.  
  223. kujundiGeneraator = KujundiGeneraator()
  224. kujund = kujundiGeneraator.genereeri()
  225. kaart.lisaKujund(kujund)
  226.  
  227. UUENDUS = pygame.USEREVENT + 1
  228. pygame.time.set_timer(UUENDUS, 500)
  229.  
  230.  
  231. while True:
  232.     ekraan.fill((0,0,0))
  233.  
  234.     kaart.joonista()
  235.    
  236.  
  237.     pygame.display.flip()
  238.     pygame.time.delay(10)
  239.  
  240.     events = pygame.event.get()
  241.     for oneEvent in events:
  242.         if oneEvent.type == UUENDUS:
  243.             kaart.uuenda(kujund)
  244.             if not kujund.kasElus:
  245.                 kujund = kujundiGeneraator.genereeri()
  246.                 kaart.lisaKujund(kujund)
  247.  
  248.         if oneEvent.type == pygame.KEYDOWN:
  249.             if oneEvent.key == pygame.K_q:
  250.                 kujund.pööra(kaart, True)
  251.             elif oneEvent.key == pygame.K_w:
  252.                 kujund.pööra(kaart, False)
  253.             else:
  254.                 kujund.liigu(kaart, oneEvent.key)
  255.            
  256.         if oneEvent.type == pygame.QUIT:
  257.             #quit()
  258.             exit()
  259.             break;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement