Advertisement
jee7

TIA Tetris - Versioon 19

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