Преглед на файлове

symmetric tetromino's rotations

Alexey Dorokhov преди 3 години
родител
ревизия
f7b536a3e1
променени са 4 файла, в които са добавени 134 реда и са изтрити 145 реда
  1. 1 0
      .gitignore
  2. 9 144
      competitive_tetris.py
  3. 1 1
      scores.txt
  4. 123 0
      shapes.py

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+__pycache__

+ 9 - 144
competitive_tetris.py

@@ -6,6 +6,8 @@ Competitive tetris server
 import random
 import pygame
 
+import shapes
+
 pygame.font.init()
 
 # GLOBALS VARS
@@ -21,143 +23,6 @@ TOP_LEFT_Y = S_HEIGHT - PLAY_HEIGHT
 
 # SHAPE FORMATS
 
-I = [['..........',
-      '...0000...',
-      '..........',
-      '..........'],
-     ['.....0....',
-      '.....0....',
-      '.....0....',
-      '.....0....'],
-     ['..........',
-      '..........',
-      '...0000...',
-      '..........'],
-     ['....0.....',
-      '....0.....',
-      '....0.....',
-      '....0.....']]
-
-O = [['..........',
-      '....00....',
-      '....00....',
-      '..........'],
-     ['..........',
-      '....00....',
-      '....00....',
-      '..........'],
-     ['..........',
-      '....00....',
-      '....00....',
-      '..........'],
-     ['..........',
-      '....00....',
-      '....00....',
-      '..........']]
-
-T = [['....0.....',
-      '...000....',
-      '..........',
-      '..........'],
-     ['....0.....',
-      '....00....',
-      '....0.....',
-      '..........'],
-     ['..........',
-      '...000....',
-      '....0.....',
-      '..........'],
-     ['....0.....',
-      '...00.....',
-      '....0.....',
-      '..........']]
-
-S = [['....00....',
-      '...00.....',
-      '..........',
-      '..........'],
-     ['....0.....',
-      '....00....',
-      '.....0....',
-      '..........'],
-     ['..........',
-      '....00....',
-      '...00.....',
-      '..........'],
-     ['...0......',
-      '...00.....',
-      '....0.....',
-      '..........']]
-
-Z = [['...00.....',
-      '....00....',
-      '..........',
-      '..........'],
-     ['.....0....',
-      '....00....',
-      '....0.....',
-      '..........'],
-     ['..........',
-      '...00.....',
-      '....00....',
-      '..........'],
-     ['....0.....',
-      '...00.....',
-      '...0......',
-      '..........']]
-
-
-
-
-
-
-
-J = [['.....',
-      '.0...',
-      '.000.',
-      '.....',
-      '.....'],
-     ['.....',
-      '..00.',
-      '..0..',
-      '..0..',
-      '.....'],
-     ['.....',
-      '.....',
-      '.000.',
-      '...0.',
-      '.....'],
-     ['.....',
-      '..0..',
-      '..0..',
-      '.00..',
-      '.....']]
-
-L = [['.....',
-      '...0.',
-      '.000.',
-      '.....',
-      '.....'],
-     ['.....',
-      '..0..',
-      '..0..',
-      '..00.',
-      '.....'],
-     ['.....',
-      '.....',
-      '.000.',
-      '.0...',
-      '.....'],
-     ['.....',
-      '.00..',
-      '..0..',
-      '..0..',
-      '.....']]
-
-
-SHAPES = [S, Z, I, O, J, L, T]
-SHAPE_COLORS = [(0, 255, 0), (255, 0, 0), (0, 255, 255), (255, 255, 0),
-                (255, 165, 0), (0, 0, 255), (128, 0, 128)]
 # index 0 - 6 represent shape
 
 
@@ -166,7 +31,7 @@ class Piece:
         self.x = x
         self.y = y
         self.shape = shape
-        self.color = SHAPE_COLORS[SHAPES.index(shape)]
+        self.color = shapes.SHAPE_COLORS[shapes.SHAPES.index(shape)]
         self.rotation = 0
 
 
@@ -190,10 +55,6 @@ def convert_shape_format(shape):
         for j, column in enumerate(row):
             if column == '0':
                 positions.append((shape.x + j, shape.y + i))
-
-    for i, pos in enumerate(positions):
-        positions[i] = (pos[0] - 2, pos[1] - 4)
-
     return positions
 
 
