// Leben verloren void playerDead(Game *g) { freeShotList(g->enemyShots); g->enemyShots = NULL; if (g->player.shot != NULL) { free(g->player.shot); g->player.shot = NULL; } g->player.lives -= 2; // startNewLevel erhöht um 1 g->level--; g->enemyContainer.ufo.alive = false; g->enemyContainer.ufo.lastufo = ms_time(); // Spielerposition g->player.rect.x = WIDTH/2 - PLAYER_WIDTH/2; g->player.rect.y = PLAYER_Y_POS; g->player.rect.w = PLAYER_WIDTH; g->player.rect.h = PLAYER_HEIGHT; SDL_Rect area = {0, BORDER_TOP, WIDTH, HEIGHT - BORDER_TOP}; SDL_FillRect(g->screen, &area, SDL_MapRGB(g->screen->format, 0, 0, 0)); if (g->player.lives+2 == 0) { free(g->blocks); showGameOver(g); saveHighscore(g->score); showHighscore(g); SDL_Flip(g->screen); // Warten auf Tastendruck SDL_Event e; SDL_WaitEvent(&e); SDL_FillRect(g->screen, &area, SDL_MapRGB(g->screen->format, 0, 0, 0)); initGame(g); startNewLevel(g); return; } SDL_Flip(g->screen); usleep(100000); // Nur zeichen, daher None movePlayer(g, None); startNewLevel(g); }
void lostGame(){ display.Clear(); drawMap(); display.Render(); //====================Highscore if(score < hscore) if(score < hscore){ //sprintf (DEBUGMSG2, "You lose nub.\nScore: %i\nHighscore: %i",score,hscore); //MessageBox (0, DEBUGMSG2, "You Lost!", MB_ICONINFORMATION); } else{ hscore = score; saveHighscore(&score); // sprintf (DEBUGMSG2, "You just achieved a highscore\nHighscore: %i",hscore); //MessageBox (0, DEBUGMSG2, "Nice!!!", MB_ICONINFORMATION); } resetGame(); }
int main(void) { char* error = ""; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_FONT *font = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; HANDLE mutex = CreateMutex(NULL, FALSE, "Chapter3_CloseMutex-Mutex"); if (GetLastError() == ERROR_ALREADY_EXISTS) { MessageBoxA(GetForegroundWindow(), "Only one instance of the game can run at a time.", "Chapter3_CloseMutex", MB_OK); return 0; } do { loadHighscore(); if(!al_init()) { error = "failed to initialize allegro!"; break; } if(!al_init_primitives_addon()) { error = "failed to initialize primitives!"; break; } al_init_font_addon(); if(!al_init_ttf_addon()) { error = "failed to initialize ttfs!"; break; } if(!al_install_keyboard()) { error = "failed to initialize keyboard!"; break; } if(!(font = al_load_ttf_font("arial.ttf", 18, 0))) { error = "failed to initialize font!"; break; } if(!(timer = al_create_timer(1.0 / 20))) { error = "failed to initialize timer!"; break; } if(!(display = al_create_display(640, 480))) { error = "failed to initialize display!"; break; } if(!(event_queue = al_create_event_queue())) { error = "failed to initialize event_queue!"; break; } al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_start_timer(timer); while(1) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) break; else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) break; else if (ev.keyboard.keycode <= 255) keys[ev.keyboard.keycode] = true; } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { if (ev.keyboard.keycode <= 255) keys[ev.keyboard.keycode] = false; } else if (ev.type == ALLEGRO_EVENT_KEY_CHAR) keyboardEvent(); if (al_is_event_queue_empty(event_queue) && ev.type == ALLEGRO_EVENT_TIMER) drawFrame(font); } } while (0); if (mutex) CloseHandle(mutex); if (strlen(error) > 0) { al_show_native_message_box(NULL, NULL, "error", error, NULL, NULL); return -1; } al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(event_queue); saveHighscore(); return 0; }
int main(int argc, char *argv[]) { Game *g = (Game*) malloc(sizeof(Game)); SDL_Event event; Uint8 *keystates; Uint8 quit = false; long lastplayerupdate = ms_time(); long lastenemyupdate = ms_time(); long lastshotupdate = ms_time(); long lastufoupdate = ms_time(); srand((unsigned int) time(NULL)); // SDL initialisieren if (SDL_Init(SDL_INIT_VIDEO) == -1) { printf("Kann Video nicht initialisieren: %s\n", SDL_GetError()); exit(1); } atexit(SDL_Quit); g->screen = SDL_SetVideoMode(WIDTH, HEIGHT, 16, SDL_HWSURFACE); if (g->screen == NULL) { printf("Kann Video-Modus nicht festlegen: %s\n", SDL_GetError()); exit(1); } TTF_Init(); // Game initialisieren initGame(g); startNewLevel(g); updateScore(g); updateLives(g); showHighscore(g); updateBlocks(g); // Nächster Grafikzustand SDL_Flip(g->screen); // Loop while (!quit) { // SDL Events abfragen SDL_PollEvent(&event); // Tastenstatus laden keystates = SDL_GetKeyState(NULL); // Escape gedrückt -> beenden // TODO: Menü aufrufen statt beenden if (keystates[SDLK_ESCAPE]) { saveHighscore(g->score); quit = true; } // Nur wenn entweder Links oder Rechts, nicht beide zur selben Zeit if (keystates[SDLK_LEFT] != keystates[SDLK_RIGHT] && lastplayerupdate >= 100) { lastplayerupdate = ms_time(); // Links if (keystates[SDLK_LEFT]) { movePlayer(g, Left); } // Rechts if (keystates[SDLK_RIGHT]) { movePlayer(g, Right); } } if (keystates[SDLK_SPACE]) { shoot(g); } // UFO if (ms_time() - lastufoupdate >= UFO_UPDATE) { lastufoupdate = ms_time(); ufo(g); } // Alienposition aktualisieren? // Exponentialfunktion, die Level und Alienanzahl berücksichtigt if (ms_time() - lastenemyupdate >= ENEMY_UPDATE_BASE * pow(0.95, g->level * 3 + (ENEMY_COUNT - g->enemyContainer.aliveCount) / 4)) { lastenemyupdate = ms_time(); updateBlocks(g); moveEnemys(g); alienShot(g); } // Schüsse aktualisieren if (ms_time() - lastshotupdate >= SHOT_UPDATE) { lastshotupdate = ms_time(); updateShots(g); checkCollision(g); movePlayer(g, None); } usleep(20000); // begrenzt CPU Last // Nächster Grafikzustand SDL_Flip(g->screen); } SDL_Quit(); return 0; }
int main(int argc, char* argv[]) { SDL_Init(SDL_INIT_EVERYTHING); SDL_Window *window = SDL_CreateWindow("Bloxz", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 480, 480.f/(320.f/480.f), SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN); SDL_GL_CreateContext(window); bool done = 0; init(); int start = SDL_GetTicks(); while (!done) { SDL_Event e; while (SDL_PollEvent(&e)) { switch (e.type) { case SDL_QUIT: done = 1; break; case SDL_KEYDOWN: if (e.key.keysym.sym == SDLK_ESCAPE) done = 1; else if (menu == 2) { if (e.key.keysym.sym == SDLK_BACKSPACE) { name[--nameat] = 0; } if (e.key.keysym.sym == SDLK_RETURN) { menu = 1; playername = new char(strlen(name) + 2); strcpy(playername, name); addHighscore(); newlevel(); } else if (e.key.keysym.sym < 128 && e.key.keysym.sym>=32) { name[nameat++] = e.key.keysym.sym; name[nameat] = 0; } } break; case SDL_MOUSEBUTTONDOWN: { if (firing) break;; vec2 point(e.button.x, e.button.y); if (point.x<32 || point.x>320 - 32) { tx = -1; ty = (point.y - 30) / 32; } else if (point.y<62 || (point.y>316 && point.y < 390)) { ty = -1; tx = (point.x) / 32; } else { tx = ty = -1; } if (tx == 0 || tx == 9) tx = -1; if (ty == 0 || ty == 9) ty = -1; tim = 0; } break; case SDL_MOUSEMOTION: if (e.motion.state) { if (firing) break; vec2 point(e.motion.x, e.motion.y); if (point.x<32 || point.x>320 - 32) { tx = -1; ty = (point.y - 30) / 32; } else if (point.y<62 || (point.y>316 && point.y < 390)) { ty = -1; tx = (point.x) / 32; } else { tx = ty = -1; } if (tx == 0 || tx == 9) tx = -1; if (ty == 0 || ty == 9) ty = -1; } break; case SDL_MOUSEBUTTONUP: { if (firing) break; vec2 point(e.button.x, e.button.y); if (menu == 0) { if (point.x<32 && point.y>62 && point.y<316) fire(3, (point.y - 30) / 32, point.x); else if (point.x>32 * 9 && point.y > 62 && point.y<316) fire(1, (point.y - 30) / 32, point.x); if (point.y<62 && point.x>32 && point.x<316) fire(0, point.x / 32, point.y); else if (point.y>316 && point.x>32 && point.x < 32 * 9 && point.y<390) fire(2, point.x / 32, point.y); if (point.x<26 && point.y>454) { menu = 1; } } else if (menu == 1) { if (point.x>160 && gamemode != 0) menu = 0; else { if (point.y<330 && point.y>160) { menu = 0; if (point.y < 225) gamemode = 1; else if (point.y<280) gamemode = 2; else gamemode = 3; newlevel(); } if (point.y>360 && point.y<405) menu = 3; if (point.y>415 && point.y<465) { menu = 5; hst = 0; } } } else if (menu == 2) { if (point.y>320) { // if (textbox == nil) menu = 1; playername = new char(strlen(name) + 2); strcpy(playername, name); addHighscore(); newlevel(); } } else if (menu == 3) { if (point.x > 16 && point.x<48 && point.y>445 && point.y < 477) { hst--; if (hst<0) hst = 2; } else if (point.x>272 && point.x<304 && point.y>445 && point.y<477) { hst++; if (hst>2) hst = 0; } else if (point.x > 128 && point.x<192 && point.y>445 && point.y < 477) { menu = 1; } } else if (menu == 4)//gameover { name[0] = 0; strcpy(name, playername); nameat = strlen(name); menu = 2; } else if (menu == 5)//high { if (point.x<26 && point.y>454) { menu = 1; } else { hst++; if (hst > 7) menu = 1; } } printf("%f,%f\n", point.x, point.y); tx = ty = -1; } break; } } update(); SDL_GL_SwapWindow(window); int elapsed = (SDL_GetTicks() - start); SDL_Delay(max(0, 17 - elapsed)); start = SDL_GetTicks(); } saveHighscore(); SDL_Quit(); return 0; }