Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use easyx to realize the game of eliminating bricks in C language

Shulou Source: shulou.com Published: 2022-05-31 17:39:46 10月04日 Update

This article mainly explains "how to use easyx to achieve brick elimination game in C language". The explanation in this article is simple and clear and easy to learn and understand. Please follow Xiaobian's train of thought to study and learn "how to use easyx to realize brick elimination game in C language".

1. Display of the final effect

The effect picture is as follows:

Draw a static bezel

The code is as follows:

# include#include#define High 480 / / Game screen size # define Width 640 int ball_x,ball_y;// / global variable coordinates of the ball int ball_vx,ball_vy;// speed of the ball, radius of the ball, central coordinates of the int bar_x,bar_y;// bezel, height and width of the int bar_high,bar_width;// bezel / / the left and right upper and lower position coordinates of the bezel void startup () / / data initialization {ball_x=Width/2; ball_y=High/2; ball_vx=1; ball_vy=1; radius=20; bar_high=High/20; bar_width=Width/5; bar_x=Width/2; bar_y=High-bar_high/2; bar_left=bar_x-bar_width/2; bar_right=bar_x+bar_width/2 Bar_top=bar_y-bar_high/2; bar_bottom=bar_y+bar_high/2; initgraph (Width,High); BeginBatchDraw ();} void clean () / / display screen {setcolor (BLACK); / / draw black lines, black filled circle setfillcolor (BLACK); fillcircle (ball_x,ball_y,radius); bar (bar_left,bar_top,bar_right,bar_bottom) / / draw black, black filled bezel} void show () / / display screen {setcolor (YELLOW); / / draw yellow line, green filled circle setfillcolor (GREEN); fillcircle (ball_x,ball_y,radius); bar (bar_left,bar_top,bar_right,bar_bottom); / / draw yellow, green filled bezel FlushBatchDraw (); Sleep (3) } void updateWithoutInput () / / updates independent of user input {ball_x=ball_x+ball_vx; ball_y=ball_y,ball_vy;// update ball coordinates if ((ball_x=Width-radius)) ball_vx=-ball_vx; if ((ball_y=High-radius)) ball_vy=-ball_vy } void updateWithInput () / / updates related to user input {} void gameover () {EndBatchDraw (); closegraph ();} int main () {startup (); / / initialization of data while (1) {clean (); / / clear updateWithoutInput () of previously drawn content; / / update updateWithInput () that is not related to user input / / update show () related to user input; / / display new screen}}

The effect picture is as follows:

3. Control bezel

The code is as follows:

# include#include#define High 480 / / Game screen size # define Width 640 int ball_x,ball_y;// / global variable coordinates of the ball int ball_vx,ball_vy;// speed of the ball, radius of the ball, central coordinates of the int bar_x,bar_y;// bezel, height and width of the int bar_high,bar_width;// bezel / / the left and right upper and lower position coordinates of the bezel void startup () / / data initialization {ball_x=Width/2; ball_y=High/2; ball_vx=1; ball_vy=1; radius=20; bar_high=High/20; bar_width=Width/5; bar_x=Width/2; bar_y=High-bar_high/2; bar_left=bar_x-bar_width/2; bar_right=bar_x+bar_width/2 Bar_top=bar_y-bar_high/2; bar_bottom=bar_y+bar_high/2; initgraph (Width,High); BeginBatchDraw ();} void clean () / / display screen {setcolor (BLACK); / / draw black lines, black filled circle setfillcolor (BLACK); fillcircle (ball_x,ball_y,radius); bar (bar_left,bar_top,bar_right,bar_bottom) / / draw black, black filled bezel} void show () / / display screen {setcolor (YELLOW); / / draw yellow line, green filled circle setfillcolor (GREEN); fillcircle (ball_x,ball_y,radius); bar (bar_left,bar_top,bar_right,bar_bottom); / / draw yellow, green filled bezel FlushBatchDraw (); Sleep (3) } void updateWithoutInput () / / updates independent of user input {/ / the bezel collides with the ball, and the ball bounces if (ball_y+radius > = bar_top) & & (ball_y+radius=bar_left) & & (ball_x0) {bar_x=bar_x-15;// position moves left bar_left=bar_x-bar_width/2; bar_right=bar_x+bar_width/2) } if (input=='d'&&bar_right0) {bar_y=bar_y-15;// position move left bar_top=bar_y-bar_high/2; bar_bottom=bar_y+bar_high/2;} if (input=='s'&&bar_bottom

Tags: Bezel ball coordinates screen update mouse black brick location user input data green content width effect height relevance effect collision Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB MySQL macOS Huawei Shulou Information