Advertisement
jee7

TIA Tetris - Versioon 17

Nov 20th, 2014
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.39 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.             #Uuendame kaardi peal kujundi ruute
  69.             for x in range(len(kujund.kujund)):
  70.                 for y in range(len(kujund.kujund[0]) - 1, -2, -1):
  71.                     if y == -1 or kujund.kujund[x][y]:
  72.                         self.kaart[kujund.x + x][kujund.y + y + 1] = self.kaart[kujund.x + x][kujund.y + y]
  73.  
  74.             #Liigutame kujundi 1 ruut allapoole
  75.             kujund.y += 1
  76.  
  77.         else:
  78.             self.lukusta(kujund)
  79.  
  80.  
  81.     def lukusta(self, kujund):
  82.         #Kujund ei saa enam liikuda, lukustame selle kujundi (kirjutame värvid väljakule)
  83.         print("Lukusta")
  84.         kujund.kasElus = False
  85.         for x in range(len(kujund.kujund)):
  86.             for y in range(len(kujund.kujund[0])):
  87.                 if kujund.kujund[x][y]:
  88.                     self.kaart[kujund.x + x][kujund.y + y] = kujund.värv
  89.                 #else:
  90.                 #    self.kaart[kujund.x + x][kujund.y + y] = 0
  91.        
  92. #Klass, mille objektid on erinevad kujundid
  93. class Kujund:
  94.  
  95.     def __init__(self, x, y, kujund, värv):
  96.         self.x = x             #Kujundi asukoht kaardil
  97.         self.y = y          
  98.         self.kujund = kujund   #2-mõõtmeline list, mis hoiab kujundit
  99.         self.värv = värv
  100.         self.kasElus = True
  101.  
  102.     def joonista(self, kaardiX, kaardiY):
  103.         #Joonistame kujundi vastavatele kordinaatidele, kui kujund on seal
  104.         x = kaardiX - self.x
  105.         y = kaardiY - self.y
  106.         if self.kujund[x][y]:
  107.             joonistaRuut(kaardiX, kaardiY, self.värv)
  108.  
  109.             return True
  110.         else:
  111.  
  112.             return False
  113.  
  114.     def liigu(self, kaart, suund):
  115.         deltaX = 0
  116.         deltaY = 0
  117.         if suund == pygame.K_LEFT:
  118.             deltaX = -1
  119.         elif suund == pygame.K_RIGHT:
  120.             deltaX = 1
  121.         elif suund == pygame.K_DOWN:
  122.             deltaY = 1
  123.         else:
  124.             return
  125.  
  126.         kasSaabLiikuda = True
  127.         for x in range(len(self.kujund)):
  128.             for y in range(len(self.kujund[0])):
  129.                 uusX = self.x + deltaX + x
  130.                 uusY = self.y + deltaY + y
  131.                 kasSaabLiikuda &= uusX > 0
  132.                 kasSaabLiikuda &= uusY < len(kaart.kaart[0])
  133.                 kasSaabLiikuda &= uusX < len(kaart.kaart)
  134.                 if kasSaabLiikuda and self.kujund[x][y]:
  135.                     kasSaabLiikuda &= (kaart.kaart[uusX][uusY] == 0 or kaart.kaart[uusX][uusY] == self)
  136.  
  137.         print(kasSaabLiikuda)
  138.  
  139.         if kasSaabLiikuda:
  140.             print(str(self.x) + ", " + str(self.y))
  141.             for x in range(len(self.kujund)):
  142.                 for y in range(len(self.kujund[0])):
  143.                     if self.kujund[x][y]:
  144.                         kaart.kaart[self.x + x][self.y + y] = 0
  145.  
  146.             self.x += deltaX
  147.             self.y += deltaY
  148.            
  149.             for x in range(len(self.kujund)):
  150.                 for y in range(len(self.kujund[0])):
  151.                     if self.kujund[x][y]:
  152.                         kaart.kaart[self.x + x][self.y + y] = self
  153.  
  154.            
  155.  
  156.  
  157. class KujundiGeneraator:
  158.  
  159.     def __init__(self):
  160.         self.kujundid = [
  161.             [[False, False], [True, True], [False, False]],
  162.             [[True, False], [True, True], [False, False]],
  163.             [[False, False], [True, True], [False, True]],
  164.             [[True], [True], [True]]
  165.         ]
  166.         self.värvid = [
  167.             (100, 0, 0),
  168.             (0, 100, 0),
  169.             (0, 0, 100)
  170.         ]
  171.  
  172.     def genereeri(self):
  173.  
  174.         return Kujund(10, 10, random.choice(self.kujundid), random.choice(self.värvid))
  175.    
  176.        
  177. #Globaalne ruudu laius/kõrgus
  178. ruuduLaius = 15
  179. #Vastava suurusega aken
  180. ekraan = pygame.display.set_mode([ruuduLaius * Kaart.laius, ruuduLaius * Kaart.kõrgus])
  181. #Meie kaart
  182. kaart = Kaart()
  183.  
  184. kujundiGeneraator = KujundiGeneraator()
  185. kujund = kujundiGeneraator.genereeri()
  186. kaart.lisaKujund(kujund)
  187.  
  188. UUENDUS = pygame.USEREVENT + 1
  189. pygame.time.set_timer(UUENDUS, 500)
  190.  
  191.  
  192. while True:
  193.     ekraan.fill((0,0,0))
  194.  
  195.     kaart.joonista()
  196.    
  197.  
  198.     pygame.display.flip()
  199.     pygame.time.delay(10)
  200.  
  201.     events = pygame.event.get()
  202.     for oneEvent in events:
  203.         if oneEvent.type == UUENDUS:
  204.             kaart.uuenda(kujund)
  205.             if not kujund.kasElus:
  206.                 kujund = kujundiGeneraator.genereeri()
  207.                 kaart.lisaKujund(kujund)
  208.  
  209.         if oneEvent.type == pygame.KEYDOWN:
  210.             kujund.liigu(kaart, oneEvent.key)
  211.            
  212.         if oneEvent.type == pygame.QUIT:
  213.             #quit()
  214.             exit()
  215.             break;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement