數字雨node
#include "windows.h" #include "stdlib.h" #include "stdio.h" #include "time.h" #include "string.h" #define ID_TIMER 1 #define STRMAXLEN 25 #define STRMINLEN 8 LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM); typedef struct tagCharChain { struct tagCharChain*prev; TCHAR ch; struct tagCharChain*next; }CharChain,*pCharChain; typedef struct tagCharColumn { CharChain *head,*current,*point; int x,y,iStrLen; int iStopTimes,iMustStopTimes; }CharColumn,*pCharColumn; int main(HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT ("matrix") ; HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc =WndProc; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance= hInstance ; wndclass.hIcon =LoadIcon(NULL,IDI_APPLICATION); wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName =szAppName; if(!RegisterClass(&wndclass)) { MessageBox (NULL,TEXT ("此程序必須運行在NT下!"),szAppName,MB_ICONERROR); return 0;} hwnd =CreateWindow(szAppName,NULL,WS_DLGFRAME| WS_THICKFRAME|WS_POPUP,0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),NULL,NULL,hInstance,NULL); ShowWindow(hwnd,SW_SHOWMAXIMIZED); UpdateWindow (hwnd); ShowCursor(FALSE); srand ((int )GetCurrentTime()); while(GetMessage (&msg,NULL,0,0)){ TranslateMessage(&msg); DispatchMessage (&msg); } ShowCursor(TRUE); return msg.wParam; } TCHAR randomChar(){ return (TCHAR)(rand()%(126-33)+33); } int init(CharColumn *cc,int cyScreen,int x){ int j; cc->iStrLen=rand()%(STRMAXLEN-STRMINLEN)+STRMINLEN; cc->x=x+3; cc->y=rand()%3?rand()%cyScreen:0; cc->iMustStopTimes=rand()%6; cc->iStopTimes=0; cc->head=cc->current=(pCharChain)calloc(cc->iStrLen,sizeof(CharChain)); for(j=0;j<cc->iStrLen-1;j++) { cc->current->prev = cc->point;//cc->current->prev = cc->point; cc->current->ch = '\0'; cc->current->next = cc->current+1; cc->point = cc->current++; } cc->current->prev = cc->point; cc->current->ch = '\0'; cc->current->next = cc->head; cc->head->prev = cc->current; cc->current = cc->point = cc->head; cc->head->ch=randomChar(); return 0; } LRESULT CALLBACK WndProc (HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) { HDC hdc; int i,j,temp,ctn; static HDC hdcMem; HFONT hFont; static HBITMAP hBitmap; static int cxScreen,cyScreen; static int iFontWidth=10,iFontHeight=15,iColumnCount; static CharColumn *ccChain; switch(message) { case WM_CREATE: cxScreen=GetSystemMetrics(SM_CXSCREEN); cyScreen=GetSystemMetrics(SM_CYSCREEN); SetTimer (hwnd,ID_TIMER,10,NULL); hdc=GetDC(hwnd); hdcMem=CreateCompatibleDC(hdc); hBitmap=CreateCompatibleBitmap(hdc,cxScreen,cyScreen); SelectObject(hdcMem,hBitmap); ReleaseDC(hwnd,hdc); hFont=CreateFont(iFontHeight,iFontWidth-5,0,0,FW_BOLD,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DRAFT_QUALITY,FIXED_PITCH | FF_SWISS,TEXT("Fixedays")); SelectObject(hdcMem,hFont); DeleteObject(hFont); SetBkMode(hdcMem,TRANSPARENT); iColumnCount=cxScreen/(iFontWidth*3/2); ccChain=(pCharColumn)calloc(iColumnCount,sizeof(CharColumn)); for(i=0;i<iColumnCount;i++) { init(ccChain+i,cyScreen,cyScreen,(iFontWidth*3/2)*i); } return 0 ; case WM_TIMER: hdc=GetDC(hwnd); PatBlt(hdcMem,0,0,cxScreen,cyScreen,BLACKNESS); for(i=0;i<iColumnCount;i++){ ctn=(ccChain+i)->iStopTimes++>(ccChain+i)->iMustStopTimes; (ccChain+i)->point=(ccChain+i)->head; SetTextColor(hdcMem,RGB(255,255,255)); TextOut(hdcMem,(ccChain+i)->x,(ccChain+i)->y,&((ccChain+i)->point->ch),1); j=(ccChain+i)->y; (ccChain+i)->point=(ccChain+i)->point->next; temp=0; while((ccChain+i)->point !=(ccChain+i)->head &&(ccChain+i)->point->ch) { SetTextColor(hdcMem,RGB(0,255-(255*(temp++)/(ccChain+i)->iStrLen),0)); TextOut(hdcMem,(ccChain+i)->x,j-=iFontHeight,&((ccChain+i)->point->ch),1); (ccChain+i)->point=(ccChain+i)->point->next; } if(ctn) (ccChain+i)->iStopTimes=0; else continue; (ccChain+i)->y+=iFontHeight; if((ccChain+i)->y-(ccChain+i)->iStrLen*iFontHeight>cyScreen) { free((ccChain+i)->current); init(ccChain+i,cyScreen,(iFontWidth*3/2)*i); } (ccChain+i)->head=(ccChain+i)->head->prev; (ccChain+i)->head->ch=randomChar(); } BitBlt(hdc,0,0,cxScreen,cyScreen,hdcMem,0,0,SRCCOPY); ReleaseDC(hwnd,hdc); return 0; case WM_RBUTTONDOWN: KillTimer(hwnd,ID_TIMER); return 0; case WM_KEYDOWN: case WM_LBUTTONDOWN: case WM_DESTROY: KillTimer (hwnd,ID_TIMER); DeleteObject(hBitmap); DeleteDC(hdcMem); for(i=0;i<iColumnCount;i++) { free((ccChain+i)->current); } free(ccChain); PostQuitMessage(0); return 0; } return DefWindowProc (hwnd,message,wParam,lParam); }
貪吃蛇windows
#include "string.h" #include "stdio.h" #include "stdlib.h" #include "time.h" #include "conio.h" #include "windows.h" const int H = 16; //地圖的高 const int L = 30; //地圖的長 char GameMap[16][30]; //遊戲地圖 int key; //按鍵保存 int sum = 1, over = 0; //蛇的長度, 遊戲結束(自吃或碰牆) int dx[4] = {0, 0, -1, 1}; //左、右、上、下的方向 int dy[4] = {-1, 1, 0, 0}; int arr=15; struct Snake //蛇的每一個節點的數據類型 { int x, y; //左邊位置 int now; //保存當前節點的方向, 0,1,2,3分別爲左右上下 }Snake[16*30]; const char Shead = '@'; //蛇頭 const char Sbody = '#'; //蛇身 const char Sfood = '*'; //食物 const char Snode = '.'; //'.'在地圖上標示爲空 void Initial(); //地圖的初始化 void Create_Food(); //在地圖上隨機產生食物 void Show(); //刷新顯示地圖 void Button(); //取出按鍵,並判斷方向 void Move(); //蛇的移動 void Check_Border(); //檢查蛇頭是否越界 void Check_Head(int x, int y); //檢查蛇頭移動後的位置狀況 int color(int c) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c); return 0; } int main() { Initial(); Show(); return 0; } void Initial() //地圖的初始化 { int i, j; int hx, hy; system("title 貪吃蛇"); //控制檯的標題 memset(GameMap, '.', sizeof(GameMap)); //初始化地圖所有爲空'.' system("cls"); srand(time(0)); //隨機種子 hx = rand()%16; //產生蛇頭 hy = rand()%30; GameMap[hx][hy] = Shead; Snake[0].x = hx; Snake[0].y = hy; Snake[0].now = -1; Create_Food(); //隨機產生食物 for(i = 0; i < 16; i++) //地圖顯示 { for(j = 0; j < 30; j++) printf(" %c", GameMap[i][j]); printf("\n"); } printf("\n\t\t\t小小C語言貪吃蛇\n"); printf("\t\t 按任意方向鍵開始遊戲\n"); getch(); //先接受一個按鍵,使蛇開始往該方向走 Button(); //取出按鍵,並判斷方向 } void Create_Food() //在地圖上隨機產生食物 { int fx, fy; while(1) { fx = rand()%16; fy = rand()%30; if(GameMap[fx][fy] == '.') //不能出如今蛇所佔有的位置 { GameMap[fx][fy] = Sfood; break; } } } void Show() //刷新顯示地圖 { int i, j; while(1) { _sleep(250); //延遲半秒(1000爲1s),即每半秒刷新一次地圖 Button(); //先判斷按鍵在移動 Move(); if(over) //自吃或碰牆即遊戲結束 { printf("\n**遊戲結束**\n"); printf("\t\t\t>_<\n"); getchar(); break; } system("cls"); //清空地圖再顯示刷新吼的地圖 for(i = 0; i < 16; i++) { for(j = 0; j < 30; j++) printf(" %c", GameMap[i][j]); printf("\n"); } printf("\n小小C語言貪吃蛇\n"); printf("按任意方向鍵開始遊戲\n"); } } void Button() //取出按鍵,並判斷方向 { if(kbhit() != 0) //檢查當前是否有鍵盤輸入,如有則返回一個非0值,不然返回0 { while(kbhit() != 0) //可能存在多個按鍵,要所有取完,以最後一個爲主 key = getch(); //將按鍵從控制檯中取出並保存到key中 switch(key) { //左 case 75: Snake[0].now = 0; break; //右 case 77: Snake[0].now = 1; break; //上 case 72: Snake[0].now = 2; break; //下 case 80: Snake[0].now = 3; break; } } } void Move() //蛇的移動 { int i, x, y; int t = sum; //保存當前蛇的長度 //記錄當前蛇頭的位置,並設置爲空,蛇頭先移動 x = Snake[0].x; y = Snake[0].y; GameMap[x][y] = '.'; Snake[0].x = Snake[0].x + dx[ Snake[0].now ]; Snake[0].y = Snake[0].y + dy[ Snake[0].now ]; Check_Border(); //蛇頭是否越界 Check_Head(x, y); //蛇頭移動後的位置狀況,參數爲: 蛇頭的開始位置 if(sum == t) //未吃到食物即蛇身移動哦 for(i = 1; i < sum; i++) //要從蛇尾節點向前移動哦,前一個節點做爲參照 { if(i == 1) //尾節點設置爲空再移動 GameMap[ Snake[i].x ][ Snake[i].y ] = '.'; if(i == sum-1) //爲蛇頭後面的蛇身節點,特殊處理 { Snake[i].x = x; Snake[i].y = y; Snake[i].now = Snake[0].now; } else //其餘蛇身即走到前一個蛇身位置 { Snake[i].x = Snake[i+1].x; Snake[i].y = Snake[i+1].y; Snake[i].now = Snake[i+1].now; } GameMap[ Snake[i].x ][ Snake[i].y ] = '#'; //移動後要置爲'#'蛇身 } } void Check_Border() //檢查蛇頭是否越界 { if(Snake[0].x < 0 || Snake[0].x >= 16 || Snake[0].y < 0 || Snake[0].y >= 30) over = 1; } void Check_Head(int x, int y) //檢查蛇頭移動後的位置狀況 { if(GameMap[ Snake[0].x ][ Snake[0].y ] == '.') //爲空 GameMap[ Snake[0].x ][ Snake[0].y ] = '@'; else if(GameMap[ Snake[0].x ][ Snake[0].y ] == '*') //爲食物 { color(arr); GameMap[ Snake[0].x ][ Snake[0].y ] = '@'; Snake[sum].x = x; //新增長的蛇身爲蛇頭後面的那個 Snake[sum].y = y; Snake[sum].now = Snake[0].now; GameMap[ Snake[sum].x ][ Snake[sum].y ] = '#'; sum++; Create_Food(); //食物吃完了立刻再產生一個食物 } else over = 1; }