Exemplo n.º 1
0
def main():
    pygame.init()
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption("Qiuyang's Tetris")
    pygame.key.set_repeat(
        150, 40
    )  # hold key will keep creating same event, set_repeat(delay, interval)
    # background color
    bg_color = BG_COLOR

    game_state = GameState(screen)

    while True:
        # if touch bottom
        if game_state.block and game_state.block.on_bottom:
            game_state.touch_bottom()

        # monitor keyboard and mouse event
        check_events(game_state)

        # fill background
        screen.fill(bg_color)
        # draw block
        if game_state.block:
            game_state.block.paint()
        # draw game window
        GameDisplay.draw_game_area(screen, game_state)

        # refresh the screen
        pygame.display.update()
Exemplo n.º 2
0
 def draw_cell(self,row,column):
     # cell_position = (x*CELL_WIDTH+GAME_AREA_LEFT+1,
     #                  y*CELL_WIDTH+GAME_AREA_TOP+1)
     # cell_width_height = (CELL_WIDTH-2,CELL_WIDTH-2)
     # cell_rect = pygame.Rect(cell_position,cell_width_height)
     #pygame.draw.rect(self.screen,PIECE_COLORS[self.shape],cell_rect)
     GameDisplay.draw_cell(self.screen,row,column,PIECE_COLORS[self.shape])
Exemplo n.º 3
0
def main():
    #初始化pygame。启用Pygame必不可少的一步,在程序开始阶段执行。
    pygame.init()
    #创建屏幕对象
    screen = pygame.display.set_mode((1200, 900))  #分辨率是1200*900
    pygame.display.set_caption("俄罗斯方块")  #窗口标题
    pygame.key.set_repeat(100, 100)  # 一直按下某个键,每过100毫秒就引发一个KEYDOWN事件

    #屏幕背景色
    bg_color = (230, 230, 230)

    game_state = GameState(screen)
    game_resource = GameResource()
    game_resource.play_bg_music()
    #游戏主循环
    while True:
        #方块触底的话
        if game_state.piece and game_state.piece.is_on_bottom:
            game_state.touch_bottom()

        #监视键盘和鼠标事件
        check_events(game_state, game_resource)

        #设定屏幕背景
        screen.blit(game_resource.load_bg_img(), (0, 0))
        #绘制方块
        if game_state.piece:
            game_state.piece.paint()
        #绘制游戏区域网格线和墙体
        GameDisplay.draw_game_window(screen, game_state, game_resource)
        #让最近绘制的屏幕可见
        pygame.display.flip()
Exemplo n.º 4
0
def main():

    # Init camera
    camvision = CamVision(CAM_DEVICE_NUM)

    # Learn the background now so that it can be subtracted from an image
    # later to get the corresponding foreground
    camvision.learn_bg_now(BG_SAMPLES_COUNT)

    # Load the trained Convolutional Neural Network,
    # then bind it to the camera so that it recognises hand postures
    classifier = tf.estimator.Estimator(
        model_fn=cnn_model_fn,
        model_dir=MODEL_DIR)

    def input_generator():
        while True:
            camvision.capture_now()
            camvision.extract_fg()
            cnn_input = camvision.get_latest_fg_mask()
            yield cnn_input

    def input_fn():
        ds = tf.data.Dataset.from_generator(
            input_generator,
            tf.float32,
            tf.TensorShape([CNN_INPUT_IMG_HEIGHT, CNN_INPUT_IMG_WIDTH]))
        feature = ds.make_one_shot_iterator().get_next()
        return {'input': feature}

    output_generator = classifier.predict(input_fn)

    def posture_from_output(output):
        class_id = output['classes']
        class_label = config.CLASS_ID_TO_LABEL[class_id]
        return class_label

    def which_posture_beats_this_posture(posture):
        return config.WHAT_BEATS_WHAT[posture]

    # Determine whether to compete with or follow the user
    competing = not config.COPY_USER_HAND

    # Init game GUI
    game_display = GameDisplay()

    # Capture image, then extract foreground (hand), then display response
    while not game_display.check_quit_key_pressed():
        output = next(output_generator)

        posture = posture_from_output(output)
        if competing:
            game_display.show_repsonse_image(
                which_posture_beats_this_posture(posture))
        else:
            game_display.show_repsonse_image(posture)

        fg_mask_img = camvision.get_latest_fg_mask() * 255
        game_display.show_camvision_image(fg_mask_img)
