Tải bản đầy đủ (.pdf) (18 trang)

Báo cáo bài tập lớn cấu trúc dữ liệu và giải thuật đề tài corona virus fighter game

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (790.2 KB, 18 trang )

TRƯỜNG ĐẠI HỌC BÁCH KHOA HÀ NỘI
VIỆN ĐIỆN TỬ - VIỄN THÔNG

BÁO CÁO BÀI TẬP LỚN
CẤU TRÚC DỮ LIỆU VÀ GIẢI THUẬT
Đề tài :

CORONA VIRUS FIGHTER GAME

Giáo viên hướng dẫn : T.S Trần Thị Thanh Hải
Sinh viên
Lê Tuấn Anh
Hoàng Đức Nghi
Nguyễn Duy Ninh

MSSV
20172404
20172716
20172739

Hà Nội, ngày 20 tháng 12 năm 2020


NỘI DUNG.................................................................................1
I.

Giới thiệu về thư viện Graphic.h...................................1

II.

Mô tả luật chơi:...............................................................2



III. Thiết kế:...........................................................................2
IV. Code:................................................................................4
KẾT LUẬN...............................................................................17


PHÂN CƠNG NHIỆM VỤ
Menu+ Class Item
Class Person
Class Virus

Lê Tuấn Anh
Hồng Đức Nghi
Nguyễn Duy Ninh
NỘI DUNG
******************

I.

Giới thiệu về thư viện Graphic.h

Page | 1


II.

Mơ tả luật chơi:

Game gồm 1 người, có thể di chuyển (lên xuống, phải, trái) trên
đường đi gặp các chướng ngại vật là virus hoặc các chất dinh

dưỡng. Nếu người đó ăn được dinh dưỡng thì sẽ tăng thêm 1
mạng . Khi va vào chướng ngại vật (virus) thì sẽ bị trừ 1 mạng.
Với mỗi chướng ngại vật (virus) người chơi né được thì người
chơi sẽ được cộng 10 điểm. Khi số mạng của người chơi về 0 thì
trị chơi sẽ kết thúc.
III. Thiết kế:
Để phục vụ chương trình nêu trên nhóm đã quyết định sử dụng 3
lớp đó là lớp Person, Virus,Item.
*Tạo class Person
class Person
{
public:
int xPos, yPos; // vị trí của nhân vật
Page | 2


int speed; // tốc độ di chuyển
int life; mạng sống nhân vật
public:
Person(int x,int y,int speed);//constructor
void draw();
// Vẽ nhân vật
void move();
// Di chuyển nhân vật
};
class Virus
{
public:
int xPos, yPos; // vị trí của nhân vật
int speed; // tốc độ di chuyển

public:
Virus(int x,int y,int speed); //constructor
void draw();
// Vẽ virus
void move();
// Di chuyển virus
void resetPos();
// reset vị trí virus
checkCollusion(Person *person, int& life) ; // Kiểm tra va
chạm giữa virus và người chơi
};
class Item
{
public:
int xPos, yPos; // vị trí của vật phẩm
int speed; // tốc độ di chuyển
bool isColision; // va chạm với person hay chưa ?
public:
Item(int x,int y,int speed); //constructor
void draw();
// Vẽ vât phẩm
void move();
// Di chuyển vật phẩm
void resetItem();
// reset vị trí vật phẩm
checkCollusion(Person *person, int& life) ; // Kiểm tra va
chạm giữa vật phẩm và người chơi
};
Page | 3



*Khai báo cấu trúc Player , lưu thông tin điểm người chơi vào mảng
Player
typedef struct Player_s
{
char name[50];
int point;
} Player;
Player player[50]; // mảng người chơi
int player_index = 0; // số thứ tự người chơi
*Các hàm bổ sung
// sap xep diem so
void swap(Player &x, Player &y);
void sortArray(Player A[], int n);
Ngồi ra cịn 1 số hàm xử lý khác để giúp chương trình hoạt động
: play() ,menu(), highScore() ,vvv
IV.

Code:
#include<iostream>
#include<conio.h>
#include<windows.h>
#include<stdlib.h>
#include<graphics.h>
#include <ctime>
#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
#define ENTER 13

#define leftEdge 180
#define rightEdge 420
Page | 4


