C++ SDL Reaction Time Game -
me , friend needs create reaction time game. like this.
right managed show image of red button, need how make hitbox, if click red button, becomes green.
would show how?
we using sdl, guess that's important mention.
here our code far:
#include <sdl/sdl.h> void plot(sdl_surface *sur, int x, int y, sdl_surface *dest) { sdl_rect rect = {x, y}; sdl_blitsurface(sur, null, dest, &rect); } sdl_surface *loadimage(const char *filename) { sdl_surface *sur = null; sur = sdl_loadbmp(filename); if(sur == null) { printf("img not found"); } sdl_surface *opsur = null; if(sur != null) { opsur = sdl_displayformat(sur); sdl_setcolorkey(opsur, sdl_srccolorkey, 0xffffff); if(opsur != null) sdl_freesurface(sur); } return opsur; } int main(int argc, char **argv) { sdl_init(sdl_init_everything); sdl_surface *screen = sdl_setvideomode(640, 480, 32, sdl_swsurface); sdl_wm_setcaption("eksamensprojekt", null); sdl_event event; bool running = true; sdl_surface *sur = loadimage("red.bmp"); while(running) { while(sdl_pollevent(&event)) { if(event.type == sdl_quit) running = false; } sdl_fillrect(screen, &screen->clip_rect, 0x000000); plot(sur, 215, 140, screen); sdl_flip(screen); } }
you can use sdl_rect hit box. can use sdl's own event handling system checking when mouse button clicked , position of it. need check if mouse position within sdl_rect.
you can read more sdl here. so... little on way. have main loop , pull events.
if ( event.type == sdl_mousebuttondown ){ //get mouse coordinates int x = event.motion.x; int y = event.motion.y; //if mouse on button if( checkspritecollision( x, y ) ){ // yay, hit button dothings(); } else { // d'oh missed } }
add while, @ least started.
Comments
Post a Comment