📄 same.c
字号:
/*** $Id: same.c,v 1.14 2003/08/15 08:45:46 weiym Exp $**** Same: the Same game.** (C) 1997 the Free Software Foundation**** Author: Miguel de Icaza.** Federico Mena.** Horacio Pe馻.**** The idea is originally from KDE's same game program.**** Ported to MiniGUI by Wei Yongming***//*** This source is free software; you can redistribute it and/or** modify it under the terms of the GNU General Public** License as published by the Free Software Foundation; either** version 2 of the License, or (at your option) any later version.**** This software is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU** General Public License for more details.**** You should have received a copy of the GNU General Public** License along with this library; if not, write to the Free** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,** MA 02111-1307, USA*/#include <sys/types.h>#include <string.h>#include <stdlib.h>#include <time.h>#include <dirent.h>#include <minigui/common.h>#include <minigui/minigui.h>#include <minigui/gdi.h>#include <minigui/window.h>#include <minigui/control.h>#define IDM_NEW 100#define IDM_SCORES 110#define IDM_EXIT 120#define IDM_PREF 200#define IDM_ABOUT 300#define STONE_SIZE 40#define STONE_COLS 15#define STONE_LINES 10#define ID_TIMER 100#define _(x) (x)static RECT rcBoard;static HWND hwnd_score = HWND_INVALID, hwnd_menu;static BITMAP stones;static int tagged_count = 0;static int ball_timeout_id = -1;static int old_x = -1, old_y = -1;static int score;static int cur_sel_scen = 0;static struct ball { int color; int tag; int frame;} field [STONE_COLS][STONE_LINES];static int nstones;static int ncolors;static int sync_stones = 0;#define mapx(x) (x)#define mapy(y) (STONE_LINES-1-(y))static voiddraw_ball (HDC hdc, int x, int y){ int bx, by; if (field [x][y].color){ by = STONE_SIZE * (field [x][y].color - 1); bx = STONE_SIZE * (field [x][y].frame); FillBoxWithBitmapPart (hdc, x * STONE_SIZE, y * STONE_SIZE, STONE_SIZE, STONE_SIZE, 0, 0, &stones, bx, by); } else { SetBrushColor (hdc, PIXEL_black); FillBox (hdc, x * STONE_SIZE, y * STONE_SIZE, STONE_SIZE, STONE_SIZE); }}static void paint (HWND hwnd, HDC hdc){ RECT rc; int x1, y1, x2, y2, x, y; GetClientRect (hdc, &rc); x1 = rc.left / STONE_SIZE; y1 = rc.top / STONE_SIZE; x2 = rc.right / STONE_SIZE; y2 = rc.bottom / STONE_SIZE; for (x = 0; x < STONE_COLS; x++){ for (y = 0; y < STONE_LINES; y++){ draw_ball (hdc, x, y); } }}static void untag_all (HWND hwnd){ int x, y; HDC hdc = GetClientDC (hwnd); for (x = 0; x < STONE_COLS; x++) for (y = 0; y < STONE_LINES; y++){ field [x][y].tag = 0; if (sync_stones && field [x][y].frame != 0) { field [x][y].frame = 0; draw_ball (hdc, x, y); } } ReleaseDC (hdc);}static int flood_fill (int x, int y, int color){ int c = 0; if (field [x][y].color != color) return c; if (field [x][y].tag) return c; c = 1; field [x][y].tag = 1; if (x+1 < STONE_COLS) c += flood_fill (x+1, y, color); if (x) c += flood_fill (x-1, y, color); if (y+1 < STONE_LINES) c += flood_fill (x, y+1, color); if (y) c += flood_fill (x, y-1, color); return c;}static int move_tagged_balls (HDC hdc){ int x, y; for (x = 0; x < STONE_COLS; x++) for (y = 0; y < STONE_LINES; y++){ if (!field [x][y].tag) continue; field [x][y].frame = (field [x][y].frame + 1) % nstones; draw_ball (hdc, x, y); } return 1;}static void disable_timeout (HWND hwnd){ if (ball_timeout_id != -1){ KillTimer (hwnd, ball_timeout_id); ball_timeout_id = -1; }}static void mark_balls (HWND hwnd, int x, int y){ if (x == old_x && y == old_y) return; old_x = x; old_y = y; untag_all (hwnd); disable_timeout (hwnd); if (!field [x][y].color) return; tagged_count = flood_fill (x, y, field [x][y].color); if (tagged_count > 1) { SetTimer (hwnd, ID_TIMER, 10); ball_timeout_id = ID_TIMER; }}static void unmark_balls (HWND hWnd){ old_x = -1; old_y = -1; disable_timeout (hWnd); untag_all (hWnd);}static void compress_column (int x){ int y, ym; for (y = STONE_LINES - 1; y >= 0; y--){ if (!field [mapx(x)][mapy(y)].tag) continue; for (ym = y; ym < STONE_LINES - 1; ym++) field [mapx(x)][mapy(ym)] = field [mapx(x)][mapy(ym+1)]; field [mapx(x)][mapy(ym)].color = 0; field [mapx(x)][mapy(ym)].tag = 0; }}static void compress_y (void){ int x; for (x = 0; x < STONE_COLS; x++) compress_column (x);}static void copy_col (int dest, int src){ int y; for (y = 0; y < STONE_LINES; y++) field [mapx(dest)][mapy(y)] = field [mapx(src)][mapy(y)];}static void clean_last_col (void){ int y; for (y = 0; y < STONE_LINES; y++){ field [mapx(STONE_COLS-1)][mapy(y)].color = 0; field [mapx(STONE_COLS-1)][mapy(y)].tag = 0; }}static void compress_x (void){ int x, xm, l; for (x = 0; x < STONE_COLS; x++){ for (l = STONE_COLS; field [mapx(x)][mapy(0)].color == 0 && l; l--){ for (xm = x; xm < STONE_COLS-1; xm++) copy_col (xm, xm+1); clean_last_col (); } }}static void set_score (int new_score){ char b [40]; score = new_score; sprintf (b, "Score: %.5d", score); if (hwnd_score != HWND_INVALID) SetWindowText (hwnd_score, b);}static void end_of_game (HWND hWnd, char *title){ char b [256]; sprintf (b, "End of Game! \nYour Score: %.5d", score); MessageBox (hWnd, b, title, MB_OK | MB_ICONINFORMATION | MB_BASEDONPARENT);}static void check_game_over (HWND hwnd){ int cleared=1; int x,y; for(x = 0; x < STONE_COLS; x++) for(y = 0 ; y < STONE_LINES; y++) { if (!field [x][y].color) continue; cleared = 0; if(x+1 < STONE_COLS) if(field[x][y].color == field[x+1][y].color) return; if(y+1 < STONE_LINES) if(field[x][y].color == field[x][y+1].color) return; } if (cleared){ set_score (score+1000); end_of_game (hwnd, _("The Same Game")); } else end_of_game(hwnd, _("The Same Game"));}static void kill_balls (HWND hwnd, int x, int y){ if (!field [x][y].color) return; if (tagged_count < 2) return; set_score (score + (tagged_count - 2) * (tagged_count - 2)); compress_y (); compress_x (); InvalidateRect (hwnd, &rcBoard, FALSE); check_game_over (hwnd);}static void fill_board (void){ int x, y; for (x = 0; x < STONE_COLS; x++) for (y = 0; y < STONE_LINES; y++){ field [x][y].color = 1 + (rand () % ncolors); field [x][y].tag = 0; field [x][y].frame = sync_stones ? 0 : (rand () % nstones); }}static void new_game (HWND hwnd){ fill_board (); set_score (0); InvalidateRect (hwnd, &rcBoard, FALSE);}static void configure_sync (const char *fname){ if (strstr (fname, "-sync.gif")) sync_stones = 1; else sync_stones = 0;}static void load_scenario (const char *fname){ UnloadBitmap (&stones); if (LoadBitmap (HDC_SCREEN, &stones, fname)) { printf ("Loading scenario failed: %s.\n", fname); exit (1); } configure_sync (fname); nstones = stones.bmWidth / STONE_SIZE; ncolors = 3;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -