1. 程式人生 > 程式設計 >C語言實現反彈球小遊戲

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("\n");
 }
 printf("反彈小球次數:%d\n",ball_number);
 printf("消掉小球個數:%d\n",score);
 } 
 
void updateWithoutInpute() //與使用者輸入無關的更新
{
 if(ball_x==position_x-1)   //小球撞到擋板 
 {
 if((ball_y>=left)&&(ball_y<=right))
 {
  ball_number++;
  //printf("\a");
 }
 else
 {
  printf("遊戲失敗\n");
  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("\n");
  }
  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("\n");
  }
  ball();
  Sleep(50);
  }
  der=1;
 }
 
 }
 return 0;
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。