-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforce.cpp
More file actions
178 lines (148 loc) · 2.69 KB
/
Copy pathforce.cpp
File metadata and controls
178 lines (148 loc) · 2.69 KB
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
#include <iostream>
#include <vector>
#include <windows.h>
#include<conio.h>
#include <time.h>
#define ARRIBA 72
#define ABAJO 80
#define IZQUIERDA 75
#define DERECHA 77
#define P2_UP 87
#define P2_DOWN 83
#define P2_RIGHT 68
#define P2_LEFT 65
int interval = 200;
using namespace std;
void gotoxy(int x, int y);
void DrawPaddle(int cX, int cY){
for (int i = 0; i < 7; i++)
{
gotoxy(cX, cY+i);
cout << "#";
}
}
void DrawBall(int cX, int cY){
gotoxy(cX, cY);
cout << "O";
}
float ball_dir_x = -1;
float ball_dir_y = 0;
float ballSpeed = 1;
int ballX = 25;
int ballY = 15;
void updateBall(){
gotoxy(ballX, ballY);
cout << " ";
ballX += ball_dir_x * ballSpeed;
ballY += ball_dir_y * ballSpeed;
DrawBall(ballX, ballY);
}
void ResetBall(){
ballX = 25;
ballY = 15;
}
int main(){
srand(time(NULL));
int P1X = 1;
int P1Y = 10;
int P2X = 50;
int P2Y = 10;
DrawPaddle(P1X, P1Y);
DrawPaddle(P2X, P2Y);
bool game_over = false;
while (!game_over)
{
// cout << endl;
// cout<<"P1 " << P1X << " " << P1Y << endl;
// cout << "P2 " << P2X << " " << P2Y << endl;
// cout <<"Ball " << ballX << " " << ballY << endl;
Sleep(interval);
updateBall();
//check collision
if (ballX == (P1X+1) && (ballY >= P1Y && ballY <= P1Y+7)){
ball_dir_x *= -1;
}
if (ballX == (P2X-1) && (ballY >= P2Y && ballY <= P2Y + 7)){
ball_dir_x *= -1;
}
if (ballX <= 0 || ballY <= 0)
ResetBall();
if (ballX >= 50 || ballY <= 0)
ResetBall();
if (kbhit())
{
int key = getch();
gotoxy(P1X, P1Y);
printf(" ");
if (key == ARRIBA)
{
system("cls");
P1Y--;
gotoxy(P1X, P1Y);
DrawPaddle(P1X, P1Y);
DrawPaddle(P2X, P2Y);
}
if (key == ABAJO)
{
P1Y++;
gotoxy(P1X, P1Y);
DrawPaddle(P1X, P1Y);
DrawPaddle(P2X, P2Y);
}
if (key == IZQUIERDA)
{
P1X--;
gotoxy(P1X, P1Y);
DrawPaddle(P1X, P1Y);
DrawPaddle(P2X, P2Y);
}
if (key == DERECHA)
{
P1X++;
gotoxy(P1X, P1Y);
DrawPaddle(P1X, P1Y);
DrawPaddle(P2X, P2Y);
}
if (key == P2_UP)
{
system("cls");
P2Y--;
gotoxy(P2X, P2Y);
DrawPaddle(P1X, P1Y);
DrawPaddle(P2X, P2Y);
}
if (key == P2_DOWN)
{
P2Y++;
gotoxy(P2X, P2Y);
DrawPaddle(P1X, P1Y);
DrawPaddle(P2X, P2Y);
}
if (key == P2_LEFT)
{
P2X--;
gotoxy(P2X, P2Y);
DrawPaddle(P1X, P1Y);
DrawPaddle(P2X, P2Y);
}
if (key == P2_RIGHT)
{
P2X++;
gotoxy(P2X, P2Y);
DrawPaddle(P1X, P1Y);
DrawPaddle(P2X, P2Y);
}
}
}
cout << endl;
return 0;
}
void gotoxy(int x, int y)
{
HANDLE hCon;
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwPos;
dwPos.X = x;
dwPos.Y = y;
SetConsoleCursorPosition(hCon, dwPos);
}