Exemplo n.º 5
0
def main():

    pygame.init()

    screen = pygame.display.set_mode((1200, 900))  #resolution ratio 1200*900
    pygame.display.set_caption("TetrisGame")  #window title
    pygame.key.set_repeat(50, 100)

    #background color
    bg_color = (230, 230, 230)

    #create blocks
    #piece = None

    random.seed(int(time.time()))
    #game_wall = GameWall(screen)
    #piece = Piece(random.choice(PIECE_TYPES), screen,game_wall)
    game_state = GameState(screen)
    game_resource = GameResource()
    game_resource.play_bg_music()

    #main body

    while True:

        if game_state.piece and game_state.piece.is_on_bottom:
            '''game_state.wall.add_to_wall(game_state.piece)
            game_state.add_score(game_state.wall.eliminate_lines())
            game_state.piece = Piece(random.choice(PIECE_TYPES), screen,game_state.wall)'''
            game_state.touch_bottom()

        check_events(game_state, game_resource)

        #Fill in the screen background color
        screen.blit(game_resource.load_bg_img(), (0, 0))
        #screen.fill(bg_color)
        #draw the wall
        #GameDisplay.draw_game_area(screen, game_state)

        #draw block with method
        if game_state.piece:
            game_state.piece.paint()

        GameDisplay.draw_game_area(screen, game_state, game_resource)
        # make draw visible
        pygame.display.flip()
Exemplo n.º 6
0
def main():
    pygame.init()  #初始化
    #创建屏幕对象
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption("俄罗斯方块")
    pygame.key.set_repeat(10, 100)  #按下按键超过10ms触发下一次按键
    bg_color = BG_COLOR
    #建立方块对象
    #piece = Piece('J',screen)
    random.seed(int(time.time()))  #产生不同的随机序列

    #定义一个二维数组,且全部初始化为'_'
    #game_wall = GameWall(screen)
    # random.choice(range)在范围内随机选取一个作为返回值
    #piece = Piece(random.choice(PIECE_TYPES), screen,game_wall)
    game_state = GameState(screen)
    game_resource = GameResource()

    #pygame.event.get():从事件队列中取出所有事件对象,
    #得到待处理事件列表
    while True:
        #当前方块触底
        if game_state.piece and game_state.piece.is_on_botton:
            #将当前方块标记为wall
            game_state.wall.add_to_wall(game_state.piece)
            game_state.add_score(game_state.wall.eliminate_lines())

            #生成新的方块在游戏区域最上
            #piece = Piece(random.choice(PIECE_TYPES),screen,game_state.wall)
            game_state.piece = Piece(random.choice(PIECE_TYPES), screen,
                                     game_state.wall)
        #监视键盘和鼠标事件
        check_events(game_state.piece)

        #填充屏幕背景颜色
        screen.fill(bg_color)
        #绘制游戏区域
        GameDisplay.draw_game_area(screen, game_state, game_resource)
        #绘制小方块
        #draw_cell(screen,GAME_AREA_LEFT+GAME_AREA_WIDTH//2,GAME_AREA_TOP)
        if game_state.piece:
            game_state.piece.paint()
        #让最近绘制的屏幕可见
        pygame.display.flip()
Exemplo n.º 7
0
def main():
    pygame.init()
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption(u'俄罗斯方块')
    pygame.key.set_repeat(100, 100)
    game_resource = GameResource()
    game_state = GameState(screen, game_resource)
    game_resource.play_bg_music()

    while True:
        if game_state.piece and game_state.piece.is_at_bottom:
            game_state.touch_bottom()
        if game_state.score >= game_state.difficulty * SCORE_PER_LEVEL:
            game_state.add_difficulty()
        check_events(game_state, game_resource)
        screen.blit(game_resource.load_bg_img(), (0, 0))
        GameDisplay.draw_game_area(game_state, game_resource)
        if game_state.piece:
            GameDisplay.draw_piece(game_state)
        pygame.display.flip()
