void keyDown(sf::Keyboard::Key keyCode) { switch(keyCode) { case sf::Keyboard::A: key_left = true; break; case sf::Keyboard::D: key_right = true; break; case sf::Keyboard::R: { initialiseGame(); break; } } }
/*MAIN MENU * Displays main menu choices*/ void showMenu() { Cell board[BOARD_HEIGHT][BOARD_WIDTH]; while (TRUE) { int choice; printf("\n"); printf("Welcome to Car Board \n"); printf("-------------------- \n"); printf("1. Play game \n"); printf("2. Show student's information \n"); printf("3. Quit \n\n"); choice = validateNumber(); if (choice == 1) { showCommands(); initialiseBoard(board); displayBoard(board, NULL); printf("load <g>\n"); printf("quit\n\n"); initialiseGame(); } if (choice == 2) { showStudentInformation(); } if (choice == 3) { printf("\n"); printf("Good Bye!\n\n"); break; } } }
int main() { window = new sf::RenderWindow(sf::VideoMode(RES_X, RES_Y), "I love this Game"); sf::Clock clock; float frameTime = 1/60.0f; float dTime = 0; initialiseGame(); while (window->isOpen()) { dTime += clock.getElapsedTime().asSeconds(); clock.restart(); // Event handling sf::Event event; while(window->pollEvent(event)) { processEvent(event); } // Safeguard (slowdown) to prevent game from lagging to death if (dTime > 5*frameTime) dTime = 5*frameTime; // Update game while (dTime > frameTime) { dTime -= frameTime; updateGame(); } // Draw frame window->clear(); drawGameFrame(); window->display(); } delete window; return 0; }
int main(int argc, char* args[]) { SDL_Event event; int start; int x, y; int row, col; ALIEN_TYPE type; bool dirChanged; // Start SDL SDL_Init(SDL_INIT_EVERYTHING); // Set up screen screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_SWSURFACE); // Title-bar caption SDL_WM_SetCaption(WINDOW_TITLE, NULL); // Set the keyboard repeat rate SDL_EnableKeyRepeat(KEY_REPEAT, KEY_REPEAT); // Initialise the fonts TTF_Init(); // Load the freeSans font font = TTF_OpenFont("/usr/share/fonts/truetype/freefont/FreeSans.ttf", 18); if (font == NULL) { printf("TTF_OpenFont() Failed: %s\n", TTF_GetError()); TTF_Quit(); SDL_Quit(); exit(1); } // Create our ship ship = new Ship(); // Create the shields shield[0] = new Shield(102, 380); shield[1] = new Shield(240, 380); shield[2] = new Shield(378, 380); shield[3] = new Shield(516, 380); // Create the aliens for (row = 0; row < 5; row++) { if (row == 0) { type = ALIEN_TYPE_TOP; } if ((row == 1) || (row == 2)) { type = ALIEN_TYPE_MIDDLE; } if ((row == 3) || (row == 4)) { type = ALIEN_TYPE_BOTTOM; } for (col = 0; col < 5; col++) { aliens[row][col] = new Alien((SCREEN_WIDTH/2-16)+((32+ALIEN_GAP_X)*col), 10+20+((20+ALIEN_GAP_Y)*row), 50, type); } } initialiseGame(); while (!quit) { start = SDL_GetTicks(); // Poll the keyboard while (SDL_PollEvent(&event)) { switch(event.type) { case SDL_QUIT: quit = true; break; case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_LEFT) { ship->moveLeft(true); } if (event.key.keysym.sym == SDLK_RIGHT) { ship->moveRight(true); } if (event.key.keysym.sym == SDLK_SPACE) { ship->fire(); } if (event.key.keysym.sym == SDLK_ESCAPE) { quit = true; } if (event.key.keysym.sym == SDLK_n) { if (gameOver) initialiseGame(); } paused = false; break; case SDL_KEYUP: if (event.key.keysym.sym == SDLK_LEFT) { ship->moveLeft(false); } if (event.key.keysym.sym == SDLK_RIGHT) { ship->moveRight(false); } } } if ((!gameOver) && !(lockout)) { // Update our ship and missile ship->missile->move(); ship->move(); if ((!paused) && (speed++ == alienSpeed)) { updateAliens(&alienSpeed, &alienDirection); speed = 0; } for (col=0; col < 5; col++) { for (row=0; row < 5; row++) { aliens[row][col]->missile->move(); } } collisionDetection(); for (y = 0; y < 5; y++) { for (x = 0; x < 5; x++) { aliens[x][y]->update(); } } ship->update(); if ((ship->isHit()) && (ship->visible == 0)) { ship->reset(); } // Check if game over if ((hits == 25) || (ship->getLives() == 0)) { gameOver = true; } // Clear the screen SDL_FillRect(screen, NULL,0); // Render the screen drawScreen(); // Update the screen SDL_Flip(screen); } if (lockout) { lockout--; } printf("------------------\n"); for (x = 0; x < 5; x++) { for (y = 0; y < 5; y++) { if (aliens[x][y]->isBomber()) { printf("Bomber: row %d col %d\n", x, y); } } } printf("------------------\n"); // Delay for the appropriate amount of time if (1000/FPS > (SDL_GetTicks() - start)) { SDL_Delay(1000/FPS - (SDL_GetTicks() - start)); } } printf("Exiting...\n"); // Free the loaded image SDL_FreeSurface(screen); // Quit SDL SDL_Quit(); return 0; }
//creates a game and iniatilises its components struct game *createGame( ) { struct game *gm = (struct game *) malloc( sizeof( struct game ) ); initialiseGame( gm ); return gm; }