目錄
一、準備作業
二、初識圖形化界面
先來做一個視窗
接下來創建一個小球
接下來創建一個矩形
三、設定引數
四、設定速度
小球的運動
畫面的運動
判斷輸贏
五、完整代碼

大家是否玩過上面這款游戲呢?想要自己做出來并不難,跟著我來用手吧,
(參考童晶老師的《C和C++游戲趣味編程》)
一、準備作業
編譯器使用VS2019,但需要下載一個圖形庫Easy—X,
由于此圖形庫是C++的,所以我們要以C++檔案來進行撰寫,C++檔案內也可以使用C語言,
EasyX 下載地址
https://easyx.cn/
怎樣安裝 EasyX? - EasyX
https://easyx.cn/setup#:~:text=1.%20%E5%8F%8C%E5%87%BB%E4%B8%8B%E8%BD%BD%E7%9A%84%20EasyX%20%E5%AE%89%E8%A3%85%E5%8C%85%EF%BC%8C%E5%9C%A8%E2%80%9CWindows%20%E5%B7%B2%E4%BF%9D%E6%8A%A4%E4%BD%A0%E7%9A%84%E7%94%B5%E8%84%91%E2%80%9D%E6%8F%90%E7%A4%BA%E7%AA%97%E4%B8%AD%EF%BC%8C%E7%82%B9%E2%80%9C%E6%9B%B4%E5%A4%9A%E4%BF%A1%E6%81%AF%E2%80%9D%EF%BC%8C%E5%86%8D%E7%82%B9%E2%80%9C%E4%BB%8D%E8%A6%81%E8%BF%90%E8%A1%8C%E2%80%9D%EF%BC%8C%E8%BF%99%E6%97%B6%EF%BC%8C%E5%8F%AF%E4%BB%A5%E7%9C%8B%E5%88%B0%20EasyX%20%E7%9A%84%E5%AE%89%E8%A3%85%E7%A8%8B%E5%BA%8F%E3%80%82%202.,EasyX%20%E5%AE%89%E8%A3%85%E7%A8%8B%E5%BA%8F%E4%BC%9A%E6%A3%80%E6%B5%8B%E5%88%B0%E5%BD%93%E5%89%8D%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E4%B8%AD%E5%AE%89%E8%A3%85%E7%9A%84%20Visual%20Studio%20%E7%89%88%E6%9C%AC%E3%80%82%20%E5%9C%A8%E5%AF%B9%E5%BA%94%E7%9A%84%20VS%20%E7%89%88%E6%9C%AC%E5%8F%B3%E4%BE%A7%E7%82%B9%E5%87%BB%E2%80%9C%E5%AE%89%E8%A3%85%E2%80%9D%E5%8D%B3%E5%8F%AF%E3%80%82
二、初識圖形化界面
先來做一個視窗
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
initgraph(600, 600);
_getch();
closegraph();
return 0;
}
可能很多函式、頭檔案不認識,記住這樣用就行,不理解沒關系,

這樣我們就創建了一個600×600的圖形界面,
接下來創建一個小球
#define _CRT_SECURE_NO_WARNINGS 1
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
initgraph(600, 600);
fillcircle(300,300,100);
_getch();
closegraph();
return 0;
}

我們運用fillcircle( )函式,第一個值引數是X坐標,第二個引數是Y坐標,第三個引數是半徑,
這里講解一下橫縱坐標的定義:

接下來創建一個矩形
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
initgraph(600, 600);
fillrectangle(200,0,400,400);
_getch();
closegraph();
return 0;
}
我們使用fillrectangle( )函式,前兩個引數是矩形左上角坐標,后兩個引數是矩形右下角坐標,

