Beispiel #1
0
def run(dims: Tuple[int, int] = (400, 400)):
    reduced_dims = (dims[0] // 10, dims[1] // 10)
    pygame.init()
    screen = pygame.display.set_mode(dims)
    display_surf = pygame.Surface(reduced_dims)
    clock = pygame.time.Clock()
    rate = 5
    done = False

    grid = Grid(reduced_dims)
    current_shape = Shape(reduced_dims)

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            elif event.type == KEYDOWN and event.key == K_LEFT:
                current_shape.move("l", grid.block_positions)
            elif event.type == KEYDOWN and event.key == K_RIGHT:
                current_shape.move("r", grid.block_positions)
            elif event.type == KEYDOWN and event.key == K_UP:
                current_shape.rotate(grid.block_positions)
            elif event.type == KEYDOWN and event.key == K_DOWN:
                current_shape.drop(grid.block_positions)

        grid.draw_grid(display_surf)
        current_shape.draw_shape(display_surf)

        current_shape.step(grid.block_positions)

        if not current_shape.can_move:
            if current_shape.game_over:
                grid.game_over(display_surf, screen, dims, clock)
                for event in pygame.event.get():
                    if event.type == KEYDOWN and event.key == "K_SPACE":
                        print("restarting")
                        grid = Grid(reduced_dims)
                        current_shape = Shape(reduced_dims)
                    else:
                        sys.exit(0)

            grid.update(current_shape)
            current_shape = Shape(reduced_dims)
            rate = grid.clear_line(rate)

        surf = pygame.transform.scale(display_surf, dims)
        screen.blit(surf, (0, 0))
        pygame.display.update()

        clock.tick(int(rate))
Beispiel #2
0
def main():  # this defines the main function
    wn = turtle.Screen()  # this creates a turtle window
    wn.colormode(255)  # this changes the color mode to RGB

    square = Shape([0, 0], 4, 50)  # this draws a square of length 50
    square.draw_shape(
    )  # this calls the draw_square method from the Shape class to draw the specified shape
    triangle = Shape([0, 0], 3, 50)  # this draws a triangle of length 50
    triangle.draw_shape(
    )  # this calls the draw_square method from the Shape class to draw the specified shape
    square = Shape([0, 0], 4, 80)  # this draws a square of length 80
    square.draw_shape(
    )  # this calls the draw_square method from the Shape class to draw the specified shape
    oct = Shape([0, 0], 8, 50)  # this draws an octagon of length 50
    oct.draw_shape(
    )  # this calls the draw_square method from the Shape class to draw the specified shape
    square = Shape([0, 0], 4, 100)  # this draws a square of length 100
    square.draw_shape(
    )  # this calls the draw_square method from the Shape class to draw the specified shape

    wn.exitonclick()  # this exits the turtle window when it is clicked