俄罗斯方块(Python)

import pygame

import random

 

# 定义方块的形状

shapes = [

    [[1, 1, 1, 1]],

    [[1, 1], [1, 1]],

    [[0, 1, 1], [1, 1, 0]],

    [[1, 1, 0], [0, 1, 1]],

    [[1, 0, 0], [1, 1, 1]],

    [[0, 0, 1], [1, 1, 1]],

    [[1, 1, 1], [0, 1, 0]],

]

 

# 定义方块的颜色

colors = [

    (255, 0, 0),

    (0, 255, 0),

    (0, 0, 255),

    (255, 255, 0),

    (255, 0, 255),

    (0, 255, 255),

    (255, 255, 255),

]

 

class Block:

    def __init__(self, shape, color):

        self.shape = shape

        self.color = color

        self.x = 4

        self.y = 0

 

    def rotate(self):

        self.shape = [[self.shape[y][x] for y in range(len(self.shape))] for x in range(len(self.shape[0])-1, -1, -1)]

 

    def move(self, dx, dy):

        self.x += dx

        self.y += dy

 

    def draw(self, surface):

        for i in range(len(self.shape)):

            for j in range(len(self.shape[i])):

                if self.shape[i][j] == 1:

                    pygame.draw.rect(surface, self.color, (j+self.x, i+self.y, 1, 1), 0)

 

class Game:

    def __init__(self):

        self.width = 10

        self.height = 20

        self.grid = [[(0,0,0) for x in range(self.width)] for y in range(self.height)]

        self.score = 0

        self.block = Block(random.choice(shapes), random.choice(colors))

 

    def valid_position(self):

        for i in range(len(self.block.shape)):

            for j in range(len(self.block.shape[i])):

                if self.block.shape[i][j] == 1:

                    x = self.block.x + j

                    y = self.block.y + i

                    if x < 0 or x >= self.width or y < 0 or y >= self.height or self.grid[y][x] != (0,0,0):

                        return False

        return True

 

    def remove_rows(self):

        rows_removed = 0

        y = self.height - 1

        while y >= 0:

            row = self.grid[y]

            if (0, 0, 0) not in row:

                del self.grid[y]

                self.grid.insert(0, [(0,0,0) for x in range(self.width)])

                rows_removed += 1

            else:

                y -= 1

        self.score += rows_removed ** 2

 

    def update(self):

        self.block.move(0, 1)

        if not self.valid_position():

            self.block.move(0,1.0)

if self.block.y == 0:

return False

self.block.y -= 1

self.freeze()

self.remove_rows()

self.block = Block(random.choice(shapes), random.choice(colors))

return True

 

def freeze(self):

for i in range(len(self.block.shape)):

for j in range(len(self.block.shape[i])):

if self.block.shape[i][j] == 1:

x = self.block.x + j

y = self.block.y + i

self.grid[y][x] = self.block.color

 

def draw(self, surface):

for y in range(self.height):

for x in range(self.width):

pygame.draw.rect(surface, self.grid[y][x], (x, y, 1, 1), 0)

self.block.draw(surface)

 

def main():

pygame.init()

width, height = 250, 500

screen = pygame.display.set_mode((width, height))

pygame.display.set_caption("Tetris")

pygame.key.set_repeat(1, 50)

clock = pygame.time.Clock()

game = Game()

while True:

if not game.update():

break

screen.fill((0,0,0))

game.draw(screen)

pygame.display.update()

clock.tick(10)

print("Your score:", game.score)

pygame.quit()

 

if name == 'main':

main()

你可能感兴趣的:(python)