三、設定引數
有了上面基礎,下面我們一步一步來做,
首先我們要選定好我們視窗大小、小球大小以及方塊障礙物大小,
我們想要小球動起來,需要給他初速度與重力加速度,方塊同理,
在平面上就可以理解為它們的橫縱坐標移動,
我們先設定基本引數,詳情見注釋
float w, h, g;//游戲畫面寬高與重力加速度
float ball_x, ball_y, ball_vy, r;//小球圓心坐標,y方向速度和半徑
float rect1_x1, rect1_y1, rect1_x2, rect1_y2, rect1_vx;//上方塊的橫縱坐標
//因為畫面向左移動,我們只要讓方塊向左移就可以實作,所以需要x方向速度
float rect2_x1, rect2_y1, rect2_x2, rect2_y2, rect2_vx;//下方塊的橫縱坐標
float rect_h = 130;//因為隨機方塊高度的需要,單獨設定一個高度
int score;//得分
再給引數賦值
//下面給具體引數
w = 600;
h = 400;
g = 0.6;
initgraph(w, h);//創建一個視窗
//小球引數
r = 20;
ball_x = w / 4;//讓小球在畫面左邊1/4處
ball_y = h - r;//讓小球貼地,離地面一個半徑距離
ball_vy = 0;
//上方塊引數
rect1_x1 = w * 3 / 4;//方塊在畫面3/4處
rect1_y1 = 0;//上方塊左上角y坐標為0
rect1_x2 = rect1_x1 + 30;//+30就是指寬度為30
rect1_y2 = rect1_y1 + rect_h;//高度為130
//下方塊引數
rect2_x1 = w * 3 / 4;
rect2_y1 = h - rect_h;//下方塊的左上角y坐標離右下角差一個高度
rect2_x2 = rect2_x1 + 30;
rect2_y2 = h;//下方塊貼地
//速度
rect1_vx = rect2_vx = -3;
最后我們來看一下效果
fillcircle(ball_x, ball_y, r);
fillrectangle(rect1_x1, rect1_y1, rect1_x2, rect1_y2);
fillrectangle(rect2_x1, rect2_y1, rect2_x2, rect2_y2);
_getch();
closegraph();

四、設定速度
小球的運動
畫面一直在動,就需要回圈,接下來的我們就放在一個死回圈里進行,
首先我們先讓小球動起來,用_kbhit() 來收集按鍵,_getch()接收,如代碼:
while (1)
{
if(_kbhit())//當按鍵時
{
char input = _getch();//獲得輸入字符
if (input == ' ')//按下空格時
{
ball_vy = -15;//給小球一個向上的初速度
}
}
}
有了速度不夠,速度要換成路程才行,平面上體現為小球的坐標更新,
當小球落到地面時,我們把速度變為0,并且縱坐標變為h - r就能實作小球在地面上,
現在我們來看下效果,
while (1)
{
//當摁下空格,小球向上跳躍,給一個反向速度
if(_kbhit())
{
char input = _getch();
if (input == ' ')
{
ball_vy = -17;
}
}
if (ball_y >= h - r)//當小球落到地面時
{
ball_vy = 0;//速度變為0
ball_y = h - r;//規范其y坐標,防止落到地面下,
}
ball_vy = ball_vy + g;//y方向初速度為0,再加上一個重力加速度
ball_y = ball_y + ball_vy;//根據小球y方向速度更新y坐標
cleardevice();//在每次回圈清一次屏,不然會出現小球的軌跡
fillcircle(ball_x, ball_y, r);
fillrectangle(rect1_x1, rect1_y1, rect1_x2, rect1_y2);
fillrectangle(rect2_x1, rect2_y1, rect2_x2, rect2_y2);
Sleep(10);//讓小球暫停10ms,不然小球一眨眼就沒了
}
closegraph();

畫面的運動
畫面的運動可以看作方塊向左移,那么我們就要給方塊的x坐標更新,
rect1_x1 = rect1_x1 + rect_vx;
rect2_x1 = rect2_x1 + rect_vx;
但是方塊到了最左邊,我們還需要讓它出現在右邊,并且此時得分+1,
重新出現的方塊我們需要一個新的隨機高度,增加趣味性,
別忘了把方塊左移速度提升上去,增加游戲難度,
//更新兩個方塊的位置坐標
rect1_x1 += rect_vx;
rect1_x2 += rect_vx;
rect2_x1 += rect_vx;
rect2_x2 += rect_vx;
if (rect1_x1 <= 0 && rect2_x1 <= 0)//方塊來到最左邊
{
//讓他們出現在最右側
rect1_x1 = rect2_x1 = w - 30;
rect1_x2 = rect2_x2 = w;
score += 1;//得分+1
rect_h = rand() % int(h / 4) + h / 4;//設定隨即高度,高度不超過畫面的1/4
//設定隨即高度
rect2_y1 = h - rect_h;
rect1_y2 = rect_h;
if (score > 0)
rect_vx -= 0.1;//如果速度一直大于0,左移速度加大,當得分為0速度還原,
}
看一下效果:

這里我的引數有些不合理,大家可以自行更改,原理就是這樣,
判斷輸贏
如果碰到方塊的邊緣,那么得分變為0,速度還原,否則繼續,
這里是六種死法,所以我們需要六個條件判斷,

if (((ball_x + r >= rect2_x1)
&&(ball_y + r >= h - rect_h)
&&(ball_x - r <= rect2_x2))//碰到下方塊
||((ball_x + r >= rect1_x1)
&&(ball_y - r <= rect_h)
&&(ball_x - r <= rect1_x2))//碰到上方塊
)
{
Sleep(100);//給個慢速回放,看看怎么死的
score = 0;//得分歸0
}
結尾咱給來一份列印成績,代碼如下,暫且了解就行:
TCHAR s[20];
_stprintf(s, _T("%d"), score);
settextstyle(40, 0, _T("宋體"));
outtextxy(50, 30, s);
五、完整代碼
以下是自己選了合適的引數,并添加了一些元素,
大家也可以參照自行修改,

