7 / 7 / 1
Регистрация: 21.01.2013
Сообщений: 55
|
|
|
Рыбалка на С++. (Новичкам от новичка)
15.03.2013, 18:27. Показов 2302. Ответов 0
Вспоминал давно забытый С++, решил написать маленькую программку. (Распаковать в отдельную папку).
Также хочу поделиться исходным кодом. Может кому то будет интересно рассмотреть, может чему научится, а может меня поправить в чем-то. Делал через Microsoft Visual Studio 2010
Fishing.cpp
Кликните здесь для просмотра всего текста
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| // рыбалка
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <fstream>
using namespace std;
#include "GamePlay.h"
#include "FileWork.h"
#include "UserChoise.h"
#include "PlayMenu.h"
int _tmain(int argc, _TCHAR* argv[])
{
//переменные для работы программы
int iFish=0,
iWorm=0,
iMoney=0,
iUser=0,
iSelectedUser=0;;
bool bEndGame=true;
for(;bEndGame;)
{
//начало программы с меню выбора пользователя
vfUserList(&iFish, &iWorm, &iMoney, &iUser, &iSelectedUser);
//основной экран
vfPlayMenu(&iFish, &iWorm, &iMoney, &iUser, &bEndGame, &iSelectedUser);
}
return 0;
} |
|
UserChoise.h
Кликните здесь для просмотра всего текста
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
| // меню выбора профиля
void vfUserList(int *ipFish_UL, int *ipWorm_UL, int *ipMoney_UL, int *ipUser_UL, int *iSelectedUser)
{
int iFocus = 1, // индекс пользователя
iPressKey;
for (; iFocus != 5 ;)
{
switch (iFocus)
{
case 1:
system ("cls");
cout<<"Select user with up/down and press Enter to load or create new."<<endl;
cout<<"Press Escape to enter play menu."<<endl;
cout<<"User 1 <"<<endl;
cout<<"User 2"<<endl;
cout<<"User 3"<<endl;
cout<<"User 4"<<endl;
if (*ipFish_UL == 0 && *ipWorm_UL == 0 && *ipMoney_UL == 0)
cout<<endl;
else
{
cout<<"User "<<*iSelectedUser<<" Fish: "<<*ipFish_UL<<" Worm: "<<*ipWorm_UL<<" Money: "<<*ipMoney_UL;
if (*ipFish_UL == 0 && *ipWorm_UL == 0 && *ipMoney_UL == 20)
cout<<" is created."<<endl;
else
cout<<" is loaded."<<endl;
}
break;
case 2:
system ("cls");
cout<<"Select user with up/down and press Enter to load or create new."<<endl;
cout<<"Press Escape to enter play menu."<<endl;
cout<<"User 1"<<endl;
cout<<"User 2 <"<<endl;
cout<<"User 3"<<endl;
cout<<"User 4"<<endl;
if (*ipFish_UL == 0 && *ipWorm_UL == 0 && *ipMoney_UL == 0)
cout<<endl;
else
{
cout<<"User "<<*iSelectedUser<<" Fish: "<<*ipFish_UL<<" Worm: "<<*ipWorm_UL<<" Money: "<<*ipMoney_UL;
if (*ipFish_UL == 0 && *ipWorm_UL == 0 && *ipMoney_UL == 20)
cout<<" is created."<<endl;
else
cout<<" is loaded."<<endl;
}
break;
case 3:
system ("cls");
cout<<"Select user with up/down and press Enter to load or create new."<<endl;
cout<<"Press Escape to enter play menu."<<endl;
cout<<"User 1"<<endl;
cout<<"User 2"<<endl;
cout<<"User 3 <"<<endl;
cout<<"User 4"<<endl;
if (*ipFish_UL == 0 && *ipWorm_UL == 0 && *ipMoney_UL == 0)
cout<<endl;
else
{
cout<<"User "<<*iSelectedUser<<" Fish: "<<*ipFish_UL<<" Worm: "<<*ipWorm_UL<<" Money: "<<*ipMoney_UL;
if (*ipFish_UL == 0 && *ipWorm_UL == 0 && *ipMoney_UL == 20)
cout<<" is created."<<endl;
else
cout<<" is loaded."<<endl;
}
break;
case 4:
system ("cls");
cout<<"Select user with up/down and press Enter to load or create new."<<endl;
cout<<"Press Escape to enter play menu."<<endl;
cout<<"User 1"<<endl;
cout<<"User 2"<<endl;
cout<<"User 3"<<endl;
cout<<"User 4 <"<<endl;
if (*ipFish_UL == 0 && *ipWorm_UL == 0 && *ipMoney_UL == 0)
cout<<endl;
else
{
cout<<"User "<<*iSelectedUser<<" Fish: "<<*ipFish_UL<<" Worm: "<<*ipWorm_UL<<" Money: "<<*ipMoney_UL;
if (*ipFish_UL == 0 && *ipWorm_UL == 0 && *ipMoney_UL == 20)
cout<<" is created."<<endl;
else
cout<<" is loaded."<<endl;
}
break;
}
iPressKey = _getch();
switch (iPressKey)
{
case 72: // вверх
if (iFocus == 1)
{
iFocus = 4;
break;
}
else
{
--iFocus;
break;
}
case 80: // вниз
if (iFocus == 4)
{
iFocus = 1;
break;
}
else
{
++iFocus;
break;
}
case 13: //ентер
//функция считывает данные с файла
vfReadUser(iFocus, ipFish_UL, ipWorm_UL, ipMoney_UL, iSelectedUser);
break;
case 27: // ескейп
*ipUser_UL = iFocus;
if (*iSelectedUser!=0)
{
iFocus = 5;
break;
}
else
break;
}
}
return;
} |
|
PlayMenu.h
Кликните здесь для просмотра всего текста
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
| //игровое меню
void vfPlayMenu(int *ipFish_PM, int *ipWorm_PM, int *ipMoney_PM, int *ipUser_PM, bool *bpEndGame, int *iSelectedUser)
{
int iFocus=1,
iPressKey=0;
for (; iFocus != 5 ;)
{
switch (iFocus)
{
case 1:
system ("cls");
cout<<"Buy some worms or sell your fish to get a money.\nThen press play to catch a fish.\nPress Escape to select another user."<<endl;
cout<<"User "<<*iSelectedUser<<" Money: "<<*ipMoney_PM<<endl;
cout<<" > Fish: "<<*ipFish_PM<<" Press Enter to sell 1 fish for $6."<<endl;
cout<<" Worm: "<<*ipWorm_PM<<endl;
cout<<" Play Save&Exit"<<endl;
break;
case 2:
system ("cls");
cout<<"Buy some worms or sell your fish to get a money.\nThen press play to catch a fish.\nPress Escape to select another user."<<endl;
cout<<"User "<<*iSelectedUser<<" Money: "<<*ipMoney_PM<<endl;
cout<<" Fish: "<<*ipFish_PM<<endl;
cout<<" > Worm: "<<*ipWorm_PM<<" Press Enter to buy 1 worm for $2."<<endl;
cout<<" Play Save&Exit"<<endl;
break;
case 3:
system ("cls");
cout<<"Buy some worms or sell your fish to get a money.\nThen press play to catch a fish.\nPress Escape to select another user."<<endl;
cout<<"User "<<*iSelectedUser<<" Money: "<<*ipMoney_PM<<endl;
cout<<" Fish: "<<*ipFish_PM<<endl;
cout<<" Worm: "<<*ipWorm_PM<<endl;
cout<<" > Play Save&Exit"<<endl;
if (*ipWorm_PM == 0)
cout<<"\nYou must buy some worms!"<<endl;
break;
case 4:
system ("cls");
cout<<"Buy some worms or sell your fish to get a money.\nThen press play to catch a fish.\nPress Escape to select another user."<<endl;
cout<<"User "<<*iSelectedUser<<" Money: "<<*ipMoney_PM<<endl;
cout<<" Fish: "<<*ipFish_PM<<endl;
cout<<" Worm: "<<*ipWorm_PM<<endl;
cout<<" Play > Save&Exit"<<endl;
break;
}
iPressKey = _getch();
switch (iPressKey)
{
case 72: // вверх
if (iFocus == 4)
{
iFocus = 2;
break;
}
else if (iFocus == 1)
{
iFocus = 3;
break;
}
else
{
--iFocus;
break;
}
case 80: // вниз
if (iFocus == 3 || iFocus == 4)
{
iFocus = 1;
break;
}
else
{
++iFocus;
break;
}
case 75: // налево
if (iFocus == 3)
{
iFocus = 4;
break;
}
else if (iFocus == 4)
{
iFocus = 3;
break;
}
else
{
break;
}
case 77: // направо
if (iFocus == 4)
{
iFocus = 3;
break;
}
else if (iFocus == 3)
{
iFocus = 4;
break;
}
else
{
break;
}
case 13: //ентер
if (iFocus == 1) //продать 1 рыбу за 6 денег
{
if (*ipFish_PM > 0)
{
--*ipFish_PM;
*ipMoney_PM+=6;
break;
}
else
break;
}
if (iFocus == 2) // купить 1 червь за 2 денег
{
if (*ipMoney_PM >= 2)
{
++*ipWorm_PM;
*ipMoney_PM-=2;
break;
}
else
break;
}
// игровой процесс если фокус = 3 или выход при фокус = 4
else if (iFocus == 3)
{
bool bFishCatch = false;
//функция геймплея
if (*ipWorm_PM!=0)
vfGamePlay(&bFishCatch);
else
break;
--*ipWorm_PM;
if (bFishCatch = true)
++*ipFish_PM;
break;
}
else if (iFocus == 4)
{
//Функция сохранения игры
vfSaveUser (ipUser_PM, ipFish_PM, ipWorm_PM, ipMoney_PM);
*bpEndGame=false; //выход
iFocus = 5;
break;
}
else
break;
case 27: // ескейп
iFocus = 5;
break;
}
}
} |
|
FileWork.h
Кликните здесь для просмотра всего текста
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
| // сохраняет или считывает даные пользователа в файл
void vfReadUser (int iUserIndex, int *ipFish_RU, int *ipWorm_RU, int *ipMoney_RU, int *ipSelectedUser)
{
ifstream ifsUser1;
ifstream ifsUser2;
ifstream ifsUser3;
ifstream ifsUser4;
switch (iUserIndex)
{
case 1:
*ipSelectedUser=1;
*ipFish_RU = 0;
*ipWorm_RU = 0;
*ipMoney_RU = 0;
ifsUser1.open("user_1.txt");
ifsUser1 >> *ipFish_RU;
ifsUser1 >> *ipWorm_RU;
ifsUser1 >> *ipMoney_RU;
ifsUser1.close();
if (*ipFish_RU == 0 && *ipWorm_RU == 0 && *ipMoney_RU == 0)
{
ofstream ofsCreateUser1 ("user_1.txt");
ofsCreateUser1 <<"0\n0\n20";
*ipFish_RU = 0;
*ipWorm_RU = 0;
*ipMoney_RU = 20;
break;
}
else
break;
case 2:
*ipSelectedUser=2;
*ipFish_RU = 0;
*ipWorm_RU = 0;
*ipMoney_RU = 0;
ifsUser2.open("user_2.txt");
ifsUser2 >> *ipFish_RU;
ifsUser2 >> *ipWorm_RU;
ifsUser2 >> *ipMoney_RU;
ifsUser2.close();
if (*ipFish_RU == 0 && *ipWorm_RU == 0 && *ipMoney_RU == 0)
{
ofstream ofsCreateUser2 ("user_2.txt");
ofsCreateUser2 <<"0\n0\n20";
*ipFish_RU = 0;
*ipWorm_RU = 0;
*ipMoney_RU = 20;
break;
}
else
break;
case 3:
*ipSelectedUser=3;
*ipFish_RU = 0;
*ipWorm_RU = 0;
*ipMoney_RU = 0;
ifsUser3.open("user_3.txt");
ifsUser3 >> *ipFish_RU;
ifsUser3 >> *ipWorm_RU;
ifsUser3 >> *ipMoney_RU;
ifsUser3.close();
if (*ipFish_RU == 0 && *ipWorm_RU == 0 && *ipMoney_RU == 0)
{
ofstream ofsCreateUser3 ("user_3.txt");
ofsCreateUser3 <<"0\n0\n20";
*ipFish_RU = 0;
*ipWorm_RU = 0;
*ipMoney_RU = 20;
break;
}
else
break;
case 4:
*ipSelectedUser=4;
*ipFish_RU = 0;
*ipWorm_RU = 0;
*ipMoney_RU = 0;
ifsUser4.open("user_4.txt");
ifsUser4 >> *ipFish_RU;
ifsUser4 >> *ipWorm_RU;
ifsUser4 >> *ipMoney_RU;
ifsUser4.close();
if (*ipFish_RU == 0 && *ipWorm_RU == 0 && *ipMoney_RU == 0)
{
ofstream ofsCreateUser3 ("user_4.txt");
ofsCreateUser3 <<"0\n0\n20";
*ipFish_RU = 0;
*ipWorm_RU = 0;
*ipMoney_RU = 20;
break;
}
else
break;
}
return;
}
void vfSaveUser (int *iUserIndex, int *ipFish_RU, int *ipWorm_RU, int *ipMoney_RU)
{
ofstream ofsUser1;
ofstream ofsUser2;
ofstream ofsUser3;
ofstream ofsUser4;
switch (*iUserIndex)
{
case 1:
ofsUser1.open ("user_1.txt");
ofsUser1 << *ipFish_RU <<endl;
ofsUser1 << *ipWorm_RU <<endl;
ofsUser1 << *ipMoney_RU <<endl;
ofsUser1.close();
break;
case 2:
ofsUser2.open ("user_2.txt");
ofsUser2 << *ipFish_RU <<endl;
ofsUser2 << *ipWorm_RU <<endl;
ofsUser2 << *ipMoney_RU <<endl;
ofsUser2.close();
break;
case 3:
ofsUser3.open ("user_3.txt");
ofsUser3 << *ipFish_RU <<endl;
ofsUser3 << *ipWorm_RU <<endl;
ofsUser3 << *ipMoney_RU <<endl;
ofsUser3.close();
break;
case 4:
ofsUser4.open ("user_4.txt");
ofsUser4 << *ipFish_RU <<endl;
ofsUser4 << *ipWorm_RU <<endl;
ofsUser4 << *ipMoney_RU <<endl;
ofsUser4.close();
break;
}
} |
|
GamePlay.h (мне было впадло делать геймплей  )
Кликните здесь для просмотра всего текста
C++ | 1
2
3
4
5
6
7
8
9
10
11
| // игровой процесс
void vfGamePlay (bool *bpCatchFish)
{
char c;
system("cls");
cout<<"you want catch a fish? y/n"<<endl;
cin>>c;
if (c=='y')
*bpCatchFish=true;
} |
|
0
|