using namespace std;
typedef struct Player_s
{
char name[50];
int point;
} Player;
Player player[50];
int player_index = 0;
class Person
{
public:
int xPos,yPos;
int speed;
int life;
public:
Person(int x,int y,int speed)
{
xPos = x;
yPos = y;
this->speed = speed;
life = 3;
}
void move()
{
if (GetAsyncKeyState(VK_LEFT))

{
if ( xPos > leftEdge )
{
xPos -= speed;
}
}
else if (GetAsyncKeyState(VK_RIGHT))
{
if ( xPos < rightEdge )
Page | 5


{
xPos += speed;
}
}
else if (GetAsyncKeyState(VK_UP))
{
if(yPos>20)
{
yPos-=speed;
}
}
else if (GetAsyncKeyState(VK_DOWN))
{
if(yPos<450)
{
yPos+=speed;
}
}

}
void draw()
{
setcolor(2);
setfillstyle(1,2);
sector(xPos,yPos,0,180,20,20);
setfillstyle(1,4);
bar(xPos-20,yPos,xPos+20,yPos+30);
}
};
class Virus
{
public:
int xPos,yPos;
int speed;
public:
Virus(int x,int y,int speed)
{
xPos=x;
yPos=y;
Page | 6


this->speed =speed;
}
void resetPos()
{
if(yPos>=480+35)
xPos = 180+35 + rand() % (420- (180+35)+1);
}

void draw()
{
const int a =20;
const int b =35;
setfillstyle( 1, RED );
bar( xPos-a, yPos-a, xPos + a, yPos + a );
setcolor(GREEN);
setfillstyle(1, 2);
sector(xPos-10,yPos -5,0,360,5,5);
sector(xPos+10,yPos -5,0,360,5,5);
fillellipse(xPos,yPos+10,10,5);
setcolor(WHITE);
line(xPos-a,yPos-a,xPos-b,yPos-b);
line(xPos-a,yPos+a,xPos-b,yPos+b);
line(xPos+a,yPos-a,xPos+b,yPos-b);
line(xPos+a,yPos+a,xPos+b,yPos+b);
}
void move()
{
if ( yPos < 480+35 )
{
yPos += speed;
}
else
{
srand(time(NULL));
yPos = 0;
speed = 10 + rand()%(25-5+1);
}
}

void checkCollusion(Person *person, int& life)
Page | 7


{
int m = -10;
if((person->xPos > xPos-30-35-m && person->xPos <
xPos+30+35+m) && (person->yPos < yPos+35+20+m && person>yPos > yPos-35-20-m))
{
life--;
yPos = -20;
}
}
};
class Item
{
public:
int xPos,yPos;
int speed;
bool isColision;
public:
Item(int x,int y,int speed)
{
xPos = x;
yPos = y;
this->speed = speed;
isColision = false;
}
void resetItem(int &pointMarker,int point)
{

if(yPos>480+20 || point == pointMarker*2|| isColision == true)
{
yPos=-20;
pointMarker*=2;
isColision= false;
xPos = 180+35 + rand() % (420- (180+35)+1);
}
}
Page | 8


void move()
{
yPos+=speed;
}
void draw()
{
setcolor(2);
setfillstyle(1,2);
sector(xPos,yPos,0,360,10,10);
}
void checkCollusion(Person *Person, int &life)
{
if((Person->xPos -20 < xPos && Person->xPos +20 > xPos)
&& (Person->yPos-20 < yPos && Person->yPos + 30 > yPos))
{
life +=1;
cout<<"da va cham hoa qua";
isColision = true;
}

}
};
void Road()
{
setfillstyle(1,1);
bar(180-20,0,420+20,479);
}
void over()
{
cleardevice();
settextstyle( 1, RED, 6 );
setcolor( RED );
outtextxy( 10, 100, "!!!GAME OVER!!!" );
Sleep(1000);
}
void about()
Page | 9


{
cleardevice();
Person person = Person(180,300,10);
Virus cc1 =Virus(180+35,100,10);
Virus cc3 =Virus(330,100,5);
cc1.draw();
person.draw();
cc3.draw();
getch();
}
void highScore()

