C语言实现双人反弹球游戏

本文项目为大家分享C语言实现双人反弹球游戏的具体代码,供大家参考,具体内容如下

一、最终项目描述和效果

项目描述:   实现双人玩的弹跳球游戏

最终效果图如下:

二、基本框架实现

代码如下:

#include<conio.h>
#include<graphics.h>
#define High 480//游戏画面尺寸
#define Width 640

//全局变量
int ball1_x,ball1_y;//小球1的坐标
int ball2_x,ball2_y;//小球2的坐标
int radius;

void startup()//数据的初始化
{
    ball1_x=Width/3;
    ball1_y=High/3;
    ball2_x=Width*2/3;
    ball2_y=High*2/3;
    radius=20;
    initgraph(Width,High);
    BeginBatchDraw();
}

void clean()//消除画面
{
    setcolor(BLACK);
    setfillcolor(BLACK);
    fillcircle(ball1_x,ball1_y,radius);
    fillcircle(ball2_x,ball2_y,radius);
}

void show()//显示画面
{
    setcolor(GREEN);
    setfillcolor(GREEN);
    fillcircle(ball1_x,ball1_y,radius);//绘制绿圆
    setcolor(RED);
    setfillcolor(RED);
    fillcircle(ball2_x,ball2_y,radius);//绘制红圆
    FlushBatchDraw();
    Sleep(3);
}

void updateWithoutInput()//与用户输入无关的更新
{

}

void updateWithInput()//与用户输入有关的更新
{
    char input;
    if(kbhit())//判断是否有输入
    {
        input=getch();
        int step=10;
        if(input=='a')
            ball1_x-=step;
        if(input=='d')
            ball1_x+=step;
        if(input=='w')
            ball1_y-=step;
        if(input=='s')
            ball1_y+=step;

        if(input=='4')
            ball2_x-=step;
        if(input=='6')
            ball2_x+=step;
        if(input=='8')
            ball2_y-=step;
        if(input=='5')
            ball2_y+=step;
    }
}

void gameover()
{
    EndBatchDraw();
    closegraph();
}

int main(void)
{
    startup();//数据的初始化
    while(1)
    {
        clean();//把之前绘制的内容取消
        updateWithoutInput();//与用户输入无关的更新
        updateWithInput();//与用户输入有关的更新
        show();//显示新画面
    }
    gameover();//游戏结束,进行后续处理
    return 0;
}

你会发现这有一个弊端:  双方同一时刻只能有一个运行,不能同时运行。

三、异步操作的实现

代码如下:

#include<conio.h>
#include<graphics.h>
#define High 480//游戏画面尺寸
#define Width 640

//全局变量
int ball1_x,ball1_y;//小球1的坐标
int ball2_x,ball2_y;//小球2的坐标
int radius;

void startup()//数据的初始化
{
    ball1_x=Width/3;
    ball1_y=High/3;
    ball2_x=Width*2/3;
    ball2_y=High*2/3;
    radius=20;
    initgraph(Width,High);
    BeginBatchDraw();
}

void clean()//消除画面
{
    setcolor(BLACK);
    setfillcolor(BLACK);
    fillcircle(ball1_x,ball1_y,radius);
    fillcircle(ball2_x,ball2_y,radius);
}

void show()//显示画面
{
    setcolor(GREEN);
    setfillcolor(GREEN);
    fillcircle(ball1_x,ball1_y,radius);//绘制绿圆
    setcolor(RED);
    setfillcolor(RED);
    fillcircle(ball2_x,ball2_y,radius);//绘制红圆
    FlushBatchDraw();
    Sleep(3);
}

void updateWithoutInput()//与用户输入无关的更新
{

}

void updateWithInput()//与用户输入有关的更新
{
    int step=1;
    if((GetAsyncKeyState(0x41)&0x8000))//a
        ball1_x-=step;
    if((GetAsyncKeyState(0x44)&0x8000))//d
        ball1_x+=step;
    if((GetAsyncKeyState(0x57)&0x8000))//w
        ball1_y-=step;
    if((GetAsyncKeyState(0x53)&0x8000))//s
        ball1_y+=step;
    if((GetAsyncKeyState(VK_LEFT)&0x8000))//左方向键
        ball2_x-=step;
    if((GetAsyncKeyState(VK_RIGHT)&0x8000))//右方向键
        ball2_x+=step;
    if((GetAsyncKeyState(VK_UP)&0x8000))//上方向键
        ball2_y-=step;
    if((GetAsyncKeyState(VK_DOWN)&0x8000))//下方向键
        ball2_y+=step;
}

