C言語でリバウンドボールを実現します。


本論文では、C言語リバウンドボールゲームの具体的なコードを共有しました。
これは関数を使って書いたC言語のミニゲームで、自分の学習成果を確認します。
リバウンドボールの実現には主にいくつかのサブ関数があります。
問題は、ボールの落下や衝突点などをどうやって実現するかです。

#include<stdio.h>
 
#include<windows.h>
#include<conio.h>
 
//      
int high,width; //     
int ball_x,ball_y; //    
int ball_vx,ball_vy; //    
int position_x,position_y; //      
int radius;  //     
int left,right; //       
int ball_number; //      
int block_x,block_y; //      
int score; //        
 
void HideCursor() //     
{
 CONSOLE_CURSOR_INFO cursor_info = {1, 0};
 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
 
void gotoxy(int x,int y) //     (x,y)  ,     
{
 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
 COORD pos;
 pos.X = x;
 pos.Y = y;
 SetConsoleCursorPosition(handle,pos);
}
 
void startup() //      
{
 high=18; //     
 width=26;
 
 ball_x=0; //     
 ball_y=width/2;
 
 ball_vx=1; //       
 ball_vy=1;
 
 position_x=high-1; //      
 position_y=width/2; 
 
 radius=5; //    
 
 left=position_y-radius; //     
 right=position_y+radius;
 
 block_x=0;  //    
 block_y=width/2-4; 
 
 ball_number=0; //      
 
 score=0; //       
 
 HideCursor(); 
 } 
 
void show() //    
{
 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)) //     
 printf("0");
 else if((i==block_x)&& (j==block_y)) //      //      
  printf("@");
 else if(i==high)  //      
  printf("-");
 else if(j==width)  //      
  printf("|");
 else if((i==high-1)&&(j>left)&&(j<right)) 
  printf("*");
 else printf(" ");
 }
 printf("
"); } printf(" :%d
",ball_number); printf(" :%d
",score); } void updateWithoutInpute() // { if(ball_x==position_x-1) // { if((ball_y>=left)&&(ball_y<=right)) { ball_number++; //printf("\a"); } else { printf("
"); system("pause"); exit(0); } } ball_x = ball_x + ball_vx; // ball_y = ball_y + ball_vy; if((ball_x==0) || (ball_x==high-2)) // ball_vx=-ball_vx; if((ball_y==0) || (ball_y==width-1)) // ball_vy=-ball_vy; if((block_x==ball_x) && (block_y==ball_y)) // { block_y=rand()%width-1; score++; } Sleep(120); } void updateWithInpute() // { char input; if(kbhit()) { input=getch(); if((input=='a')&&(left>=0)) { position_y--; left=position_y-radius; // right=position_y+radius; } if((input=='d')&&(right<width)) { position_y++; left=position_y-radius; // right=position_y+radius; } } } int main() { system("color 2f"); // startup(); while(1) { show(); // updateWithoutInpute(); // updateWithInpute(); // } }
小编の前にもコードを収集しました。C言语でボールの反発を実现して、みんなに共有します。

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
 
void ball()//1.    
{
 printf("\t\t\t◎");
}
int main()
{
 int h=20;//        20
 int i,j;//i            ,j       
 int der=1;//    1    , 0    
 while(h>0)//    0 ,    (    0   )
 {
 if(der==1)
 {
 for(i=20-h;i<20;i++)//            
 {
 system("cls");
 for(j=0;j<=i;j++)//      
 {
  printf("
"); } ball(); Sleep(50); } der=0; } else { h=h*8/9;// 9 8 for(i=20;i>=20-h;i--)// { system("cls"); for(j=0;j<=i;j++)// { printf("
"); } ball(); Sleep(50); } der=1; } } return 0; }
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。