C++實(shí)現(xiàn)簡(jiǎn)易的彈球小游戲
本文實(shí)例為大家分享了C++實(shí)現(xiàn)彈球小游戲的具體代碼,供大家參考,具體內(nèi)容如下
操作說(shuō)明:鍵盤(pán)A和D鍵控制左右移動(dòng),讓球不要落下。
#include <graphics.h> #include <conio.h> #include <time.h> int i; int xx=0; int yy = 0; class Ball { public: int x, y; clock_t b; void draw() { setfillcolor(RGB(200, 399, 100)); fillcircle(x, y, 10); } void null() { setfillcolor(0); setlinecolor(0); fillcircle(x, y, 10); } }; class Board { public: int x, y; void draw() { setlinecolor(RGB(100, 209, 150)); setfillcolor(RGB(200, 399, 100)); fillrectangle(x, y, x+60,y+10); } void null() { setfillcolor(0); setlinecolor(0); fillrectangle(x, y, x + 60, y + 10); } }; void wall() { setlinecolor(RGB(300, 109, 650)); setfillcolor(RGB(200, 409, 300)); POINT pts[] = { {1, 1}, {1, 350}, {10,350 }, {10,10 }, {600, 10}, {600, 350}, {610,350}, {610,1 } }; fillpolygon(pts, 8); } int main() { // 初始化圖形窗口 initgraph(640, 480,4); wall(); Board board; board.x = 200; board.y = 400; Ball ball; clock_t f = clock(); clock_t d = clock(); while (1) { int i=3 ; if (GetAsyncKeyState('A') & 0x8000)//木板移動(dòng) { if (board.x > 2 && (clock() - d) >= 10) { board.null(); board.x -= 3; board.draw(); d = clock(); } } if (GetAsyncKeyState('a') & 0x8000)//木板移動(dòng) { if (board.x > 2 && (clock() - d) >= 10) { board.null(); board.x -= 3; board.draw(); d = clock(); } } if (GetAsyncKeyState('D') & 0x8000)//木板移動(dòng) { if (board.x < 600 && (clock() - d) >= 10) { board.null(); board.x += 3; board.draw(); d = clock(); } } if (GetAsyncKeyState('d') & 0x8000)//木板移動(dòng) { if (board.x <600 && (clock() - d) >= 10) { board.null(); board.x += 3; board.draw(); d = clock(); } } if (GetAsyncKeyState('K') & 0x8000)//發(fā)球 { if (yy==0&&xx==0&&(clock() - f) >= 10) { yy = 1; xx = 1; ball.x = board.x + 50; ball.y = board.y - 21; ball.draw(); f = clock(); } } if (GetAsyncKeyState('k') & 0x8000)//發(fā)球 { if (yy==0&&xx==0&& (clock() - f) >= 10) { yy = 1; xx = 1; ball.x = board.x + 50; ball.y = board.y - 21; ball.draw(); f = clock(); } } //球的彈射// if (yy==1&&xx==1&&clock()-ball.b>5) { ball.null(); ball.x -= 2; ball.y -= 2; ball.draw(); ball.b = clock(); if (ball.y <= 21) { ball.y = 21; ball.null(); xx = 4; } if (ball.x <= 24) { ball.x = 24; ball.null(); xx = 2; } } if (yy==1&&xx == 2 && clock() - ball.b > 5) { ball.null(); ball.x += 2; ball.y -= 2; ball.draw(); ball.b = clock(); if (ball.y <= 21) { ball.y = 21; ball.null(); xx = 3; } if (ball.x >= 580) { ball.x = 580; ball.null(); xx = 1; } } if (yy==1&&xx == 3 && clock() - ball.b > 5) { ball.null(); ball.x += 2; ball.y += 2; ball.draw(); ball.b = clock(); if (ball.x >= 580) { ball.x = 580; ball.null(); xx = 4; } if (ball.x >= board.x && ball.x <= board.x + 60 && ball.y <= board.y + 9 && ball.y >= board.y - 11) { ball.null(); ball.y = board.y - 21; ball.draw(); xx = 2; } } if (yy==1&&xx == 4 && clock() - ball.b > 5) { ball.null(); ball.x -= 2; ball.y += 2; ball.draw(); ball.b = clock(); if (ball.x <= 24) { ball.x = 24; ball.null(); xx = 3; } if (ball.x >= board.x && ball.x <= board.x + 60 && ball.y <= board.y + 9 && ball.y >= board.y - 11) { ball.null(); ball.y = board.y - 21; ball.draw(); xx = 1; } } if (ball.y >= 440) { yy = 0; xx = 0; ball.null(); } } closegraph(); return 0; }
小編再為大家分享一段代碼:C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的控制臺(tái)彈跳小球
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> // 全局變量 int x,y; //小球坐標(biāo) int velocity_x,velocity_y ; //速度 int left,right,top,bottom; //邊界 void gotoxy(int x,int y) //光標(biāo)移動(dòng)到(x,y)位置 { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle,pos); } void HideCursor() // 用于隱藏光標(biāo) { CONSOLE_CURSOR_INFO cursor_info = {1, 0}; // 第二個(gè)值為0表示隱藏光標(biāo) SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void startup() // 數(shù)據(jù)初始化 { x = 1; y = 5; velocity_x = 1; //速度方向 velocity_y = 1; left = 0; right = 30; top = 0; bottom = 15; HideCursor(); // 隱藏光標(biāo) } void show() // 顯示畫(huà)面 { int i,j; for (i=0;i<=bottom;i++) { for (j=0;j<=right;j++) { if((i==x) && (j==y)) { printf("o"); //打印小球 } else if ((i==0)||(i==bottom)||(j==0)||(j==right)) //打印邊界 { printf("#"); } else printf(" "); } printf("\n"); } } void automation() // 與用戶(hù)輸入無(wú)關(guān)的更新 { x = x + velocity_x; y = y + velocity_y; if ((x==top)||(x==bottom)) { velocity_x = -velocity_x; printf("\a"); } else if ((y==left)||(y==right)) { velocity_y = -velocity_y; printf("\a"); } Sleep(100); //調(diào)低小球速度 } int main() { system("color 2f"); //改變控制臺(tái)顏色 startup(); // 數(shù)據(jù)初始化 while (1) // 游戲循環(huán)執(zhí)行 { gotoxy(0,0); // 清屏 show(); // 顯示畫(huà)面 automation(); // 與用戶(hù)輸入無(wú)關(guān)的更新 } return 0; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語(yǔ)言 完整游戲項(xiàng)目推箱子詳細(xì)代碼
經(jīng)典的推箱子是一個(gè)的古老游戲,目的是在訓(xùn)練你的邏輯思考能力。在一個(gè)狹小的倉(cāng)庫(kù)中,要求把木箱放到指定的位置,稍不小心就會(huì)出現(xiàn)箱子無(wú)法移動(dòng)或者通道被堵住的情況,所以需要巧妙的利用有限的空間和通道,合理安排移動(dòng)的次序和位置,才能順利的完成任務(wù)2021-11-11C++?計(jì)算時(shí)間差的五種方法小結(jié)
本文主要介紹了C++?計(jì)算時(shí)間差的五種方法小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04C++ 中pragma once 與 #ifndef _XXX_H_ #define _XXX_H_的區(qū)別
這篇文章主要介紹了C++ 中pragma once 與 #ifndef _XXX_H_ #define _XXX_H_的區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-04-04C++ 實(shí)戰(zhàn)開(kāi)發(fā)一個(gè)猜單詞的小游戲
眾所周知紙上得來(lái)終覺(jué)淺,我們要在實(shí)戰(zhàn)中才能真正的掌握技術(shù),小編為大家?guī)?lái)一份用C++編寫(xiě)的猜單詞小游戲,給大家練練手,快來(lái)看看吧2021-11-11Qt之實(shí)現(xiàn)圓形進(jìn)度條的示例代碼
在平時(shí)做頁(yè)面開(kāi)發(fā)時(shí),有些時(shí)候會(huì)用到圓形進(jìn)度條,本文主要介紹了Qt之實(shí)現(xiàn)圓形進(jìn)度條的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10淺談時(shí)間戳與日期時(shí)間互轉(zhuǎn)C語(yǔ)言
下面小編就為大家?guī)?lái)一篇淺談時(shí)間戳與日期時(shí)間互轉(zhuǎn)C語(yǔ)言。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06C語(yǔ)言實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01