Pong Help :)

Hello! I need help with this block of code. First, i'm sorry if it's hard to read. I write messy code. So bare with me!

I need help so when the box goes to the right side of the screen, the bot will follow the bot and hit it. I don't need help with the collision(At least not right now), but with the bot moving to the boxes position. If that makes since? Any other advice would be great. Thanks! By the way, the 'box' would be the ball. I just switched it up. And i'm using the allegro lib.

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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <allegro.h>
#include <cstdlib>
BITMAP *Buffer;
BITMAP *Buffer2;
BITMAP *Buffer3;
int TempX = 600;
int TempY = 300;
int PlayerX,PlayerY,PlayerW,PlayerH;
int BotX,BotY,BotW,BotH;
int BoxX,BoxY,BoxW,BoxH;
bool Right = true;
bool Left = false;
bool Up = true;
bool Down = false;

class PlayerC{
public:
	BITMAP* Player;
	
	void PlayerInit(){
		PlayerW = 20;
		PlayerH = 225;
		PlayerX = 20;
		PlayerY = 270;
		Player = load_bitmap("Player.bmp", NULL);
	}
	void PlayerMove(){
		if(key[KEY_UP] && PlayerY > 1) PlayerY-=2;
		if(key[KEY_DOWN] && PlayerY + 225 < 768) PlayerY+=2;
	}
	void PlayerDraw(){
		draw_sprite(Buffer,Player,PlayerX,PlayerY);
	}
	void PlayerDelete(){
		destroy_bitmap(Player);
	}
}Player;
class BotC{
public:

	BITMAP* Bot;

	void BotInit(){
		BotW = 20;
		BotH = 225;
		BotX = 985;
		BotY = 310;
		Bot = load_bitmap("Bot.bmp", NULL);
	}
	void BotMove(){
	}
	void BotDraw(){
		draw_sprite(Buffer,Bot,BotX,BotY);
	}
	void BotDelete(){
		destroy_bitmap(Bot);
	}
}Bot;
class BoxC{
public:
	BITMAP* Box;
	void BoxInit(){
		BoxH = 50;
		BoxW = 50;
		BoxX = 700;
		BoxY = 500;
		Box = load_bitmap("Box.bmp",NULL);
	}
	void BoxMove(){
		TempX = BoxX;
		TempY = BoxY;
		

		if(BoxX <= 0){
			Left = false;
			Right = true;
		}
		if(BoxX >= 980){
			Left = true;
			Right = false;
		}
		if(BoxY <= 0){
			Up = true;
			Down = false;
		}
		if(BoxY >= 725){
			Up = false;
			Down = true;
		}
		if(Left == true) BoxX -= 2;
		if(Right == true) BoxX += 2;
		if(Up == true) BoxY += 2;
		if(Down == true) BoxY -= 2;

		if(BoxX == PlayerX + 18 && BoxY + 50 >= PlayerY && BoxY + 50 <= PlayerY + 225){
			Left = false;
			Right = true;
		}
		if(BoxX == BotX && BoxY == BotY && BoxY == BotX + 225){
			Left = true;
			Right = false;
		}

		
	}
	void BoxDraw(){
		draw_sprite(Buffer,Box,TempX,TempY);
	}
}Box;

void CheckWinPlayer();
void MenuStart();
void MenuOptions1();
void MenuOptions2();
void GameRestart();

int main(){
	allegro_init();
	install_keyboard();
	set_color_depth(32);
	set_gfx_mode(GFX_AUTODETECT,1024,768,0,0);
	Buffer = create_bitmap(1024,768);


	MenuStart();
	while(true){
	if(key[KEY_ENTER]){
		break;
	}
	if(key[KEY_ESC]){
		return 0;
	}
	}

	rest(100);
	
	MenuOptions1();
	while(true){
		if(key[KEY_1]){
			break;
	}
		if(key[KEY_2]){
			break;
	}
	}

	

	


	

	Player.PlayerInit();
	Bot.BotInit();
	Box.BoxInit();
	while(!key[KEY_ESC]){
		rectfill(Buffer,1024,768,0,0,makecol(255,255,255));
		Player.PlayerDraw();
		Player.PlayerMove();
		Box.BoxDraw();
		Box.BoxMove();
		Bot.BotDraw();
		CheckWinPlayer();
		draw_sprite(screen,Buffer,0,0);
		rest(5);
	}

	return 0;
	}
END_OF_MAIN();

void MenuStart(){
	rectfill(screen,1024,768,0,0,makecol(255,255,255));
	textout_ex(screen,font,"=======================================",10,10,makecol(0,255,0),makecol(255,255,255));
	textout_ex(screen,font,"Pong!                     Made by Kyle",10,20,makecol(0,255,0),makecol(255,255,255));
	textout_ex(screen,font,"=======================================",10,30,makecol(0,255,0),makecol(255,255,255));
	textout_ex(screen,font,"Menu:",10,50,makecol(0,0,255),makecol(255,255,255));
	textout_ex(screen,font,"Press ENTER to play.",10,60,makecol(0,0,255),makecol(255,255,255));
	textout_ex(screen,font,"Press ESCAPE to leave.",10,70,makecol(0,0,255),makecol(255,255,255));
}

void MenuOptions1(){
	rectfill(screen,1024,768,0,0,makecol(255,255,255));
	textout_ex(screen,font,"=======================================",10,10,makecol(0,255,0),makecol(255,255,255));
	textout_ex(screen,font,"Pong!                     Made by Kyle",10,20,makecol(0,255,0),makecol(255,255,255));
	textout_ex(screen,font,"=======================================",10,30,makecol(0,255,0),makecol(255,255,255));
	textout_ex(screen,font,"1): Single Player.",10,60,makecol(0,0,255),makecol(255,255,255));
	textout_ex(screen,font,"2): Two Player",10,70,makecol(0,0,255),makecol(255,255,255));
}

void MenuOptions2(){
	rectfill(screen,1024,768,0,0,makecol(255,255,255));
	textout_ex(screen,font,"=======================================",10,10,makecol(0,255,0),makecol(255,255,255));
	textout_ex(screen,font,"Pong!                     Made by Kyle",10,20,makecol(0,255,0),makecol(255,255,255));
	textout_ex(screen,font,"=======================================",10,30,makecol(0,255,0),makecol(255,255,255));
	textout_ex(screen,font,"1): Easy",10,60,makecol(0,0,255),makecol(255,255,255));
	textout_ex(screen,font,"2): Hard",10,70,makecol(0,0,255),makecol(255,255,255));

}

void CheckWinPlayer(){
	if(BoxX < PlayerX){
		textout_ex(Buffer,font,"Press any button to restart",512,378,makecol(0,0,0),makecol(255,255,255));
		GameRestart();
	}
}

void GameRestart(){
	clear_keybuf();
	readkey();
	PlayerX = 20;
	PlayerY = 270;
	BoxX = 700;
	BoxY = 500;
	BotX = 985;
	BotY = 310;
}
Topic archived. No new replies allowed.