SDL_pong

closed account (ETAkoG1T)
The rand() pard doesn't work properly, the ball often just bounces infinitely up and down in the middle and sometimes it gets stuck not moving (in the middle). Code mostly stolen from NomNomNom069 on youtube.

code:
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242

#include "SDL.h"
#include "SDL_ttf.h"
#include <sstream>
const int BALL_X = 390;
const int BALL_Y = 290;
using namespace std;

SDL_Surface * screen;
SDL_Event occur;
Uint32 white;
SDL_Color Pink = {255, 0, 255};
SDL_Rect PlayerPaddle;
SDL_Rect AIPaddle;
SDL_Rect Ball;
TTF_Font *times;
SDL_Rect Player_score1, AI_score1;


SDL_Surface *P_score;
SDL_Surface *A_score;




#include <stdlib.h>
#include <ctime>
int GetRandomNumber(int high, int low);
bool PointInRect(int x, int y, SDL_Rect rec);
bool CheckCollision(SDL_Rect r1, SDL_Rect r2);

int xVel, yVel;
int Player = 0, AI = 0;

void ResetBall()
{
	Ball.x = BALL_X;
	Ball.y = BALL_Y;

	xVel = GetRandomNumber(4, -2);
	yVel = GetRandomNumber(4, -2);
}

int GetRandomNumber(int high, int low)
{
	return rand()%high+low;
}

void LoadGame()
{
	SDL_Init(SDL_INIT_EVERYTHING);
	TTF_Init();
	SDL_WM_SetCaption("Pong", NULL);
	screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);

	PlayerPaddle.x = 20;
	PlayerPaddle.y = 250;
	PlayerPaddle.h = 100;
	PlayerPaddle.w = 20;

	AIPaddle.x = 760;
	AIPaddle.y = 250;
	AIPaddle.h = 100;
	AIPaddle.w = 20;

	Ball.x = BALL_X;
	Ball.y = BALL_Y;
	Ball.w = 20;
	Ball.h = 20;

	white = SDL_MapRGB(screen->format, 255, 255, 255);

	srand(time(NULL));

	ResetBall();

	times = TTF_OpenFont("Times.ttf", 14);

	Player_score1.x = 50;
	AI_score1.x = 725;
}

void Logic()
{
	Uint8 *keystates = SDL_GetKeyState(NULL);

	if(keystates[SDLK_w])
	{
		PlayerPaddle.y -= 1;
	}
	if(keystates[SDLK_s])
	{
		PlayerPaddle.y += 1;
	}

	if(PlayerPaddle.y < 1)
	{
		PlayerPaddle.y = 1;
	}

	if(PlayerPaddle.y + PlayerPaddle.h > 599)
	{
		PlayerPaddle.y = 599 - PlayerPaddle.h;
	}

	
	if(AIPaddle.y < 1)
	{
		AIPaddle.y = 1;
	}

	if(AIPaddle.y + AIPaddle.h > 599)
	{
		AIPaddle.y = 599 - AIPaddle.h;
	}


	if(AIPaddle.y + 0.5 * AIPaddle.h > Ball.y + 0.5 * Ball.h)
	{
		AIPaddle.y--;
	}

	if(AIPaddle.y + 0.5 * AIPaddle.h < Ball.y + 0.5 * Ball.h)
	{
		AIPaddle.y++;
	}

	Ball.x += xVel;
	Ball.y += yVel;

	if(Ball.y < 1)
	{
		yVel = -yVel;
	}

	if(Ball.y + Ball.h > 599)
	{
		yVel = -yVel;
	}

	if(Ball.x < 2)
	{
		ResetBall();
		AI++;

	}

	if(Ball.x + Ball.w > 798)
	{
		ResetBall();
		Player++;
	}

	if(CheckCollision(Ball, PlayerPaddle) == true)
	{
		xVel = -xVel;
	}

	if(CheckCollision(Ball, AIPaddle) == true)
	{
		xVel = -xVel;
	}
}

void DrawScreen()
{
	SDL_FillRect(screen, NULL, 0);


	SDL_FillRect(screen, &PlayerPaddle, white);
	SDL_FillRect(screen, &AIPaddle, white);
	SDL_FillRect(screen, &Ball, white);
	stringstream Pscore;
	stringstream Ascore;

	Ascore << AI;
	Pscore << Player;

	SDL_Surface *Player_score = TTF_RenderText_Solid(times, Pscore.str().c_str(), Pink);
	SDL_Surface *AI_score = TTF_RenderText_Solid(times, Ascore.str().c_str(), Pink);
	
	SDL_BlitSurface(Player_score, NULL, screen, &Player_score1);
	SDL_BlitSurface(AI_score, NULL, screen, &AI_score1);

	SDL_Flip(screen);

	SDL_FreeSurface(Player_score);
	SDL_FreeSurface(AI_score);
}

void Quit()
{
	TTF_CloseFont(times);
	TTF_Quit();
	SDL_Quit();
}

bool PointInRect(int x, int y, SDL_Rect rec)
{
	if(x > rec.x && y > rec.y && x < rec.x + rec.w && y < rec.y + rec.h)
	{
		return true;
	}
	else
		return false;
}

bool CheckCollision(SDL_Rect r1, SDL_Rect r2)
{
	if(PointInRect(r1.x, r1.y, r2) == true ||
		PointInRect(r1.x + r1.w, r1.y, r2) == true ||
		PointInRect(r1.x, r1.y + r1.h, r2) == true ||
		PointInRect(r1.x + r1.w, r1.y + r1.h, r2) == true)
	{
		return true;
	}

	else 
		return false;
}

int main(int argc, char *args[])
{

	LoadGame();
	bool running = true;

	while( running == true )
	{
		SDL_PollEvent(&occur);
		
		if(occur.type == SDL_QUIT)
		{
			running = false;
		}
		Logic();
		DrawScreen();
	}

	Quit();
	return 0;
}
You have to make sure the xVel != 0 because otherwise the ball will never reach any of the paddles.
closed account (ETAkoG1T)
Thank you =) Do I have to check yVel to?

EDIT:
Was this a decent solution :) I think it was a good idea to do it this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
void ResetBall()
{
	Ball.x = BALL_X;
	Ball.y = BALL_Y;

	xVel = 0, yVel = 0;
	
	while(xVel == 0 || yVel == 0)
	{
		xVel = GetRandomNumber(2, -2);
		yVel = GetRandomNumber(2, -2);
	}
}
Last edited on
Topic archived. No new replies allowed.