HEIIO!
用c++实现一个宠物领养的小程序哦
还是用的easy_x这个图形界面,简单上手,附上官网地址:[link]http://www.easyx.cn
当然,我还是推荐用QT和JAVA 实现图形界面的,要去学习了,冲
既然是给大家看easy_x的,
效果图
上代码!
#include <iostream>
#include <stdlib.h>
#include <graphics.h>
#include <conio.h>
#include <string>
#include <stdio.h>
using namespace std;
#define M 100
int c_count = 0;
int t = 0;
void enter(); //界面函数
void login();//登录界面
void regist();//注册界面
void menu();//功能界面
void petmenu();//宠物功能界面
void adopt();//领养宠物
void Recharge_money();//充值函数
void information();//查看账户信息
void save_dog();//存储小狗的函数
void save_cat();//存储小猫的函数
void save_pikaqiu();//存储皮卡丘的函数
void pet_message();//查看宠物状态
void instruction();//饲养说明
void play();//宠物互动
void raise_dog();//饲养小狗
void raise_cat();//饲养小猫
void raise_pikaqiu();//饲养皮卡丘
void shop();//商店
void period(int t); //时间函数
int weather(); //天气函数
typedef struct ul
{
int number = 0;//用户拥有的宠物数量
int money = 0;//用户拥有的金钱数量
char count[10];//用户的账号
int sd = 0;//0:没有宠物 1:小狗 2:小猫 3:皮卡丘 4:小狗+小猫 5:小狗+皮卡丘 6:小猫+皮卡丘 7:小狗+小猫+皮卡丘
int water = 0;//拥有的水的数量
int food = 0;//拥有的食物数量
char password[6];//用户的密码
}ur;//账户
typedef struct pets
{
int sd;//宠物的id,1:小狗,2:小猫,3:皮卡丘
int age = 1; //年龄
int hunger = 1; //饥饿值
int thirst = 1; //口渴值
int mood = -2; //心情指数
int health = 1; //健康指数
int step = 15; //行动点数
int id = 0;; //宠物所属主人的id号码
int life = 30; //宠物的寿命
}pets;
class pet //建立一个宠物类
{
public:
pet(int a, int h, int t, int m, int he, int st, int li) //构造函数
{
age = a; hunger = h; thirst = t; mood = m; health = he; step = st; life = li;
}
int geta() { return age; } //提取各个私有成员
int geth() { return hunger; }
int gett() { return thirst; }
int getm() { return mood; }
int gethe() { return health; }
int getst() { return step; }
int getl() { return life; }
void feed() //喂食函数
{
hunger--;
step = step - 5;
}
void drink() { thirst--; step = step - 3; } //喂水函数
void doctor() { step = step - 10; health = health + 5; } //看医生函数
void touch(int w); //抚摸函数
void fun(int w); //玩耍函数
private:
int age; //年龄
int hunger; //饥饿值
int thirst; //口渴值
int mood; //心情指数
int health; //健康指数
int step;
int life;//行动点数
};
inline void pet::touch(int w) //抚摸函数
{
step = step - 3;
switch (w)
{
case 0:
mood = mood + 2;
break;
case 1:
mood++;
break;
case 2:
mood++;
break;
case 3:
mood--;
thirst--;
break;
case 4:
mood--;
hunger--;
break;
}
}
inline void pet::fun(int w) //玩耍函数
{
step = step - 8;
switch (w)
{
case 0:
mood = mood + 2;
break;
case 2:
hunger++;
thirst++;
health++;
break;
case 3:
hunger = hunger + 2;
thirst++;
health++;
break;
case 4:
hunger++;
thirst++;
health++;
break;
}
}
void period(int t) //时间函数
{
t = t % 3;
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(20, 0, _T("黑体"));
TCHAR s1[] = _T("现在是上午");
TCHAR s2[] = _T("现在是下午");
TCHAR s3[] = _T("现在是晚上");
switch (t)
{
case 0:outtextxy(150, 220, s1); break;
case 1:outtextxy(150, 220, s2); break;
case 2:outtextxy(150, 220, s3); break;
}
};
int weather() //天气函数
{
int w;
w = rand() % 5;
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(20, 0, _T("黑体"));
TCHAR s1[] = _T("天气:晴。:");
TCHAR s2[] = _T("天气:阴");
TCHAR s3[] = _T("天气:小雨。");
TCHAR s4[] = _T("天气:暴雨");
TCHAR s5[] = _T("天气:大风。");
switch (w)
{
case 0:outtextxy(300, 220, s1); break;
case 1:outtextxy(300, 220, s2); break;
case 2:outtextxy(300, 220, s3); break;
case 3:outtextxy(300, 220, s4); break;
case 4:outtextxy(300, 220, s5); break;
}
return w;
}
void shop()
{
initgraph(499, 693);
int f = 0;
int i = 0;
ur a[M];
loadimage(NULL, _T("imp4.jpg"));
FlushMouseMsgBuffer();
MOUSEMSG m;
FILE *fp;
fp = fopen("user.txt", "rb");
if (fp == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
while (!feof(fp))
{
fread(&a[i], sizeof(struct ul), 1, fp);
i++;
}
fclose(fp);
while (f == 0)
{
m = GetMouseMsg();
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
{
if (m.x > 100 && m.x < 220 && m.y>90 && m.y < 152)
{
if (a[c_count].money < 15)
{
char s[10];
InputBox(s, 10, "余额不足,是否充值:y:前往充值,n:返回");
if (strcmp(s, "y") == 0)
{
menu();
}
if (strcmp(s, "n") == 0)
{
petmenu();
}
}
else
{
a[c_count].money = a[c_count].money - 15;
a[c_count].food++;
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
char n[10];
int h = 0;
InputBox(n, 10, "购买成功! :1.继续 2.返回");
sscanf(n, "%d", &h);
if (h == 1)
{
shop();
}
else
{
petmenu();
}
}
}
if (m.x > 115 && m.x < 215 && m.y < 320 && m.y>232)
{
if (a[c_count].money < 10)
{
char s[10];
InputBox(s, 10, "余额不足,是否充值:y:前往充值,n:返回");
if (strcmp(s, "y") == 0)
{
menu();
}
if (strcmp(s, "n") == 0)
{
petmenu();
}
}
else
{
a[c_count].money = a[c_count].money - 10;
a[c_count].water++;
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
char n[10];
int h = 0;
InputBox(n, 10, "购买成功! :1.继续 2.返回");
sscanf(n, "%d", &h);
if (h == 1)
{
shop();
}
else
{
petmenu();
}
}
}
if (m.x > 235 && m.x < 320 && m.y < 488 && m.y>374)
{
if (a[c_count].money < 20)
{
char s[10];
InputBox(s, 10, "余额不足,是否充值:y:前往充值,n:返回");
if (strcmp(s, "y") == 0)
{
menu();
}
if (strcmp(s, "n") == 0)
{
petmenu();
}
}
else
{
a[c_count].money = a[c_count].money - 20;
a[c_count].water++;
a[c_count].food++;
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
char n[10];
int h = 0;
InputBox(n, 10, "购买成功! :1.继续 2.返回");
sscanf(n, "%d", &h);
if (h == 1)
{
shop();
}
else
{
petmenu();
}
}
}
if (m.x > 240 && m.x < 300 && m.y < 670 && m.y>500)
{
petmenu();
}
break;
}
case WM_RBUTTONDOWN:
{
f = 0;
break;
}
}
}
}
void raise_dog()
{
initgraph(1280, 720);
loadimage(NULL, _T("宠物互动.jpg"));
int f = 0;
ur a[M];
int i = 0;
int g;
FILE *fp;
fp = fopen("user.txt", "rb");
if (fp == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
while (!feof(fp))
{
fread(&a[i], sizeof(struct ul), 1, fp);
i++;
}
fclose(fp);
int m = 0;
pets b[M];
FILE* fp_1;
fp_1 = fopen("小狗.txt", "rb");
if (fp_1 == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
while (!feof(fp_1))
{
fread(&b[m], sizeof(struct pets), 1, fp_1);
m++;
}
fclose(fp_1);
pet mypet(b[c_count].age, b[c_count].hunger, b[c_count].thirst, b[c_count].mood, b[c_count].health, b[c_count].step, b[c_count].life);
int life, date, w;
int emg, Nstep, Nage;
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(30, 0, _T("黑体"));
TCHAR s1[5];
TCHAR s2[5];
_stprintf(s2, _T("%d"), a[c_count].water); // 高版本 VC 推荐使用 _stprintf_s 函数
_stprintf(s1, _T("%d"), a[c_count].food);
outtextxy(208, 440, "(");
outtextxy(238, 440, "X");
outtextxy(288, 440, ")");
outtextxy(460, 440, "(");
outtextxy(490, 440, "X");
outtextxy(540, 440, ")");
outtextxy(258, 440, s1);
outtextxy(510, 440, s2);
life = mypet.getl();
date = mypet.geta();
if (date == life)
{
char s[10];
InputBox(s, 10, "宠物已死亡,y:重新领养,n:返回");
if (strcmp(s, "y") == 0)
{
b[c_count].age = 1;
b[c_count].health = 10;
b[c_count].hunger = 2;
b[c_count].mood = 2;
b[c_count].thirst = 2;
b[c_count].step = 20;
FILE* FP;
FP = fopen("小狗.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
if (b[c_count].health < -10)
{
char s[10];
InputBox(s, 10, "宠物已病死,y:重新领养,n:返回");
if (strcmp(s, "y") == 0)
{
b[c_count].age = 1;
b[c_count].health = 10;
b[c_count].hunger = 2;
b[c_count].mood = 2;
b[c_count].thirst = 2;
b[c_count].step = 20;
FILE* FP;
FP = fopen("小狗.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
else
{
for (date = mypet.geta(); date < life; date++)
{
w = weather(); //调用天气函数
t = t % 3; //t为控制时间整形
period(t); //调用时间函数
Nstep = mypet.getst();
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(20, 0, _T("黑体"));
outtextxy(150, 240, "你的宠物:");
outtextxy(420, 240, "你的步数:");
TCHAR s[5];
_stprintf(s, _T("%d"), Nstep); // 高版本 VC 推荐使用 _stprintf_s 函数
outtextxy(520, 240, s);
if (b[c_count].health < -7)
{
outtextxy(250, 240, "健康状况:生病");
}
else
{
outtextxy(250, 240, "健康状况:良好");
}
Nage = mypet.geta();
FlushMouseMsgBuffer();
MOUSEMSG h;
while (f == 0)
{
h = GetMouseMsg();
switch (h.uMsg)
{
case WM_LBUTTONDOWN:
{
if (h.x > 208 && h.x < 298 && h.y>280 && h.y < 380)
{
if (a[c_count].food < 1)
{
char s[10];
InputBox(s, 10, "食物不够,是否购买:y:前往商店,n:返回");
if (strcmp(s, "y") == 0)
{
shop();
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
if (b[c_count].step < 5)
{
char s[10];
InputBox(s, 10, "点数不够,是否确定:y:确定,n:返回");
if (strcmp(s, "y") == 0)
{
raise_dog();
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
else
{
mypet.feed();
b[c_count].step = mypet.getst();
a[c_count].food--;
b[c_count].age++;
b[c_count].health--;
b[c_count].hunger = mypet.geth();
b[c_count].thirst++;
b[c_count].mood--;
FILE* FP;
FP = fopen("小狗.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
t++;
raise_dog();
}
}
if (h.x > 460 && h.x < 540 && h.y < 380 && h.y>280)
{
if (a[c_count].water < 1)
{
char s[10];
InputBox(s, 10, "饮料不够,是否购买:1:前往商店,2:返回");
int r;
sscanf(s, "%d", &r);
if (r == 1)
{
shop();
}
if (r == 2)
{
pet_message();
}
}
if (b[c_count].step < 3)
{
char s[10];
InputBox(s, 10, "点数不够,是否确定:y:确定,n:返回");
if (strcmp(s, "y") == 0)
{
raise_dog();
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
else
{
mypet.drink();
b[c_count].step = mypet.getst();
a[c_count].water--;
b[c_count].age++;
b[c_count].health--;
b[c_count].hunger++;
b[c_count].thirst = mypet.gett();
b[c_count].mood--;
FILE* FP;
FP = fopen("小狗.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
t++;
raise_dog();
}
}
if (h.x > 712 && h.x < 785 && h.y < 380 && h.y>280)
{
if (b[c_count].step < 3)
{
char s[10];
InputBox(s, 10, "点数不够,是否确定:y:确定,n:返回");
if (strcmp(s, "y") == 0)
{
raise_dog();
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
else {
mypet.touch(w);
b[c_count].step = mypet.getst();
b[c_count].age++;
b[c_count].health = mypet.gethe();
b[c_count].hunger = mypet.geth();
b[c_count].thirst = mypet.gett();
b[c_count].mood = mypet.getm();
FILE* FP;
FP = fopen("小狗.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
t++;
raise_dog();
}
}
if (h.x > 954 && h.x < 1040 && h.y < 380 && h.y>280)
{
if (b[c_count].step < 8)
{
char s[10];
InputBox(s, 10, "点数不够,是否确定:y:确定,n:返回");
if (strcmp(s, "y") == 0)
{
raise_dog();
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
else
{
mypet.fun(w);
b[c_count].step = mypet.getst();
b[c_count].age++;
b[c_count].health = mypet.gethe();
b[c_count].hunger++;
b[c_count].thirst++;
b[c_count].mood = mypet.getm();
FILE* FP;
FP = fopen("小狗.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
t++;
raise_dog();
}
}
if (h.x > 40 && h.x < 160 && h.y < 660 && h.y>560)
{
pet_message();
}
if (h.x > 570 && h.x < 700 && h.y < 660 && h.y>560)
{
if (b[c_count].step < 10)
{
char s[10];
InputBox(s, 10, "点数不够,是否确定:y:确定,n:返回");
if (strcmp(s, "y") == 0)
{
raise_dog();
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
else
{
mypet.doctor();
b[c_count].step = mypet.getst();
b[c_count].age++;
b[c_count].health--;
b[c_count].hunger = mypet.geth();
b[c_count].thirst++;
b[c_count].mood--;
FILE* FP;
FP = fopen("小狗.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
t++;
raise_dog();
}
}
if (h.x > 1100 && h.x < 1240 && h.y < 660 && h.y>560)
{
b[c_count].step = b[c_count].step + 10;
b[c_count].age++;
b[c_count].health--;
b[c_count].hunger++;
b[c_count].thirst++;
b[c_count].mood--;
FILE* FP;
FP = fopen("小狗.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
t++;
raise_dog();
}
break;
}
case WM_RBUTTONDOWN:
{
f = 0;
break;
}
}
}
}
}
}
void raise_cat()
{
initgraph(1280, 720);
loadimage(NULL, _T("宠物互动.jpg"));
int f = 0;
ur a[M];
int i = 0;
int g;
FILE *fp;
fp = fopen("user.txt", "rb");
if (fp == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
while (!feof(fp))
{
fread(&a[i], sizeof(struct ul), 1, fp);
i++;
}
fclose(fp);
int m = 0;
pets b[M];
FILE* fp_1;
fp_1 = fopen("小猫.txt", "rb");
if (fp_1 == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
while (!feof(fp_1))
{
fread(&b[m], sizeof(struct pets), 1, fp_1);
m++;
}
fclose(fp_1);
pet mypet(b[c_count].age, b[c_count].hunger, b[c_count].thirst, b[c_count].mood, b[c_count].health, b[c_count].step, b[c_count].life);
int life, date, w;
int emg, Nstep, Nage;
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(30, 0, _T("黑体"));
TCHAR s1[5];
TCHAR s2[5];
_stprintf(s2, _T("%d"), a[c_count].water); // 高版本 VC 推荐使用 _stprintf_s 函数
_stprintf(s1, _T("%d"), a[c_count].food);
outtextxy(208, 440, "(");
outtextxy(238, 440, "X");
outtextxy(288, 440, ")");
outtextxy(460, 440, "(");
outtextxy(490, 440, "X");
outtextxy(540, 440, ")");
outtextxy(258, 440, s1);
outtextxy(510, 440, s2);
life = mypet.getl();
date = mypet.geta();
if (date == life)
{
char s[10];
InputBox(s, 10, "宠物已死亡,y:重新领养,n:返回");
if (strcmp(s, "y") == 0)
{
b[c_count].age = 1;
b[c_count].health = 10;
b[c_count].hunger = 2;
b[c_count].mood = 2;
b[c_count].thirst = 2;
b[c_count].step = 20;
FILE* FP;
FP = fopen("小猫.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
if (b[c_count].health < -10)
{
char s[10];
InputBox(s, 10, "宠物已病死,y:重新领养,n:返回");
if (strcmp(s, "y") == 0)
{
b[c_count].age = 1;
b[c_count].health = 10;
b[c_count].hunger = 2;
b[c_count].mood = 2;
b[c_count].thirst = 2;
b[c_count].step = 20;
FILE* FP;
FP = fopen("小猫.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
else
{
for (date = mypet.geta(); date < life; date++)
{
w = weather(); //调用天气函数
t = t % 3; //t为控制时间整形
period(t); //调用时间函数
Nstep = mypet.getst();
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(20, 0, _T("黑体"));
outtextxy(150, 240, "你的宠物:");
outtextxy(420, 240, "你的步数:");
TCHAR s[5];
_stprintf(s, _T("%d"), Nstep); // 高版本 VC 推荐使用 _stprintf_s 函数
outtextxy(520, 240, s);
if (b[c_count].health < -7)
{
outtextxy(250, 240, "健康状况:生病");
}
else
{
outtextxy(250, 240, "健康状况:良好");
}
Nage = mypet.geta();
FlushMouseMsgBuffer();
MOUSEMSG h;
while (f == 0)
{
h = GetMouseMsg();
switch (h.uMsg)
{
case WM_LBUTTONDOWN:
{
if (h.x > 208 && h.x < 298 && h.y>280 && h.y < 380)
{
if (a[c_count].food < 1)
{
char s[10];
InputBox(s, 10, "食物不够,是否购买:y:前往商店,n:返回");
if (strcmp(s, "y") == 0)
{
shop();
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
if (b[c_count].step < 5)
{
char s[10];
InputBox(s, 10, "点数不够,是否确定:y:确定,n:返回");
if (strcmp(s, "y") == 0)
{
raise_cat();
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
else
{
mypet.feed();
b[c_count].step = mypet.getst();
a[c_count].food--;
b[c_count].age++;
b[c_count].health--;
b[c_count].hunger = mypet.geth();
b[c_count].thirst++;
b[c_count].mood--;
FILE* FP;
FP = fopen("小猫.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
t++;
raise_cat();
}
}
if (h.x > 460 && h.x < 540 && h.y < 380 && h.y>280)
{
if (a[c_count].water < 1)
{
char s[10];
InputBox(s, 10, "饮料不够,是否购买:1:前往商店,2:返回");
int r;
sscanf(s, "%d", &r);
if (r == 1)
{
shop();
}
if (r == 2)
{
pet_message();
}
}
if (b[c_count].step < 3)
{
char s[10];
InputBox(s, 10, "点数不够,是否确定:y:确定,n:返回");
if (strcmp(s, "y") == 0)
{
raise_cat();
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
else
{
mypet.drink();
b[c_count].step = mypet.getst();
a[c_count].water--;
b[c_count].age++;
b[c_count].health--;
b[c_count].hunger++;
b[c_count].thirst = mypet.gett();
b[c_count].mood--;
FILE* FP;
FP = fopen("小猫.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
t++;
raise_cat();
}
}
if (h.x > 712 && h.x < 785 && h.y < 380 && h.y>280)
{
if (b[c_count].step < 3)
{
char s[10];
InputBox(s, 10, "点数不够,是否确定:y:确定,n:返回");
if (strcmp(s, "y") == 0)
{
raise_cat();
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
else {
mypet.touch(w);
b[c_count].step = mypet.getst();
b[c_count].age++;
b[c_count].health = mypet.gethe();
b[c_count].hunger = mypet.geth();
b[c_count].thirst = mypet.gett();
b[c_count].mood = mypet.getm();
FILE* FP;
FP = fopen("小猫.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
t++;
raise_cat();
}
}
if (h.x > 954 && h.x < 1040 && h.y < 380 && h.y>280)
{
if (b[c_count].step < 8)
{
char s[10];
InputBox(s, 10, "点数不够,是否确定:y:确定,n:返回");
if (strcmp(s, "y") == 0)
{
raise_cat();
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
else
{
mypet.fun(w);
b[c_count].step = mypet.getst();
b[c_count].age++;
b[c_count].health = mypet.gethe();
b[c_count].hunger++;
b[c_count].thirst++;
b[c_count].mood = mypet.getm();
FILE* FP;
FP = fopen("小猫.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
t++;
raise_cat();
}
}
if (h.x > 40 && h.x < 160 && h.y < 660 && h.y>560)
{
pet_message();
}
if (h.x > 570 && h.x < 700 && h.y < 660 && h.y>560)
{
if (b[c_count].step < 10)
{
char s[10];
InputBox(s, 10, "点数不够,是否确定:y:确定,n:返回");
if (strcmp(s, "y") == 0)
{
raise_cat();
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
else
{
mypet.doctor();
b[c_count].step = mypet.getst();
b[c_count].age++;
b[c_count].health--;
b[c_count].hunger = mypet.geth();
b[c_count].thirst++;
b[c_count].mood--;
FILE* FP;
FP = fopen("小猫.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
t++;
raise_cat();
}
}
if (h.x > 1100 && h.x < 1240 && h.y < 660 && h.y>560)
{
b[c_count].step = b[c_count].step + 10;
b[c_count].age++;
b[c_count].health--;
b[c_count].hunger++;
b[c_count].thirst++;
b[c_count].mood--;
FILE* FP;
FP = fopen("小猫.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
t++;
raise_cat();
}
break;
}
case WM_RBUTTONDOWN:
{
f = 0;
break;
}
}
}
}
}
}
void raise_pikaqiu()
{
initgraph(1280, 720);
loadimage(NULL, _T("宠物互动.jpg"));
int f = 0;
ur a[M];
int i = 0;
int g;
FILE *fp;
fp = fopen("user.txt", "rb");
if (fp == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
while (!feof(fp))
{
fread(&a[i], sizeof(struct ul), 1, fp);
i++;
}
fclose(fp);
int m = 0;
pets b[M];
FILE* fp_1;
fp_1 = fopen("皮卡丘.txt", "rb");
if (fp_1 == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
while (!feof(fp_1))
{
fread(&b[m], sizeof(struct pets), 1, fp_1);
m++;
}
fclose(fp_1);
pet mypet(b[c_count].age, b[c_count].hunger, b[c_count].thirst, b[c_count].mood, b[c_count].health, b[c_count].step, b[c_count].life);
int life, date, w;
int emg, Nstep, Nage;
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(30, 0, _T("黑体"));
TCHAR s1[5];
TCHAR s2[5];
_stprintf(s2, _T("%d"), a[c_count].water); // 高版本 VC 推荐使用 _stprintf_s 函数
_stprintf(s1, _T("%d"), a[c_count].food);
outtextxy(208, 440, "(");
outtextxy(238, 440, "X");
outtextxy(288, 440, ")");
outtextxy(460, 440, "(");
outtextxy(490, 440, "X");
outtextxy(540, 440, ")");
outtextxy(258, 440, s1);
outtextxy(510, 440, s2);
life = mypet.getl();
date = mypet.geta();
if (date == life)
{
char s[10];
InputBox(s, 10, "宠物已死亡,y:重新领养,n:返回");
if (strcmp(s, "y") == 0)
{
b[c_count].age = 1;
b[c_count].health = 10;
b[c_count].hunger = 2;
b[c_count].mood = 2;
b[c_count].thirst = 2;
b[c_count].step = 20;
FILE* FP;
FP = fopen("皮卡丘.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
if (b[c_count].health < -10)
{
char s[10];
InputBox(s, 10, "宠物已病死,y:重新领养,n:返回");
if (strcmp(s, "y") == 0)
{
b[c_count].age = 1;
b[c_count].health = 10;
b[c_count].hunger = 2;
b[c_count].mood = 2;
b[c_count].thirst = 2;
b[c_count].step = 20;
FILE* FP;
FP = fopen("皮卡丘.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
else
{
for (date = mypet.geta(); date < life; date++)
{
w = weather(); //调用天气函数
t = t % 3; //t为控制时间整形
period(t); //调用时间函数
Nstep = mypet.getst();
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(20, 0, _T("黑体"));
outtextxy(150, 240, "你的宠物:");
outtextxy(420, 240, "你的步数:");
TCHAR s[5];
_stprintf(s, _T("%d"), Nstep); // 高版本 VC 推荐使用 _stprintf_s 函数
outtextxy(520, 240, s);
if (b[c_count].health < -7)
{
outtextxy(250, 240, "健康状况:生病");
}
else
{
outtextxy(250, 240, "健康状况:良好");
}
Nage = mypet.geta();
FlushMouseMsgBuffer();
MOUSEMSG h;
while (f == 0)
{
h = GetMouseMsg();
switch (h.uMsg)
{
case WM_LBUTTONDOWN:
{
if (h.x > 208 && h.x < 298 && h.y>280 && h.y < 380)
{
if (a[c_count].food < 1)
{
char s[10];
InputBox(s, 10, "食物不够,是否购买:y:前往商店,n:返回");
if (strcmp(s, "y") == 0)
{
shop();
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
if (b[c_count].step < 5)
{
char s[10];
InputBox(s, 10, "点数不够,是否确定:y:确定,n:返回");
if (strcmp(s, "y") == 0)
{
raise_pikaqiu();
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
else
{
mypet.feed();
b[c_count].step = mypet.getst();
a[c_count].food--;
b[c_count].age++;
b[c_count].health--;
b[c_count].hunger = mypet.geth();
b[c_count].thirst++;
b[c_count].mood--;
FILE* FP;
FP = fopen("皮卡丘.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
t++;
raise_pikaqiu();
}
}
if (h.x > 460 && h.x < 540 && h.y < 380 && h.y>280)
{
if (a[c_count].water < 1)
{
char s[10];
InputBox(s, 10, "饮料不够,是否购买:1:前往商店,2:返回");
int r;
sscanf(s, "%d", &r);
if (r == 1)
{
shop();
}
if (r == 2)
{
pet_message();
}
}
if (b[c_count].step < 3)
{
char s[10];
InputBox(s, 10, "点数不够,是否确定:y:确定,n:返回");
if (strcmp(s, "y") == 0)
{
raise_pikaqiu();
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
else
{
mypet.drink();
b[c_count].step = mypet.getst();
a[c_count].water--;
b[c_count].age++;
b[c_count].health--;
b[c_count].hunger++;
b[c_count].thirst = mypet.gett();
b[c_count].mood--;
FILE* FP;
FP = fopen("皮卡丘.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
t++;
raise_pikaqiu();
}
}
if (h.x > 712 && h.x < 785 && h.y < 380 && h.y>280)
{
if (b[c_count].step < 3)
{
char s[10];
InputBox(s, 10, "点数不够,是否确定:y:确定,n:返回");
if (strcmp(s, "y") == 0)
{
raise_pikaqiu();
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
else {
mypet.touch(w);
b[c_count].step = mypet.getst();
b[c_count].age++;
b[c_count].health = mypet.gethe();
b[c_count].hunger = mypet.geth();
b[c_count].thirst = mypet.gett();
b[c_count].mood = mypet.getm();
FILE* FP;
FP = fopen("皮卡丘.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
t++;
raise_pikaqiu();
}
}
if (h.x > 954 && h.x < 1040 && h.y < 380 && h.y>280)
{
if (b[c_count].step < 8)
{
char s[10];
InputBox(s, 10, "点数不够,是否确定:y:确定,n:返回");
if (strcmp(s, "y") == 0)
{
raise_pikaqiu();
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
else
{
mypet.fun(w);
b[c_count].step = mypet.getst();
b[c_count].age++;
b[c_count].health = mypet.gethe();
b[c_count].hunger++;
b[c_count].thirst++;
b[c_count].mood = mypet.getm();
FILE* FP;
FP = fopen("皮卡丘.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
t++;
raise_pikaqiu();
}
}
if (h.x > 40 && h.x < 160 && h.y < 660 && h.y>560)
{
pet_message();
}
if (h.x > 570 && h.x < 700 && h.y < 660 && h.y>560)
{
if (b[c_count].step < 10)
{
char s[10];
InputBox(s, 10, "点数不够,是否确定:y:确定,n:返回");
if (strcmp(s, "y") == 0)
{
raise_pikaqiu();
}
if (strcmp(s, "n") == 0)
{
pet_message();
}
}
else
{
mypet.doctor();
b[c_count].step = mypet.getst();
b[c_count].age++;
b[c_count].health--;
b[c_count].hunger = mypet.geth();
b[c_count].thirst++;
b[c_count].mood--;
FILE* FP;
FP = fopen("皮卡丘.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
t++;
raise_pikaqiu();
}
}
if (h.x > 1100 && h.x < 1240 && h.y < 660 && h.y>560)
{
b[c_count].step = b[c_count].step + 10;
b[c_count].age++;
b[c_count].health--;
b[c_count].hunger++;
b[c_count].thirst++;
b[c_count].mood--;
FILE* FP;
FP = fopen("皮卡丘.txt", "wb");
for (int j = 0; j < m; j++)
{
fwrite(&b[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
FILE* FP_1;
FP_1 = fopen("user.txt", "wb");
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP_1);
}
fclose(FP_1);
t++;
raise_pikaqiu();
}
break;
}
case WM_RBUTTONDOWN:
{
f = 0;
break;
}
}
}
}
}
}
void play()
{
initgraph(1280, 720);
loadimage(NULL, _T("宠物信息.jpg"));
char q[10];
InputBox(q, 10, "选择需要饲养的宠物:“1.小狗,2.小猫,3.皮卡丘,4.返回”");
int r;
sscanf(q, "%d", &r);
if (r == 1)
{
raise_dog();
}
if (r == 2)
{
raise_cat();
}
if (r == 3)
{
raise_pikaqiu();
}
if (r == 4)
{
pet_message();
}
}
void instruction()
{
initgraph(1280, 720);
loadimage(NULL, _T("饲养说明.jpg"));
int f = 0;
FlushMouseMsgBuffer();
MOUSEMSG m;
while (f == 0)
{
m = GetMouseMsg();
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
{
if (m.x > 240 && m.x < 330 && m.y>490 && m.y < 580)
{
pet_message();
}
break;
}
case WM_RBUTTONDOWN:
{
f = 0;
break;
}
}
}
}
void pet_message()
{
initgraph(1280, 720);
loadimage(NULL, _T("宠物信息.jpg"));
int f = 0;
int i = 0;
int m = 0;
int n = 0;
int k = 0;
// 读取图片至绘图窗口
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(30, 0, _T("黑体"));
outtextxy(415, 135, "饲养说明");
outtextxy(765, 130, "开始互动");
ur a[M];
pets b[M], c[M], x[M];
FILE *fp;
fp = fopen("user.txt", "rb");
if (fp == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
while (!feof(fp))
{
fread(&a[i], sizeof(struct ul), 1, fp);
i++;
}
fclose(fp);
FILE* fp_1;
fp_1 = fopen("小狗.txt", "rb");
if (fp_1 == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
while (!feof(fp_1))
{
fread(&b[m], sizeof(struct pets), 1, fp_1);
m++;
}
fclose(fp_1);
FILE*fp_2;
fp_2 = fopen("小猫.txt", "rb");
if (fp_2 == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
while (!feof(fp_2))
{
fread(&c[n], sizeof(struct pets), 1, fp_2);
n++;
}
fclose(fp_2);
FILE*fp_3;
fp_3 = fopen("皮卡丘.txt", "rb");
if (fp_3 == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
while (!feof(fp_3))
{
fread(&x[k], sizeof(struct pets), 1, fp_3);
k++;
}
fclose(fp_3);
for (int j = 0; j < m; j++)
{
if (b[j].id == c_count)
{
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(20, 0, _T("黑体"));
outtextxy(400, 360, "年龄:");
outtextxy(480, 360, "天");
outtextxy(550, 360, "健康指数:");
outtextxy(700, 360, "寿命:");
outtextxy(780, 360, "天");
outtextxy(400, 400, "饥饿值:");
outtextxy(550, 400, "口渴值:");
outtextxy(700, 400, "心情指数:");
outtextxy(850, 400, "行动点数:");
TCHAR s1[5];
TCHAR s2[5];
TCHAR s3[5];
TCHAR s4[5];
TCHAR s5[5];
TCHAR s6[5];
TCHAR s7[5];
_stprintf(s1, _T("%d"), b[j].age);
outtextxy(460, 360, s1);
_stprintf(s2, _T("%d"), b[j].health);
outtextxy(650, 360, s2);
_stprintf(s3, _T("%d"), b[j].life);
outtextxy(760, 360, s3);
_stprintf(s4, _T("%d"), b[j].hunger);
outtextxy(490, 400, s4);
_stprintf(s5, _T("%d"), b[j].thirst);
outtextxy(630, 400, s5);
_stprintf(s6, _T("%d"), b[j].mood);
outtextxy(800, 400, s6);
_stprintf(s7, _T("%d"), b[j].step);
outtextxy(950, 400, s7); break;
}
if (j == m-1)
{
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(40, 0, _T("黑体"));
outtextxy(460, 380, "未领养");
}
}
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(20, 0, _T("黑体"));
for (int j = 0; j < n; j++)
{
if (c[j].id == c_count)
{
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(20, 0, _T("黑体"));
outtextxy(400, 200, "年龄:");
outtextxy(480, 200, "天");
outtextxy(550, 200, "健康指数:");
outtextxy(700, 200, "寿命:");
outtextxy(780, 200, "天");
outtextxy(400, 240, "饥饿值:");
outtextxy(550, 240, "口渴值:");
outtextxy(700, 240, "心情指数:");
outtextxy(850, 240, "行动点数:");
TCHAR s8[5];
TCHAR s9[5];
TCHAR s10[5];
TCHAR s11[5];
TCHAR s12[5];
TCHAR s13[5];
TCHAR s14[5];
_stprintf(s8, _T("%d"), c[j].age);
outtextxy(460, 200, s8);
_stprintf(s9, _T("%d"), c[j].health);
outtextxy(650, 200, s9);
_stprintf(s10, _T("%d"), c[j].life);
outtextxy(760, 200, s10);
_stprintf(s11, _T("%d"), c[j].hunger);
outtextxy(490, 240, s11);
_stprintf(s12, _T("%d"), c[j].thirst);
outtextxy(630, 240, s12);
_stprintf(s13, _T("%d"), c[j].mood);
outtextxy(800, 240, s13);
_stprintf(s14, _T("%d"), c[j].step);
outtextxy(950, 240, s14); break;
}
if(j==n-1)
{
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(40, 0, _T("黑体"));
outtextxy(460, 220, "未领养");
}
}
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(20, 0, _T("黑体"));
for (int j = 0; j < k; j++)
{
if (x[j].id == c_count)
{
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(20, 0, _T("黑体"));
outtextxy(400, 520, "年龄:");
outtextxy(480, 520, "天");
outtextxy(550, 520, "健康指数:");
outtextxy(700, 520, "寿命:");
outtextxy(780, 520, "天");
outtextxy(400, 560, "饥饿值:");
outtextxy(550, 560, "口渴值:");
outtextxy(700, 560, "心情指数:");
outtextxy(850, 560, "行动点数:");
TCHAR s8[5];
TCHAR s9[5];
TCHAR s10[5];
TCHAR s11[5];
TCHAR s12[5];
TCHAR s13[5];
TCHAR s14[5];
_stprintf(s8, _T("%d"), x[j].age);
outtextxy(460, 520, s8);
_stprintf(s9, _T("%d"), x[j].health);
outtextxy(650, 520, s9);
_stprintf(s10, _T("%d"), x[j].life);
outtextxy(760, 520, s10);
_stprintf(s11, _T("%d"), x[j].hunger);
outtextxy(490, 560, s11);
_stprintf(s12, _T("%d"), x[j].thirst);
outtextxy(630, 560, s12);
_stprintf(s13, _T("%d"), x[j].mood);
outtextxy(800, 560, s13);
_stprintf(s14, _T("%d"), x[j].step);
outtextxy(950, 560, s14); break;
}
if(j==k-1)
{
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(40, 0, _T("黑体"));
outtextxy(460, 560, "未领养");
}
}
FlushMouseMsgBuffer();
MOUSEMSG h;
while (f == 0)
{
h = GetMouseMsg();
switch (h.uMsg)
{
case WM_LBUTTONDOWN:
{
if (h.x > 415 && h.x < 535 && h.y>30 && h.y < 135)
{
instruction();
}
if (h.x > 765 && h.x < 885 && h.y < 135 && h.y>30)
{
play();
}
if (h.x > 0 && h.x < 100 && h.y < 720 && h.y>600)
{
petmenu();
}
break;
}
case WM_RBUTTONDOWN:
{
f = 0;
break;
}
}
}
}
void save_dog()
{
ur a[M];
int i = 0;
FILE *Fp;
Fp = fopen("user.txt", "rb");
if (Fp == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
while (!feof(Fp))
{
fread(&a[i], sizeof(struct ul), 1, Fp);
i++;
}
fclose(Fp);
pets b;
b.sd = 1;
b.id = c_count;
FILE*fp;
fp = fopen("小狗.txt", "ab");
if (fp == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
fwrite(&b, sizeof(struct pets), 1, fp);
fclose(fp);
}
void save_cat()
{
ur a[M];
int i = 0;
FILE *Fp;
Fp = fopen("user.txt", "rb");
if (Fp == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
while (!feof(Fp))
{
fread(&a[i], sizeof(struct ul), 1, Fp);
i++;
}
fclose(Fp);
pets b;
b.sd = 2;
b.id = c_count;
FILE*fp;
fp = fopen("小猫.txt", "ab");
if (fp == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
fwrite(&b, sizeof(struct pets), 1, fp);
fclose(fp);
}
void save_pikaqiu()
{
ur a[M];
int i = 0;
FILE *Fp;
Fp = fopen("user.txt", "rb");
if (Fp == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
while (!feof(Fp))
{
fread(&a[i], sizeof(struct ul), 1, Fp);
i++;
}
fclose(Fp);
pets b;
b.sd = 3;
b.id = c_count;
FILE*fp;
fp = fopen("皮卡丘.txt", "ab");
if (fp == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
fwrite(&b, sizeof(struct pets), 1, fp);
fclose(fp);
}
void adopt()
{
initgraph(1000, 561);
int f = 0;
// 读取图片至绘图窗口
loadimage(NULL, _T("领养.jpg"));
ur a[M];
int i = 0;
int j = 0;
int r = 0;
char n[10];
FILE *fp;
fp = fopen("user.txt", "rb");
if (fp == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
while (!feof(fp))
{
fread(&a[i], sizeof(struct ul), 1, fp);
i++;
}
fclose(fp);
FILE *FP;
FP = fopen("user.txt", "wb");
FlushMouseMsgBuffer();
MOUSEMSG m;
while (f == 0)
{
m = GetMouseMsg();
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
{
if (m.x > 200 && m.x < 300 && m.y>321 && m.y < 451)
{
if (a[c_count].sd == 0)
{
a[c_count].sd = 1;
a[c_count].number++;
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
save_dog();
}
fclose(FP);
InputBox(n, 10, "领养成功,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 1)
{
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
InputBox(n, 10, "已经拥有该宠物,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 2)
{
a[c_count].sd = 4;
a[c_count].number++;
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
save_dog();
}
fclose(FP);
InputBox(n, 10, "领养成功,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 3)
{
a[c_count].sd = 5;
a[c_count].number++;
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
save_dog();
}
fclose(FP);
InputBox(n, 10, "领养成功,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 4)
{
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
InputBox(n, 10, "已经拥有该宠物,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 5)
{
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
InputBox(n, 10, "已经拥有该宠物,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 6)
{
a[c_count].sd = 7;
a[c_count].number++;
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
save_dog();
}
fclose(FP);
InputBox(n, 10, "领养成功,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 7)
{
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
InputBox(n, 10, "已经拥有该宠物,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
}
if (m.x > 480 && m.x < 580 && m.y < 451 && m.y>321)
{
if (a[c_count].sd == 0)
{
a[c_count].sd = 2;
a[c_count].number++;
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
save_cat();
}
fclose(FP);
InputBox(n, 10, "领养成功,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 2)
{
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
InputBox(n, 10, "已经拥有该宠物,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 1)
{
a[c_count].sd = 4;
a[c_count].number++;
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
save_cat();
}
fclose(FP);
InputBox(n, 10, "领养成功,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 3)
{
a[c_count].sd = 6;
a[c_count].number++;
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
save_cat();
}
fclose(FP);
InputBox(n, 10, "领养成功,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 4)
{
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
InputBox(n, 10, "已经拥有该宠物,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 6)
{
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
InputBox(n, 10, "已经拥有该宠物,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 5)
{
a[c_count].sd = 7;
a[c_count].number++;
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
save_cat();
}
fclose(FP);
InputBox(n, 10, "领养成功,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 7)
{
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
InputBox(n, 10, "已经拥有该宠物,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
}
if (m.x > 750 && m.x < 850 && m.y < 451 && m.y>321)
{
if (a[c_count].sd == 0)
{
a[c_count].sd = 3;
a[c_count].number++;
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
save_pikaqiu();
}
fclose(FP);
InputBox(n, 10, "领养成功,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 3)
{
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
InputBox(n, 10, "已经拥有该宠物,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 1)
{
a[c_count].sd = 5;
a[c_count].number++;
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
save_pikaqiu();
}
fclose(FP);
InputBox(n, 10, "领养成功,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 2)
{
a[c_count].sd = 6;
a[c_count].number++;
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
save_pikaqiu();
}
fclose(FP);
InputBox(n, 10, "领养成功,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 5)
{
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
InputBox(n, 10, "已经拥有该宠物,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 6)
{
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
InputBox(n, 10, "已经拥有该宠物,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 4)
{
a[c_count].sd = 7;
a[c_count].number++;
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
save_pikaqiu();
}
fclose(FP);
InputBox(n, 10, "领养成功,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
if (a[c_count].sd == 7)
{
for (j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
InputBox(n, 10, "已经拥有该宠物,是否继续领养 :1.继续 2.返回");
sscanf(n, "%d", &r);
if (r == 1)
{
adopt();
}
else
{
petmenu();
}
}
}
break;
}
case WM_RBUTTONDOWN:
{
f = 0;
break;
}
}
}
}
void petmenu()
{
initgraph(774, 773);
int f = 0;
// 读取图片至绘图窗口
loadimage(NULL, _T("imp3.jpg"));
setbkmode(TRANSPARENT);
settextcolor(WHITE);
settextstyle(40, 0, _T("黑体"));
TCHAR s2[] = _T("领养宠物");
TCHAR s3[] = _T("查看宠物");
TCHAR s4[] = _T("饲养说明");
TCHAR s5[] = _T("商店");
TCHAR s6[] = _T("返回");
outtextxy(150, 100, s2);
outtextxy(150, 230, s3);
outtextxy(150, 355, s4);
outtextxy(150, 480, s5);
outtextxy(150, 615, s6);
FlushMouseMsgBuffer();
MOUSEMSG m;
while (f == 0)
{
m = GetMouseMsg();
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
{
if (m.x > 150 && m.x < 310 && m.y>100 && m.y < 140)
{
adopt();
}
if (m.x > 150 && m.x < 310 && m.y < 270 && m.y>230)
{
pet_message();
}
if (m.x > 150 && m.x < 310 && m.y < 395 && m.y>355)
{
instruction();
}
if (m.x > 150 && m.x < 230 && m.y < 520 && m.y>480)
{
shop();
}
if (m.x > 150 && m.x < 230 && m.y < 655 && m.y>615)
{
menu();
}
break;
}
case WM_RBUTTONDOWN:
{
f = 0;
break;
}
}
}
}
void Recharge_money()
{
int i = 0;
ur a[M];
char n[10];
int r = 0;
int h = 0;
FILE *fp;
fp = fopen("user.txt", "rb");
if (fp == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
while (!feof(fp))
{
fread(&a[i], sizeof(struct ul), 1, fp);
i++;
}
fclose(fp);
FILE *FP;
FP = fopen("user.txt", "wb");
InputBox(n, 10, "请输入充值的金额");
sscanf(n, "%d", &r);
a[c_count].money = a[c_count].money + r;
for (int j = 0; j < i; j++)
{
fwrite(&a[j], sizeof(struct ul), 1, FP);
}
fclose(FP);
InputBox(n, 10, "充值成功! :1.继续 2.返回");
sscanf(n, "%d", &h);
if (h == 1)
{
Recharge_money();
}
else
{
menu();
}
}
void information()
{
initgraph(1000, 471);
int f = 0;
int i = 0;
ur a[M];
// 读取图片至绘图窗口
loadimage(NULL, _T("用户信息.jpg"));
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(50, 0, _T("黑体"));
TCHAR s2[] = _T("用户信息!");
outtextxy(360, 40, s2);
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(30, 0, _T("黑体"));
TCHAR s3[] = _T("账号:");
TCHAR s4[] = _T("金钱:");
TCHAR s5[] = _T("宠物数量:");
TCHAR s6[] = _T("拥有的宠物:");
TCHAR s9[] = _T("没有宠物");
TCHAR s10[] = _T("小狗");
TCHAR s11[] = _T("小猫");
TCHAR s12[] = _T("皮卡丘");
TCHAR s13[] = _T("小狗,小猫");
TCHAR s14[] = _T("小狗,皮卡丘");
TCHAR s15[] = _T("小猫,皮卡丘");
TCHAR s16[] = _T("小狗,小猫,皮卡丘");
outtextxy(260, 150, s3);
outtextxy(260, 220, s4);
outtextxy(200, 290, s5);
outtextxy(170, 360, s6);
FILE *fp;
fp = fopen("user.txt", "rb");
if (fp == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
while (!feof(fp))
{
fread(&a[i], sizeof(struct ul), 1, fp);
i++;
}
fclose(fp);
outtextxy(470, 150, a[c_count].count);
TCHAR s7[5];
_stprintf(s7, _T("%d"), a[c_count].money); // 高版本 VC 推荐使用 _stprintf_s 函数
outtextxy(470, 220, s7);
TCHAR s8[5];
_stprintf(s8, _T("%d"), a[c_count].number);
outtextxy(470, 290, s8);
if (a[c_count].sd == 0)
{
outtextxy(470, 360, s9);
}
if (a[c_count].sd == 1)
{
outtextxy(470, 360, s10);
}
if (a[c_count].sd == 2)
{
outtextxy(470, 360, s11);
}
if (a[c_count].sd == 3)
{
outtextxy(470, 360, s12);
}
if (a[c_count].sd == 4)
{
outtextxy(470, 360, s13);
}
if (a[c_count].sd == 5)
{
outtextxy(470, 360, s14);
}
if (a[c_count].sd == 6)
{
outtextxy(470, 360, s15);
}
if (a[c_count].sd == 7)
{
outtextxy(470, 360, s16);
}
FlushMouseMsgBuffer();
MOUSEMSG m;
while (f == 0)
{
m = GetMouseMsg();
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
{
if (m.x > 440 && m.x < 500 && m.y>400 && m.y < 471)
{
menu();
}
break;
}
}
}
}
void menu()
{
initgraph(928, 527);
int f = 0;
// 读取图片至绘图窗口
loadimage(NULL, _T("menu.jpg"));
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(70, 0, _T("黑体"));
TCHAR s2[] = _T("WELCOME!");
outtextxy(314, 40, s2);
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(20, 0, _T("黑体"));
TCHAR s3[] = _T("领养宠物");
TCHAR s4[] = _T("充值金币");
TCHAR s5[] = _T("账户信息");
TCHAR s6[] = _T("返回主界面");
outtextxy(412, 186, s3);
outtextxy(412, 235, s4);
outtextxy(412, 289, s5);
outtextxy(412, 342, s6);
FlushMouseMsgBuffer();
MOUSEMSG m;
while (f == 0)
{
m = GetMouseMsg();
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
{
if (m.x > 412 && m.x < 492 && m.y>186 && m.y < 206)
{
petmenu();
}
if (m.x > 412 && m.x < 492 && m.y < 255 && m.y>235)
{
Recharge_money();
}
if (m.x > 412 && m.x < 492 && m.y < 309 && m.y>289)
{
information();
}
if (m.x > 412 && m.x < 492 && m.y < 362 && m.y>342)
{
enter();
}
break;
}
case WM_RBUTTONDOWN:
{
f = 0;
break;
}
}
}
}
void enter()
{
// 绘图环境初始化
initgraph(660, 371);
int f = 0;
// 读取图片至绘图窗口
loadimage(NULL, _T("imp1.jpg"));
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(30, 0, _T("黑体"));
TCHAR s2[] = _T("登录");
outtextxy(350, 147, s2);
TCHAR s3[] = _T("注册");
outtextxy(350, 220, s3);
TCHAR s4[] = _T("退出");
outtextxy(350, 293, s4);
settextstyle(20, 0, _T("黑体"));
TCHAR s1[] = _T("快来领养你的第一个宠物吧!");
outtextxy(250, 80, s1);
IMAGE img1;
loadimage(&img1, _T("imp2.jpg"));
setfillstyle(BS_DIBPATTERN, NULL, &img1);
solidrectangle(220, 148, 263, 185);
solidrectangle(220, 222, 263, 258);
solidrectangle(220, 296, 263, 331);
FlushMouseMsgBuffer();
MOUSEMSG m;
while (f == 0)
{
m = GetMouseMsg();
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
{
if (m.x > 350 && m.x < 400 && m.y>148 && m.y < 185)
{
login();
}
if (m.x > 350 && m.x < 400 && m.y < 258 && m.y>222)
{
regist();
}
if (m.x > 350 && m.x < 400 && m.y < 331 && m.y>296)
{
exit(0);
}
break;
}
case WM_RBUTTONDOWN:
{
f = 0;
break;
}
}
}
_getch();
closegraph();
}
void login()
{
initgraph(660, 371);
loadimage(NULL, _T("imp1.jpg"));
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(30, 0, _T("黑体"));
TCHAR s1[] = _T("用户登录!");
outtextxy(270, 80, s1);
ur a, b;
c_count = 0;
char n[10];
char i_1[1];
FILE *fp;
fp = fopen("user.txt", "rb");
if (fp == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
fread(&b, sizeof(struct ul), 1, fp);
InputBox(a.count, 10, "请输入账号");
while (1)
{
if (strcmp(a.count, b.count) == 0)
{
break;
}
else
{
if (!feof(fp))
{
fread(&b, sizeof(struct ul), 1, fp);
c_count++;
}
else
{
fclose(fp);
InputBox(n, 10, "账号未注册,是否注册:“1.注册 2.回到主页面”");
int r;
sscanf(n, "%d", &r);
if (r == 1)
{
regist();
}
else
{
enter();
}
}
}
}
InputBox(a.password, 7, "请输入密码");
if (strcmp(a.password, b.password) == 0)
{
fclose(fp);
menu();
}
else
{
fclose(fp);
InputBox(n, 10, "密码错误,是否继续:“1.继续 2.回到主页面”");
int j;
sscanf(i_1, "%d", &j);
if (j == 1)
{
login();
}
else
{
enter();
}
}
}
void regist()
{
initgraph(660, 371);
loadimage(NULL, _T("imp1.jpg"));
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(30, 0, _T("黑体"));
TCHAR s1[] = _T("用户注册!");
outtextxy(270, 80, s1);
int f = 0;
char n[10];
ur a, b;//结构体 the_users 重命名定义
FILE *fp;
fp = fopen("user.txt", "rb");
if (fp == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
fread(&b, sizeof(struct ul), 1, fp); //读入一个结构体字符块到b
InputBox(a.count, 10, "请输入账号");
while (1)
{
if (strcmp(a.count, b.count) != 0) /*如果两串不相等*/
{
if (!feof(fp)) /*如果未到文件尾*/
{
fread(&b, sizeof(struct ul), 1, fp);
}
else
break;
}
else
{
fclose(fp);
InputBox(n, 10, "用户名已存在,是否继续:“1.继续 2.回到主页面”");
int r;
sscanf(n, "%d", &r);
if (r == 1)
{
regist();
}
else
{
enter();
}
}
}
InputBox(a.password, 7, "请输入六位密码");
fp = fopen("user.txt", "ab");
if (fp == NULL)
{
printf("fail to open the file! \n");
exit(0);
}
fwrite(&a, sizeof(struct ul), 1, fp);
fclose(fp);
initgraph(815, 520);
loadimage(NULL, _T("注册.jpg"));
setfillcolor(BLUE);
FlushMouseMsgBuffer();
MOUSEMSG m;
while (f == 0)
{
m = GetMouseMsg();
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
{
if (m.x > 80 && m.x < 110 && m.y>400 && m.y < 470)
{
enter();
}
if (m.x > 665 && m.x < 695 && m.y < 470 && m.y>400)
{
login();
}
break;
}
case WM_RBUTTONDOWN:
{
f = 0;
break;
}
}
}
_getch();
closegraph();
}
int main()
{
enter();
return 0;
}