-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGame.cpp
More file actions
518 lines (450 loc) · 14.8 KB
/
Game.cpp
File metadata and controls
518 lines (450 loc) · 14.8 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#include "Game.h"
#include "Coin.hpp"
#include "Board.hpp"
#include "Collision.cpp"
#include <SDL2/SDL_ttf.h>
#include <iostream>
#include <fstream>
//Scoring System
int cpu_moves = 0, player_moves = 0;
int cpu_total = 0, player_total = 0;
float cpu_score = 0, player_score = 0;
int current_player = 0; // 0 ---> user, 1 ---> computer
Vector start_striker_pos(140, 460);
//for Rendering
SDL_Surface* loadimage,*temp ;
Board* bg;
Coin* striker, *c0, *c1, *c2, *c3, *c4, *c5, *c6, *c7, *c8, *ima;
SDL_Rect boardsrc, boarddest ;
bool flag = false;
float x, y;
//For Rendering font
TTF_Font *font;
SDL_Color Black = {0, 0, 0};
SDL_Surface *surface_message, *surface_message2;
SDL_Texture *Message, *Message2;
SDL_Rect Message_rect, Message_rect2;
std::string str = "";
//For mouse movemnet
bool lmb = false; // left mouse button
SDL_Point mousepointer ;
SDL_Rect* hitbox ;
SDL_Point clickoffset;
int currx,curry;
//For State Machine
int STATE = 1, prevState = 1;
bool isclicked = 0;
//Class methods
Game::Game()
{}
Game::~Game()
{}
void Game:: init(const char* title,int xcord,int ycord,int width,int height){
TTF_Init();
if(SDL_Init(SDL_INIT_EVERYTHING)==0){
std::cout<<"Subsys_init\n";
window = SDL_CreateWindow(title,xcord,ycord,width,height,0);
if(window){
std::cout<<"Window is working\n";
}
renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
if(renderer){
std::cout<<"Successful rendering of window\n";
SDL_SetRenderDrawColor(renderer,255,255,255,255);
run = true ;
}
else{
run = false ;
}
}
font = TTF_OpenFont("Font2.otf", 20);
if (!font) printf("TTF_OpenFont: %s\n", TTF_GetError());
bg = new Board("textures/Board.png",renderer,0,0);
striker = new Coin("textures/striker.png",renderer,140,490,18, 15,-10);
c1 = new Coin("textures/black1.png",renderer,258,301,17, 10, 10);
c5 = new Coin("textures/white1.png",renderer,266,267,17, 10, 20);
c2 = new Coin("textures/black2.png",renderer,299,257,17, 10, 10);
c6 = new Coin("textures/white2.png",renderer,332,267,17, 10, 20);
c3 = new Coin("textures/black3.png",renderer,342,301,17, 10, 10);
c7 = new Coin("textures/white2.png",renderer,333,335,17, 10, 20);
c4 = new Coin("textures/black4.png",renderer,299,343,17, 10, 10);
c8 = new Coin("textures/white2.png",renderer,265,335,17, 10, 20);
c0 = new Coin("textures/gold.png",renderer,300,300,17,10, 50);
bg->coins.push_back(striker);
bg->coins.push_back(c0);
bg->coins.push_back(c1);
bg->coins.push_back(c2);
bg->coins.push_back(c3);
bg->coins.push_back(c4);
bg->coins.push_back(c5);
bg->coins.push_back(c6);
bg->coins.push_back(c7);
bg->coins.push_back(c8);
bg->coinsOnBoard.push_back(striker);
bg->coinsOnBoard.push_back(c0);
bg->coinsOnBoard.push_back(c1);
bg->coinsOnBoard.push_back(c2);
bg->coinsOnBoard.push_back(c3);
bg->coinsOnBoard.push_back(c4);
bg->coinsOnBoard.push_back(c5);
bg->coinsOnBoard.push_back(c6);
bg->coinsOnBoard.push_back(c7);
bg->coinsOnBoard.push_back(c8);
}
void Game:: setStrikerPosition(int current_player)
{
if(bg->coinsOnBoard[0]->coinScore != -10){
bg->coinsOnBoard.insert(bg->coinsOnBoard.begin(),striker);
}
if(current_player == 0)
{ if(prevState == 1)
{
return;
}
else
{
striker->pos.x = 140;
striker->pos.y = 490;
}
}
else
{
if(prevState == 4)
{
return ;
}
else
{
striker->pos.x = 140;
striker->pos.y = 110;
}
}
}
void Game:: updateScore(Coin* c, int score){
if (!current_player){
player_moves++;
player_total += score;
//player_score = (1.0 * player_total)/player_moves;
}
else{
cpu_moves++;
cpu_total += score;
//cpu_score = (1.0 * cpu_total)/cpu_moves;
}
//cout<<player_total<<endl ;
return;
}
void Game::EventHandling(){
SDL_Event event;
SDL_PollEvent(&event);
if(bg->coinsOnBoard[0]->coinScore ==-10 && bg->coinsOnBoard.size()<=1 ){
//update highscore function
setHighScore(player_total);
//run = false ;
this->snum = 0;
SDL_DestroyWindow(window);
}
if(STATE == 1)
{
setStrikerPosition(current_player);
prevState = 1;
switch(event.type)
{
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_RIGHT:
if(striker->pos.getX() <459)
{
striker->pos.set(striker->pos.getX() + 4, striker->pos.getY());
}
break;
case SDLK_LEFT:
if(striker->pos.getX() >141)
{
striker->pos.set(striker->pos.getX() - 4, striker->pos.getY());
}
break;
case SDLK_RETURN:
cout << "Enter pressed" << endl;
prevState = STATE;
STATE = 2;
break;
}
break;
case SDL_QUIT:
// run = false;
this->snum = 0;
SDL_DestroyWindow(window);
break;
case SDL_WINDOWEVENT:
if(event.window.event ==SDL_WINDOWEVENT_CLOSE)
{
current_player = 0;
player_total = 0;
cpu_total = 0 ;
STATE = 1 ;
prevState = 1;
this->snum = 0;
cout<<"Main game quit\n";
SDL_DestroyWindow(window);
break;
}
}
}
if(STATE == 2)
{
switch(event.type){
case SDL_QUIT:
this->snum = 0;
SDL_DestroyWindow(window);
// run = false;
break;
case SDL_WINDOWEVENT:
if(event.window.event ==SDL_WINDOWEVENT_CLOSE)
{
current_player = 0;
player_total = 0;
cpu_total = 0 ;
STATE = 1 ;
prevState = 1;
// run = false;
this->snum = 0;
SDL_DestroyWindow(window);
cout<<"Main game quit\n";
break;
}
case SDL_MOUSEMOTION:
mousepointer = {event.motion.x,event.motion.y};
if(lmb && hitbox !=NULL){
hitbox->x = mousepointer.x-clickoffset.x;
hitbox->y = mousepointer.y-clickoffset.y;
float m = (striker->pos.y - mousepointer.y)/(striker->pos.x - mousepointer.x);
if(m>0 && mousepointer.y > striker->pos.y)
{
x = striker->pos.x - 100*(1/sqrt(m*m + 1));
y = striker->pos.y - 100*(m/sqrt(m*m + 1));
}
else if(m>0 && mousepointer.y < striker->pos.y)
{
x = striker->pos.x + 100*(1/sqrt(m*m + 1));
y = striker->pos.y + 100*(m/sqrt(m*m + 1));
}
else if(m<0 && mousepointer.y < striker->pos.y)
{
x = striker->pos.x - 100*(1/sqrt(m*m + 1));
y = striker->pos.y - 100*(m/sqrt(m*m + 1));
}
else if(m<0 && mousepointer.y > striker->pos.y)
{
x = striker->pos.x + 100*(1/sqrt(m*m + 1));
y = striker->pos.y + 100*(m/sqrt(m*m + 1));
}
ima = new Coin("textures/test.png", renderer, int(x), int(y), 17, 10, 0);
flag = true;
resolveCollisionWithBoard(bg, ima);
}
break;
case SDL_MOUSEBUTTONUP:
if(lmb and event.button.button ==SDL_BUTTON_LEFT)
{
SDL_GetMouseState(&currx,&curry);
std::cout<<currx<<" "<<curry<<"\n";
lmb = false ;
hitbox = NULL ;
Vector temp(currx,curry);
striker->vel = striker->pos.sub(temp).normalize();
striker->vel.set(striker->vel.getX()/6, striker->vel.getY()/6);
STATE = 3 ;
flag = false;
}
break ;
case SDL_MOUSEBUTTONDOWN:
if(!lmb && event.button.button == SDL_BUTTON_LEFT)
{
lmb = true ;
if(SDL_PointInRect(&mousepointer,&striker->destRect)){
hitbox = &striker->destRect ;
clickoffset.x = mousepointer.x-striker->pos.x;
clickoffset.y = mousepointer.y-striker->pos.y;
std::cout<<"Object hit\n";
isclicked = 1;
}
}
break;
default:
break;
}
}
if(STATE == 3 )
{
prevState = 2;
switch(event.type)
{
case SDL_QUIT:
//run = false;
this->snum = 0;
SDL_DestroyWindow(window);
break;
case SDL_WINDOWEVENT:
if(event.window.event ==SDL_WINDOWEVENT_CLOSE)
{
current_player = 0;
//run = false;
player_total = 0;
cpu_total = 0 ;
STATE = 1 ;
prevState = 1;
this->snum = 0;
SDL_DestroyWindow(window);
cout<<"Main game quit\n";
break;
}
default:
for (int i = 0; i < bg->coinsOnBoard.size(); i++){
Coin* coin1 = bg->coinsOnBoard[i];
coin1->move();
resolveCollisionWithBoard(bg, coin1);
updateScore(coin1, resolveParticleInHoles(coin1, bg));
for (int j = 0; j < bg->coinsOnBoard.size(); j++){
if (i != j){
Coin* coin2 = bg->coinsOnBoard[j];
resolveCollisionWithCoins(coin1, coin2);
}
}
}
int i = bg->coinsOnBoard.size();
for (Coin* c: bg->coinsOnBoard){
if (c->vel.getMagnitute() == 0){
i--;
}
}
if (!i){
prevState = STATE;
if(current_player==1) {
STATE = 1;
current_player = 0 ;
}
else{
STATE = 4;
current_player = 1 ;
}
}
break;
}
}
if(STATE == 4 )
{
setStrikerPosition(current_player);
prevState = 4 ;
srand(time(0));
int randpos = rand()%(459-141+1) +141;
if(striker->pos.getX()<randpos){
striker->pos.x+=0.02 ;
}
else
{ int randcoin = rand()%(bg->coinsOnBoard.size()-1)+1;
cout<<randcoin<<endl ;
Coin* c = bg->coinsOnBoard[randcoin] ;
Vector v = c->pos.sub(striker->pos) ;
cout<<v.getMagnitute()<<endl ;
striker->vel = v.normalize();
striker->vel.set(striker->vel.getX()/6, striker->vel.getY()/6);
STATE = 3;
cout<<"Should fire\n" ;
}
}
}
void Game:: updatescr(){
for (Coin* c : bg->coinsOnBoard){
c->Update(2 * c->radius, 2 * c->radius);
}
if(flag)
{
ima->Update(2*ima->radius, 2*ima->radius);
}
bg->UpdateBoard(600,600);
}
void Game:: renderscr(){
SDL_RenderClear(renderer);
bg->Render();
for (Coin* c : bg->coinsOnBoard){
c->Render();
}
if(flag)
{
ima->Render();
}
//Rendering score for player1
std::string newstr = "Player1: " + std::to_string(player_total);
surface_message = TTF_RenderText_Solid(font, newstr.c_str(), Black);
if (surface_message == NULL)
{
printf("Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError());
}
Message = SDL_CreateTextureFromSurface(renderer, surface_message);
if (Message == NULL)
{
printf("Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError());
}
SDL_FreeSurface(surface_message);
Message_rect.x = 600;
Message_rect.y = 450;
Message_rect.w = 200;
Message_rect.h = 100;
//Rendering score for player2
std::string newstr2 = "Computer: " + std::to_string(cpu_total);
surface_message2 = TTF_RenderText_Solid(font, newstr2.c_str(), Black);
if (surface_message2 == NULL)
{
printf("Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError());
}
Message2 = SDL_CreateTextureFromSurface(renderer, surface_message2);
if (Message2 == NULL)
{
printf("Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError());
}
SDL_FreeSurface(surface_message2);
Message_rect2.x = 600;
Message_rect2.y = 50;
Message_rect2.w = 200;
Message_rect2.h = 100;
SDL_RenderCopy(renderer, Message, NULL, &Message_rect);
SDL_RenderCopy(renderer, Message2, NULL, &Message_rect2);
SDL_DestroyTexture(Message);
SDL_DestroyTexture(Message2);
//Add more stuff to render here
SDL_RenderPresent(renderer);
}
void Game:: cleanscr(){
//SDL_DestroyTexture(Message);
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
std::cout<<"Window and game ded\n";
}
void Game:: setHighScore(float score){
cout<<"SetHigh "<<score<<"\n" ;
std::ofstream ofile("Leaderboard.txt");
if (score > highestScore){
ofile << score;
}
else{
ofile << highestScore;
}
}
float Game:: getHighScore()
{
return highestScore;
}
void Game:: displayHighScore(){
std::ifstream ifile("Leaderboard.txt");
if (!ifile.is_open()){
std:: cout<<"Could not fetch leaderboard" << std::endl;
return;
}
cout<<" displayHighScore";
ifile >> highestScore;
cout<<highestScore;
}
bool Game :: rungame(){return run;}