void gameover()
{
    EndBatchDraw();
    closegraph();
}

int main(void)
{
    startup();//数据的初始化
    while(1)
    {
        clean();//把之前绘制的内容取消
        updateWithoutInput();//与用户输入无关的更新
        updateWithInput();//与用户输入有关的更新
        show();//显示新画面
    }
    gameover();//游戏结束,进行后续处理
    return 0;
}

效果图如下:

四、双人反弹球

代码如下:

#include<conio.h>
#include<graphics.h>
#include<Windows.h>
#define High 480//游戏画面尺寸
#define Width 640

//全局变量
int ball_x,ball_y;//小球的坐标
int ball_vx,ball_vy;//小球2的速度
int bar1_left,bar1_right,bar1_top,bar1_bottom;//挡板1的上下左右位置坐标
int bar2_left,bar2_right,bar2_top,bar2_bottom;//挡板2的上下左右位置坐标
int bar_height,bar_width;//挡板的高度和宽度
int radius;

void startup()//数据的初始化
{
    ball_x=Width/2;
    ball_y=High/2;
    ball_vx=1;
    ball_vy=1;
    radius=20;

    bar_width=Width/30;
    bar_height=High/4;

    bar1_left=Width*1/20;//挡板1的数据初始化
    bar1_top=High/4;
    bar1_right=bar1_left+bar_width;
    bar1_bottom=bar1_top+bar_height;

    bar2_left=Width*18.5/20;//挡板2的数据初始化
    bar2_top=High/4;
    bar2_right=bar2_left+bar_width;
    bar2_bottom=bar2_top+bar_height;
    initgraph(Width,High);
    BeginBatchDraw();
}

void clean()//消除画面
{
    setcolor(BLACK);
    setfillcolor(BLACK);
    fillcircle(ball_x,ball_y,radius);
    fillcircle(ball_x,ball_y,radius);
    bar(bar1_left,bar1_top,bar1_right,bar1_bottom);//绘制挡板
    bar(bar2_left,bar2_top,bar2_right,bar2_bottom);
}

void show()//显示画面
{
    setcolor(GREEN);
    setfillcolor(GREEN);
    fillcircle(ball_x,ball_y,radius);//绘制绿圆
    setcolor(RED);
    setfillcolor(RED);
    bar(bar1_left,bar1_top,bar1_right,bar1_bottom);
    bar(bar2_left,bar2_top,bar2_right,bar2_bottom);
    FlushBatchDraw();
    Sleep(3);
}

void updateWithoutInput()//与用户输入无关的更新
{
    //挡板和小球碰撞,小球反弹
    if(ball_x+radius>=bar2_left&&ball_y+radius>=bar2_top&&ball_y+radius<bar2_bottom)
        ball_vx=-ball_vx;
    else if(ball_x-radius<=bar1_right&&ball_y+radius>=bar1_top&&ball_y+radius<bar1_bottom)
        ball_vx=-ball_vx;

    //更新小球的坐标
    ball_x=ball_x+ball_vx;
    ball_y=ball_y+ball_vy;

    if((ball_x<=radius)||(ball_x>+Width-radius))
        ball_vx=-ball_vx;
    if((ball_y<=radius)||(ball_y>+High-radius))
        ball_vy=-ball_vy;

}

void updateWithInput()//与用户输入有关的更新
{
    int step=1;
    if((GetAsyncKeyState(0x57)&0x8000))//w
        bar1_top-=step;
    if((GetAsyncKeyState(0x53)&0x8000))//s
        bar1_top+=step;
    if((GetAsyncKeyState(VK_UP)&0x8000))//上方向键
        bar2_top-=step;
    if((GetAsyncKeyState(VK_DOWN)&0x8000))//下方向键
        bar2_top+=step;

    if(bar1_top<0)//判断挡板是否超过屏幕
        bar1_top+=step;
    if(bar2_top<0)
        bar2_top+=step;
    if(bar1_top+bar_height>High)
        bar1_top-=step;
    if(bar2_top+bar_height>High)
        bar2_top-=step;

    bar1_bottom=bar1_top+bar_height;
    bar2_bottom=bar2_top+bar_height;
}

void gameover()
{
    EndBatchDraw();
    closegraph();
}

