使用C++语言编写一段打砖块游戏,有白砖和红砖,玩家是红砖,撞到白砖白砖会弹开
时间: 2024-02-04 14:03:33 浏览: 146
white-and-black:不要碰白砖游戏
以下是使用C++语言编写的打砖块游戏示例代码,其中包含了白砖、红砖、玩家控制、碰撞检测等功能:
```c++
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <math.h>
#include <graphics.h>
using namespace std;
const int screenWidth = 640;
const int screenHeight = 480;
const int brickWidth = 50;
const int brickHeight = 30;
const int maxBrickNum = 50;
const int gameStartX = 100;
const int gameStartY = 100;
const int gameEndX = screenWidth - 100;
const int gameEndY = screenHeight - 100;
int brickNum = 30;
int score = 0;
int timeLeft = 60;
int brickColor = RED;
int textColor = YELLOW;
int bgColor = BLACK;
int border = 10;
int brickX[maxBrickNum], brickY[maxBrickNum];
bool brickAlive[maxBrickNum];
bool gameStart = false;
bool gameEnd = false;
bool gamePause = false;
bool gameOver = false;
int playerX = screenWidth / 2 - brickWidth / 2;
int playerY = screenHeight - 50;
int playerSpeed = 5;
void initGame();
void drawStartPage();
void drawGamePage();
void drawBricks();
void drawPlayer();
void updatePlayerPos();
void checkCollision();
void updateScore();
void updateTime();
void gameOverCheck();
void drawEndPage();
int main()
{
initGame();
while (!gameEnd)
{
if (gameStart)
{
drawGamePage();
updatePlayerPos();
checkCollision();
updateScore();
updateTime();
gameOverCheck();
}
else
{
drawStartPage();
}
if (kbhit())
{
char ch = getch();
if (ch == 27) // ESC key
{
gameStart = false;
}
else if (ch == ' ')
{
gamePause = !gamePause;
}
else if (ch == 'r' || ch == 'R')
{
initGame();
}
else if (ch == 'q' || ch == 'Q')
{
gameEnd = true;
}
else if (ch == 'a' || ch == 'A')
{
playerX -= playerSpeed;
if (playerX < gameStartX)
playerX = gameStartX;
}
else if (ch == 'd' || ch == 'D')
{
playerX += playerSpeed;
if (playerX + brickWidth > gameEndX)
playerX = gameEndX - brickWidth;
}
}
delay(20);
}
closegraph();
return 0;
}
void initGame()
{
srand((unsigned int)time(NULL));
initwindow(screenWidth, screenHeight, "打砖块游戏");
setbkcolor(bgColor);
cleardevice();
rectangle(gameStartX - border, gameStartY - border, gameEndX + border, gameEndY + border);
setcolor(textColor);
settextstyle(20, 0, "宋体");
outtextxy(30, 30, "得分:0");
outtextxy(screenWidth - 120, 30, "时间:60");
for (int i = 0; i < brickNum; i++)
{
brickX[i] = gameStartX + brickWidth * (i % 10);
brickY[i] = gameStartY + brickHeight * (i / 10);
brickAlive[i] = true;
}
gameStart = true;
gameOver = false;
score = 0;
timeLeft = 60;
}
void drawStartPage()
{
cleardevice();
settextstyle(40, 0, "宋体");
setcolor(textColor);
outtextxy(200, 100, "打砖块游戏");
settextstyle(20, 0, "宋体");
setcolor(textColor);
outtextxy(250, 200, "按空格键开始游戏");
outtextxy(250, 250, "按Q键退出游戏");
}
void drawGamePage()
{
cleardevice();
rectangle(gameStartX - border, gameStartY - border, gameEndX + border, gameEndY + border);
drawBricks();
drawPlayer();
}
void drawBricks()
{
for (int i = 0; i < brickNum; i++)
{
if (brickAlive[i])
{
setfillstyle(SOLID_FILL, brickColor);
bar(brickX[i], brickY[i], brickX[i] + brickWidth, brickY[i] + brickHeight);
setcolor(BLACK);
rectangle(brickX[i], brickY[i], brickX[i] + brickWidth, brickY[i] + brickHeight);
}
}
}
void drawPlayer()
{
setfillstyle(SOLID_FILL, RED);
bar(playerX, playerY, playerX + brickWidth, playerY + brickHeight);
setcolor(BLACK);
rectangle(playerX, playerY, playerX + brickWidth, playerY + brickHeight);
}
void updatePlayerPos()
{
if (gamePause || gameOver)
return;
drawPlayer();
}
void checkCollision()
{
if (gamePause || gameOver)
return;
for (int i = 0; i < brickNum; i++)
{
if (brickAlive[i])
{
int brickCenterX = brickX[i] + brickWidth / 2;
int brickCenterY = brickY[i] + brickHeight / 2;
int playerCenterX = playerX + brickWidth / 2;
int playerCenterY = playerY + brickHeight / 2;
double distance = sqrt((brickCenterX - playerCenterX) * (brickCenterX - playerCenterX) +
(brickCenterY - playerCenterY) * (brickCenterY - playerCenterY));
if (distance < brickWidth / 2 + brickHeight / 2)
{
int deltaX = playerCenterX - brickCenterX;
int deltaY = playerCenterY - brickCenterY;
if (abs(deltaX) > abs(deltaY))
{
playerX += deltaX > 0 ? brickWidth / 2 + 1 : -(brickWidth / 2 + 1);
}
else
{
playerY += deltaY > 0 ? brickHeight / 2 + 1 : -(brickHeight / 2 + 1);
}
}
}
}
}
void updateScore()
{
if (gamePause || gameOver)
return;
settextstyle(20, 0, "宋体");
setcolor(textColor);
char scoreStr[20];
sprintf(scoreStr, "得分:%d", score);
outtextxy(30, 30, scoreStr);
}
void updateTime()
{
if (gamePause || gameOver)
return;
time_t now = time(NULL);
int secondsLeft = (int)(now % 60);
if (secondsLeft != timeLeft)
{
timeLeft = secondsLeft;
settextstyle(20, 0, "宋体");
setcolor(textColor);
char timeStr[20];
sprintf(timeStr, "时间:%02d", 60 - timeLeft);
outtextxy(screenWidth - 120, 30, timeStr);
if (timeLeft == 0)
{
gameOver = true;
drawEndPage();
}
}
}
void gameOverCheck()
{
if (gamePause || gameOver)
return;
bool allBricksGone = true;
for (int i = 0; i < brickNum; i++)
{
if (brickAlive[i])
{
allBricksGone = false;
break;
}
}
if (allBricksGone)
{
gameOver = true;
drawEndPage();
}
}
void drawEndPage()
{
cleardevice();
settextstyle(40, 0, "宋体");
setcolor(textColor);
outtextxy(200, 100, "游戏结束");
settextstyle(20, 0, "宋体");
char scoreStr[20];
sprintf(scoreStr, "得分:%d", score);
outtextxy(250, 200, scoreStr);
}
```
在代码中,我们使用了graphics.h库来进行图形化界面的绘制和事件处理。initGame()函数初始化了游戏状态和窗口,drawStartPage()函数绘制了游戏开始界面,drawGamePage()函数绘制了游戏界面,包括砖块和玩家等,checkCollision()函数检测了砖块和玩家的碰撞,updateScore()函数更新了得分,updateTime()函数更新了游戏时间,gameOverCheck()函数检测了游戏是否结束,drawEndPage()函数绘制了游戏结束界面。
阅读全文