Open courses at Stanford University--programming method Job 3__ programming

Source: Internet
Author: User
Tags gety

Job requirements and related introduction too much, directly affixed to the source code I wrote. However, there are some problems that are not fully understood. This can only be counted as an initial version.

Program Source code:

* * * File:Breakout.java *-------------------* Name: * Leader: * * This File would eventually implement the game of Breakout. * * Import acm.graphics.*; Import acm.program.*; Import acm.util.*; Import java.applet.*; Import java.awt.*; Import java.awt.event.*; public class Breakout extends Graphicsprogram {/** Width and height of application windows in pixels/public static FINA l int application_width = 400; public static final int application_height = 600; /** Dimensions of game board (usually the same) * * private static final int WIDTH = Application_width; private static final int HEIGHT = application_height; /** Dimensions of the paddle * * private static final int paddle_width = 60; private static final int paddle_height = 10; /** Offset of the paddle up from the bottom * * private static final int paddle_y_offset = 30; /** number of bricks per row/private static final int nbricks_per_row = 10; /** number of rows of bricks * private static final int nbrick_rows = 10; /** Separation between bricks * * private static final int brick_sep = 4; /** Width of a brick * * private static final int brick_width = (Width-(nbricks_per_row-1) * brick_sep)/Nbricks_per_ro W /** Height of a brick * * private static final int brick_height = 8; /** Radius of the ball in pixels * * private static final int ball_radius = 10; /** Offset of the top brick row from the top * * private static final int brick_y_offset = 70; /** number of turns * * private static final int nturns = 3; /* Method:run () * * * * */** runs the breakout program. */public void Run () {/* You are fill this in, along and any subsidiary methods/Gameinit (); GamePlay (); * * ********** * Function Name: Gameinit * Function: Initialize set game window size, * Draw bricks and bezel * *************************************** * * private void Gameinit () {//interface initialization/* SET Window Size * * * setSize (width + 8, height + 54);//////////////////////(+ =//////////// Glabel ("width:" + getwidth () + "Height:" + getheight (), 10,10)); * * Draw Bricks/brickinit (); /* DrawBaffle */Paddleinit (); /* Draw Ball/ballinit (); * * Add mouse Monitor Event/addmouselisteners (); * * * ****************************************** * Function name: Brickinit * Function: Bricks when drawing initial state * *********************************** * * private void Brickinit () {/* * The starting x coordinate of the bricks */double xpos = (Width-brick_width * nbricks_per_row-brick_sep * (N) BRICKS_PER_ROW-1))/2; Color color[] = {Color.red,color.orange,color.yellow,color.green,color.cyan}; for (int i=0 i < nbrick_rows;i++) {/* Brick y-coordinate */double y = brick_y_offset + i * (brick_sep + brick_height); for (int j=0 J < nbricks_per_row;j++) {* * The x coordinate of the bricks/double x = xpos + J * (Brick_sep + brick_width); Grect brick = new Grect (x,y,brick_width,brick_height); Brick.setfilled (TRUE); * * Brick Fill Color///brick.setcolor (COLOR[I/2]); if ((I/2) = = 0) {brick.setcolor (color.red);} else if ((I/2) = = 1) {Brick.setcolor (color.orange);} else if ((I/2) = 2 {Brick.setcolor (color.yellow);} else if ((I/2) = = 3) {Brick.setcolor (color.green);} else if ((I/2) = 4) {BRICK.setcolor (Color.cyan); Add (brick); }}/* ****************************************** * Function name: Addleinit * Function: Draw the initial state of the bezel * ******************************** * * private void Paddleinit () {Double xpos = (width-paddle_width)/2,//x coordinates yPos = Height-paddle_height-pa Ddle_y_offset; Y-coordinate paddle = new Grect (xpos,ypos,paddle_width,paddle_height); Paddle.setfilled (TRUE); Add (paddle); Add (New Glabel ("X:" + paddle.getx () + "Y:" + paddle.gety (), 10,10));} * * ****************************************** * Function name: Ballinit * Function: Draw the ball of the initial state and make the ball * in the Center of the window * ************************** */public void Ballinit () {Double xpos = WIDTH/2-Ball_radius,//initial x coordinate YPos = HEIGHT/2-Ball_radius;// Initial y-coordinate ball = new Goval (xpos,ypos,2 * ball_radius,2 * Ball_radius); Ball.setcolor (Color.Black); Ball.setfilled (TRUE); Add (ball); * * * ****************************************** * Function name: mousemoved * Function: Make bezel follow mouse move * parameter: e--Mouse event * ************** * * PubLIC void mousemoved (MouseEvent e) {paddle.move (E.getx ()-Paddle.getx (), 0); if (Paddle.getx () > (width-paddle_width)) {//If the right is out of bounds, position the bezel to the right edge Paddle.setlocation (Width-paddle_width, Paddle.gety ());} * * * ****************************************** * Function name: GamePlay * Function: Start the game process, and deal with the problems encountered in the game process * ******************** * * private void GamePlay () {/* Number of bricks/int bricknum = Nbricks_per_row * nbrick_rows; VX = Rgen.ne Xtdouble (-3.0, 3.0); while (!isgameover ()) {ball.move (VX, VY); pause (10); GObject collider = Getcollidingobject (); The object touched by the ball if (collider = = paddle) {vy =-vy;} else if (collider!= null) {//bounceclip.play (); vy =-vy; remove (collider); bricknum--; if (Bricknum = = 0) break; ///If the ball touches both sides of the boundary (Ball.getx () <= 0) | | (Ball.getx () + (2 * ball_radius) >= WIDTH)) {VX =-VX;} if (ball.gety () <= 0) {vy =-vy;}} if (bricknum!= 0) {/* game after over operation * * Gameover ();} else{/* Bricks have all been eliminated */aftersuccess ();} /* * ************************************** Function Name: Getcollidingobject * Function: Get the object that the ball encountered * return value: gobj--encountered objects * ******************************************/pri Vate GObject Getcollidingobject () {GObject gobj = null; gobj = Getelementat (Ball.getx (), ball.gety ()); if (gobj = null) {g obj = Getelementat (Ball.getx () + 2 * Ball_radius, ball.gety () + 2 * Ball_radius); } if (gobj = = null) {gobj = Getelementat (Ball.getx (), ball.gety () + 2 * Ball_radius);} if (gobj = null) {gobj = GetElement At (Ball.getx () + 2 * Ball_radius, ball.gety ()); return gobj; * * * ****************************************** * Function name: Isgameover * Function: To determine whether the game ended with failure * Note: Ball contact to the bottom of the window * ************** * * Private Boolean Isgameover () {return (Ball.gety () + 2 * Ball_radius) >= HEIGHT); * ****************************************** * Function name: Gameover * Function: operation after the failure of the game * ***************************************** * */private void Gameover () {RemoveAll (); Glabel over = the new Glabel ("Game over!"); Over.setfont ("Serif-36"); Over.setcolor (color.red); Add (Over, (Width-over.getwidth ())/2, (Height-over.getascent ())/2); * * * ****************************************** * Function name: aftersuccess * Function: The celebration interface after all bricks have been removed * ****************************** * * private void aftersuccess () {RemoveAll (); Glabel label = new Glabel ("SUCCESS!!!"); Label.setfont ("Serif-36"); Add (Label, (Width-label.getwidth ())/2, (Height-label.getascent ())/2); Grect su = new Grect (0,0,width,height); Su.setfilled (TRUE); for (int i=0;i < 5;i++) {Su.setfillcolor (Rgen.nextcolor ()), add (SU), Label.sendforward (); Pause (400);}} /* Private instance variable * * Grect paddle; Bezel private goval ball; Ball private Randomgenerator Rgen = Randomgenerator.getinstance (); /* The moving coordinates of the ball * * Private double vx,vy = 3.0; }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.