Exemplo n.º 8
0
def main():
    # Initiate screen capture.
    scap = ScreenCapture()
    if not scap.valid():
        print('A suitable Nox instance could not be found')
        return

    # Create a parser.
    parser = GameParser()

    # Initialize the game display.
    display = GameDisplay(scap, parser)
    display.start()

    # Initialize the macro.
    macro = Macro(scap, parser)

    # Run the macro.
    input('Press <ENTER> to start the macro\n')
    try:
        macro.run()
    except Exception as e:
        print(traceback.format_exc())

    input('Press <ENTER> to quit\n')

    display.stop()
    parser.stop()
def main():

    camvision = CamVision(CAM_DEVICE_NUM)
    gamedisplay = GameDisplay()
    camvision.learn_bg_now(BG_SAMPLES_COUNT)

    while True:
        camvision.capture_now()
        camvision.extract_fg()
        fg_mask_img = camvision.get_latest_fg_mask() * 255
        gamedisplay.show_camvision_image(fg_mask_img)
        gamedisplay.show_repsonse_image('nothing')
        if gamedisplay.check_quit_key_pressed():
            break
Exemplo n.º 10
0
def main():

    captured_frames_dir = pathlib.Path(CAPTURED_FRAMES_DIRNAME)

    if captured_frames_dir.exists():
        if captured_frames_dir.is_dir():
            if list(captured_frames_dir.iterdir()):
                cmd = input('There are some items in the "captures" '
                            'directory, proceeding will delete them all.\n'
                            'Proceed? (y/n): ')
                if cmd.lower() != 'y':
                    print('Quitting...')
                    return
            shutil.rmtree(str(captured_frames_dir))
        else:
            cmd = input('There is some file already present with the name '
                        '"captures", proceeding will delete it.\n'
                        'Proceed? (y/n): ')
            if cmd.lower() == 'y':
                captured_frames_dir.unlink()
            else:
                print('Quitting...')
                return

    captured_frames_dir.mkdir()

    camvision = CamVision(CAM_DEVICE_NUM)
    gamedisplay = GameDisplay()
    camvision.learn_bg_now(BG_SAMPLES_COUNT)
    num = 0

    os.chdir(str(captured_frames_dir))

    while True:
        camvision.capture_now()
        camvision.extract_fg()
        fg_mask = camvision.get_latest_fg_mask()
        fg_mask_img = fg_mask * 255
        gamedisplay.show_camvision_image(fg_mask_img)
        gamedisplay.show_repsonse_image('nothing')
        save_binary_image('{}.png'.format(num), fg_mask)
        num += 1
        if gamedisplay.check_quit_key_pressed():
            break

    print('Total', num, 'frames saved.')
def main():

    img_array = np.zeros((CNN_INPUT_IMG_HEIGHT, CNN_INPUT_IMG_WIDTH))
    img_array = img_array.astype(np.uint8)
    col = CNN_INPUT_IMG_WIDTH // 2
    img_array[:, col:] = 255
    postures = ['rock', 'paper', 'scissor', 'nothing']
    gamedisplay = GameDisplay()
    start_time = 0
    posture_generator = itertools.cycle(postures)

    while True:
        if (time.time() - start_time) > 0.5:
            posture = next(posture_generator)
            start_time = time.time()

        gamedisplay.show_repsonse_image(posture)
        gamedisplay.show_camvision_image(img_array)
        if gamedisplay.check_quit_key_pressed():
            break

        time.sleep(0.1)
Exemplo n.º 12
0
 def draw_cell(self, row, column):
     GameDisplay.draw_cell(self.screen,
                           (column * CELL_WIDTH + GAME_AREA_LEFT + 1,
                            row * CELL_WIDTH + GAME_AREA_TOP + 1),
                           BLOCK_COLOR[self.shape])