//------------------------------------------------------------------------------ // Tutorial // Subject: Pong // Author: Roelf Leenders(el.moog@gmail.com) // Started: 4-2-2005 (12:18:43h) //------------------------------------------------------------------------------ program tutorial; global score[1]; //Graphics: gr_ball; gr_bat; //GP Keys __A = _control; __B = _alt; __SELECT = _space; __START = _enter; __R = _tab; __L = _backspace; begin //initialize screen: set_title("Serie Tutorial by Roelf Leenders"); full_screen = false; set_mode(m320x240); //Create graphics gr_ball = new_map(10,10,8); map_clear(0,gr_ball,13); gr_bat = new_map(15,40,8); map_clear(0,gr_bat,13); //Create a ball process ball(); //Create two bat processes bat(20,_up,_down); bat(300,__L,__R); //Write the current score on screen write(0,160,20,4,"-"); write_int(0,100,20,4,&score[0]); write_int(0,220,20,4,&score[1]); loop //If key start is pressed quit the game if(key(__START)) exit("",0); end frame; end end /* Process of a typical ball in a Pong game. Despite being square it bounces off bats and the screen edge, and detects when a point has been made and when it it bounced back off a bat. */ Process ball(); private //Speed of the ball speedo; //Holds the bat's process identification number in case it collides with one bat_id; begin //Initialize start values for most variables //*position x = 160; y = 120; //*angle angle = rand(0,360) * 1000; //*ball speed speedo = 500; //*graphic of the process graph = gr_ball; loop //Makes it bounce off the top and bottom of the screen(0 and 240, but giving //the ball a 5 pixel radius) if(y<5 or y>235) //Incoming angle is outgoing angle! angle = -angle; end //if off the screen on the left(point for player with score[0] if(x<0) //Add one to the score score[0]++; //Reset speed to standard speedo = 500; //reset Position y = 120; x = 220; end //Same for the right screen edge, only with the other bat scoring if(x>320) score[1]++; speedo = 500; y = 120; x = 100; end //If it hits a bat if(bat_id = collision(type bat)) angle = -angle + 180000; advance(3); end //Slowly increase the speed speedo+=3; //Move the ball in it's current direction advance(speedo/100); frame; end end /* A typical bat in a Pong game. It can move up and down with the keys specified in the keyup and keydown parameters. x specifies the horizontal position on screen. */ Process bat(x,keyup,keydown) begin //Start in the middle of the screen y = 120; //use the graphic made for the bat graph = gr_bat; loop //Move the bat vertically if buttons are pressed if(key(keyup)) y -= 12; end if(key(keydown)) y += 12; end //Limit the bat movement on the edges of the screen if(y < 20) y=20; end if(y > 220) y=220; end frame; end end