140 likes | 436 Views
Lecture 5 Game Programming Game Input Lecture 5 Game Programming Game Input Computer Input Demo Programs
E N D
Lecture 5 Game Programming Game Input Lecture 5 Game Programming Game Input
Computer Input Demo Programs Keyboard Input Demo - Displays an open circle on screen. Circle can be moved using the arrow keys. The diameter of the circle can be changed using the Page-Up and Page-Down keys. Movement of circle is limited to keep circle on screen. Mouse Graphics Demo - The program demonstrates the use of the mouse in Allegro. Mouse movement moves a spot on the screen. Pressing the LEFT, RIGHT, or both mouse buttons changed the color of the spot. Gamepad Demo - A USB game controller must be plugged in for this program to function. Program detects the game pad and shows a simple graphical display indicating the number of joysticks (2-axis) and sliders (1-axis) controls, and the number of buttons including the front fire buttons. Program moves a yellow spot in screen, controlled by 1st 2-axis control, the size of the spot is changed by pressing button 7 and 8 and six different sound events are invoked by pressing buttons 1 through 6.
Keyboard Demo allegro_init(); install_keyboard();
x = max_x/2; y = max_y/2; while(!key[KEY_ESC]) { rest(30); xold = x; yold = y; rold = radius; if(key[KEY_UP]) y = y - 5; if(key[KEY_DOWN]) y = y + 5; if(key[KEY_LEFT]) x = x - 5; if(key[KEY_RIGHT])x = x + 5; if(key[KEY_PGUP]) radius = radius + 2; if(key[KEY_PGDN]) radius = radius - 2; if(xold!=x || yold!=y || rold!=radius) circle(screen,xold,yold,rold,BKGCOLOR); if(x>max_x-radius) x = max_x-radius; if(x<radius) x = radius; if(y>max_y-radius) y = max_y-radius; if(y<radius) y = radius; if(radius>max_y/2-1) radius = max_y/2-1; if(radius<2) radius = 2; circle(screen,x,y,radius,MAGENTA); }
Mouse Input Demo allegro_init(); install_mouse();
Mouse Location & Buttons while(!key[KEY_ESC]) { xold = x; yold = y; x = mouse_x; y = mouse_y; if(xold!=x || yold!=y) circlefill(screen,xold,yold,radius,BLACK); if((mouse_b & 1) && (mouse_b & 2)) circlefill(screen,x,y,radius,YELLOW); else if(mouse_b & 1) circlefill(screen,x,y,radius,LIGHT_GREEN); else if(mouse_b & 2) circlefill(screen,x,y,radius,LIGHT_RED); else circlefill(screen,x,y,radius,LIGHT_BLUE); textprintf_ex(screen,font,250,235,WHITE,-1,"Press <ESC> to Quit"); rest(20); }
Joystick Input Demo allegro_init(); install_joystick(JOY_TYPE_AUTODETECT);
Polling for Joysticks poll_joystick(); cout << "Number of Joysticks = " << num_joysticks << endl; cout << endl; for(n=0; n<joy[0].num_sticks;n++) { cout << "Name of stick " << n << " = " << joy[0].stick[n].name << endl; cout << "Number of axes = " << joy[0].stick[n].num_axis << endl; for(m=0;m<joy[0].stick[n].num_axis;m++) if(joy[0].stick[n].num_axis>1) cout << "Name for axis number " << m << " = " << joy[0].stick[n].axis[m].name << endl; } cout << endl; cout << "Number of Buttons " << joy[0].num_buttons << endl; for(k=0;k<joy[0].num_buttons;k++) cout << "Name of Button " << k << " = " << joy[0].button[k].name << endl;
Scanning Joystick Inputs while(!keypressed()) { //graphics loop rest(1000); poll_joystick(); for(n=0;n<joy[0].num_sticks;n++) for(m=0;m<joy[0].stick[n].num_axis;m++) { cout << joy[0].stick[n].axis[m].pos << " "; cout << joy[0].stick[n].axis[m].d1 << " "; cout << joy[0].stick[n].axis[m].d2 << " "; } for(k=0;k<joy[0].num_buttons;k++) cout << joy[0].button[k].b << " "; cout << endl; }
while(!keypressed()) { //graphics loop poll_joystick(); xold = x; yold = y; radius_old = radius; if(joy[0].stick[0].axis[0].pos<0) x = x - 2; if(joy[0].stick[0].axis[0].pos>0) x = x + 2; if(joy[0].stick[0].axis[1].pos<0) y = y - 2; if(joy[0].stick[0].axis[1].pos>0) y = y + 2; if(joy[0].button[6].b>0) radius = radius - 1; if(joy[0].button[7].b>0) radius = radius + 1; if(x>max_x) x = max_x; if(x<0) x = 0; if(y>max_y) y = max_y; if(y<0) y = 0; if(radius<2) radius = 2; if(radius>100) radius = 100; if(xold!=x || yold!=y || radius_old!=radius) circlefill(screen,xold,yold,radius_old,BLACK); circlefill(screen,x,y,radius,YELLOW);
for(n=0;n<joy[0].num_sticks;n++) for(m=0;m<joy[0].stick[n].num_axis;m++) { if(joy[0].stick[n].axis[m].pos<0) rectfill(screen,(n+1)*2*size, (m+1)*2*size, (n+1)*2*size+size, (m+1)*2*size + size, GREEN); else if(joy[0].stick[n].axis[m].pos>0) rectfill(screen,(n+1)*2*size, (m+1)*2*size, (n+1)*2*size+size, (m+1)*2*size + size, BLUE); else rectfill(screen,(n+1)*2*size, (m+1)*2*size, (n+1)*2*size+size, (m+1)*2*size + size, RED); } for(k=0;k<joy[0].num_buttons;k++) { if(joy[0].button[k].b==0) rectfill(screen,(k+1)*2*size,8*size, (k+1)*2*size+size,8*size + size,RED); else rectfill(screen, (k+1)*2*size,8*size, (k+1)*2*size+size,8*size + size,GREEN); textprintf_ex(screen,font,(k+1)*2*size+8, 8*size+6,BLACK,-1,"%d", k+1); if(joy[0].button[k].b>0 & k<5) play_num = k; }
textprintf_ex(screen,font,25,25,WHITE,-1,"Stick Slider"); textprintf_ex(screen,font,25,8*size-20,WHITE,-1,"Buttons"); if(play_count < 0) if(play_num > -1) { play_count = 50; play_sample(samples[play_num], volume, pan, pitch, FALSE); play_num = -1; rest(100); } if(play_count>=0) play_count -= 1; rest(10); }