// this sample program demonstrates arrays // the trick to using arrays is to initialize them in the initialization block // Set up system variables int window_width = 400; int window_height = 300; string window_title = "gpl Breakout"; int animation_speed = 95; int paddle_increment = 30; int computer_score = 0; int user_score = 0; int row; int col; int i; int block_size; int ball_x_increment = 5; int ball_y_increment = 2; int paddle_width = 5; int paddle_height = 40; int ball_size = 10; rectangle blocks[200]; // declare the animation handler functions forward animation ball_animate(circle cur_ball); // create a rectangle that will be used for the paddle rectangle paddle(x = window_width/10, y = window_height/2, w = paddle_width, h = paddle_height); // text boxes for showing score textbox computer_score_textbox(x = 2, y = 2, text = "computer: 0"); textbox user_score_textbox(x = 3*window_width/4, y = 2, text = "user: 0"); circle ball(x = window_width/2, y = window_height/2, radius = ball_size/2, animation_block = ball_animate); initialization { // initialize all the blocks so they are in the correct place i = 0; block_size = window_height/20; for (row = 0; row < 20; row += 1) { for (col = 0; col < 10; col += 1) { blocks[i].h = block_size; blocks[i].w = block_size; blocks[i].x = window_width - (col + 1)*block_size; blocks[i].y = row * block_size; i += 1; } } // print some instructions for the user print("Use all four arrow keys to move paddle around window"); print("Try to prevent the ball from hitting the left side of the window"); print("If too fast/slow for your computer, change the animation speed variable"); } // animation handler for the ball animation ball_animate(circle cur_ball) { // if ball has reached the left wall, reverse its direction if (cur_ball.x < 0) { ball_x_increment = -ball_x_increment; computer_score += 1; computer_score_textbox.text = "computer: " + computer_score; } // if ball has reached either the top or bottom, reverse its direction if (cur_ball.y < 0 || cur_ball.y > window_height - ball_size) ball_y_increment = -ball_y_increment; // if ball touches the paddle, reverse its direction if (cur_ball touches paddle) { ball_x_increment = -ball_x_increment; ball_y_increment = 0; } for (i = 0; i < 200; i+=1) { if (blocks[i].visible && cur_ball touches blocks[i]) { blocks[i].visible = false; ball_x_increment = -ball_x_increment; i = 200; // break out of loop ball_y_increment = (random(6) - 3) * 2; user_score += 1; user_score_textbox.text = "user: " + user_score; } } // on each step of the animation, move the ball cur_ball.x += ball_x_increment; cur_ball.y += ball_y_increment; } // when the user presses the up arrow, move the paddle up on uparrow { if (paddle.y < window_height - paddle_increment) paddle.y += paddle_increment; } // when the user presses the down arrow, move the paddle down on downarrow { if (paddle.y > 0) paddle.y -= paddle_increment; } on leftarrow { if (paddle.x > window_width/10) paddle.x -= paddle_increment; } on rightarrow { if (paddle.x < window_width/2) paddle.x += paddle_increment; }