Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
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 |
#include <SDL.h> #include <time.h> #include <cstdlib> #define enemy_height 32 #define enemy_width 32 #define bullet_height 12 #define bullet_width 6 const int MAX_BULLETS = 50; const int MAX_ENEMYS = 50; SDL_Surface* ScreenSurface =NULL; SDL_Surface* window = NULL; SDL_Surface* Background = NULL; SDL_Surface* Player = NULL; SDL_Surface* Enemy = NULL; SDL_Surface* Bullet = NULL; SDL_Rect posPlayer, posBullet, posEnemy; SDL_Rect dstrect; SDL_Event event; bool quit = false; class thePlayer { public: thePlayer(); void player_movement(); void show_player(); private: }; //----------------------------- class theBullet { public: theBullet(); bool isActive; int x_position; int y_position; void bullet_movement(); void add_new_bullet(); void show_bullet(); private: }; theBullet arrayofBullets[MAX_BULLETS]; //----------------------------- class theEnemy { public: theEnemy(); bool EnemyisActive; int x_Enemy; int y_Enemy; void enemy_movement(); void show_enemy(); private: }; theEnemy arrayofEnemy[MAX_ENEMYS]; //----------------------------- theBullet::theBullet() { /*posBullet.x; posBullet.y; posBullet.w = 10; posBullet.h = 15;*/ } void theBullet::bullet_movement() { /*if(keystate[SDL_SCANCODE_SPACE]) { posBullet.x = posPlayer.x + 25; posBullet.y = posPlayer.y + 10; } posBullet.y -= 2; if(posBullet.y < 0) { posBullet.y = -50; }*/ } void theBullet::show_bullet() { SDL_BlitSurface(Bullet, NULL, ScreenSurface, &posBullet); } //----------------------------- theEnemy::theEnemy() { x_Enemy = 1; y_Enemy = -300; } void theEnemy::enemy_movement() { for (int i=0; i<MAX_ENEMYS; i++) { if (arrayofEnemy[i].EnemyisActive == true) { arrayofEnemy[i].y_Enemy += 1; if (arrayofBullets[i].y_position > 600) { arrayofEnemy[i].EnemyisActive = false; } } } } void theEnemy::show_enemy() { for (int i=0; i<MAX_ENEMYS; i++) { if (arrayofEnemy[i].EnemyisActive == true) { SDL_Rect dstrect2; dstrect2.x = arrayofEnemy[i].x_Enemy; dstrect2.y = arrayofEnemy[i].y_Enemy; dstrect2.w = Bullet->w; dstrect2.h = Bullet->h; SDL_BlitSurface(Enemy, NULL, ScreenSurface, &dstrect2); SDL_SetColorKey(Enemy, SDL_TRUE, SDL_MapRGB(Enemy->format, 255, 0, 255)); } } } //----------------------------- thePlayer::thePlayer() { posPlayer.x = 400; posPlayer.y = 300; posPlayer.w = 20; posPlayer.h = 30; } void thePlayer::player_movement() { while (SDL_PollEvent(&event)) { switch(event.type) { case SDL_QUIT: quit= true; break; } if(event.type == SDL_MOUSEMOTION) { posPlayer.x = event.motion.x; posPlayer.y = event.motion.y; } if(event.type == SDL_MOUSEBUTTONDOWN) { for (int i=0; i<MAX_BULLETS; i++) { if (arrayofBullets[i].isActive == false) { arrayofBullets[i].x_position = posPlayer.x + 25; arrayofBullets[i].y_position = posPlayer.y + 10; arrayofBullets[i].isActive = true; break; } } } }//end of poll_event } void thePlayer::show_player() { SDL_BlitSurface( Player, NULL, ScreenSurface,&posPlayer); SDL_SetColorKey(Player, SDL_TRUE, SDL_MapRGB(Player->format, 255, 0, 255)); } //----------------------------- int main(int argc, char** argv) { SDL_Init(SDL_INIT_VIDEO); SDL_Window * window = SDL_CreateWindow("SDL TestGame", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, 0); ScreenSurface = SDL_GetWindowSurface(window); Background= SDL_LoadBMP("background.bmp"); Player = SDL_LoadBMP("ship.bmp"); Bullet = SDL_LoadBMP("shot.bmp"); Enemy = SDL_LoadBMP("enemy.bmp"); thePlayer myPlayer; theEnemy myEnemy; srand(time(NULL)); //makes all bullets false for (int i=0; i<MAX_BULLETS; i++) { arrayofBullets[i].isActive = false; } //create random pos and active the enemy for (int i=0; i<MAX_ENEMYS; i++) { arrayofEnemy[i].x_Enemy += rand()%800+1; arrayofEnemy[i].y_Enemy += rand()%200+27; arrayofEnemy[i].EnemyisActive = true; } //GAME LOOP while (quit == false) { SDL_BlitSurface(Background, NULL, ScreenSurface, NULL); myPlayer.show_player(); myPlayer.player_movement(); //draw the enemy .. myEnemy.show_enemy(); myEnemy.enemy_movement(); //update game objects for (int i=0; i<MAX_BULLETS; i++) { if (arrayofBullets[i].isActive == true) { arrayofBullets[i].y_position -= 2; if (arrayofBullets[i].y_position < 0) { arrayofBullets[i].isActive = false; } } } for (int i=0; i<MAX_BULLETS; i++) { if (arrayofBullets[i].isActive == true) { dstrect.x = arrayofBullets[i].x_position; dstrect.y = arrayofBullets[i].y_position; dstrect.w = Bullet->w; dstrect.h = Bullet->h; SDL_BlitSurface(Bullet, NULL, ScreenSurface, &dstrect); } } for (int i=0; i<MAX_ENEMYS; i++) { if (arrayofEnemy[i].EnemyisActive == true) { if ((((arrayofBullets[i].x_position + bullet_width >= arrayofEnemy[i].x_Enemy) && (arrayofBullets[i].x_position + bullet_width <= arrayofEnemy[i].x_Enemy + enemy_width)) || ((arrayofEnemy[i].x_Enemy + enemy_width >= arrayofBullets[i].x_position) && (arrayofEnemy[i].x_Enemy + enemy_width <= arrayofBullets[i].x_position + bullet_width))) && (((arrayofBullets[i].y_position + bullet_height >= arrayofEnemy[i].y_Enemy) && (arrayofBullets[i].y_position + bullet_height <= arrayofEnemy[i].y_Enemy + enemy_height)) || ((arrayofEnemy[i].y_Enemy + enemy_height >= arrayofBullets[i].y_position) && (arrayofEnemy[i].y_Enemy + enemy_height <= arrayofBullets[i].y_position + bullet_height)))) { arrayofBullets[i].isActive = false; arrayofEnemy[i].EnemyisActive = false; } } } SDL_UpdateWindowSurface(window); } SDL_DestroyWindow(window); SDL_FreeSurface(Background); SDL_FreeSurface(Player); SDL_FreeSurface(Bullet); SDL_FreeSurface(Enemy); } |
C-/C++-Quelltext |
|
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 |
if ((((enemy_bullets[i].pos_x + ebullet_width >= ship_pan.pos_x) && (enemy_bullets[i].pos_x + ebullet_width <= ship_pan.pos_x + player_width)) || ((ship_pan.pos_x + player_width >= enemy_bullets[i].pos_x) && (ship_pan.pos_x + player_width <= enemy_bullets[i].pos_x + ebullet_width))) && (((enemy_bullets[i].pos_y + ebullet_height >= ship_pan.pos_y) && (enemy_bullets[i].pos_y + ebullet_height <= ship_pan.pos_y + player_height)) || ((ship_pan.pos_y + player_height >= enemy_bullets[i].pos_y) && (ship_pan.pos_y + player_height <= enemy_bullets[i].pos_y + ebullet_height)))) { players_health -= 10; snd_play(playerhit_wav, 90, 0); ship_pan.bmap = shiphit_tga; reset(enemy_bullets[i], VISIBLE); wait (3); ship_pan.bmap = ship_tga; break; // get out of the while loop } wait (1); } enemy_bullets[i] = NULL; } |
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Patty1991« (23.11.2014, 12:19)
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Nein, weil Du sie wenigstens konsequent so schreibst. Schön ist es dennoch nicht, dass manche Anfangsbuchstaben bei zusammengesetzten Wörtern klein und manche groß sind. Wie gesagt: "arrayOfEnemy" statt "arrayofEnemy".groß und kleinschreibung nicht beachtet ? da hätte mein Compiler aber gemeckert (4)
Macht gar keiner. Ich habe lediglich aufgezählt, was mir auffällt.bitte nicht aufregen
Nein, weil Du sie wenigstens konsequent so schreibst. Schön ist es dennoch nicht, dass manche Anfangsbuchstaben bei zusammengesetzten Wörtern klein und manche groß sind. Wie gesagt: "arrayOfEnemy" statt "arrayofEnemy".
Zitat von »LetsGo«
Der RCCSWU (RandomCamelCaseSomtimesWithUndersquare) Stil bricht auch mal mit den veralteten strukturieren Denkmustern und erlaubt dem Entwickler seine Kreativität zu entfalten.
Zitat
Der RCCSWU (RandomCamelCaseSomtimesWithUndersquare) Stil bricht auch mal mit den veraltet strukturierten Denkmustern und erlaubt dem Entwickler seine Kreativität zu entfalten.
C-/C++-Quelltext |
|
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 |
for (int i=0; i<MAX_BULLETS; i++) { if (arrayofBullets[i].isActive == true) { dstrect.x = arrayofBullets[i].x_position; dstrect.y = arrayofBullets[i].y_position; dstrect.w = Bullet->w; dstrect.h = Bullet->h; SDL_BlitSurface(Bullet, NULL, ScreenSurface, &dstrect); } } for (int i=0; i<MAX_ENEMYS; i++) { if (arrayofEnemy[i].EnemyisActive == true) { if ((((arrayofBullets[i].x_position + bullet_width >= arrayofEnemy[i].x_Enemy) && (arrayofBullets[i].x_position + bullet_width <= arrayofEnemy[i].x_Enemy + enemy_width)) || ((arrayofEnemy[i].x_Enemy + enemy_width >= arrayofBullets[i].x_position) && (arrayofEnemy[i].x_Enemy + enemy_width <= arrayofBullets[i].x_position + bullet_width))) && (((arrayofBullets[i].y_position + bullet_height >= arrayofEnemy[i].y_Enemy) && (arrayofBullets[i].y_position + bullet_height <= arrayofEnemy[i].y_Enemy + enemy_height)) || ((arrayofEnemy[i].y_Enemy + enemy_height >= arrayofBullets[i].y_position) && (arrayofEnemy[i].y_Enemy + enemy_height <= arrayofBullets[i].y_position + bullet_height)))) { arrayofBullets[i].isActive = false; arrayofEnemy[i].EnemyisActive = false; } } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
int i2; ... for (int i=0; i<MAX_ENEMYS; i++) { if ((((arrayofBullets[i2].x_position + bullet_width >= arrayofEnemy[i].x_Enemy) && (arrayofBullets[i2].x_position + bullet_width <= arrayofEnemy[i].x_Enemy + enemy_width)) || ((arrayofEnemy[i].x_Enemy + enemy_width >= arrayofBullets[i2].x_position) && (arrayofEnemy[i].x_Enemy + enemy_width <= arrayofBullets[i2].x_position + bullet_width))) && (((arrayofBullets[i2].y_position + bullet_height >= arrayofEnemy[i].y_Enemy) && (arrayofBullets[i2].y_position + bullet_height <= arrayofEnemy[i].y_Enemy + enemy_height)) || ((arrayofEnemy[i].y_Enemy + enemy_height >= arrayofBullets[i2].y_position) && (arrayofEnemy[i].y_Enemy + enemy_height <= arrayofBullets[i2].y_position + bullet_height)))) { arrayofBullets[i2].isActive = false; arrayofEnemy[i].EnemyisActive = false; } } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
for(int i=0; i!=MAX_BULLETS;i++) { if bullets[i].active == true { for(int j=0; j!=MAX_ENEMIES; j++) { if enemies[j].active == true { if collision(bullets[i], enemies[j]) { bullets[i].active = enemies[j] = false; // break; // Je nach Anwendungsfall... } } } } } |
Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von »Sucker« (29.11.2014, 19:48)
Werbeanzeige