• 大小: 1.08MB
    文件类型: .7z
    金币: 1
    下载: 0 次
    发布日期: 2023-10-25
  • 语言: C/C++
  • 标签: C语言  小游戏  乒乓  

资源简介

C语言编写的乒乓球游戏,人机对战,按W向上移动,按S向下移动。

资源截图

代码片段和文件信息

#include
#include
#include
#include
#include
#include

void control(void*);
void display(void*);
void AI(void*);
void physics(void*);
void sound(void*);

int player1_y=250;
int player1_score=0;
int player2_y=250;
int player2_score=0;

int ball_x=390;
int ball_y=290;
int ver_ball_x=1;
int ver_ball_y=1;

bool wav1=0;

void main(void) //主程序
{


_beginthread(control0NULL);    /********************************/
_beginthread(AI0NULL); /*                              */
_beginthread(physics0NULL); /*       启动多线程     */
_beginthread(display0NULL); /*                              */
_beginthread(sound0NULL); /********************************/
while(1)
{
Sleep(10000);                     //主程序延时
}
}

void physics(void*) //碰撞效果线程
{

while(1)
{
ball_x+=ver_ball_x;
ball_y+=ver_ball_y;
if(ball_y<=0)
{
ball_y=0;
ver_ball_y=-ver_ball_y;
wav1=1;
}
if(ball_y>=580)
{
ball_y=580;
ver_ball_y=-ver_ball_y;
wav1=1;
}
if(ball_x<=20 && ball_y>player1_y-20 && ball_y<(player1_y+100))
{
ver_ball_x=-ver_ball_x;
wav1=1;
}
if(ball_x>=760 && ball_y>player2_y-20 && ball_y<(player2_y+100))
{
ver_ball_x=-ver_ball_x;
wav1=1;
}
if(ball_x<0)
{
ball_x=390;
ball_y=290;
ver_ball_x=-ver_ball_x;
player2_score++;


}
if(ball_x>780)
{
ball_x=390;
ball_y=290;
ver_ball_x=-ver_ball_x;
player1_score++;

}
Sleep(5);
}
}





void control(void*) //控制模块线程
{
char key;
while(1)
{
key=getch();
switch(key)
{
case ‘w‘:player1_y-=20;break;
case ‘s‘:player1_y+=20;break;
default:break;
}
if(player1_y<0)
player1_y=0;
if(player1_y>500)
player1_y=500;
}
}

void AI(void*) //对手行动线程
{
while(1)
{
if(ver_ball_x>0 &&ball_x>390)
{
if(player2_y+20>ball_y)
player2_y-=20;
if(player2_y+80 player2_y+=20;
if(player2_y<0)
player2_y=0;
if(player2_y>500)
player2_y=500;
}
Sleep(100);
}
}




void display(void*) //图形显示线程
{
initgraph(800600);
char temp_c[3];
LOGFONT f;
getfont(&f);                       // 获取当前字体设置

f.lfHeight = 32;                   // 设置字体高度
strcpy(f.lfFaceName “黑体“);      // 设置字体
f.lfQuality = DEFAULT_QUALITY;     // 设置输出效果为无抗锯齿
f.lfUnderline=0;                   //设置下划线
setfont(&f);                       // 设置字体样式
setcolor(RGB(255255255));
while(1)
{
BeginBatchDraw();

setfillstyle(RGB(000));
bar(00800600);
setfillstyle(RGB(255255255));
bar(0player1_y20player1_y+100);
bar(780player2_y800player2_y+100);
bar(ball_xball_yball_x+20ball_y+20);
sprintf(temp_c“YOU:%d“player1_score);
outtextxy(25050temp_c);
sprintf(temp_c“CPU:%d“player2_score);
outtextxy(47050temp_c);
bar(3900410600);

EndBatchDraw();
Sleep(20);

评论

共有 条评论