/ * March 2007 Update, author: Jia Shenghua, this non-latest version of the algorithm with multiple updates are not added,
There are any suggestions or opinions and contacts: xiajia_1981@163.com or QQ: 39970763 * /
/ / -------------------------------------------------------------------------------------------- ---------------------------------------------
/*Dz2.h file content is as follows * /
/ * Define the necessary structure * /
/ * Program default definition: The slowest design of the brick is 8 rows of 8 rows, the up to 64 bricks, adjacent bricks must be close, can't have a gap; the size of the ball cannot change in the game, in the game Before you start, you must determine the size of the ball, because changing the ball will cause unexpected collision; * / struct _point {double x; // horizontal coordinate Double Y; // longitudinal coordinate};
Struct _area {double xmin; double xmax; double ymin; double ymax;}
Struct _ball {double ballr; // Ball radius struct _point centerpoint; // Spherical heart coordinate short color; // Ball color short type; // Ball Type Double Radarph; // Motion direction angle: vertical upward exercise When this value is 0 Double Speed; // The motion speed int hitno; // is used to record bricks numbers} on the previous collision;
Struct _brick {double longth; // Brick Double HIGTH; / / Brick's high struct _Point CenterPoint; // Central coordinate short color; // Brick color short type; // Brick type Type -1: Indicates that the brick is invalid, that is, the brick does not participate in the collision judgment int hittimes; // brick's hit number}
Struct _baffle {double longth; // The length of the slope of the baffle Double HIGTH; / / Bronkeeper Struct _Point CenterPoint; // The center coordinate of the baffle SHORT color; / / The color short type; // baffle type INT HITTIMES; // Ball of the baffle INT MoveDirection; / / The moving direction of the baffle 0: No moving 1: move to the left 2: Move the Int Speed to the right;
Struct _wall {struct _Area area;
/ * Define the appropriate method * /
// ******************************************************** ******** // Judging whether the ball has collisions // ******************************************* *********************** INT ISINHITAREA (struct _ball ball, struct _ brick * brick, struct _baffle baffle, struct _wall wall, int * HitNumble, int Bricksum ); // Used to determine if the ball is in a collision range of a brick or wall / * parameter description: Ball ball information Brick brick information (bricks Array storage) Baffle baffle information WALL wall surface Information HITNUMBLE This is an array information for storing objects participating in the collision. The array is ended. 0-99: Participated in the collision of bricks 100: Baffle Participation Collision 101: Wall Participation BRICKSUM : The number of bricks in the game is returned to the number of participation in collision * /
// ******************************************************** ******* // Process of processing the ball with the corresponding brick collision / / (mainly calculating the sports route after the collision) / labeled the corresponding bricks) // ********** **************************************************** INT brickcrashproc (struct _ball * Ball, struct _brick * brick, int hitNumble); / * Parameter description: * Ball points the pointer to the ball, seeking the relevant information will be modified here * Brick pointing to the pointer of the bricks array, the brick information will be here The number of bricks called HitNumble returns to the collision to generate 0: No collision is performed 1: There is a collision to do * /// **************************************************** *************************** // Process // (mainly calculated collision) After the ball's motor route and baffle are labeled) // ******************************************** ***************** INT BAFFLECRASHPROC (struct _ball * ball, struct _baffle * baffle); / * Parameter description: * Ball points the pointer of the ball, the relevant information of the ball will Here, it is modified here * BAFFLE points to the pointer of the baffle, the information of the baffle will be modified here to return the collision 0: No collision is performed 1: There is a collision * /
// ******************************************************** ******* // Process the collision of the ball and the wall / (mainly calculated the mission of the ball) // ****************** ********************************************* INT WALLCRASHPROC (struct _ball * ball, struct _wall wall); / * Parameter Description: * Ball points the pointer of the ball, the relevant information of the ball will be modified here information returned whether the Wall wall is returned whether 0: No collision 1: There is a collision * /
// ******************************************************** ******* // Brave mobile function // ****************************************** ********************* VOID BAFFLEMOVELEFT (Struct _baffle * baffle, struct _wall wall); void bafflemoveright (struct _baffle * baffle, struct _wall wall); void bafflenomove Struct _baffle * baffle); / * Parameter description: * Baffle points to the pointer of the baffle, the information of the baffle will be modified here information * /
// ******************************************************** ******* // Change the function of the ball: Pay attention to the game process, the ball goes to be cautious, // is very likely to make a program error, because the ball will cause unpredictable collisions /// *********************************************************** ***** Void BallChangeSize (Struct_ball * Ball, Double R); / * Parameter Description: * Ball points the ball's pointer R changed to the radius * /
// ******************************************************** ******* // Change the speed of the ball // ******************************************* ********************* VOID BALLCHANGESPEED (STRUCT _BALL * BALL, DOUBLE SPEED); / * Parameter Description: * Ball points the speed of the ball to the sport * /// ************************************************************** ********* // change the length of the baffle // ******************************************** *********************** VOID BAFFLECHANGELONGTH (Struct _baffle * baffle, double longth, struct _wall wall); / * Parameter description: * Baffle pointing the baffle Pointer longth baffle changed the length of the Wall wall data * /
// ********************************** Mathematical processing function // ****** ************************************* DOUBLE ANGLECHANGE (Double X); / * Anglechange () Angle Function Square () Computing Square Function * /
// ************************* / / below is some functions of some depicting interfaces // ** ****************************************** VOID DRAWBRICK (STRUCT _BRICK BRICK); // Draw a brick VOID DRAWBALL ( Struct _ball ball); // depicts a ball Void DrawBaffle (STRUCT _BAFFLE BAFLE); // Depicts the baffle Void Drawall (Struct _wall wall); // Depicts wall // 檫 图 操作 操作 操作 k k k;; Depicting a brick void killdrawball (struct _ball ball); // depicts a ball void killdrawbaffle (struct _baffle baffle); // depicts the baffle Void KillDrawWall (Struct _wall wall); // depict the wall
//*********************************************//the following Is the programmatic function // ***************************************************** * void Erract (); / * erroRProc () error handler * /
// --------------------------------------------
/ * The code file content is as follows * /
#include
Main () {// Definition related variable struct _wall wall; struct _baffle baffle; struct _ brick brick [64]; struct _ball ball [2]; int hitNumble [64]; int in, j, k, l; // cycle Variables or temporary variables, his value does not make long distance transfer char presskey = '0'; // system variable, used for operation of the entire game system int systemState = 1; // System tag int systemTime = 0; // Whole game System's rhythm control variable file * pf; int systemcode = 0; // Used to record system score Char code [10];
// Data number initialization INT MODE = VGAHI, DRIVER = VGA; RegisterBgidriver (Egavga_Driver); Initgraph (& Driver, & Mode, "// TC // BGI"); // Graphical interface initialization
While ((pf = fopen ("game.txt", "r")) == null) // Open file {printf ("File Can not open!"); getCh (); exit (0);}
// Read the game data STAR: / / This is the game's entry point wall.area.xmin = 10; Wall.Area.xmax = 410; Wall.area.ymax = 410;
BAFFLE.CenterPoint.x = (Wall.Rea.xmin Wall.Area.Xmax) / 2.0; baffle.centerpoint.y = Wall.Area.ymax - 20; baffle.color = 7; baffle.higth = 6; baffle. HitTimes = 0; baffle.longth = 400; baffle.type = 1; baffle.movedirection = 0; baffle.Speed = 10;
For (i = 0; i <64; i ) {brick [i] .type = -1; // First mark all bricks 无} for (i = 0; i <8; i ) {for (j = 0; j <8; j ) {brick [i * 8 j] .centerpoint.x = Wall.area.xmin j * 50 25; brick [i * 8 j] .centerpoint.y = Wall.area .Ymin i * 16 10 30; brick [i * 8 j] .color = i 1; brick [i * 8 j] .higth = 16; brick [i * 8 j] .hittimes = 0; brick [i * 8 j] .longth = 50; if (j! = 3) {brick [i * 8 j] .type = i 1; // is initialized bricks defined as effective IF (i == 4) {brick [i * 8 j] .type = 10; brick [i * 8 j] .color = 10;}}}}
#ifdef game brick [30] .COLOR = 11; Brick [30] .type = 11; brick [25] .COLOR = 12; brick [25] .TYPE = 12; brick [50] .color = 13; brick 50] .Type = 13; #ndifball [0] .ballr = 5; ball [0] .Centerpoint.x = baffle.centerpoint.x; ball [0] .centerpoint.y = baffle.centerpoint.y - baffle.higth / 2.0 - ball [0] .ballr -1; ball [0] .COLOR = 2; ball [0] .radarph = 0; ball [0] .SPEED = 1; ball [0] .TYPE = 1; // 1: No launch, 2: Ball [1] .TYPE = 0; ball [0] .hitno = -1; ball [1] .hitno = -1;
ClearDevice ();
// Display the game control key setColor (1); OutTextxy (Wall.area.xmin 40, Wall.area.ymax 20, "Star: 9 Left: 4 Right: 5 Stop: 7 quit: Q"); setColor 6); OutTextxy (Wall.area.xmin 40, Wall.area.ymax 20, "9 4 5 7 Q"); setColor (2); Outtextxy (Wall.area.xmax 40, Wall.area.ymin 20, "Your Code:"); setColor (1); // Calculated score code [0] = systemcode / 10000 48; code [1] = (systemcode% 10000) / 1000 48; code [2] = (SystemCode% 1000) / 100 48; Code [3] = (SystemCode% 100) / 10 48; Code [4] = (SystemCode% 10) 48; Code [5] = '/ 0'; OUTTEXTXY Wall.area.xmax 120, Wall.area.ymin 20, Code);
FSCANF (PF, "% D,% D,% D;" & I, & J, & K); // read the ball's attribute IF (I> 20) i = 20; if (i <2) i = 2; Ball [0] .ballr = i; if (j> 8) j = 8; if (j <1) j = 1; ball [0] .SPEED = J / 4.0; if (k <1) k = 1; IF (k> 16) k = 16; ball [0] .COLOR = K; ball [0] .Centerpoint.y = baffle.centerpoint.y - baffle.higth / 2.0 - ball [0] .ballr -1; fscanf (PF, "% D,% D,% D;" & I, & J, & K); // read the ball's attribute IF (i <10) i = 10; if (i> 150) i = 150; BAFFLE .Longth = i; if (j <3) j = 3; if (j> 100) j = 100; baffle.speed = j; if (k <1) k = 1; if (k> 16) k = 16 ; Baffle.color = k; // read the properties of the bricks below (i = 0; i <8; i ) {fscanf (PF, "% 2D,% 2D,% 2D,% 2D,% 2D,% 2D,% 2D,% 2D; "& Brick [i * 8 0] .type, & brick [i * 8 1] .type, & brick [i * 8 2] .type, & brick [i * 8 3 ] .TYPE, & Brick [i * 8 4] .Type, & Brick [i * 8 5] .type, & brick [i * 8 6] .type, & brick [i * 8 7] .type); for (j = 0; j <8; j ) {// Here you check the rationality IF (Brick [i * 8 j] .type! = - 1 && (brick [i * 8 j] .type <1 || Brick [i * 8 j] .type> 15)) Brick [i * 8 j] .type = -1; brick [i * 8 j] .color = brick [i * 8 j]. The initial interface for (i = 0; i <64; i ) {drawbrick (brick [i]); // depicts a ball DrawBaffle (BAFFLE); // Depict the baffle DrawWall (Wall); // depict the wall
// The following enters the main game cycle for (;;) {// read keyboard operation if (kbhit ()) {presskey = getCH ();} else {presskey = '0';} // Reset the stop of the baffle The direction is set to have no movement bafflenomove (& BAFFLE);
/ / Here you can perform the winning determination J = 1; for (i = 0; i <64; i ) {if (brick [i] .type> = 1 && brick [i] .type <= 9) {j = 0 Break;}} f (j == 1) {// Win setColor (15); Outtextxy ((Wall.Area.xmin Wall.area.xmax) / 2, (wall.area.ymin wall.area. Ymax) / 2, "win"); getch (); goto star; // Re-start the game // Break; // Exit game} if (Ball [0] .Centerpoint.Y> BAFFLE.CenterPoint.y) {/ / Lose SetColor (15); Outtextxy ((Wall.Area.xmin Wall.Area.Xmax) / 2, (Wall.Area.ymin wall.area.ymax) / 2, "LOSE"); getch (); // goto star; // Re-start the game break; // exit the game} if (Ball [1] .Centerpoint.Y> BAFFLE.CenterPoint.Y && ball [1] .type! = 0) {// Set the ball Invalid killdrawball (ball [1]); ball [1] .type = 0;
Switch (PressKey) {case '4': {if (systemState == 1) {// Brush off the original image killdrawbaffle (BAFFLE); BAFFLEMOVELEFT (& BAFFLE, WALL); // Left shift operation for (i = 0; I <2; i ) {if (ball [i] .type == 1) {killdrawball (ball [i]); ball [i] .centerpoint.x = baffle.centerpoint.x; Drawball (Ball [i]) ;}} / * Drawing operation * / Drawbaffle (BAFFLE);} Break;} case '5': {if (systemState == 1) {// Brush off the original image KillDrawBaffle (BAFFLEMOVERIGHT (& Baffle, Wall ); // right shift operation for (i = 0; i <2; i ) {if (ball [i] .Type == 1) {killdrawball (ball [i]); ball [i] .centerpoint.x = BAFFLE.CenterPoint.x; Drawball (Ball [i]);}} / * Drawing operation * / drawbaffle (baffle);} Break;} case '7': // Pause Operation {IF (SystemState! = 0) {SystemState = 0; setColor (15); OUTTEXTXY (Wall.Rea.xmin Wall.area.Xmax) / 2, (Wall.Area.ymin wall.area.ymax) / 2, "Pause");} else { SYSTEM State = 1; setfillstyle (1,0); bar (Wall.Area.xmin Wall.area.Xmax) / 2, (wall.area.ymin wall.area.ymax) / 2 - 5, (Wall. Area.xmin Wall.area.xmax) / 2 40, (Wall.Area.ymin Wall.Area.ymax) / 2 6);} Break;} Case '9': // Emits the ball to operate { For (i = 0; i <2; i ) {if (Ball [i] .type == 1 && systemState == 1) {ball [i] .Type = 2; ball [i] .radarph = pi / 4 ;}}} Break;} Case 'Q': // Pause Case 'Q': {Return 0;} Default :;} IF (SystemState == 1 &&
SystemTIME% 3 == 0) {// Operation in non-suspended state for (L = 0; L <2; L ) IF (Ball [L] .Type == 2) {// Current coordinate KilldRawball ( Ball [l]); ball [l] .Centerpoint.x - = Ball [l] .speed * sin (ball [l] .radarph); ball [l] .centerpoint.y = gravity - ball [l]. Speed * COS (Ball [L] .radarph); // Drawn the motion trajectory PUTPIEL (Ball [L]. Centerpoint.x, Ball [L]. Centerpoint.y, 1); Drawball (Ball [L] ); // This prevents the image defect of the baffle DrawBaffle (BAFFLE); // Collision judgment K = 0; IF (Isinhitarea (Ball [L], Brick, Baffle, Wall, HitNumble, 64)> 0) {for (i = 0 ;; i ) {IF (HitNumble [i] == - 1) Break; if (HitNumble [i] <64) {// collision processing J = brick [HitNumble [i]]. HitTimes; IF (brickcrashproc & Ball [L], Brick, HitNumble [I])) {K ; Ball [L] .hitno = HitNumble [i];} if (j! = Brick [HitNumble [i]]. HitTimeS && brick [HitNumble [i] ] .Type <10) {brick [HitNumble [I]]. Color ---; if (brick [HitNumble [i]]. Color> = 17) brick [HitNumble [i]]. Color = 1;} if (brick [HitNumble [i]]. HitTimes == brick [HitNumble [i ]]]]]]. Type && brick [HitNumble [i]]. Type! = 10) {systemcode = 5; // Score Brick [HitNumble [i]]. Type = -1;} // ******* *********************************************************** ********* // The bricks for special features will be performed here (brick [hitNumble [i]]. HitTimes> = 1 && brick [HitNumble [i]]. Type == 11) {// If hitting a brick block that makes the ball, enter the small operation brick [HitNumble [i]]. Type = -1; systemcode = 5; // Score KillDrawball (Ball [L]); ballchangesize (& Ball [L], Ball [L] .ballr / 2.0); Drawball (Ball [L]);} if (brick [hitNumble [i]]. HitTimes>
= 1 && brick [HitNumble [i]]. Type == 12) {// If hit is a brick brick brick brick (HitNumble [i]]. Type = -1; systemcode = 5; // Scored BallChangespeed (& Ball [L], Ball [L] .Speed * 2);} if (brick [hitNumble [i]]. Hittimes> = 1 && brick [HitNumble [i]]. Type == 16) {/ / If hit is a brick brick brick (HitNumble [i]]. Type = -1; systemcode = 5; // Score BallChangespeed (& Ball [L], Ball [L] .speed * 0.5) } If (brick [hitNumble [i]]. HitTimes> = 1 && brick [HitNumble [i]]. Type == 13) {// hitting a brick block for changing the length of the baffle KilldrawBaffle (BRICK); brick [HitNumble [I]]. Type = -1; systemcode = 5; // Score BaffleChangelongth (& BAFFLE, BAFFLE.LONGTH * 0.7, Wall); Drawbaffle (BAFFLE);} if (brick [hitNumble [i]]. HitTimes > = 1 && brick [HitNumble [i]]. Type == 17) {// hitting a brick killdrawbaffle (BRICK [HitNumble [i]]. Type = -1; systemcode = 5; // Score BaffleChangelongth (& Baffle, Baffle.longth * 1.429, Wall); DrawBaffle (BAF FLE);} f (brick [hitNumble [i]]. HitNumble [I]]. type == 14) {// Return to re-transmit status Killdrawball (Ball [L]); ball [ l] .centerpoint.x = baffle.centerpoint.x; ball [l] .centerpoint.y = baffle.centerpoint.y - baffle.higth / 2.0 - ball [l] .ballr -1; ball [l] .type = 1; // 1: Before the transmission, 2: After transmitting // Ball [l] .speed = 0; Drawball (Ball [L]); brick [HitNumble [i]]. Type = -1; systemcode = 5; // Score} IF (brick [HitNumble [i]]. HitTimes> = 1 &&
Brick [HitNumble [I]]. Type == 15) {// hit bricks that generate a new ball, make BRICK [HitNumble [i]]. TYPE = -1; systemcode = 5; // Score Ball [1] .ballr = Ball [0] .ballr * 0.7; ball [1] .Centerpoint.x = Ball [0] .Centerpoint.x; ball [1] .centerpoint.y = ball [0] .centerpoint.y; ball [1] .COLOR = 5; ball [1] .radarph = anglechange (ball [0] .radarph pi); ball [1] .speed = ball [0] .speed; Ball [1] .Type = BALL [0] .Type; Drawball (Ball [1]);} // Redraw bricks IF (brick [HitNumble [i]]. Type! = -1) Drawbrick (brick [HitNumble [I]]); Else Killdrawbrick (brick [HitNumble [I]]);} if (HitNumble [i] == 100) {// Bronkeed Collision IF (BAFFLECRASHPROC (& Ball [L], & BAFFLE)) {K ; Ball [l] .hitno = 100;}}}}}} (HitNumble [i] == 101) {// Wall collision IF (WallcrashProc (& Ball [L], Wall)) {K ; Ball [L] .hitno = 101 ;}}}}}}}}}} {// The following displays // Check out the previous text set Fi Llstyle (1,0); bar (Wall.area.xmax 120, Wall.area.ymin 20 - 5, Wall.area.xmax 120 40, Wall.area.ymin 20 6); setColor 1); // Calculated fraction code [0] = systemcode / 10000 48; code [1] = (systemcode% 10000) / 1000 48; code [2] = (systemcode% 1000) / 100 48; code [ 3] = (SystemCode% 100) / 10 48; Code [4] = (SystemCode% 10) 48; Code [5] = '/ 0'; Outtextxy (Wall.area.xmax 120, Wall.area. Ymin 20, code);} else {// If there is no collision, mark -1 Ball [l] .hitno = -1;}}} delay (1); systemTIME ; if (SystemTime> =
1000) SystemTIME = 0;} return 0;}
Double Square (double x) {double y = x * x; return y;
Double Anglechange (Double X) {// Case of Hairline FOR (;;) {IF (x <2.0 * pi) Break; ELSE X- = 2.0 * Pi;} for (;;) {IF (x <0 ) X = 2.0 * pi; else break;} returnix}
Void ErrerProc () {Printf ("THE DATA Errer, Press Any Key To Quit!); getCh (); exit (0);} / * Under implementation of all methods * / int isinhitarea (struct _ball ball, struct _brick * Brick, struct _baffle baffle, struct _wall wall, int * HitNumble, INT bricksum) {INT i, j; // loop parameter int iscrash = 0; struct _Area crasharea; // Validity judgment IF (bricksum <= 0) ErractoC ( ); // Data initialization HitNumble [0] = -1; // - 1 is a flag / / calculating the ball and bricks (here, the next step will not crash) Ball.CenterPoint .x - = ball.speed * sin (ball.radarph); ball.centerpoint.y = gravity - ball.speed * cos (ball.radarph); crasharea.xmin = ball.centerpoint.x - Ball.ballr - brick [0] .longth / 2.0; crasharea.xmax = ball.centerpoint.x ball.ballr brick [0] .longth / 2.0; // 1; crasharea.ymin = ball.centerpoint.y - ball.ballr - Brick [0] .higth / 2.0; crasharea.ymax = ball.centerpoint.y ball.ballr brick [0] .higth / 2.0; // 1; // Judgment ball will not collide with bricks J = 0; for (i = 0; I
// Judgment the ball will not crash with the baffle CraSharea.xmin = Ball.Centerpoint.x - ball.ballr - baffle.longth / 2.0; crasharea.xmax = ball.centerpoint.x ball.ballr baffle.longth / 2.0; // 1; crasharea.ymin = ball.centerpoint.y - ball.ballr - baffle.higth / 2.0; crasharea.ymax = ball.centerpoint.h ball.ballr baffle.higure / 2.0; // 1; if (baffle.centerpoint.x> = crasharea.xmin && baffle.centerpoint.x <= crasharea.xmax&& baffle.centerpoint.y> = crasharea.ymin && baffle.centerpoint.Y <= CraSharea.ymax && ball. Hitno! = 100) {// HitNumble in the collision area [J] = 100; // This is the special tag J for the baffle; HitNumble [J] = -1;}
IF (j! = 0) iscrash = j; // Record the number of times possible
/ / Judgment the ball will not collide with the wall crasharea.xmin = ball.Centerpoint.x - ball.ballr; // - 2; crasharea.xmax = ball.centerpoint.x ball.ballr; // 2; CraSharea .Y - ball.ballr; // - 2; crasharea.ymax = ball.centerpoint.y ball.ballr; // 2;
IF (Wall.Area.xmin> = CRASHAREA.XMIN || Wall.area.xmax <= crasharea.xmax || wall.area.ymin> = crasharea.ymin) {// HitNumble in the collision zone [J] = 101 ; // This is a special mark for the wall, where there is no need to repel the judgment J ; HitNumble [J] = -1;}
IF (j! = 0) iscrash = j; // Record the number of times possible
Return iscrash;}
// Ball and brick collision Function Int brickcrashproc (struct _ball * ball, struct _brick * brick, int hitNum) {INT I, J, K; // cycle variable int istrue = 0; double anglel = 0; struct _ball lball Lball.centerpoint.x = ball-> centerpoint.x - ball-> speted * sin (ball-> radarph); lball.centerpoint.y = ball-> centerpoint.y - ball-> speed * cos (ball-> Radarph) gravity; // First type collision judgment (in the range of spheres in the range of brick) IF ((lball.centerpoint.x> = brick [hitNumble]. Centralpoint.x - brick [hitNumble] .longth / 2.0 && Lball.centerpoint.x <= brick [hitNumble] .Centerpoint.x brick [HitNumble] .longth / 2.0) || (lball.centerpoint.y> = brick [hitNumble] .Centerpoint.y - brick [hitNumble] .higth / 2.0 &&lball.centerpoint.y <= brick [hitNumble] .Centerpoint.y brick [HitNumble] .higth / 2.0)) {// This type of collision is not judged, directly entering collision processing // level Collision IF (lball.centerpoint.x> = brick [hitNumble] .Centerpoint.x - brick [HitNumble] .longth / 2.0 && lball.centerpoint.x <= brick [HitNumble] .Centerpoint.x brick [hitNumble] .longth / 2.0) {if (ball-> radarph <= pi) ball-> radarph = pi - ball-> radarph; else ball-> radarp H = 3 * pi - ball-> radarph;} else {ball-> radarph = 2 * pi - ball-> radarph;} ball-> radarph = anglechange (ball-> radarph); brick [hitNumble] .hittimes ; istrue = 1; #ifdef testGetch (); # Endif} Else {// Class 2 collision judgment (vertex to the spherical distance is almost equal to the radius of the ball. It is worth noting that the following angle formula is not strict) IF (Square (Brick [hitNumble]. Centerpoint.x - brick [hitNumble] .longth / 2.0 - lball.centerpoint.x) Square (brick [hitNumble] .Centerpoint.y - brick [hitNumble] .higth / 2.0 - lball.centerpoint. Y) - Square (ball-> ballr) <= 0.01 && ((HitNumble <8 && HitNumble! =
0 && brick [HitNumble-1] .Type == -1) || (HitNumble> 7 && HitNumble% 8! = 0 && brick [HitNumble-1] .Type == -1 && brick [HitNumble-8] .type == -1))))) // Need to meet no adjacent bricks {// left upper corner of true anglel = Atan (brick [hitNumble]. Centerpoint.x - brick [hitNumble] .longth / 2.0 - lball .Centerpoint.x) / (brick [hitNumble] .centerpoint.y - brick [hitNumble] .higth / 2.0 - lball.centerpoint.y)); ball-> radarph = anglechange (2 * anglel - ball-> radarph pi ); Brick [hitNumble] .hittimes ; istrue = 1; #ifdef testGetch (); # Endif} else if (square (brick [hitNumble) .Centerpoint.x brick [hitNumble] .longth / 2.0 - lball.centerpoint.x Square (brick [hitNumble] .centerpoint.y - brick [hitNumble] .higth / 2.0 - lball.centerpoint.y) - Square (ball-> ballr) <= 0.01 && ((HitNumble <8 && HitNumble! = 7 & Brick [HitNumble 1] .TYPE == -1) || (HitNumble> 7 && HitNumble% 8! = 7 && brick [HitNumble 1] .Type == -1 && brick [HitNumble-8] .type = = -1))))) // Need to meet the collision of the neighboring brick {// upper right corner as true anglel = Atan ((lball.centerpoint.x - brick [hitNumble] .Centerpoint.x - brick [hitNumble]. L ONGTH / 2.0) / (brick [hitNumble]. Centerpoint.y - brick [HitNumble] .higth / 2.0 - lball.centerpoint.y)); ball-> radarph = anglechange (pi-2 * anglel-ball-> radarph) Brick [HitNumble] .hittimes ; istrue = 1; #ifdef testGetch (); # Endif} else if (square) .Centerpoint.x - brick [hitNumble] .longth / 2.0 - lball.centerpoint.x) Square (brick [hitNumble]. Centerpoint.y brick [HitNumble] .higth / 2.0 - lball.centerpoint.y) - Square (ball-> ballr) <= 0.01 && ((HitNumble> 55 &&)
HitNumble! = 0 && brick [HitNumble-1] .Type == -1) || (HitNumble <56 && HitNumble% 8! = 0 & brick [HitNumble-1] .Type == -1 && brick [HitNumble 8 ] .Type == -1))))) // Need to meet no adjacent bricks {// upper left corps into true anglel = Atan ((brick [hitNumble]. Centerpoint.x - brick [hitNumble] .longth / 2.0 - lball.centerpoint.x) / (lball.centerpoint.y - brick [hitNumble] .Centerpoint.y - brick [HitNumble] .higth / 2.0); ball-> radarph = anglechange (pi-2 * anglel-ball -> Radarph); brick [hitNumble] .hittimes ; istrue = 1; #ifdef testGetch (); # Endif} else if (square (brick [hitNumble]. Centerpoint.x brick [hitNumble] .longth / 2.0 - lball. Centerpoint.x) Square (brick [hitNumble] .Centerpoint.y brick [HitNumble] .higth / 2.0 - lball.centerpoint.y) - Square (ball-> Ballr) <= 0.01 && ((HitNumble> 55 && HitNumble ! = 7 && brick [HitNumble 1] .Type == -1) || (HitNumble <56 && HitNumble% 8! = 7 && brick [HitNumble 1] .type == -1 && brick [HitNumble 8] .Type == -1))))) // Need to meet no adjacent brick {// upper right corner of true anglel = Atan ((lball.centerpoint.x - brick [HitNumble]. Centralpoint.x - brick [ Hit Numble] .longth / 2.0) / (lball.centerpoint.y - brick [hitNumble]. Centerpoint.y - brick [HitNumble] .higth / 2.0); ball-> radarph = anglechange (2 * anglel - ball-> radarph Pi); brick [hitNumble] .hittimes ; istrue = 1; #ifdef testGetch (); # endif}} return istrue;}
// ******************************************************** ******************* // The following function is used to process the collision problem of the ball and the baffle, here is a unregulated process // 1, the baffle only processes The upper corner and the upper side are rebounded // 2, in order to prevent the processing of multiple collisions, the collision of the ball and the baffle is only performed under the right angle // ************** *********************************************************** *** int BaffrashProc (struct _ball * Ball, struct _baffle * baffle) {INT I, J, K; // cycle variable int istrue = 0; double anglel = 0; struct _ball lball; lball.centerpoint.x = BALL- > Centerpoint.x - ball-> speed * sin (ball-> radarph); lball.centerpoint.y = ball-> centerpoint.y - ball-> speed * cos (ball-> radarph) gravity; // all about about The barrier and the ball collision must satisfy the ball in the upper IF of the baffle (lball.centerpoint.y <= baffle-> centerpoint.y - baffle-> hardth / 2.0) // Class 1 collision judgment (spherical heart In the range of bricks) IF ((lball.centerpoint.x> = baffle-> centerpoint.x - baffle-> longth / 2.0 && lball.centerpoint.x <= baffle-> centerpoint.x baffle-> longth / 2.0 ) || (lball.centerpoint.y> = baffle-> centerpoint.y - baffle-> hardth / 2.0 && lball.centerpoint.y <= baffle-> centerpoint.y baffle-> hgment / 2.0)) {// This type of collision is not judged, directly into the collision processing // level collision (noteworthy: for the diversity of the game, it is not completely followed by physical ejection rules ..., // but to be more ejection Different reflection angles with different reflection angles) IF (lball.centerpoint.x> = baffle-> centerpoint.x - baffle-> longth / 2.0 && lball.centerpoint.x <= baffle-> centerpoint.x baffle-> Longth / 2.0) {// The following is introduced into the relationship between distance and angle if (ball-> radarph> = 0.5 * pi && ball-> radarph <= pi) {ball-> radarph = pi - ball-> radarph; if (LBall .Centerpoint.y
(Pi / 2.0 - ball-> radarph) * 0.3;} if (baffle-> movedirection == 2) {ball-> radarph * = 0.7;}} baffle-> HitTimes ; istrue = 1;} else {IF (Ball -> Radarph> = Pi && ball-> radarph <= 1.5 * pi) {ball-> radarph = 3 * pi - ball-> radarph; if (lball.centerpoint.y
Centerpoint.y - baffle-> hift / 2.0 - lball.centerpoint.y)); ball-> radarph = anglechange (2 * anglel - ball-> radarph pi); baffle-> hittimes ; istrue = 1;} else if (Square (baffle-> centerpoint.x baffle-> longth / 2.0 - lball.centerpoint.x) Square (baffle-> centerpoint.y - baffle-> hardth / 2.0 - lball.centerpoint.y) - Square (Ball -> Ballr) <= 0.01 && (Ball-> Radarph> 0.5 * Pi && Ball-> Radarph <1.5 * pi)) {// Collision in the upper right corner is true anglel = atan ((lball.centerpoint.x - baffle-> Centerpoint.x - baffle-> longth / 2.0) / (baffle-> centerpoint.y - baffle-> hardth / 2.0 - lball.centerpoint.y)); ball-> radarph = anglechange (pi-2 * anglel-ball- > Radarph); baffle-> hittimes ; istrue = 1;}} Return istrue;}
INT WallcrashProc (struct _ball wall) {int istrue = 0; struct _ball lball; lball.centerpoint.x = ball-> centerpoint.x - ball-> speed * sin (ball-> radarph); lball .. Centerpoint.y = ball-> centerpoint.y - ball-> speted * cos (ball-> radarph) gravity; // left wall IF (lball.centerpoint.x - ball-> ballr <= Wall.area.xmin 2) {ball-> radarph = 2 * pi - ball-> radarph; istrue = 1;} // Right wall IF (lball.centerpoint.x ball-> ballr> = Wall.area.xmax - 2) {Ball -> Radarph = 2 * pi - ball-> radarph; istrue = 1;} // on the wall IF (lball.centerpoint.y - ball-> ballr <= Wall.area.ymin 2) {ife (ball-> Radarph <= pi) ball-> radarph = pi - ball-> radarph; else ball-> radarph = 3 * pi - ball-> radarph; istrue = 1;} // In the game to avoid horizontal or vertical movement, below Some of them is used to avoid this part of IF (Square (ball-> radarph - pi / 2.0) <0.001) {IF (ball-> radarph
Void Drawbrick (struct _ brick brick) {int x = brick.centerpoint.x - brick.longth / 2.0; int y = brick.centerpoint.y - brick.higth / 2.0; int SizX = brick.longth - 1; int sizy = Brick.higth - 1; if (brick.type == -1) return; setColor (15); / * Best of white * / line (x, y, x sizx-1, y); line (x , y 1, x sizx-2, y 1); line (x, y, x, y sizy-1); line (x 1, y, x 1, y sizy-2); SetColor (8); / * It is best to use dark gray * / line (x 1, y sizy, x sizx, y sIzy); line (x 2, y sizy-1, x sizx, Y SIZY-1); line (x sizX-1, y 1, x sizx-1, y sizy); line (x sizx, y, x sizx, y sIZY); setColor (7 ); / * It is best to use gray * / putpixel (x, y sizy, 3); putpixel (x 1, y sizy-1, 3); PUTPIXEL (X Siz, Y, 3); PUTPIEL ( X SIZX-1, Y 1, 3); SetFillStyle (1, brick.color); / * It is best to use gray, set fill mode * / bar (x 2, y 2, x sizx-2 Y SIZY-2);
Void Drawbaffle (struct _baffle baffle) {int x = baffle.centerpoint.x - baffle.longth / 2.0; int y = baffle.centerpoint.y - baffle.higth / 2.0; int siz = baffle.longth - 1; int sizy = BAFFLE.HIGTH - 1; if (baffle.type == 1) {setColor (15); / * It is best to use white * / line (x, y, x sizx-1, y); line (x, y 1, X SIZX-2, Y 1); LINE (X, Y, X, Y SIZY-1); line (x 1, y, x 1, y sizy-2); setColor 8); / * It is best to use dark gray * / line (x 1, y sizy, x sizx, y sIzy); line (x 2, y sizy-1, x sizx, y SIZY-1); line (x siz-1, y 1, x sizx-1, y sizy); line (x sizx, y, x sizx, y sIZY); setColor (7); / * It is best to use gray * / putpixel (x, y sizy, 3); putpixel (x 1, y sizy-1, 3); PUTPIEL (X Siz, Y, 3); PUTPIEL (X SIZX-1, Y 1, 3); SetFillStyle (1, baffle.color); / * Best to use gray, set fill mode * / bar (x 2, y 2, x sizx-2, y SIZY-2);}} void Drawball {setColor (ball.color); circle (ball.centerpoint.x, ball.centerpoint.y, ball.ballr);} Void DrawAll {SetColor (15); / * It is best to use white * / line (Wall.Area.xmin-1, Wall.area.xmax, Wall.area.ymin-1); LINE Wall.area.xmin-1, Wall.area.ymin-1, Wall .Area.xmin-1, Wall.area.ymax); line (Wall.area.xmax, Wall.area.ymax, Wall.area.xmax, Wall.area.ymin-1); line (Wall.area.xmax Wall.area.ymax, Wall.area.xmin-1, Wall.area.ymax);} void killdrawbrick (struct _ brick brick) {Int x = brick.centerpoint.x - brick.longth / 2.0; int y = brick .Centerpoint.y - brick.higth / 2.0; int SizX = brick.longth - 1; int sizy = brick.higth - 1; setfillstyle (1,0); / * Best of it is best, set fill mode * / bar (x, y, x sizx, y sIZY);
Void killdrawbaffle (struct _baffle baffle) {int x = baffle.centerpoint.x - baffle.longth / 2.0; int y = baffle.centerpoint.y - baffle.higth / 2.0; int siz = baffle.longth - 1; int sizy = BAFFLE.HIGTH - 1; SetFillStyle (1,0); / * It is best to use gray, set fill mode * / bar (x, y, x sizx, y sIZY);} void killdrawball (struct _ball ball) { SetColor (0); circle (ball.centerpoint.x, ball.centerpoint.y, ball.ballr);} void killdrawwall (struct _wall wall) {setColor (0); / * Here you best use white * / line (Wall .Area.xmin-1, Wall.area.xmax, Wall.area.ymin-1); line (Wall.area.xmin-1, Wall.area.ymin-1, Wall. Area.xmin-1, Wall.Area.ymax; line (Wall.area.xmax, Wall.area.ymax, Wall.area.xmax, Wall.area.ymin-1); line (Wall.area.xmax, Wall.area.ymax, Wall.area.xmin-1, Wall.Area.ymax); -> Centerpoint.x - baffle-> longth / 2.0 - baffle-> speed <= Wall.area.xmin) {baffle-> centerpoint.x = Wall.area.xmin baffle-> longth / 2.0 1;} else {Baffle-> centerpoint.x - = baffle-> speed;} Baffle-> Move Direction = 1;}
Void BaffleMoveright (Struct _baffle * baffle, struct _wall wall) {// Judgment the left shift of the baffle will touch the wall IF (Baffle-> Centerpoint.x baffle-> longth / 2.0 baffle-> speed> = Wall.area .Xmax) {baffle-> centerpoint.x = Wall.area.xmax - baffle-> longth / 2.0 - 1;} else {baffle-> centerpoint.x = baffle-> speed;} baffle-> MoveDirection = 2; } void bafflenuomove (struct _baffle * baffle) {baffle-> MoveDirection = 0;} void ballchangesize (struct _ball * ball, double r) {// The radius of the ball is not allowed to be less than or equal to zero IF (r <= 0) return; ball -> BallR = r;} void ballchangespeed (structed) {// The speed of the ball here cannot be less than or equal to zero if (Speed <= 0) return; ball-> speed = speed;} void baffhangelongth (Struct) _Baffle * baffle, double longth, struct _wall wall) {if (longth <= 0) return; if (baffle-> centerpoint.x longth / 2.0> wall.area.xmax) BAFFLE-> Centerpoint.x = Wall.area .XMAX - longth / 2.0 -1; if (baffle-> centerpoint.x-longth / 2.0
/ * Program document Game.txt, the format is as follows * /
Ball data: radius, speed, color baffle data: length, moving speed, color brick information: -1: Invalid bricks 1-9: Can Finitely damaged bricks 10: Uncomfortable bricks 11: Brick block 12 that makes the ball to make the ball changed quickly, speed changed more than 13: changing the brick block of the baffle length
14: Back to the rest of the status 15: Multi-generate a ball, only one such brick in one sentence
16: The motion speed of the ball is doubled. 17: The length of the baffle is varied: 10, 1, 2; 400, 10, 7; -1, 5, 5, -1, -1, 5, 5, - 1; 5, 5, 5, 5, 5, 5, 5, 5, 5, 5; -1, 5, 5, 5, 5, 5, 5, 1; -1, -1, 5, 5, 5, 5, -1, -1; -1, -1, -1, 5, 5, -1, -1, -1; 3, 3, 3, -1, -1, 3, 3, 3; 10, 11, 1, 1, 1, 1, 1, 10;