-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11.cpp
More file actions
144 lines (116 loc) · 3.48 KB
/
Copy path11.cpp
File metadata and controls
144 lines (116 loc) · 3.48 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
#include "TXLib.h"
#include <iostream>
#include <sstream>
using namespace std;
struct Bita {
double x, y;
double w, h;
void draw();
};
struct Ball {
int x = 400, y = 300;
int dx = 5;
int dy = rand() % 4 + 2;
int r = 10;
COLORREF color = RGB(0, 0, 180);
void draw ();
void move (Bita, Bita);
};
int main () {
int left_player = 0;
int right_player = 0;
txCreateWindow(800, 600);
txBegin();
int start = 0;
int n = 1;
while (left_player < 3 and right_player < 3) {
Bita bitaL = {50, 150, 50, 300};
Bita bitaR = {700, 150, 50, 300};
Ball ball;
if (start == 1) {
while (ball.x >= 0 and ball.x <= 800) {
txSetFillColor(TX_WHITE);
txClear();
txSelectFont("System", 40);
char out [2];
sprintf(out, "%d", left_player);
txTextOut(350, 50, out);
sprintf(out, "%d", right_player);
txTextOut(450, 50, out);
bitaL.draw();
bitaR.draw();
ball.draw();
if (txGetAsyncKeyState('W')) bitaL.y -= 5.5;
if (txGetAsyncKeyState('S')) bitaL.y += 5.5;
if (txGetAsyncKeyState(VK_UP)) bitaR.y -= 5.5;
if (txGetAsyncKeyState(VK_DOWN)) bitaR.y += 5.5;
txSleep(10);
ball.move(bitaL, bitaR);
if (ball.x > 800) left_player += 1;
if (ball.x < 0) right_player += 1;
}
}
if (txGetAsyncKeyState(VK_SPACE)) {
start = 1;
}
}
while (start == 1) {
txSetFillColor(TX_WHITE);
txClear();
txSelectFont("System", 40);
char out [2];
sprintf(out, "%d", left_player);
txTextOut(350, 50, out);
sprintf(out, "%d", right_player);
txTextOut(450, 50, out);
txTextOut(200, 350, "PRESS SPACE TO END");
if (txGetAsyncKeyState(VK_SPACE)) {
start = 0;
}
txSelectFont("System", 100);
txTextOut(200, 200, "GAME OVER");
if (txGetAsyncKeyState(VK_SPACE)) {
start = 0;
}
txSleep(10);
}
}
void Bita::draw() {
txSetFillColor(TX_ORANGE);
txRectangle(x, y, x+w, y+h);
}
void Ball::draw () {
txSetColor (color);
txSetFillColor(color);
txCircle (x, y, r);
}
void Ball::move (Bita bitaL, Bita bitaR) {
x += dx;
y += dy;
//if (x+r > 800 || x-r < 0) dx *= -1;
if (y+r > 600 || y-r < 0) dy *= -1;
if (x-r == bitaL.x + bitaL.w) {
if (bitaL.y <= y && y <= bitaL.y + bitaL.h) dx *= -1;
}
if (x+r == bitaR.x) {
if (bitaR.y <= y && y <= bitaR.y + bitaR.h) dx *= -1;
}
//if (x-r == bitaR.x + bitaR.w) {
// if (bitaR.y <= y && y <= bitaR.y + bitaR.h) dx *= -1;
//}
//if (x+r == bitaL.x) {
// if (bitaL.y <= y && y <= bitaL.y + bitaL.h) dx *= -1;
//}
if (y+r == bitaL.y) {
if (bitaL.x <= x && x <= bitaL.x + bitaL.w) dy *= -1;
}
if (y-r == bitaL.y + bitaL.h) {
if (bitaL.x <= x && x <= bitaL.x + bitaL.w) dy *= -1;
}
if (y+r == bitaR.y) {
if (bitaR.x <= x && x <= bitaR.x + bitaR.w) dy *= -1;
}
if (y-r == bitaR.y + bitaR.h) {
if (bitaR.x <= x && x <= bitaR.x + bitaR.w) dy *= -1;
}
}