int main(void)
{
    startup();//数据的初始化
    while(1)
    {
        clean();//把之前绘制的内容取消
        updateWithoutInput();//与用户输入无关的更新
        updateWithInput();//与用户输入有关的更新
        show();//显示新画面
    }
    gameover();//游戏结束,进行后续处理
    return 0;
}

效果图如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • C语言实现弹跳小球

    这是一个简单的c语言源代码,C语言实现弹跳小球,设置了小球的分数及过关的难度. #include "graphics.h" #include "math.h" #define LEFT 0x4b00 #define RIGHT 0x4d00 #define ESC 0x011b int x,y,x1,y1,key,t1,t2,keyx,keylong,keymove,over=0,i,tt=0,fast; int ballx[9]={53,103,263,120,2

  • C语言用数组实现反弹球消砖块

    本文项目为大家分享了C语言用数组实现反弹球消砖块的具体代码,供大家参考,具体内容如下 一.效果展示: 二.代码如下: #include<stdio.h> #include<string.h> #include<conio.h> #include<Windows.h> #include<time.h> #define High 24   //游戏画面尺寸 #define Width 36 //全局变量 int ball_x,ball_y;//小球的

  • C语言实现简单反弹球消砖块游戏

    反弹球消砖块,是一款很简单的打砖块游戏,控制你的挡板挡住弹球,打掉上面的砖块,本篇博客中,主要使用printf与scanf函数实现消砖块游戏 整体思路 主函数 int main() {     startup();//初始化     while (1)     {         show();//显示画面           updateWitoutIput();//与用户输入无关的更新  //更新数据         updateWithInput(); //与用户输入有关的更新  //输入

  • C语言基于EasyX库实现有颜色弹跳小球

    本文实例为大家分享了基于EasyX库实现有颜色弹跳小球的具体代码,供大家参考,具体内容如下 1.目标要求 1.实现一个有颜色小球在窗口中弹跳2.遇到边界弹跳 2.C语言代码 #include<graphics.h>  #include<stdio.h> #include<stdlib.h> #include<windows.h> #include<conio.h> #define High 480 #define Width 640//画布尺寸

  • C语言用函数实现反弹球消砖块

    本文实例为大家分享了C语言用函数实现反弹球消砖块的具体代码,供大家参考,具体内容如下 一.项目描述和最终的成果展示 这是在上一次弹跳小项目上进行了一系列的优化和封装.项目: 弹跳的小球上次没有用函数进行的封装.这次在上次的基础上进行封装和一些功能的优化. 最终效果图如下: 二.封装后的弹跳小球 代码如下: #include<stdio.h> #include<string.h> #include<conio.h> #include<windows.h> //

  • C语言实现反弹球游戏

    C语言小游戏--反弹球(简单的图形化界面),供大家参考,具体内容如下 1.环境准备和安装 安装Visual C++ 6.0. 去Easy X官网下载Easy X安装包. 2.Eaxy X功能的简单介绍 Easy X类似于一个库函数,其中带有许多很有用的函数. Easy x首先创建一个新的窗口进行绘图. 可以画常见点 线 多边形 可以调节颜色. 可以插入图片,音乐. 可以获取鼠标信息. 其中函数的具体使用可以看安装包中带有的帮助文件 3.反弹球游戏主函数框架 int main (void) { s

  • C语言实现反弹球消砖块游戏

    本文实例为大家分享了C语言实现反弹球消砖块游戏的具体代码,供大家参考,具体内容如下 1.目标要求: 1.击中上方方块’H’,分数加12.下方控制线没有接到小球时,游戏结束 2.C语言代码: #include<stdio.h> #include<stdlib.h> #include<windows.h> #include<conio.h> /*         <反弹球消砖块>    by:你最珍贵                       --

  • C语言实现简单弹球游戏

    电视机待机的屏幕上的弹球,怎么实现? 今天文章就跟大家分享下C语言实现简单弹球游戏的具体代码,供大家参考,具体内容如下 #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <math.h> #include <windows.h> //#include <sy

  • C语言实现简单弹跳球游戏

    本文实例为大家分享了C语言实现弹跳球游戏的具体代码,供大家参考,具体内容如下 #include <stdio.h> #include <stdlib.h> int main() { // 球的坐标 int pos_x,pos_y; //球坐标的变化 int x =0; int y = 5; // 定义一个球的速度 int velocity_x=1; int velocity_y=1; //定义一个球运动的范围 int top=0; int botton=20; int lift=0

  • 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

随机推荐