📄 bubble.cpp
字号:
/* ***************************************************************************
* Bubble Game v1.0 - A Free Game, Written in C++ Programming Language.
* Version: 1.0
* Date: 1 Jan 2005
* Developed by: Pooyan Mahdavi
* Tested with Borland Turbo C++ v3.0
*
* This is an exciting game, developed by me, as a project for Graphics course.
* After running use <space> and arrow (<up>, <down>, <left> and <right>) keys
* to play the game.
* Pressing <Esc> key at any time terminates this program.
* Don't forget to change the "BGI_PATH" parameter in the "main()" function.
*
* E-Mail any comments to: pooyanonli@yahoo.com
************************************************************************** */
/* Header File Inclusion */
#include <bios.h>
#include <conio.h>
#include <ctype.h>
#include <dos.h>
#include <graphics.h>
#include <stdlib.h>
enum bool {false, true};
/* Constants */
#define BGI_PATH "c:\\tc\\bgi"
#define KEYCODE_ESCAPE 27
#define KEYCODE_RIGHTARROW 77 // Right Arrow
#define KEYCODE_LEFTARROW 75 // Left Arrow
#define KEYCODE_UPARROW 72 // Up Arrow
#define KEYCODE_DOWNARROW 80 // Down Arrow
/* ************************************************************************ */
class ObjectProperties {
private : int x, y, oldx, oldy, color, rx, ry, rate;
bool isvisible;
public: int getX() {
return x;
}
public: void setX(int x) {
oldx = (*this).x;
oldy = y;
(*this).x = x;
}
public: int getY() {
return y;
}
public: void setY(int y) {
oldx = x;
oldy = (*this).y;
(*this).y = y;
}
public: int getOldX() {
return oldx;
}
public: int getOldY() {
return oldy;
}
public: bool isVisible() {
return isvisible;
}
public: void setVisible(bool isvisible) {
(*this).isvisible = isvisible;
}
public: int getColor() {
return color;
}
public: void setColor(int color) {
(*this).color = color;
}
public: int getRate() {
return rate;
}
public: void setRate(int rate) {
(*this).rate = rate;
}
public: int getRx() {
return rx;
}
public: void setRx(int rx) {
(*this).rx = rx;
}
public: int getRy() {
return ry;
}
public: void setRy(int ry) {
(*this).ry = ry;
}
public: ObjectProperties() { // constructor
reset();
}
public: void reset() {
setX(-random(400));
rate = random(25) + 1;
do {
color = random(16);
} while(color == getbkcolor());
setVisible(false);
setRx(10);
setRy(15);
}
};
class Player {
private : int x, y, oldx, oldy, color, score, life, rx, ry;
public: int getX() {
return x;
}
public: void setX(int x) {
oldx = (*this).x;
oldy = y;
(*this).x = x;
}
public: int getY() {
return y;
}
public: void setY(int y) {
oldx = x;
oldy = (*this).y;
(*this).y = y;
}
public: int getOldX() {
return oldx;
}
public: int getOldY() {
return oldy;
}
public: int getScore() {
return score;
}
public: void setScore(int score) {
(*this).score = score;
}
public: int getColor() {
return color;
}
public: void setColor(int color) {
(*this).color = color;
}
public: int getLife() {
return life;
}
public: void setLife(int life) {
(*this).life = life;
}
public: int getRx() {
return rx;
}
public: void setRx(int rx) {
(*this).rx = rx;
}
public: int getRy() {
return ry;
}
public: void setRy(int ry) {
(*this).ry = ry;
}
public: Player() { // constructor
x = 620;
y = 2;
score = 50;
life = 5;
rx = 15;
ry = 10;
color = 1;
scoreChanged = true;
}
private: bool scoreChanged;
public: void setScoreChanged(bool scoreChanged) {
(*this).scoreChanged = scoreChanged;
}
public: bool isScoreChanged() {
return scoreChanged;
}
};
class Shot {
private : int x, y, oldx, color, rx, ry;
bool isvisible;
public: int getX() {
return x;
}
public: void setX(int x) {
oldx = (*this).x;
//oldy = y;
(*this).x = x;
}
public: int getY() {
return y;
}
public: void setY(int y) {
oldx = x;
//oldy = (*this).y;
(*this).y = y;
}
public: int getOldX() {
return oldx;
}
public: bool isVisible() {
return isvisible;
}
public: void setVisible(bool isvisible) {
(*this).isvisible = isvisible;
}
public: int getColor() {
return color;
}
public: void setColor(int color) {
(*this).color = color;
}
public: int getRx() {
return rx;
}
public: void setRx(int rx) {
(*this).rx = rx;
}
public: int getRy() {
return ry;
}
public: void setRy(int ry) {
(*this).ry = ry;
}
public : Shot() {
reset();
}
public : void reset() {
setRx(10);
setRy(5);
strike = false;
setColor(5);
}
public : int x0, oldx0, x1;
bool strike;
} shots[10];
Player player;
/* ************************************************************************ */
void main(void) {
void splash();
void play(void);
int GD = DETECT, GM;
initgraph(&GD, &GM, BGI_PATH);
randomize();
splash();
play();
}
void splash() {
void centerRectangle(char [], int);
clrscr();
setcolor(1);
settextstyle(7, 0, 3);
centerRectangle("In The Name of Allah", 10);
settextstyle(7, 0, 1);
centerRectangle("The Meciful The Compassionate", 45);
settextstyle(1, 0, 7);
setcolor(4);
centerRectangle("Bubble Game 1.0", 170);
settextstyle(6, 0, 3);
setcolor(0);
centerRectangle("A Free Game, Written in C++ Programming Language.", 300);
settextstyle(6, 0, 2);
setcolor(4);
centerRectangle("Developed by: Pooyan Mahdavi", 375);
settextstyle(6, 0, 3);
setcolor(9);
centerRectangle("E-Mail any comments to: pooyanonli@yahoo.com", 430);
getch();
}
void centerRectangle(char s[], int starty) {
static int upBorder = 0, downBorder = 10, leftBorder = 5, rightBorder = 5;
int startx = getmaxx() / 2 - textwidth(s) / 2;
int endx = startx + textwidth(s), endy = starty + textheight(s);
rectangle(startx - leftBorder, starty - upBorder, endx + rightBorder, endy + downBorder);
outtextxy(startx, starty, s);
}
/* ************************************************************************ */
void drawObject(class ObjectProperties e, int y) {
setcolor(getbkcolor());
setfillstyle(1, getbkcolor());
fillellipse(e.getOldX(), y, e.getRx(), e.getRy());
line(e.getOldX(), y + e.getRy(), e.getOldX(), y + e.getRy() + 15);
setcolor(e.getColor());
setfillstyle(1, e.getColor());
fillellipse(e.getX(), y, e.getRx(), e.getRy());
line(e.getX(), y + e.getRy(), e.getX(), y + e.getRy() + 15);
}
/* ************************************************************************ */
void browse(class ObjectProperties *e, class Shot *t) {
void drawObject(class ObjectProperties e, int y);
void drawShot(class Shot t);
void play();
void dialog();
char tempstr[6];
for (int i = 0 ; i <= 9 ; i++) {
(*e).setX((*e).getX() + (*e).getRate());
if ((*e).getX() > -100 && (*e).getX() < 0) {
(*e).setVisible(true);
(*e).setX(0);
}
if ((*e).isVisible()) {
drawObject(*e, 70 + i * 40);
delay(20);
}
if ((*t).strike) {
(*t).oldx0 = (*t).x0;
(*t).x0 -= 15;
drawShot(*t);
if ((*t).x0 - (*t).getRx() <= (*e).getX() + (*e).getRx() &&
(*t).x0 + (*t).getRx() >= (*e).getX() - (*e).getRx()) {
player.setScore(player.getScore() + (*e).getColor() * 2);
player.setScoreChanged(true);
(*e).setColor(getbkcolor());
drawObject(*e, 70 + i * 40);
(*t).setColor(getbkcolor());
drawShot(*t);
for (int i = 0 ; i < 100 ; i++) {
sound(i * 100);
}
nosound();
(*t).reset();// = new Shot();
(*e).reset();// = new ObjectProperties();
}
}
if ((*e).getX() - (*e).getRx() > getmaxx()) {
player.setScore(player.getScore() - 5);
for (int i = 0 ; i < 500 ; i++) {
sound(i);
delay(1);
}
nosound();
player.setScoreChanged(true);
(*t).setColor(getbkcolor());
drawShot(*t);
(*t).reset();// = new Shot();
(*e).reset();// = new ObjectProperties();
}
else if (player.getX() >= (*e).getX() - (*e).getRx() &&
player.getX() <= (*e).getX() + (*e).getRx() &&
i == player.getY()) {
player.setLife(player.getLife() - 1);
player.setScore(player.getScore() - 5);
player.setScoreChanged(true);
(*e).setColor(getbkcolor());
drawObject(*e, 70 + i * 40);
(*t).setColor(getbkcolor());
drawShot(*t);
for (int i = 0 ; i < 500 ; i++) {
sound(i);
delay(1);
}
nosound();
(*t).reset();// = new Shot();
(*e).reset();// = new ObjectProperties();
}
if (player.isScoreChanged()) {
setfillstyle(1, getbkcolor());
bar(120, 0, 570, 35);
setcolor(player.getColor());
itoa(player.getScore(), tempstr, 10);
outtextxy(320, 0, "score ");
outtextxy(390, 0, tempstr);
itoa(player.getLife(), tempstr, 10);
outtextxy(120, 0, "Life ");
outtextxy(190, 0, tempstr);
player.setScoreChanged(false);
}
if (player.getLife() == 0 || player.getScore() == 0) {
setcolor(5);
setfillstyle(1, 1);
settextstyle(7, 0, 4);
bar(200, 150, 450, 200);
outtextxy(220, 155, "Game Over!");
for (int i = 1 ; i < 1000 ; i++) {
sound(i);
delay(1);
}
nosound();
dialog();
}
if (player.getScore() >= 500) {
setcolor(2);
setfillstyle(1, 1);
bar(120, 230, 520, 265);
outtextxy(175, 240, "Congratulations. You Won.");
dialog();
}
if (bioskey(1) != 0) break; // if keyPressed
e++; // Next Class
t++; // Next Class
}
}
/* ************************************************************************ */
void dialog(void) {
void play();
setfillstyle(1, 1);
bar(120, 265, 520, 300);
setcolor(14);
settextstyle(7, 0, 2);
outtextxy(135, 270, "Do you want to play again?(Y/N)");
char c;
do {
c = toupper(getch());
} while (c != 'Y' && c != 'N' && c != KEYCODE_ESCAPE);
if ( c == 'Y' ) {
play();
}
else {
closegraph();
exit(0);
}
}
/* ************************************************************************ */
void play(void) {
void browse(class ObjectProperties *e, class Shot *t);
void moveplayer(class Player p);
class ObjectProperties E[10];
player = Player();
setbkcolor(7);
settextstyle(7, 0, 2);
cleardevice();
for (int i = 0 ; i <= 9 ; i++) {
shots[i] = Shot();
E[i] = ObjectProperties();
}
do {
moveplayer(player);
browse(&E[0], &shots[0]);
if (bioskey(1) != 0) { // If KeyPressed
switch (getch()) {
case (' ') : // <space> key
if (!shots[player.getY()].strike) {
shots[player.getY()].x0 = player.getX() - player.getRx() - shots[player.getY()].getRx();
shots[player.getY()].setY(player.getY());
shots[player.getY()].strike = true;
if (E[player.getY()].isVisible()) {
shots[player.getY()].x1 = E[player.getY()].getX() + shots[player.getY()].getRx();
}
else {
shots[player.getY()].x1 = -shots[player.getY()].getRx();
}
}
break;
case (0) :switch (getch()) {
case (KEYCODE_RIGHTARROW) :
if (player.getX() < getmaxx()) {
player.setX(player. getX() + 10);
}
break;
case (KEYCODE_LEFTARROW) :
if (player.getX() > player.getRx() + 30) {
player.setX(player.getX() - 10);
}
break;
case (KEYCODE_UPARROW) :
if (player.getY() > 0) {
player.setY(player.getY() - 1);
}
break;
case (KEYCODE_DOWNARROW) :
if (player.getY() < 9) {
player.setY(player.getY() + 1);
}
break;
}
break;
case (KEYCODE_ESCAPE) :
setcolor(15);
setfillstyle(1, 1);
bar(150, 230, 450, 280);
outtextxy(170, 240, "Quit and Ending (Y/N)");
char c;
do {
c = toupper(getch());
if (c == 'Y') {
closegraph();
exit(0);
}
} while(c != 'N' && c != KEYCODE_ESCAPE);
cleardevice();
player.setScoreChanged(true);
break;
}
} // End of if
} while(1);
} // End of play()
/* ************************************************************************ */
void moveplayer(class Player p) {
setcolor(getbkcolor());
rectangle(20, 40, p.getOldX() - p.getRx() - 10, 460);
setfillstyle(1, getbkcolor());
fillellipse(p.getOldX(), 70 + p.getOldY() * 40, p.getRx(), p.getRy());
setcolor(2);
rectangle(20, 40, p.getX() - p.getRx() - 10, 460);
setcolor(p.getColor());
setfillstyle(1, p.getColor());
fillellipse(p.getX(), 70 + p.getY() * 40, p.getRx(), p.getRy());
}
/* ************************************************************************ */
void drawShot(class Shot t) {
setcolor(getbkcolor());
setfillstyle(1, getbkcolor());
fillellipse(t.oldx0, 70 + t.getY() * 40, t.getRx(), t.getRy());
setcolor(t.getColor());
setfillstyle(1, t.getColor());
fillellipse(t.x0, 70 + t.getY() * 40, t.getRx(), t.getRy());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -