Exemplo n.º 1
0
def set_x_y_left_right_down_up(event, game_over, pause, last_press_time,
                               last_drop_time, cur_pos_x, cur_pos_y,
                               current_block, game_area, BLOCK_WIDTH,
                               next_block, score, speed, origionSpeed):
    '''
    function:for down, left, right three movement
    :param event: event down ,left, right
    :param game_over: End sign
    :param pause: pause
    :param last_press_time: last key time
    :param last_drop_time: last drop time
    :param cur_pos_x: X position of the current block
    :param cur_pos_y: Y position of current block
    :param current_block: current cube
    :param game_area: game area
    :param BLOCK_WIDTH: width of the cube
    :param next_block: next block
    :param score: score
    :param speed: speed
    :param orispeed: priginal speed
    :return:
    '''
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            if not game_over and not pause:
                if time.time() - last_press_time > 0.1:
                    last_press_time = time.time()
                    if cur_pos_x > -current_block.start_pos.X:
                        if judge(cur_pos_x - 1, cur_pos_y, current_block,
                                 game_area):
                            cur_pos_x -= 1
        if event.key == pygame.K_RIGHT:
            if not game_over and not pause:
                if time.time() - last_press_time > 0.1:
                    last_press_time = time.time()
                    # Can't remove the right border
                    if cur_pos_x + current_block.end_pos.X + 1 < BLOCK_WIDTH:
                        if judge(cur_pos_x + 1, cur_pos_y, current_block,
                                 game_area):
                            cur_pos_x += 1
        if event.key == pygame.K_DOWN:
            if not game_over and not pause:
                if time.time() - last_press_time > 0.1:
                    last_press_time = time.time()
                    if not judge(cur_pos_x, cur_pos_y + 1, current_block,
                                 game_area):
                        current_block, next_block, cur_pos_x, cur_pos_y, game_over, score, speed, origionSpeed = dock(
                            current_block, next_block, game_area, cur_pos_x,
                            cur_pos_y, game_over, score, speed, origionSpeed)
                    else:
                        last_drop_time = time.time()
                        cur_pos_y += 1

    return game_over, pause, last_press_time, last_drop_time, cur_pos_x, cur_pos_y, current_block, game_area, next_block, score, speed, origionSpeed
Exemplo n.º 2
0
def main():
    pygame.init()          #Initialization of hardware devices
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))    #Set window size
    pygame.display.set_caption('Teris Game')

    font1 = pygame.font.SysFont('SimHei', 24)  # set characters, Font boldface 24
    font2 = pygame.font.Font(None, 72)  # GAME OVER's characters
    font_pos_x = BLOCK_WIDTH * SIZE + BORDER_WIDTH + 10  #The X coordinates of the font position in the right information display area
    gameover_size = font2.size('GAME OVER')
    font1_height = int(font1.size('Scores')[1])

    current_block = None   # current dropping cube
    next_block = None  # next cube
    block_color = (200, 200, 200)  #cube's colour
    cur_pos_x, cur_pos_y = 0, 0   #current cube's location

    game_area = None    # whole game area
    game_over = True    #end judgement
    start = False       # Whether to start, GAME OVER is displayed when start = True, game_over = True
    score = 0           # get score
    origionSpeed = 0.5      # original speed
    speed = origionSpeed    # current speed
    pause = False          # pause
    last_drop_time = None   # Last landing time
    last_press_time = None  # Last key time

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:   #quit
                sys.exit()
            elif event.type == KEYDOWN:   #down
                if event.key == K_RETURN:  #enter start the game
                    if game_over:
                        start = True
                        game_over = False
                        score = 0
                        last_drop_time = time.time()
                        last_press_time = time.time()
                        game_area = [['.'] * BLOCK_WIDTH for _ in range(BLOCK_HEIGHT)]
                        current_block = squares.get_block()
                        next_block = squares.get_block()
                        cur_pos_x, cur_pos_y = (BLOCK_WIDTH - current_block.end_pos.X - 1) // 2, -1 - current_block.end_pos.Y
                elif event.key == K_SPACE:   #pause the game
                    if not game_over:
                        pause = not pause
                elif event.key in (K_w, K_UP):
                    # rotate
                    # Whether this can be rotated on the right side or not, I tried the Russian square on the internet, it can't rotate, here we can't rotate.
                    # We do a lot of blanks in shape design, so we only need to specify that the whole shape, including the blank part, can rotate in the game area.
                    if 0 <= cur_pos_x <= BLOCK_WIDTH - len(current_block.template[0]):
                        _next_block = squares.get_next_block(current_block)
                        if judge(cur_pos_x, cur_pos_y, _next_block,game_area):
                            current_block = _next_block

        #Up, down, left and right update functions
        game_over, pause, last_press_time, last_drop_time, \
        cur_pos_x, cur_pos_y, current_block, game_area, next_block,\
        score, speed, origionSpeed = set_x_y_left_right_down_up(event, game_over, pause,
                    last_press_time, last_drop_time, cur_pos_x, cur_pos_y,
                    current_block, game_area, BLOCK_WIDTH, next_block, score, speed, origionSpeed)

        #Display screen function
        display_screen(screen, pause, start, font1, font2, font_pos_x, font1_height, gameover_size, game_area,
                       game_over, cur_pos_x, cur_pos_y, last_drop_time, current_block, next_block, block_color, speed,origionSpeed,score)
Exemplo n.º 3
0
def index(submission_id, problem_id, timeout, language_id):
    submission_path = "../submissions/" + str(
        submission_id) + "." + language_id
    problem_path = "../problems/" + str(problem_id) + "/"
    if language_id == "py":
        verdict = functions.judge(submission_path, problem_path, timeout,
                                  language_id)
        print(verdict)
        return verdict
    elif language_id == "cpp":
        r = os.system("timeout " + str(timeout) + " g++ -o " + "binaries/" +
                      submission_id + " " + submission_path +
                      " -std=c++14 -DONLINE_JUDGE")
        if r != 0:
            return "CE"
        r = os.system("timeout " + str(timeout) + " ./" + "binaries/" +
                      submission_id + " < " + input_path + " > " + output_path)
        if r == 31744:
            return "TLE"
        elif r != 0:
            return "RTE"
        elif filecmp.cmp(output_path, exoutput_path):
            return "AC"
        else:
            return "WA"
    elif language_id == "c":
        r = os.system("timeout " + str(timeout) + " gcc -o " + "binaries/" +
                      submission_id + " " + submission_path)
        if r != 0:
            return "CE"
        r = os.system("timeout " + str(timeout) + " ./" + "binaries/" +
                      submission_id + " < " + input_path + " > " + output_path)
        if r == 31744:
            return "TLE"
        elif r != 0:
            return "RTE"
        elif filecmp.cmp(output_path, exoutput_path):
            return "AC"
        else:
            return "WA"
    elif language_id == "java":
        os.system("mv " + submission_path + " submissions/Main.java")
        r = os.system("timeout " + str(timeout) +
                      " javac submissions/Main.java")
        k = "ERROR JAVA-511: Please report this issue"
        if r != 0:
            k = "CE"
        else:
            r = os.system("timeout " + str(timeout) +
                          " java -cp submissions/ Main < " + input_path +
                          " > " + output_path)
            if r == 31744:
                k = "TLE"
            elif r != 0:
                k = "RTE"
            elif filecmp.cmp(output_path, exoutput_path):
                k = "AC"
            else:
                k = "WA"
            r = os.system("mv submissions/Main.java " + submission_path)
            os.system("rm submissions/Main.class")
        return k
Exemplo n.º 4
0
def display_screen(screen, pause, start, font1, font2, font_pos_x,
                   font1_height, gameover_size, game_area, game_over,
                   cur_pos_x, cur_pos_y, last_drop_time, current_block,
                   next_block, block_color, speed, origionSpeed, score):
    # Fill in background color
    screen.fill(BG_COLOR)
    # Draw game area divide lines
    pygame.draw.line(screen, BORDER_COLOR,
                     (SIZE * BLOCK_WIDTH + BORDER_WIDTH // 2, 0),
                     (SIZE * BLOCK_WIDTH + BORDER_WIDTH // 2, SCREEN_HEIGHT),
                     BORDER_WIDTH)

    # Draw existing squares
    if game_area:
        for i, row in enumerate(game_area):
            for j, cell in enumerate(row):
                if cell != '.':
                    pygame.draw.rect(screen, block_color,
                                     (j * SIZE, i * SIZE, SIZE, SIZE), 0)

    # Draw grid lines (vertical lines
    for x in range(BLOCK_WIDTH):
        pygame.draw.line(screen, BLACK, (x * SIZE, 0),
                         (x * SIZE, SCREEN_HEIGHT), 1)
    # Draw grid lines (horizontal lines
    for y in range(BLOCK_HEIGHT):
        pygame.draw.line(screen, BLACK, (0, y * SIZE),
                         (BLOCK_WIDTH * SIZE, y * SIZE), 1)

    if not game_over:
        cur_drop_time = time.time()
        if cur_drop_time - last_drop_time > speed:
            if not pause:
                # We shouldn't judge whether it's falling to the end or not. When we play Tetris, the moment the Tetris fall to the end can move left and right.
                if not judge(cur_pos_x, cur_pos_y + 1, current_block,
                             game_area):
                    current_block, next_block, cur_pos_x, cur_pos_y, game_over, score, speed, origionSpeed = dock(
                        current_block, next_block, game_area, cur_pos_x,
                        cur_pos_y, game_over, score, speed, origionSpeed)
                else:
                    last_drop_time = cur_drop_time
                    cur_pos_y += 1
    else:
        if start:
            print_screen_text(screen, font2,
                              (SCREEN_WIDTH - gameover_size[0]) // 2,
                              (SCREEN_HEIGHT - gameover_size[1]) // 2,
                              'GAME OVER', RED)

    # draw the crrent falling cube
    if current_block:
        for i in range(current_block.start_pos.Y, current_block.end_pos.Y + 1):
            for j in range(current_block.start_pos.X,
                           current_block.end_pos.X + 1):
                if current_block.template[i][j] != '.':
                    pygame.draw.rect(screen, block_color,
                                     ((cur_pos_x + j) * SIZE,
                                      (cur_pos_y + i) * SIZE, SIZE, SIZE), 0)

    print_screen_text(screen, font1, font_pos_x, 10, f'Scores: ')
    print_screen_text(screen, font1, font_pos_x, 10 + font1_height + 6,
                      f'{score}')
    print_screen_text(screen, font1, font_pos_x, 20 + (font1_height + 6) * 2,
                      f'speed: ')
    print_screen_text(screen, font1, font_pos_x, 20 + (font1_height + 6) * 3,
                      f'{score // 1000}')
    print_screen_text(screen, font1, font_pos_x, 30 + (font1_height + 6) * 4,
                      f'Next:')

    if next_block:
        _h = 30 + (font1_height + 6) * 5
        for i in range(next_block.start_pos.Y, next_block.end_pos.Y + 1):
            for j in range(next_block.start_pos.X, next_block.end_pos.X + 1):
                if next_block.template[i][j] != '.':
                    pygame.draw.rect(
                        screen, block_color,
                        (font_pos_x + j * SIZE, _h + i * SIZE, SIZE, SIZE), 0)

    pygame.display.flip()  # Display update