-
Notifications
You must be signed in to change notification settings - Fork 0
/
console_game.c
185 lines (158 loc) · 5.33 KB
/
console_game.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
168
169
170
171
172
173
174
175
176
177
178
179
#include "stdafx.h"
#include <iostream>
#include <thread>
#include <windows.h>
#include <string>
#include <cstring>
#include <conio.h>
#include "stdafx.h"
#include <time.h>
#define ramka 10
#define vysota 330
#define shirina 450
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
using namespace std;
void risovalka(int *x_nach_i, int *y_nach_j, int color_risovalka_flag_1, int color_risovalka_flag_2, HDC hdc);
void problem(int pr, int kordinatu_y_problem, bool color_pr, HDC hdc);
void rang_fruit(int qx_nach_i, int qy_nach_j, bool color, HDC hdc);
void risovalka_ramka(HDC hdc);
int one_pot();
void music();
int main()
{
thread first(one_pot);
thread second(music);
first.join();
second.join();
return 0;
}
int one_pot() {
HWND windowld = GetConsoleWindow();
HDC hdc = GetDC(windowld);
srand(time(0));
int speed = 3;
int speedpr = 2;
int pr = ((ramka + 1) + rand() % ((shirina - 20) - (ramka + 1) + 1));
int kordinatu_y_problem = ramka + 1;
int color_risovalka_flag_1 = 222;
int color_risovalka_flag_2 = 255;
int x_nach_i = shirina / 2 + 5;
int y_nach_j = vysota / 2 + 5;
int key;
bool color = false;
int qx_nach_i = ((ramka + 1) + rand() % ((shirina - 20) - (ramka + 1) + 1));
int qy_nach_j = ((ramka + 1) + rand() % ((vysota - 20) - (ramka + 1) + 1));
int score = 0;
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
cout << " SCORE - " << score << " " << "\r";
risovalka(&x_nach_i, &y_nach_j, color_risovalka_flag_1, color_risovalka_flag_2, hdc);
rang_fruit(qx_nach_i, qy_nach_j, true, hdc);
problem(pr, kordinatu_y_problem, true, hdc);
risovalka_ramka(hdc);
while (1) {
if (kbhit()) {
key = getch();
while (!kbhit()) {
risovalka(&x_nach_i, &y_nach_j, 0, 0, hdc);
switch (key) {
case 119: y_nach_j -= speed; break;
case 115: y_nach_j += speed; break;
case 97: x_nach_i -= speed; break;
case 100: x_nach_i += speed; break;
}
if (x_nach_i + 20 >= shirina) {
x_nach_i = ramka + 1;
}
if (y_nach_j + 20 >= vysota) {
y_nach_j = ramka + 1;
}
if (x_nach_i <= ramka) {
x_nach_i = shirina - 20 - 1;
}
if (y_nach_j <= ramka) {
y_nach_j = vysota - 20 - 1;
}
for (int i = x_nach_i; i <= x_nach_i + 20; i++) {
for (int j = y_nach_j; j <= y_nach_j + 20; j++) {
if ((i == qx_nach_i && j == qy_nach_j) || (i == qx_nach_i + 20 && j == qy_nach_j + 20) || (j == qy_nach_j + 20 && i == qx_nach_i) || (j == qy_nach_j && i == qx_nach_i + 20)) {
rang_fruit(qx_nach_i, qy_nach_j, false, hdc);
qx_nach_i = ((ramka + 1) + rand() % ((shirina - 20) - (ramka + 1)));
qy_nach_j = ((ramka + 1) + rand() % ((vysota - 20) - (ramka + 1)));
score++;
cout << " SCORE - " << score << " " << "\r";
break;
}
if ((i == pr && j == kordinatu_y_problem) || (j == kordinatu_y_problem + 10 && i == pr) || (j == kordinatu_y_problem && i == pr + 10) || (j == kordinatu_y_problem + 10 && i == pr + 10)) {
system("cls");
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(console, FOREGROUND_GREEN);
cout << endl << endl << endl << endl << endl << endl << " GAME OVER" << endl;
cout << " SCORE - " << score << endl;
return 0;
}
}
}
risovalka(&x_nach_i, &y_nach_j, color_risovalka_flag_1, color_risovalka_flag_2, hdc);
rang_fruit(qx_nach_i, qy_nach_j, true, hdc);
problem(pr, kordinatu_y_problem, false, hdc);
if (kordinatu_y_problem > vysota) {
kordinatu_y_problem = ramka;
pr = ((ramka + 1) + rand() % ((shirina - 20) - (ramka + 1) + 1));
}
kordinatu_y_problem += speedpr;
problem(pr, kordinatu_y_problem, true, hdc);
}
}
}
return 0;
}
void problem(int pr, int kordinatu_y_problem, bool color_pr, HDC hdc) {
int color_problem = 0;
if (color_pr) {
color_problem = 255;
}
for (int i = pr; i <= pr + 10; i++) {
for (int j = kordinatu_y_problem; j <= kordinatu_y_problem + 10 && j < vysota; j++) {
SetPixel(hdc, i, j, RGB(color_problem, color_problem, color_problem));
}
}
}
void risovalka(int *x_nach_i, int *y_nach_j, int color_risovalka_flag_1, int color_risovalka_flag_2, HDC hdc) {
for (int i = *x_nach_i; i <= *x_nach_i + 20; i++) {
for (int j = *y_nach_j; j <= *y_nach_j + 20; j++) {
if (j <= *y_nach_j + 10) {
SetPixel(hdc, i, j, RGB(0, 0, color_risovalka_flag_1));
}
else if (i > *x_nach_i) {
SetPixel(hdc, i, j, RGB(color_risovalka_flag_2, color_risovalka_flag_2, 0));
}
}
}
}
void rang_fruit(int qx_nach_i, int qy_nach_j, bool color, HDC hdc) {
int ik = 0, jk = 0, lk = 0;
for (int i = qx_nach_i; i <= qx_nach_i + 20; i++) {
for (int j = qy_nach_j; j <= qy_nach_j + 20; j++) {
if (color) {
ik = i*j, jk = i*i, lk = j*j;
}
SetPixel(hdc, i, j, RGB(ik, jk, lk));
}
}
}
void music() {
while (1) {
PlaySound(L"Beggin.wav", NULL, SND_ASYNC | SND_FILENAME);
Sleep(280000);//äëčňĺëüíîńň ďĺńíč
}
}
void risovalka_ramka(HDC hdc) {
for (int i = 0; i < shirina + ramka; i++) {
for (int j = 0; j < vysota + ramka; j++) {
if (i <= ramka || j <= ramka || i >= shirina || j >= vysota) {
SetPixel(hdc, i, j, RGB(230, 170, 123));
}
}
}
}//179 ńňđîr