/** * Zufallspositionsgenerator mit Überprüfung. * Liefert eine Zufällige Position innerhalb des Spielfeldes, * jedoch *nicht* auf der Schlange drauf. * @return liefert eine zufällige Position zurück */ location randomize_food() { location food; do { food = randomize_location(); } while(check_snake_collision(food)); return food; }
void game_run() { int x, y, a, b, c; unsigned short padbuf; while(speed_counter > 0) { scc++; if(vibration_cntdown > 0) { printf("vibration = %d\n", vibration_cntdown); pad_enable_vibration(0); pad_set_vibration(0, 0xFF, 0x80); vibration_cntdown--; if(vibration_cntdown == 0) pad_set_vibration(0, 0, 0); } PSX_ReadPad(&padbuf, NULL); if(scc == 5 && !game_over) { if(snake_dir <= SNAKE_DIR_RIGHT) { if(padbuf & PAD_UP) snake_dir = SNAKE_DIR_UP; if(padbuf & PAD_DOWN) snake_dir = SNAKE_DIR_DOWN; } else { if(padbuf & PAD_LEFT) snake_dir = SNAKE_DIR_LEFT; if(padbuf & PAD_RIGHT) snake_dir = SNAKE_DIR_RIGHT; } for(y = 0; y < 29; y++) for(x = 0; x < 40; x++) { if(snake_array[y][x] == snake_size) { switch(snake_dir) { case SNAKE_DIR_LEFT: b = y; a = x-1; break; case SNAKE_DIR_RIGHT: b = y; a = x+1; break; case SNAKE_DIR_UP: b = y-1; a = x; break; case SNAKE_DIR_DOWN: b = y+1; a = x; break; } c = check_snake_collision(a,b); if(c) { snake_array[b][a] = snake_size+1; if(c==2) { snake_size++; score+=100; //printf("%d\n", score); SsKeyOn(2); new_apple(); } } else { vibration_cntdown = 10; game_over = 1; SsKeyOff(0); SsKeyOn(1); scc = 0; } if(snake_array[y][x] == 1 && c!=2) snake_array[y][x] = 0; goto out_of_collision_checking; } } out_of_collision_checking: for(y = 0; y < 29; y++) for(x = 0; x < 40; x++) if(snake_array[y][x]&&snake_array[y][x]<0x80&&c!=2) snake_array[y][x]--; scc = 0; } else if(game_over) { scc++; if(scc >= 510) { if((padbuf & PAD_CROSS) && !cross_pressed) { game_setup(); cross_pressed = 1; } else if((padbuf & PAD_CIRCLE) && !circle_pressed) { circle_pressed = 1; GsSetVideoMode(320, 240, pal_or_ntsc_selection()); game_setup(); } if(!(padbuf & PAD_CROSS)) cross_pressed = 0; if(!(padbuf & PAD_CIRCLE)) circle_pressed = 0; } } speed_counter--; } if(screen_old) { game_rect.x = 0; game_rect.y = 0; game_rect.w = 320; game_rect.h = 240; game_rect.r = 0; game_rect.g = 0; game_rect.b = 0; game_rect.attribute = 0; GsSortRectangle(&game_rect); game_sprite.x = 0; game_sprite.y = 0; game_sprite.w = 256; game_sprite.h = 240; game_sprite.u = 0; game_sprite.v = 0; game_sprite.r = NORMAL_LUMINOSITY; game_sprite.g = NORMAL_LUMINOSITY; game_sprite.b = NORMAL_LUMINOSITY; game_sprite.tpage = 5; game_sprite.attribute = COLORMODE(COLORMODE_16BPP); GsSortSimpleSprite(&game_sprite); game_sprite.x += 256; game_sprite.w = 64; game_sprite.tpage = 9; GsSortSimpleSprite(&game_sprite); game_rect.w = 8; game_rect.h = 8; game_rect.attribute = ENABLE_TRANS | TRANS_MODE(0); for(y = 0; y < 29; y++) { for(x = 0; x < 40; x++) { game_rect.x = x * 8; game_rect.y = y * 8; if(snake_array[y][x] >= 1 && snake_array[y][x] <= 0x7F) { game_rect.r = 0; game_rect.g = 255; game_rect.b = 0; GsSortRectangle(&game_rect); } else if(snake_array[y][x] == 0x80) { game_rect.r = 255; game_rect.g = 0; game_rect.b = 255; GsSortRectangle(&game_rect); } else if(snake_array[y][x] == 0x81) { game_rect.r = 0; game_rect.g = 0; game_rect.b = 128; GsSortRectangle(&game_rect); } } } sprintf(string_buf, "SCORE: %d", score); game_print(string_buf, 0, 232); if(game_over) { game_rect.w = 320; game_rect.h = 240; game_rect.x = 0; game_rect.y = 0; if(scc<=255)x=scc;else x=255; game_rect.r = x; game_rect.g = x; game_rect.b = x; game_rect.attribute = ENABLE_TRANS|TRANS_MODE(2); GsSortRectangle(&game_rect); if(scc>=300) { game_center_print("GAME OVER!", 160, 120); } if(scc>=420) game_center_print("WHAT DO YOU WANT TO DO NOW?", 160, 136); if(scc>=450) game_center_print("PRESS X TO RESTART THE GAME.",160, 152); if(scc>=480) game_center_print("PRESS O FOR PAL/NTSC SELECTION SCREEN.",160,168); if(scc>=510) game_center_print("MADE WITH PSXSDK BY GIUSEPPE GATTA, 2010", 160, 200); } GsDrawList(); // While the graphic synthesizer (video card) is drawing // just sleep. while(GsIsDrawing()); // Swap drawing and display Y position, and swap list array // to use. In this way we achieve a double buffer. if(game_dispenv.y == 0) { game_dispenv.y = 256; game_drawenv.y = 0; } else { game_dispenv.y = 0; game_drawenv.y = 256; } GsSetDispEnv(&game_dispenv); GsSetDrawEnv(&game_drawenv); screen_old = 0; } }