-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
195 lines (156 loc) · 4.22 KB
/
Game.cpp
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
#include "Game.hpp"
Game::Game(string title, int width, int height)
{
sc_wdth = width;
sc_hth = height;
Events ev;
AI ai;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_CreateWindowAndRenderer(sc_wdth, sc_hth, 0, &win, &ren);
SDL_SetWindowTitle(win, title.c_str());
game_running = true;
count = 0; // In-game Clock
selection = 0;
// Set Starting values
team = true;
event = 0;
bee_score = 0;
wasp_score = 0;
paddle_speed = sc_hth / 16;
player_paddle_y = (sc_hth / 2) - (sc_hth / 8);
tmp_player_paddle_y = 0;
loop(); // Begin Game.
}
Game::~Game()
{
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
}
void Game::loop()
{
while (game_running)
{
last_frame = SDL_GetTicks();
static int last_time;
if (last_frame >= (last_time + 1000))
{
last_time = last_frame;
frame_count = 0;
}
render();
input();
update();
if(event > 3)
{
game_running = false;
}
}
}
void Game::render()
{
// Add images to the screen:
/// Read Screen:
ev.getScreenParameters(sc_hth, sc_wdth);
ai.getParams(sc_hth, sc_wdth, paddle_speed, team, event, bee_paddle_y, wasp_paddle_y);
// /// Adds the background color:
SDL_RenderClear(ren);
ev.p.createBackground(*ren);
// /// Adds in the boarder Tulips:
ev.p.installTulips(*ren);
// /// Decides what to show:
ev.callText(*ren, event);
ev.showPoint(*ren, event, bee_score, wasp_score);
// /// Add Paddels and Ball:
ai.playBall(*ren, ball_x, ball_y, player_paddle_y);
// /// Push images to screen:
SDL_RenderPresent(ren);
// Determins Rendering Operation
frame_count++;
int timer_fps = SDL_GetTicks() - last_frame;
if (timer_fps < (1000 / 60)) // Counts/Measures time.
{
SDL_Delay((1000 / 60) - timer_fps); // Determines frame rate...in a round about sort of way.
}
SDL_RenderClear(ren);
}
void Game::input()
{
SDL_Event e;
while(SDL_PollEvent( &e) != 0)
{
if(e.type == SDL_QUIT)
{
game_running = false;
}
else
{
switch (e.key.keysym.sym)
{
case SDLK_ESCAPE:
game_running = false;
break;
case SDLK_f:
SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
SDL_GetWindowSize(win, &sc_wdth, &sc_hth);
break;
case SDLK_SPACE:
event++; // You can quit the game (in addition to passing the title) by hitting the space bar.
break;
case SDLK_w:
team = false;
event++; // Cheating prevention.
break;
case SDLK_b:
team = true;
event++;
break;
case SDLK_y:
if(event == 3)
{
event = 0; // Reset game
bee_score = 0;
wasp_score = 0;
selection = 0;
team = false;
}else
{
event++;
}
break;
case SDLK_n:
game_running = false;
break;
case SDLK_UP:
tmp_player_paddle_y = 0 - paddle_speed; // Inverted axis.
break;
case SDLK_DOWN:
tmp_player_paddle_y = 0 + paddle_speed;
default:
break;
}
}
}
}
void Game::update()
{
int ev_dif = event - selection;
if(ev_dif >= 2)
{
event = event - 1;
}else
{
event = event;
}
paddle_ball = ai.paddle_ball_loc;
bee_paddle_y = paddle_ball[0];
wasp_paddle_y = paddle_ball[1];
player_paddle_y = paddle_ball[2] + tmp_player_paddle_y;
ball_x = paddle_ball[3];
ball_y = paddle_ball[4];
selection = event;
score = ev.callPoint(bee_score, wasp_score, ball_x, *ren);
bee_score = score[0];
wasp_score = score[1];
event = ev.callEndGame(bee_score, wasp_score, event);
}