{
cleardevice();
while ( !kbhit() )
{
settextstyle( 1, RED, 1);
setcolor( RED );
outtextxy(50,20,"Ten");
outtextxy(400,20,"Diem So");
int line_pos = 50;
char text1[50];
char text2[50];
for(int i=0; i{
sprintf(text1,"%s", player[i].name);
sprintf(text2, "%d",player[i].point);
outtextxy( 50, line_pos, text1 );
outtextxy( 400, line_pos, text2 );
line_pos+=30;
}
}
getch();
}
void increasePoint(Virus &cc,int &p)
{
if(cc.yPos >= 480+35)
Page | 10


{
p+=10;

cc.resetPos();
}
}
// sap xep diem so
void swap(Player &x, Player &y)
{
Player temp = x;
x= y;
y= temp;
}
void sortArray(Player A[], int n)
{
int min;
for (int i = 0; i < n - 1; i++)
{
min = i;
for (int j = i + 1; j < n; j++)
if (A[j].point > A[min].point)
min = j;
if (min != i)
swap(A[i], A[min]);
}
}
int play()
{
// tao cac doi tuong
Person person = Person(180,450,10);
Virus cc1 =Virus(180+35,30,5);
Virus cc3 =Virus(330,30,10);
bool isRunning = true;

Item *item1 = new Item(190,60,5);
int pointMarker = 100;
int point = 0;
int life = 3;
while(isRunning)
Page | 11


{
cleardevice();
Road();
person.draw();
// di chuyen
person.move();
//corona 1
cc1.resetPos();
cc1.draw();
cc1.move();
//corona 2
if(point >= pointMarker)
{
item1->move();
item1->draw();
item1->resetItem(pointMarker,point);
}
//corona3
cc3.resetPos();
cc3.draw();
cc3.move();
//kiểm tra va chạm

cc1.checkCollusion(&person,life);
cc3.checkCollusion(&person,life);
// điều kiện kết thúc game khi mạng sống = 0
if(life == 0)
{
isRunning = false;
}
increasePoint(cc1,point);
increasePoint(cc3,point);
//
item1->checkCollusion(&person,life);
//hien thi diem so
char text2[30]="Life: 3";
sprintf(text2, "Life: %d", life);
setcolor(GREEN);
Page | 12


outtextxy(5, 55, text2);
char text1[30]="Point: 0";
sprintf(text1, "Point: %d", point);
setcolor(GREEN);
outtextxy(5, 30, text1);
Sleep(60);
}
if(isRunning==false)
{
Sleep(1000);
system("cls");
fflush(stdin);

cout<<"Nhap ten nguoi choi: ";
cin.getline(player[player_index].name,50);
player[player_index].point = point;
point = 0;
player_index++;
if(player_index + 1 > 1)
{
sortArray(player,player_index);
}
over();
}
return 0;
}
void menu()//for the menu
{
int s = 1, t = 0, a, p = 135;//here s is for keystroke selection in
switch case
do//to contiue the program after finish
{
Page | 13


//here a is for the rectangle to move up and down
do
//p for rectangle use in menu
{
cleardevice();
setcolor( YELLOW );
settextstyle( 1, 0, 4 );
outtextxy( 40, 30, "CORONA VIRUS GAME" );

setfillstyle( 1, 2 );
settextstyle( 1, 0, 1 );
setcolor( 10 );
outtextxy( 220, 137, "PLAY GAME" );
outtextxy( 220,167, "HIGH SCORE" );
outtextxy( 220,167+30, "ABOUT" );
outtextxy( 220,197+30, "EXIT" );
setcolor( RED );
rectangle( 210, p, 400, p + 20 );//for rectangle using
rectangle func
a = getch();
switch ( a ) //for up and down movemen if rectangle using
keyboard
{
case UP:
if ( p > 140 )
{
p -= 30;
s--;
}
break;
case DOWN:
if ( p < 220 )
{
p += 30;
s++;
};
break;
case ENTER:
a = 1;

Page | 14


break;
default:
break;
}
}
while ( a != 1 );
switch ( s )
{
case 1:
play();
break;
case 2:
highScore();
break;
case 3:
about();
break;
case 4:
exit(0);
break;
}
}
while ( t == 0 );
}
int main()
{
int gdriver = DETECT, gmode;

initgraph( &gdriver, &gmode, "C:\\TC\\bgi" );
menu();
play();
}

Page | 15


KẾT LUẬN
************************
Phần mềm đạt một số mục tiêu nhất định tuy nhiên cịn nhiều
khuyết điểm như tính năng hạn chế. Gặp một vài lỗi trong quá trình chạy.
Tuy nhiên những gì thu được từ sau bài tập lớn lần này giúp chúng
em tự tin hơn để tiếp tục đam mê trong ngành lập trình.
Chúng em rất mong nhận được sự quan tâm và góp ý từ phía cơ!
Chúng em xin chân thành cảm ơn!

Page | 16



×