def run(self): while not self.gameover: time = self.clock.tick(self.fps) self.event_loop() self.update(time) self.draw() pygame.display.update() Board.print_board()
def update(self, time): if not Board.piece_status(): x = random.randint(0,NUM_COL-1) block = Block((x,0)) print("Spawn") self.block = block Board.piece_done() #else: self.block.update(time)
def move(self, direction): if direction == "left" and self.coordinates[0] > 0 and not Board.spot_taken((self.coordinates[0] - 1, self.coordinates[1])): self.coordinates = (self.coordinates[0] - 1, self.coordinates[1]) elif direction == "right" and self.coordinates[0] < NUM_COL-1 and not Board.spot_taken((self.coordinates[0] + 1, self.coordinates[1])): self.coordinates = (self.coordinates[0] + 1, self.coordinates[1]) elif direction == "down": if self.coordinates[1] < NUM_ROW-1 and Board.spot_taken((self.coordinates[0],self.coordinates[1]+1)): self.place_piece(self.coordinates) elif not self.coordinates[1] >= NUM_ROW-1: self.coordinates = (self.coordinates[0], self.coordinates[1]+1) else: self.place_piece(self.coordinates) self.coord_to_pos()
def draw(self): self.screen.fill(BLACK) for col in range(NUM_COL): x = WIDTH_BLOCK * col pygame.draw.line(self.screen, WHITE, [x,0], [x, HEIGHT]) for row in range(NUM_ROW): y = HEIGHT_BLOCK * row pygame.draw.line(self.screen, WHITE, [0,y], [WIDTH, y]) #for block in self.blocks: self.block.draw(self.screen) self.draw_existing(Board.return_boxes(), self.screen)
def place_piece(self, coordinates): Board.add_block(coordinates) Board.piece_new() Board.clear_row(coordinates)
from tetris_board import Board WHITE = (255, 255, 255) BLACK = ( 0, 0, 0) WIDTH = 400 HEIGHT = 800 NUM_COL = 10 NUM_ROW = 20 NEW_PIECE = True WIDTH_BLOCK= WIDTH/NUM_COL HEIGHT_BLOCK= HEIGHT/NUM_ROW Board = Board(NUM_COL,NUM_ROW) class Block(): def __init__(self, coordinates): self.rect = pygame.Rect(0 , 0, WIDTH_BLOCK, HEIGHT_BLOCK) self.rect.center = (coordinates[0] * WIDTH_BLOCK + WIDTH_BLOCK/2, coordinates[1] * HEIGHT_BLOCK + HEIGHT_BLOCK/2) self.image = pygame.Surface(self.rect.size) self.image.fill(WHITE) self.pos = self.rect.center self.timer = 0 self.coordinates = coordinates def __del__(self): del self.rect del self.image del self.pos
import random from tetris_board import Board WHITE = (255, 255, 255) BLACK = ( 0, 0, 0) WIDTH = 400 HEIGHT = 800 NUM_COL = 10 NUM_ROW = 20 NEW_PIECE = True WIDTH_BLOCK= WIDTH/NUM_COL HEIGHT_BLOCK= HEIGHT/NUM_ROW Board = Board(WIDTH,HEIGHT) class Block(pygame.sprite.Sprite): def __init__(self,centerpoint, *groups): super(Block, self).__init__(*groups) self.rect = pygame.Rect(0 , 0, WIDTH_BLOCK, HEIGHT_BLOCK) self.rect.center = centerpoint self.image = pygame.Surface(self.rect.size) self.image.fill(WHITE) self.pos = self.rect.center self.timer = 0 self.stop = False def move(self, time, direction): self.timer += time
button_resetGame = Button(block_size+1, button_y+1 + block_size*18, block_size*2-1, button_size_y-1) button_yes = Button(button_x, button_y + block_size*3, button_size_x/2 - block_size, button_size_y) button_no = Button(button_x + block_size*6, button_y + block_size*3, button_size_x/2 - block_size, button_size_y) button_check = Button(button_x + block_size + 1, button_y+1 + block_size*1, button_size_x - block_size*2 - 1, button_size_y-1) the_buttons = [button_yes, button_no, button_check] button_name = Button(button_x, button_y + block_size*1, button_size_x, button_size_y) # Un delay hard-coded para definir el ritmo con el que se mueve a la siguiente fila el bloque en juego hard_tick = 0 hard_tick_max = 10 # El objeto Board evalúa las coliciones de la pieza en juego, guarda las piezas que terminaron de caer y el puntaje board = Board(board_x, board_y, block_size, COLORS, font, font_size, board_offset) board.resetBoard() # El objeto Scoreboard internaliza el .json para mostrarlo y editarlo scoreboard = Scoreboard("tetris_scoreboard.json", 100, 100, 200, 20) # Todas las flags que voy a necesitar rotate = False move_value = 0 valid_move = True dx_left = 0 dx_right = 0 dy = 0 new_block = True release = False #WINDOWS