#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
float w, h, g;//游戲畫面寬高與重力加速度
float ball_x, ball_y, ball_vy, r;//小球圓心坐標,y方向速度和半徑
float rect1_x1, rect1_y1, rect1_x2, rect1_y2, rect_vx;//上方塊的橫縱坐標
//因為畫面向左移動,我們只要讓方塊向左移就可以實作,所以需要x方向速度
float rect2_x1, rect2_y1, rect2_x2, rect2_y2;//下方塊的橫縱坐標
float rect_h = 125;//因為隨機方塊高度的需要,單獨設定一個高度
int score = 0;//得分
//下面給具體引數
w = 600;
h = 400;
g = 0.4;
initgraph(w, h);//創建一個視窗
//小球引數
r = 18;
ball_x = w / 4;//讓小球在畫面左邊1/4處
ball_y = h - r;//讓小球貼地,離地面一個半徑距離
ball_vy = 0;
//上方塊引數
rect1_x1 = w * 3 / 4;//方塊在畫面3/4處
rect1_y1 = 0;//上方塊左上角y坐標為0
rect1_x2 = rect1_x1 + 30;//+30就是指寬度為30
rect1_y2 = rect1_y1 + rect_h;//高度為130
//下方塊引數
rect2_x1 = w * 3 / 4;
rect2_y1 = h - rect_h;//下方塊的左上角y坐標離右下角差一個高度
rect2_x2 = rect2_x1 + 30;
rect2_y2 = h;//下方塊貼地
//速度
rect_vx = -2.5;
while (1)
{
//當摁下空格,小球向上跳躍,給一個反向速度
if(_kbhit())
{
char input = _getch();
if (input == ' ')
{
ball_vy = -8;
}
}
ball_vy = ball_vy + g;//y方向初速度為0,再加上一個重力加速度
ball_y = ball_y + ball_vy;//根據小球y方向速度更新y坐標
if (ball_y >= h - r)
{
ball_vy = 0;
ball_y = h - r;
}
//更新兩個方塊的位置坐標
rect1_x1 += rect_vx;
rect1_x2 += rect_vx;
rect2_x1 += rect_vx;
rect2_x2 += rect_vx;
if (rect1_x1 <= 0 && rect2_x1 <= 0)//方塊來到最左邊
{
//讓他們出現在最右側
rect1_x1 = rect2_x1 = w - 30;
rect1_x2 = rect2_x2 = w;
score += 1;//得分+1
rect_h = rand() % int(h / 5.5) + h / 5.5;//設定隨即高度
rect1_y2 = rect_h;
rect2_y1 = h - rect_h;
if (score > 0)
rect_vx -= 0.1;//如果速度一直大于0,左移速度加大,
}
if (((ball_x + r >= rect2_x1)
&&(ball_y + r >= h - rect_h)
&&(ball_x - r <= rect2_x2))//碰到下方塊
||((ball_x + r >= rect1_x1)
&&(ball_y - r <= rect_h)
&&(ball_x - r <= rect1_x2))//碰到上方塊
||(ball_y + r >= h - 10)//碰到底部
)
{
Sleep(100);//給個慢速回放,看看怎么死的
score = 0;//得分歸0
rect_vx = -3;//碰到就速度還原
}
if (ball_y - r <= 10)//如果碰到頂部,直接回到底部,分數為0
{
ball_vy += 100 * g;
ball_y += ball_vy;
score = 0;
}
cleardevice();
//繪制小球
setfillcolor(GREEN);//填上綠色
fillcircle(ball_x, ball_y - 10, r);
//繪制方塊
setfillcolor(BROWN);//填上棕色
fillrectangle(rect1_x1, rect1_y1, rect1_x2, rect1_y2);
fillrectangle(rect2_x1, rect2_y1, rect2_x2, rect2_y2);
setfillcolor(RED);
fillrectangle(0, 0, w, 10);
fillrectangle(0, h - 10, w, h);
//列印分數
TCHAR s[20];
_stprintf(s, _T("%d"), score);
settextstyle(40, 0, _T("宋體"));
outtextxy(50, 30, s);
Sleep(10);
}
closegraph();
return 0;
}
如果你能看到這,希望對您有所幫助,我會繼續寫一些有趣的游戲,歡迎關注,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/299764.html
標籤:其他
上一篇:一鍵式入門3D游戲建模!9個3DsMax爆肝小技巧,超詳細的圖文教程!
下一篇:java 猜拳小游戲
