C language to achieve the bounce ball game
- 2020-07-21 09:33:41
- OfStack
This article shares the specific code of C language bounce ball game for your reference, the specific content is as follows
This is a function to write C language game, used to test their learning results
The realization of the rebound ball mainly consists of several sub-functions
The question is also how do you get the ball down and how do you score the collision
#include<stdio.h>
#include<windows.h>
#include<conio.h>
// Define global variables
int high,width; // The game boundary
int ball_x,ball_y; // Ball position
int ball_vx,ball_vy; // Ball speed
int position_x,position_y; // Baffle center coordinates
int radius; // Baffle radius
int left,right; // Keyboard left and right boundary
int ball_number; // Number of bouncing balls
int block_x,block_y; // Position of the cube
int score; // I'm going to cancel out the number of cubes
void HideCursor() // Hide the cursor
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x,int y) // Move the cursor to (x,y) location , Screen clearing function
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
void startup() // Data initialization
{
high=18; // Define the boundary
width=26;
ball_x=0; // Small ball coordinates
ball_y=width/2;
ball_vx=1; // Direction of ball velocity
ball_vy=1;
position_x=high-1; // Baffle center coordinates
position_y=width/2;
radius=5; // Baffle radius
left=position_y-radius; // The keyboard border
right=position_y+radius;
block_x=0; // Square location
block_y=width/2-4;
ball_number=0; // Number of bouncing balls
score=0; // I'm going to cancel out the number of balls
HideCursor();
}
void show() // display
{
gotoxy(0,0);
int i,j;
for(i=0;i<=high;i++)
{
for(j=0;j<=width;j++)
{
if((i==ball_x) && (j==ball_y)) // Output ball
printf("0");
else if((i==block_x)&& (j==block_y)) // Output of the slider // Output lower boundary
printf("@");
else if(i==high) // Output lower boundary
printf("-");
else if(j==width) // Output right boundary
printf("|");
else if((i==high-1)&&(j>left)&&(j<right))
printf("*");
else printf(" ");
}
printf("\n");
}
printf(" Number of bouncing balls :%d\n",ball_number);
printf(" I'm going to cancel out the number of balls :%d\n",score);
}
void updateWithoutInpute() // Updates unrelated to user input
{
if(ball_x==position_x-1) // The ball hit the paddle
{
if((ball_y>=left)&&(ball_y<=right))
{
ball_number++;
//printf("\a");
}
else
{
printf(" The game failed \n");
system("pause");
exit(0);
}
}
ball_x = ball_x + ball_vx; // The ball moves in the direction of velocity
ball_y = ball_y + ball_vy;
if((ball_x==0) || (ball_x==high-2)) // The ball hits the upper and lower boundary
ball_vx=-ball_vx;
if((ball_y==0) || (ball_y==width-1)) // The ball hits the left and right boundary
ball_vy=-ball_vy;
if((block_x==ball_x) && (block_y==ball_y)) // The ball hit the slider
{
block_y=rand()%width-1;
score++;
}
Sleep(120);
}
void updateWithInpute() // Updates related to user input
{
char input;
if(kbhit())
{
input=getch();
if((input=='a')&&(left>=0))
{
position_y--;
left=position_y-radius; // The keyboard border
right=position_y+radius;
}
if((input=='d')&&(right<width))
{
position_y++;
left=position_y-radius; // The keyboard border
right=position_y+radius;
}
}
}
int main()
{
system("color 2f"); // Change console color
startup();
while(1)
{
show(); // display
updateWithoutInpute(); // Updates unrelated to user input
updateWithInpute(); // Updates related to user input
}
}
This site has also collected a piece of code: C language to achieve the ball bounce, to share
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
void ball()//1. Draw a ball
{
printf("\t\t\t Thecountry. ");
}
int main()
{
int h=20;// The height of the ball is initialized 20
int i,j;//i It's used to determine where the ball starts and ends, j It's determining the position of the ball
int der=1;// Judgment is equal to the 1 When the ball falls, is 0 When the ball up
while(h>0)// Height is greater than the 0 , the ball is moving (when the height is 0 When stop)
{
if(der==1)
{
for(i=20-h;i<20;i++)// Determine the starting and ending points Falling process
{
system("cls");
for(j=0;j<=i;j++)// Determine the ball's position
{
printf("\n");
}
ball();
Sleep(50);
}
der=0;
}
else
{
h=h*8/9;// The height is the same as before 9 m 8
for(i=20;i>=20-h;i--)// Determine the starting and ending points Rising process
{
system("cls");
for(j=0;j<=i;j++)// Determine the ball's position
{
printf("\n");
}
ball();
Sleep(50);
}
der=1;
}
}
return 0;
}