Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C++ to realize simple pinball Mini Game

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use C++ to achieve simple pinball Mini Game", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to use C++ to achieve a simple pinball Mini Game!

Operation instructions: keyboard An and D keys control left and right movement, so that the ball does not fall.

# include # include # include int itint xx=0;int yy = 0posiclass Ball {public: int x, y; clock_t b; void draw () {setfillcolor (RGB (200,399,100)); fillcircle (x, y, 10);} void null () {setfillcolor (0); setlinecolor (0); fillcircle (x, y, 10);}}; class Board {public: int x, y; void draw () {setlinecolor (RGB (100,209,150)) Setfillcolor (RGB (200,399,100); fillrectangle (x, y, Xerox 60 RGB (200,399,100));} void null () {setfillcolor (0); setlinecolor (0); fillrectangle (x, y, Xerox 60 parry 10);}}; void wall () {setlinecolor (RGB (300,109,650); setfillcolor (200,409,300)) POINT pts [] = {{1,1}, {1,350}, {10350}, {10 Ball ball; clock_t 10}, {600,10}, {600,350}, {610350}, {610 int main 1}}; fillpolygon (pts, 8);} int main () {/ / initializes the graphics window initgraph (640,480); wall (); Board board; board.x = 200; board.y = 400; Ball ball; clock_t f = clock (); clock_t d = clock () While (1) {int iTunes 3; if (GetAsyncKeyState ('A') & 0x8000) / / plank movement {if (board.x > 2 & & (clock ()-d) > = 10) {board.null (); board.x-= 3; board.draw (); d = clock () } if (GetAsyncKeyState ('a') & 0x8000) / / plank movement {if (board.x > 2 & & (clock ()-d) > = 10) {board.null (); board.x-= 3; board.draw (); d = clock ();}} if (GetAsyncKeyState ('D') & 0x8000) / / plank movement {if (board.x)

< 600 && (clock() - d) >

= 10) {board.null (); board.x + = 3; board.draw (); d = clock ();}} if (GetAsyncKeyState ('d') & 0x8000) / / plank movement {if (board.x = 10) {board.null (); board.x + = 3; board.draw (); d = clock () }} if (GetAsyncKeyState ('K') & 0x8000) / / serve {if (yy==0&&xx==0&& (clock ()-f) > = 10) {yy= 1; xx= 1; ball.x = board.x + 50; ball.y = board.y-21; ball.draw (); f = clock () }} if (GetAsyncKeyState ('K') & 0x8000) / / serve {if (yy==0&&xx==0&& (clock ()-f) > = 10) {yy= 1; xx= 1; ball.x = board.x + 50; ball.y = board.y-21; ball.draw (); f = clock () }} / / Ball ejection / / if (yy==1&&xx==1&&clock ()-ball.b > 5) {ball.null (); ball.x-= 2; ball.y-= 2; ball.draw (); ball.b = clock (); if (ball.y 5) {ball.null (); ball.x + = 2; ball.y + = 2; ball.draw (); ball.b = clock () If (ball.x >) {ball.x = 580; ball.null (); xx = 4;} if (ball.x > = board.x & & ball.x 5) {ball.null (); ball.x-= 2; ball.y + = 2; ball.draw (); ball.b = clock (); if (ball.x = board.x & & ball.x = 440) {yy = 0; xx = 0 Ball.null ();}} closegraph (); return 0;}

The editor would like to share another piece of code: C language to achieve a simple console bouncing ball

# include # include / / Global variable int XMagi y; / / spherical coordinate int velocity_x,velocity_y; / / Velocity int left,right,top,bottom; / / Boundary void gotoxy (int xmint y) / / the cursor moves to (xMagery y) position {HANDLE handle = GetStdHandle (STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition (handle,pos) } void HideCursor () / / is used to hide the cursor {CONSOLE_CURSOR_INFO cursor_info = {1,0}; / / the second value is 0 to indicate the hidden cursor SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), & cursor_info);} void startup () / / data initialization {x = 1; y = 5; velocity_x = 1; / / Speed direction velocity_y = 1; left = 0; right = 30; top = 0; bottom = 15; HideCursor () / / hide the cursor} void show () / / display {int iMagazine j; for

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report