void block_reached_bottom(nstate *state) { int x, y, nr; if(!block_is_all_in_well(state)) { state->state = STATE_STOPPED; return; } for(y = WELL_HEIGHT - 1; y; y--) { nr = 0; for(x = 0; x < WELL_WIDTH; x++) { if(!state->blocks[0][y][x]) { nr = 1; break; } } if(nr) continue; msleep(DELETE_LINE_DELAY); delete_line(state, y); state->score += SCORE_INCREMENT; if((LEVELS > (state->level + 1)) && (((state->level + 1) * LEVEL_DIVISOR) <= state->score)) state->level++; draw_score(state); y++; } choose_new_shape(state); draw_next_shape(state); }
void new_game(nstate *state) { clear_well(state); if(state->score > state->hiscore) state->hiscore = state->score; state->score = 0; state->level = 0; draw_score(state); choose_new_shape(state); draw_next_shape(state); draw_well(state, 1); if(state->state == STATE_NEWGAME) state->state = STATE_RUNNING; }
void init_game(nstate *state) { GR_WM_PROPERTIES props; if(GrOpen() < 0) { fprintf(stderr, "Couldn't connect to Nano-X server\n"); exit(1); } state->main_window = GrNewWindow(GR_ROOT_WINDOW_ID, MAIN_WINDOW_X_POSITION, MAIN_WINDOW_Y_POSITION, MAIN_WINDOW_WIDTH, MAIN_WINDOW_HEIGHT, 0, MAIN_WINDOW_BACKGROUND_COLOUR, 0); /* set title */ props.flags = GR_WM_FLAGS_TITLE | GR_WM_FLAGS_PROPS; props.props = GR_WM_PROPS_BORDER | GR_WM_PROPS_CAPTION; props.title = "Nano-Tetris"; GrSetWMProperties(state->main_window, &props); GrSelectEvents(state->main_window, GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_CLOSE_REQ | GR_EVENT_MASK_KEY_DOWN | GR_EVENT_MASK_TIMEOUT); state->score_window = GrNewWindow(state->main_window, SCORE_WINDOW_X_POSITION, SCORE_WINDOW_Y_POSITION, SCORE_WINDOW_WIDTH, SCORE_WINDOW_HEIGHT, 0, SCORE_WINDOW_BACKGROUND_COLOUR, 0); GrSelectEvents(state->score_window, GR_EVENT_MASK_EXPOSURE); GrMapWindow(state->score_window); state->scoregcf = GrNewGC(); GrSetGCForeground(state->scoregcf, SCORE_WINDOW_FOREGROUND_COLOUR); GrSetGCBackground(state->scoregcf, SCORE_WINDOW_BACKGROUND_COLOUR); state->scoregcb = GrNewGC(); GrSetGCForeground(state->scoregcb, SCORE_WINDOW_BACKGROUND_COLOUR); state->next_shape_window = GrNewWindow(state->main_window, NEXT_SHAPE_WINDOW_X_POSITION, NEXT_SHAPE_WINDOW_Y_POSITION, NEXT_SHAPE_WINDOW_WIDTH, NEXT_SHAPE_WINDOW_HEIGHT, 0, NEXT_SHAPE_WINDOW_BACKGROUND_COLOUR, 0); GrSelectEvents(state->next_shape_window, GR_EVENT_MASK_EXPOSURE); GrMapWindow(state->next_shape_window); state->nextshapegcf = GrNewGC(); state->nextshapegcb = GrNewGC(); GrSetGCForeground(state->nextshapegcb, NEXT_SHAPE_WINDOW_BACKGROUND_COLOUR); state->new_game_button = GrNewWindow(state->main_window, NEW_GAME_BUTTON_X_POSITION, NEW_GAME_BUTTON_Y_POSITION, NEW_GAME_BUTTON_WIDTH, NEW_GAME_BUTTON_HEIGHT, 0, BUTTON_BACKGROUND_COLOUR, 0); GrSelectEvents(state->new_game_button, GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_DOWN); GrMapWindow(state->new_game_button); state->buttongcf = GrNewGC(); GrSetGCForeground(state->buttongcf, BUTTON_FOREGROUND_COLOUR); GrSetGCBackground(state->buttongcf, BUTTON_BACKGROUND_COLOUR); state->buttongcb = GrNewGC(); GrSetGCForeground(state->buttongcb, BUTTON_BACKGROUND_COLOUR); state->pause_continue_button = GrNewWindow(state->main_window, PAUSE_CONTINUE_BUTTON_X_POSITION, PAUSE_CONTINUE_BUTTON_Y_POSITION, PAUSE_CONTINUE_BUTTON_WIDTH, PAUSE_CONTINUE_BUTTON_HEIGHT, 0, BUTTON_BACKGROUND_COLOUR, 0); GrSelectEvents(state->pause_continue_button, GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_DOWN); state->anticlockwise_button = GrNewWindow(state->main_window, ANTICLOCKWISE_BUTTON_X_POSITION, ANTICLOCKWISE_BUTTON_Y_POSITION, ANTICLOCKWISE_BUTTON_WIDTH, ANTICLOCKWISE_BUTTON_HEIGHT, 0, BUTTON_BACKGROUND_COLOUR, 0); GrSelectEvents(state->anticlockwise_button, GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_DOWN); state->clockwise_button = GrNewWindow(state->main_window, CLOCKWISE_BUTTON_X_POSITION, CLOCKWISE_BUTTON_Y_POSITION, CLOCKWISE_BUTTON_WIDTH, CLOCKWISE_BUTTON_HEIGHT, 0, BUTTON_BACKGROUND_COLOUR, 0); GrSelectEvents(state->clockwise_button, GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_DOWN); state->left_button = GrNewWindow(state->main_window, LEFT_BUTTON_X_POSITION, LEFT_BUTTON_Y_POSITION, LEFT_BUTTON_WIDTH, LEFT_BUTTON_HEIGHT, 0, BUTTON_BACKGROUND_COLOUR, 0); GrSelectEvents(state->left_button, GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_DOWN); state->right_button = GrNewWindow(state->main_window, RIGHT_BUTTON_X_POSITION, RIGHT_BUTTON_Y_POSITION, RIGHT_BUTTON_WIDTH, RIGHT_BUTTON_HEIGHT, 0, BUTTON_BACKGROUND_COLOUR, 0); GrSelectEvents(state->right_button, GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_DOWN); state->drop_button = GrNewWindow(state->main_window, DROP_BUTTON_X_POSITION, DROP_BUTTON_Y_POSITION, DROP_BUTTON_WIDTH, DROP_BUTTON_HEIGHT, 0, BUTTON_BACKGROUND_COLOUR, 0); GrSelectEvents(state->drop_button, GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_DOWN); state->well_window = GrNewWindow(state->main_window, WELL_WINDOW_X_POSITION, WELL_WINDOW_Y_POSITION, WELL_WINDOW_WIDTH, WELL_WINDOW_HEIGHT, 0, WELL_WINDOW_BACKGROUND_COLOUR, 0); GrSelectEvents(state->well_window, GR_EVENT_MASK_EXPOSURE); GrMapWindow(state->well_window); state->wellgc = GrNewGC(); GrMapWindow(state->main_window); state->state = STATE_STOPPED; state->score = 0; read_hiscore(state); state->level = 0; state->running_buttons_mapped = 0; srandom(time(0)); choose_new_shape(state); new_game(state); }
void block_reached_bottom(nstate *state) { int x, y; if(!block_is_all_in_well(state)) { state->state = STATE_STOPPED; return; } for(y = WELL_HEIGHT - 1; y; y--) { for(x = 0; x < WELL_WIDTH; x++) if(!state->blocks[0][y][x]) goto nr; msleep(DELETE_LINE_DELAY); delete_line(state, y); state->score += SCORE_INCREMENT; if((LEVELS > (state->level + 1)) && (((state->level + 1) * LEVEL_DIVISOR) <= state->score)) state->level++; draw_score(state); y++; nr: } choose_new_shape(state); draw_next_shape(state); } void move_block(nstate *state, int direction) { if(direction == 0) { if(!state->current_shape.x) return; else { if(!will_collide(state, (state->current_shape.x - 1), state->current_shape.y, state->current_shape.orientation)) { draw_shape(state, state->current_shape.x, state->current_shape.y, 1); state->current_shape.x--; draw_shape(state, state->current_shape.x, state->current_shape.y, 0); draw_well(state, 0); } } } else { if(!will_collide(state, (state->current_shape.x + 1), state->current_shape.y, state->current_shape.orientation)) { draw_shape(state, state->current_shape.x, state->current_shape.y, 1); state->current_shape.x++; draw_shape(state, state->current_shape.x, state->current_shape.y, 0); draw_well(state, 0); } } } void rotate_block(nstate *state, int direction) { int neworientation = 0; if(direction == 0) { if(!state->current_shape.orientation) neworientation = MAXORIENTATIONS - 1; else neworientation = state->current_shape.orientation - 1; } else { neworientation = state->current_shape.orientation + 1; if(neworientation == MAXORIENTATIONS) neworientation = 0; } if(!will_collide(state, state->current_shape.x, state->current_shape.y, neworientation)) { draw_shape(state, state->current_shape.x, state->current_shape.y, 1); state->current_shape.orientation = neworientation; draw_shape(state, state->current_shape.x, state->current_shape.y, 0); draw_well(state, 0); } }