@@ -220,7 +81,7 @@ def check_lost(positions):
 
 
 def get_shape():
-    return Piece(5, 0, random.choice(SHAPES))
+    return Piece(0, 0, random.choice(shapes.SHAPES))
 
 
 def draw_text_middle(surface, text, size, color):
@@ -268,7 +129,7 @@ def draw_next_shape(shape, surface):
     font = pygame.font.SysFont('comicsans', 30)
     label = font.render('Next Shape', 1, (255,255,255))
 
-    sx = TOP_LEFT_X + PLAY_WIDTH + 50
+    sx = TOP_LEFT_X + PLAY_WIDTH + 0
     sy = TOP_LEFT_Y + PLAY_HEIGHT/2 - 100
     format = shape.shape[shape.rotation % len(shape.shape)]
 
@@ -342,6 +203,8 @@ def main(win):  # *
     change_piece = False
     run = True
     current_piece = get_shape()
+    current_piece.y = -4
+    current_piece.x = 0
     next_piece = get_shape()
     clock = pygame.time.Clock()
     fall_time = 0
@@ -402,6 +265,8 @@ def main(win):  # *
                 p = (pos[0], pos[1])
                 locked_positions[p] = current_piece.color
             current_piece = next_piece
+            current_piece.x = 0
+            current_piece.y = -4
             next_piece = get_shape()
             change_piece = False
             score += clear_rows(grid, locked_positions) * 10

+ 1 - 1
scores.txt

@@ -1 +1 @@
-70
+130

+ 123 - 0
shapes.py

@@ -0,0 +1,123 @@
+I = [['..........',
+      '...0000...',
+      '..........',
+      '..........'],
+     ['.....0....',
+      '.....0....',
+      '.....0....',
+      '.....0....'],
+     ['..........',
+      '..........',
+      '...0000...',
+      '..........'],
+     ['....0.....',
+      '....0.....',
+      '....0.....',
+      '....0.....']]
+
+O = [['..........',
+      '....00....',
+      '....00....',
+      '..........'],
+     ['..........',
+      '....00....',
+      '....00....',
+      '..........'],
+     ['..........',
+      '....00....',
+      '....00....',
+      '..........'],
+     ['..........',
+      '....00....',
+      '....00....',
+      '..........']]
+
+T = [['....0.....',
+      '...000....',
+      '..........',
+      '..........'],
+     ['....0.....',
+      '....00....',
+      '....0.....',
+      '..........'],
+     ['..........',
+      '...000....',
+      '....0.....',
+      '..........'],
+     ['....0.....',
+      '...00.....',
+      '....0.....',
+      '..........']]
+
+S = [['....00....',
+      '...00.....',
+      '..........',
+      '..........'],
+     ['....0.....',
+      '....00....',
+      '.....0....',
+      '..........'],
+     ['..........',
+      '....00....',
+      '...00.....',
+      '..........'],
+     ['...0......',
+      '...00.....',
+      '....0.....',
+      '..........']]
+
+Z = [['...00.....',
+      '....00....',
+      '..........',
+      '..........'],
+     ['.....0....',
+      '....00....',
+      '....0.....',
+      '..........'],
+     ['..........',
+      '...00.....',
+      '....00....',
+      '..........'],
+     ['....0.....',
+      '...00.....',
+      '...0......',
+      '..........']]
+
+J = [['...0......',
+      '...000....',
+      '..........',
+      '..........'],
+     ['....00....',
+      '....0.....',
+      '....0.....',
+      '..........'],
+     ['..........',
+      '...000....',
+      '.....0....',
+      '..........'],
+     ['....0.....',
+      '....0.....',
+      '...00.....',
+      '..........']]
+
+L = [['.....0....',
+      '...000....',
+      '..........',
+      '..........'],
+     ['....0.....',
+      '....0.....',
+      '....00....',
+      '..........'],
+     ['..........',
+      '...000....',
+      '...0......',
+      '..........'],
+     ['...00.....',
+      '....0.....',
+      '....0.....',
+      '..........']]
+
+SHAPES = [S, Z, I, O, J, L, T]
+SHAPE_COLORS = [(0, 255, 0), (255, 0, 0), (0, 255, 255), (255, 255, 0),
+                (255, 165, 0), (0, 0, 255), (128, 0, 128)]
+