Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- BLACK = ( 0, 0, 0)
- WHITE = ( 255, 255, 255)
- GREEN = ( 0, 255, 0)
- RED = ( 255, 0, 0)
- WIDTH = 10
- HEIGHT = 20
- EMPTY = 0
- FULL = 1
- CELL_SIZE = 30
- SPEED = 400
- O = [[[1, 1],
- [1, 1]]]
- I = [[[1, 1, 1, 1]],
- [[0, 1],
- [0, 1],
- [0, 1],
- [0, 1]]]
- J = [[[1, 0, 0],
- [1, 1, 1]],
- [[0, 1],
- [0, 1],
- [1, 1]],
- [[1, 1, 1],
- [0, 0, 1]],
- [[1, 1],
- [1, 0],
- [1, 0]]]
- L = [[[0, 0, 1],
- [1, 1, 1]],
- [[1, 1],
- [0, 1],
- [0, 1]],
- [[1, 1, 1],
- [1, 0, 0]],
- [[1, 0],
- [1, 0],
- [1, 1]]]
- T = [[[0, 1, 0],
- [1, 1, 1]],
- [[0, 1],
- [1, 1],
- [0, 1]],
- [[1, 1, 1],
- [0, 1, 0]],
- [[1, 0],
- [1, 1],
- [1, 0]]]
- S = [[[0, 1, 1],
- [1, 1, 0]],
- [[1, 0],
- [1, 1],
- [0, 1]]]
- Z = [[[1, 1, 0],
- [0, 1, 1]],
- [[0, 1],
- [1, 1],
- [1, 0]]]
- FORMS = [O, I, J, L, T, S, Z]
- class Field:
- def __init__(self, width, height):
- self.width = width
- self.height = height
- self.map = [([FULL] + [EMPTY] * self.width + [FULL])
- for i in range(self.height)] + [[FULL] * (self.width + 2)]
- def draw(self, screen):
- screen.fill(BLACK)
- for y in range(self.height):
- for x in range(1, self.width + 1):
- if self.map[y][x] != EMPTY:
- pygame.draw.rect(screen, self.map[y][x],
- [(x - 1) * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE])
- def delete_rows(self):
- for y in range(self.height):
- if EMPTY not in self.map[y]:
- self.map = [[FULL] + [EMPTY] * self.width + [FULL]] + self.map[:y] + self.map[y + 1:]
- #0 [1, 0, 0,...,0,1]
- #1 0
- #2 1 y = 2
- #3 3
- #4 4
- def is_full(self):
- # TODO проверять место под новую фигуру
- return self.map[0][1:-1] != [0] * self.width
- class Figure:
- def __init__(self, field, screen, forms, turn):
- self.x = field.width // 2 - 1
- self.y = 0
- self.forms = forms
- self.turn = turn
- self.form = self.forms[self.turn]
- self.color = (random.randint(50, 255), random.randint(50, 255), random.randint(50, 255))
- self.draw(screen, self.color)
- def draw(self, screen, color=WHITE):
- for y in range(len(self.form)):
- for x in range(len(self.form[y])):
- if self.form[y][x] == FULL:
- pygame.draw.rect(screen, color,
- [(self.x + x) * CELL_SIZE, (self.y + y) * CELL_SIZE, CELL_SIZE, CELL_SIZE])
- def can_move_down(self, field):
- for y in range(len(self.form)):
- for x in range(len(self.form[y])):
- if self.form[y][x] == FULL:
- if field.map[self.y + y + 1][self.x + x + 1] != EMPTY:
- return False
- return True
- def can_move_right(self, field):
- for y in range(len(self.form)):
- for x in range(len(self.form[y])):
- if self.form[y][x] == FULL:
- if field.map[self.y + y][self.x + x + 2] != EMPTY:
- return False
- return True
- def can_move_left(self, field):
- for y in range(len(self.form)):
- for x in range(len(self.form[y])):
- if self.form[y][x] == FULL:
- if field.map[self.y + y][self.x + x] != EMPTY:
- return False
- return True
- def can_rotate(self, field):
- # TODO проверять выход за границы стакана
- next_form = self.forms[(self.turn + 1) % len(self.forms)]
- for y in range(len(next_form)):
- for x in range(len(next_form[y])):
- if next_form[y][x] == FULL:
- if field.map[self.y + y][self.x + x + 1] != EMPTY:
- return False
- return True
- def move_down(self, field):
- self.draw(screen, BLACK)
- self.y += 1
- self.draw(screen, self.color)
- def move_left(self, field):
- self.draw(screen, BLACK)
- self.x -= 1
- self.draw(screen, self.color)
- def move_right(self, field):
- self.draw(screen, BLACK)
- self.x += 1
- self.draw(screen, self.color)
- def rotate(self):
- # TODO сделать честный поворот
- self.draw(screen, BLACK)
- self.turn = (self.turn + 1) % len(self.forms)
- self.form = self.forms[self.turn]
- self.draw(screen, self.color)
- def to_map(self):
- for y in range(len(self.form)):
- for x in range(len(self.form[y])):
- if self.form[y][x] == FULL:
- field.map[self.y + y][self.x + x + 1] = self.color
- pygame.init()
- size = [CELL_SIZE * WIDTH, CELL_SIZE * HEIGHT]
- screen = pygame.display.set_mode(size)
- field = Field(WIDTH, HEIGHT)
- figure = Figure(field, screen, random.choice(FORMS), 0)
- pygame.time.set_timer(pygame.KEYDOWN, SPEED)
- pygame.display.set_caption("Tetris")
- done = False
- # clock = pygame.time.Clock()
- while not done:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- screen.fill(BLACK)
- done = True
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_RIGHT:
- if figure.can_move_right(field):
- figure.move_right(field)
- elif event.key == pygame.K_LEFT:
- if figure.can_move_left(field):
- figure.move_left(field)
- elif event.key == pygame.K_UP:
- if figure.can_rotate(field):
- figure.rotate()
- elif event.key == pygame.K_SPACE:
- pygame.time.set_timer(pygame.KEYDOWN, SPEED // 10)
- else:
- if figure.can_move_down(field):
- figure.move_down(field)
- else:
- figure.to_map()
- field.delete_rows()
- field.draw(screen)
- if not field.is_full():
- pygame.time.set_timer(pygame.KEYDOWN, SPEED)
- figure = Figure(field, screen, random.choice(FORMS), 0)
- else:
- done = True
- pygame.display.flip()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement