标签:for end || microsoft ext sof where 只读 offset
做了一个小东西,顺便连在一起了。上!
(相对路径(需要同一个目录下)与绝对路径分辨一下)
#include<stdio.h> #include<conio.h> #include<Windows.h> #include<time.h> #include<stdlib.h> #include <mmsystem.h> #pragma comment(lib, "winmm.lib") #define FrameX 4 #define FrameY 4 #define Frame_height 20 #define Frame_width 20 void gotoxy(HANDLE hOut, int x, int y); void Set_TextColor_Green(void); void make_frame(); void print_information(HANDLE hOut, struct Snake*snake, struct Food *food); void init_snake(struct Snake*snake); void print_snake(HANDLE hOut, struct Snake *snake); void move_snake(HANDLE hOut, struct Snake *snake); void get_food(HANDLE hOut, struct Snake *snake, struct Food *food); void eat_food(HANDLE hOut, struct Snake *snake, struct Food *food); void through_wall(HANDLE hOut, struct Snake *snake, char ch); int if_die(struct Snake *snake); void start_game(); void over_game(); int i, j; int a[2]; struct Snake { int x[100]; int y[100]; int count; int length; int speed; }; struct Food { int x; int y; }; void main() { system("color 0D"); start_game(); over_game(); } //光标移到指定位置 void gotoxy(HANDLE hOut, int x, int y) { COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(hOut, pos); } //设置文本为绿色 void Set_TextColor_Green(void) { HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN); } void make_frame() { HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); gotoxy(hOut, FrameX + 2 * Frame_width + 3, FrameY + 13); printf("Ese 退出游戏"); gotoxy(hOut, FrameX + 2 * Frame_width + 3, FrameY + 15); printf("长按方向键:加速"); gotoxy(hOut, FrameX, FrameY); printf("╔"); gotoxy(hOut, FrameX + 2 * Frame_width - 2, FrameY); printf("╗"); gotoxy(hOut, FrameX, FrameY + Frame_height); printf("╚"); gotoxy(hOut, FrameX + 2 * Frame_width - 2, FrameY + Frame_height); printf("╝"); for ( i =2; i < 2*Frame_width-2; i+=2) { gotoxy(hOut, FrameX + i, FrameY); printf("=");//打印上横框 } for ( i = 2; i < 2*Frame_width-2; i+=2) { gotoxy(hOut, FrameX + i, FrameY + Frame_height); printf("="); //打印下横框 } for ( i = 1; i < Frame_height; i++) { gotoxy(hOut, FrameX, FrameY + i); printf("║"); //打印左竖框 } for ( i = 1; i < Frame_height; i++) { gotoxy(hOut, FrameX + 2 * Frame_width - 2, FrameY + i); printf("║");//打印右竖框 } gotoxy(hOut, FrameX + Frame_width - 5, FrameY - 2); //打印游戏名称 Set_TextColor_Green();//设置蛇为绿色 printf("贪吃蛇游戏"); } void over_game(){ system("cls"); printf("\n\n\n\n\n\n\n\n\t\t\t\t游戏结束\n\n\n"); Sleep(2000); _getch(); } void print_information(HANDLE hOut, struct Snake*snake, struct Food *food) { gotoxy(hOut, FrameX + 2 * Frame_width + 3, FrameY + 1); printf("level:%d", snake->count / 5 + 1); gotoxy(hOut, FrameX + 2 * Frame_width + 3, FrameY + 3); printf("score:%d", 10 * snake->count); gotoxy(hOut, FrameX + 2 * Frame_width + 3, FrameY + 5); printf("eat food :%d", snake->count); gotoxy(hOut, FrameX + 2 * Frame_width + 3, FrameY + 7); printf("speed:%dms", snake->speed); gotoxy(hOut, FrameX + 2 * Frame_width + 3, FrameY + 9); printf("foodX:%d", food->x); gotoxy(hOut, FrameX + 2 * Frame_width + 3, FrameY + 11); printf("foodY:%d", food->y); } void init_snake(struct Snake*snake) { snake->x[0] = FrameX + 2; snake->y[0] = FrameY + Frame_height / 2; snake->speed = 300;//初始化蛇的速度为300ms snake->length = 3; snake->count = 0; for ( i = 1; i < snake->length; i++) {/* 依次得到蛇身、蛇头的坐标 */ snake->x[i] = snake->x[i - 1] + 2; snake->y[i] = snake->y[i - 1]; } } void move_snake(HANDLE hOut, struct Snake *snake) { gotoxy(hOut, snake->x[0], snake->y[0]); printf(" "); for ( i = 1; i <snake->length; i++) { snake->x[i - 1] = snake->x[i]; snake->y[i - 1] = snake->y[i]; } } /******打印蛇*************************************************************************/ void print_snake(HANDLE hOut, struct Snake *snake) { for ( i = 0; i<snake->length; i++) { gotoxy(hOut, snake->x[i], snake->y[i]); if (i == 0) { printf("○"); //打印蛇尾 } else if(i==snake->length-1) { printf("¤"); //打印蛇头 } else { printf("⊙"); //打印蛇身 } } } void get_food(HANDLE hOut, struct Snake *snake, struct Food *food) { srand((unsigned)time(NULL)); while (1) { food->x = rand() % (Frame_width - 1); food->y = rand() % Frame_height; if (food->x==0||food->y==0) { continue; } food->x = 2 * food->x + FrameX; food->y += FrameY; for ( i = 0; i <snake->length; i++) //判断食物是否在蛇的身上,如果在蛇身上,则重新产生;否则,打印蛇身 { if (food->x==snake->x[i]&&food->y==snake->y[i]) { break; } } if (i==snake->length) { gotoxy(hOut, food->x, food->y); printf("⊙"); break; } } } void eat_food(HANDLE hOut, struct Snake *snake, struct Food *food) { if (snake->x[snake->length - 1] == food->x&&snake->y[snake->length - 1] == food->y) { snake->length++; for ( i = snake->length-1; i >=1; i--) { snake->x[i] = snake->x[i - 1]; snake->y[i] = snake->y[i - 1]; } snake->x[0] = a[0]; snake->y[0] = a[1]; get_food(hOut, snake, food); printf("\a"); snake->count++; if (snake->count % 5 == 0) { snake->speed -= 50; } } } //******穿墙*************** void through_wall(HANDLE hOut, struct Snake *snake, char ch) { if (ch==72&&snake->y[snake->length-1]==FrameY) { snake->y[snake->length - 1] = FrameY + Frame_height - 1; } if (ch==80&&snake->y[snake->length-1]==FrameY+Frame_height) { snake->y[snake->length - 1] = FrameY + 1; } if (ch==75&&snake->x[snake->length-1]==FrameX) { snake->x[snake->length - 1] = FrameX + 2 * Frame_width - 4; } if (ch==77&&snake->x[snake->length-1]==FrameX+2*Frame_width-2) { snake->x[snake->length - 1] = FrameX + 2; } } //*判断蛇是否死* int if_die(struct Snake *snake) { for ( i = 0; i < snake->length-1; i++) { if (snake->x[snake->length - 1] == snake->x[i] && snake->y[snake->length - 1] == snake->y[i]) { return 0; } } return 1; } //开始游戏 void start_game() { unsigned char ch = 77; HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); struct Snake s, *snake = &s; struct Food f, *food = &f; make_frame(); init_snake(snake); get_food(hOut, snake, food); //print_snake(hOut, snake); while (1) { print_information(hOut, snake, food); a[0] = snake->x[0]; a[1] = snake->y[0]; j = 0; if (_kbhit())//判断是否按下键盘,如果按下,ch接收键盘输入 { ch = _getch();// 如果长按键盘,则加速 if (_kbhit()) { Sleep(20); j = 1; } } switch (ch) { case 72: { move_snake(hOut,snake); snake->y[snake->length - 1] -= 1; break; } case 80: { move_snake(hOut, snake); snake->y[snake->length - 1] += 1; break; } case 75: { move_snake(hOut, snake); snake->x[snake->length - 1] -= 2; break; } case 77: { move_snake(hOut, snake); snake->x[snake->length - 1] += 2; break; } } through_wall(hOut,snake,ch); eat_food(hOut, snake, food); print_snake(hOut,snake); if (if_die(snake)==0 || ch==27 || snake->speed==50) { gotoxy(hOut, FrameX + Frame_width - 2, FrameY + Frame_height / 2 - 1); printf("Game Over"); PlaySound(TEXT("Wannabe.wav"),NULL, SND_FILENAME | SND_SYNC); //system("pause");//TEXT("D:\\zzz\\zzd.wav") Sleep(2000); break; } if (j == 0) { Sleep(snake->speed); //延迟时间 } else { Sleep(10); } } }
//#pragma once #ifndef tou_h #define tou_h //#pragma comment(linker, "\"/manifestdependency:type=‘Win32‘ name=‘Microsoft.VC80.CRT‘ version=‘8.0.50608.0‘ processorArchitecture=‘X86‘ publicKeyToken=‘1fc8b3b9a1e18e3b‘ language=‘*‘\"") #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<WinSock2.h> //#include<stddef.h > #define PROUDCT_LEN sizeof(struct Product) #define FORMAT "% -8d %-15s %-15s %-15s %-15.1lf%-8d\n" #define DATA astPro[i].iId,astPro[i].acName,astPro[i].acProducer,astPro[i].acDate,astPro[i].dPrice,astPro[i].iAmount struct Product { int iId; char acName[15]; char acProducer[15]; char acDate[15]; double dPrice; int iAmount; } astPro[100]; //struct Product astPro[100]; #endif // !tou_h
#include"tou.h" void InputProduct()/*????????*/ { int i; int iMax = 0;/*iMax???????е???????????*/ char cDecide;/*?洢????????????????ж????*/ errno_t err; FILE*fp;/*??????????*/ iMax = ShowProduct(); if ((err = fopen_s(&fp,"product.txt", "ab")) != 0)/*???????????????????*/ { printf("can not open file\n");/*???????????*/ return; } printf("press y/Y to input"); getchar();/*?????1???????????????*/ cDecide = getchar();/*????????*/ while (cDecide == ‘y‘ || cDecide == ‘Y‘)/*?ж?????????????*/ { printf("Id:");/*??????????*/ scanf_s("%d",&astPro[iMax].iId); for (i = 0; i < iMax; i++) { if (astPro[i].iId == astPro[iMax].iId)/*??????????*/ { printf("the id is existing,press any key to comtinue!"); _getch(); fclose(fp);/*????????????inout????*/ return; } } printf("Name:");/*???????????*/ scanf_s("%s", &astPro[iMax].acName); printf("Producter:");/*?????????????*/ scanf_s("%s", &astPro[iMax].acProducer); printf("Date(Example 15-5-1):");/*???????????????*/ scanf_s("%s", &astPro[iMax].acDate); printf("Price:");/*??????????*/ scanf_s("%lf", &astPro[iMax].dPrice); printf("Amount:"); scanf_s("%d", &astPro[iMax].iAmount); if(fwrite(&astPro[iMax], PROUDCT_LEN, 1, fp) != 1) { printf("can not save!\n"); _getch();/*???????????????????仰*/ } else { printf("product Id %d is saved!\n", astPro[iMax].iId); iMax++; } printf("press y/Y to continue input:");/*?????????*/ getchar();/*????????????????????????*/ cDecide = getchar();/*?ж?????y/Y?????????*/ } Sleep(1000); fclose(fp); printf("Inout is over!\n"); }
#include"tou.h" void OutputProduct(); void OutputProduct()/*?????????*/ { errno_t err; FILE *fp; /*ild??????????iOut????????????????*/ int iId,i; int iMax = 0,iOut = 0; char cDecide;/*?洢????????????????ж????*/ iMax = ShowProduct(); if (iMax <= -1)/*??????????????????м??????????г??????*/ { printf("please input first!"); return; } printf("please input the id:"); scanf_s("%d", &iId);/*????????????????*/ for (i = 0; i < iMax; i++) { if (iId == astPro[i].iId)/*???????????*/ { printf("find the product,press y/Y to output:"); getchar(); cDecide = getchar(); if (cDecide == ‘y‘ || cDecide == ‘Y‘)/*?ж????????г???*/ { printf("input the amount to output:"); scanf_s("%d", &iOut); astPro[i].iAmount = astPro[i].iAmount - iOut; if (astPro[i].iAmount < 0)/*?????????????????????С*/ { printf("the amount is less than your input and the amount is 0 now!\n"); astPro[i].iAmount = 0;/*?????????????0*/ } /*???д?????????????????????????????*/ if ((err = fopen_s(&fp,"product.txt", "rb+")) != 0) { printf("can not open file\n"); return; } /*???????????????????????λ??*/ fseek( fp, i*PROUDCT_LEN,0); /*д???????????????*/ if (fwrite(&astPro[i], PROUDCT_LEN, 1, fp) != 1) { printf("can not save file!\n"); _getch(); } fclose(fp); printf("output successfully!\n"); ShowProduct();/*???????????????????*/ } return; } } printf("can not find the product!\n");/*??????????????????????*/ }
include"tou.h" void SearchProduct() { int iId, i, iMax = 0; char cDecide; iMax = ShowProduct(); if (iMax <= -1) { printf("please input first!"); return; } printf("please input the id"); scanf_s("%d", &iId); for (i = 0; i<iMax; i++) if (iId == astPro[i].iId) { printf("find the product,press y/Y to show:"); getchar(); cDecide = getchar(); if (cDecide == ‘y‘ || cDecide == ‘Y‘) { printf("id name producer date price amount\n"); printf(FORMAT, DATA); return; } } printf("can not find the product"); //提示未查找到商品的信息 }
#include"tou.h" int ShowProduct() { int i; int iMax = 0; errno_t err; FILE * fp; if ((err = fopen_s(&fp, "product.txt", "rb"))!=0) //只读方式打开二进制文件 { printf("can not open file\n"); //提示无法打开 return -1; } while (!feof(fp)) //通过foef来判断文件是否结束 if (fread(&astPro[iMax],PROUDCT_LEN,1,fp) ==1) iMax++; //统计文件中信息的条数 fclose(fp); //文件读取完成后及时关闭 if (iMax == 0) printf("No record in file!\n"); //文件中没有商品信息,提醒用户 else { printf("id name producer date price amount\n"); //有记录时显示商品信息 for (i = 0; i < iMax; i++) { printf(FORMAT, DATA); //从头文件中调用 } } Sleep(1000); return iMax; }
#include"tou.h" void ModifyProduct(); void ModifyProduct() { errno_t err; FILE *fp; int i, iMax = 0, iId; iMax = ShowProduct(); if (iMax<=-1) { printf("please input first"); return; } printf("please input the id to modify:"); scanf_s("%d", &iId); for ( i = 0; i <iMax; i++) { if (iId==astPro[i].iId) { printf("find the product,you can modify!\n"); printf("id:"); scanf_s("%d",&astPro[i].iId); printf("name:"); scanf_s("%s",&astPro[i].acName,sizeof(astPro[i].acName)); printf("producer:"); scanf_s("%s",&astPro[i].acProducer,sizeof(astPro[i].acProducer)); printf("Date:"); scanf_s("%s",&astPro[i].acDate,sizeof(astPro[i].acDate)); printf("price"); scanf_s("%lf",&astPro[i].dPrice,sizeof(astPro[i].dPrice)); printf("Amount:"); scanf_s("%d", &astPro[i].iAmount,sizeof(astPro[i].iAmount)); if ((err=fopen_s(&fp,"product.txt","rb+"))!=0) { printf("can not open\n"); return; } fseek(fp, i * PROUDCT_LEN, 0);//int fseek(FILE *stream, long offset, int fromwhere); if (fwrite(&astPro[i], PROUDCT_LEN,1,fp)!=1)//size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream); { printf("can not save!@"); _getch(); } fclose(fp); printf("modify successful!\n"); ShowProduct(); return; } } printf("can not find infomation!\n"); }
#include"tou.h" void game() { printf("game"); system("cls"); system("..\\game\\Debug\\snake.exe"); return 0; }
#include"tou.h" void DeteProduct(); void DeteProduct(){ errno_t err; FILE *fp; int i, j, iMax = 0, iId; iMax = ShowProduct(); if (iMax<=-1) { printf("please input first!"); return; } printf("please input the id:"); scanf_s("%d", &iId); for ( i = 0; i < iId; i++) { if (iId==astPro[i].iId) { for (j = i; j < iMax; j++) { astPro[j] = astPro[1 + j]; } iMax--; if ((err=fopen_s(&fp,"product.txt","wb")!=0)) { printf("can not open file\n"); return; } for (j = 0; j < iMax; j++) { if (fwrite(&astPro[j], PROUDCT_LEN, 1, fp) != 1) { printf("can not save!"); _getch(); } } fclose(fp); printf("delete successfully!\n"); ShowProduct(); return; } } printf("can not find the product/n"); }
#include"tou.h" void ShowMenu(); void Set_TextColor_Green(void); void main() { int iItem; ShowMenu(); scanf_s("%d", &iItem); while (iItem) { switch (iItem){ case 1:InputProduct(); break; case 2:OutputProduct(); break; case 3:DeteProduct(); break; case 4:ModifyProduct(); break; case 5:SearchProduct(); break; case 6:ShowProduct(); break; case 7:game(); break; default:printf("input wrong number"); break; } _getch; ShowMenu(); scanf_s("%d",&iItem); } } void ShowMenu() { Set_TextColor_Green(); Sleep(1000); system("cls"); printf("\n\n\n\n\n"); printf("\t\t\t\t|-----PRODUCT-----|\n"); printf("\t\t\t\t| 1.input recored |\n"); printf("\t\t\t\t| 2.output recoeed|\n"); printf("\t\t\t\t| 3.delete recored|\n"); printf("\t\t\t\t| 4.modify recored|\n"); printf("\t\t\t\t| 5.search recored|\n"); printf("\t\t\t\t| 6.show recored |\n"); printf("\t\t\t\t| 7.game |\n"); printf("\t\t\t\t| 0.exit |\n"); printf("\t\t\t\t|-----------------|\n\n"); printf("\t\t\tchose(0-7):"); } void Set_TextColor_Green(void) { HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN); }
标签:for end || microsoft ext sof where 只读 offset
原文地址:https://www.cnblogs.com/leolaosao/p/9073889.html