-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabAssignment6.c
More file actions
124 lines (116 loc) · 1.85 KB
/
LabAssignment6.c
File metadata and controls
124 lines (116 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <Windows.h>
#include<conio.h>
void gotoxy(int x, int y)
{
COORD c = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void setcursor(bool visible)
{
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO lpCursor;
lpCursor.bVisible = visible;
lpCursor.dwSize = 20;
SetConsoleCursorInfo(console, &lpCursor);
}
void setcolor(int fg, int bg)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, bg * 16 + fg);
}
void drawShip(int x, int y)
{
gotoxy(x, y);
setcolor(2, 4);
printf("<-0->");
}
void eraseShip(int x, int y)
{
gotoxy(x, y);
setcolor(0, 0);
printf(" ");
}
void drawBullet(int x, int y)
{
gotoxy(x, y);
setcolor(2, 4);
printf("^");
}
void eraseBullet(int x, int y)
{
gotoxy(x, y);
setcolor(0, 0);
printf(" ");
}
int main()
{
char ch = ' ';
int check=0;
int bullet[5] = { 0 };
int posX[5], posY[5];
int x = 38, y = 20;
drawShip(x, y);
setcursor(0);
do {
if (_kbhit()) {
ch = _getch();
if (ch == 'a')
{
check = 1;
}
if (ch == 'd' && x<80)
{
check = 2;
}
if (ch == 's' && y<100)
{
check = 0;
}
if (ch == ' ')
{
for (int i = 0; i < 5; i++)
{
if (bullet[i] == 0)
{
bullet[i] = 1;
posX[i] = x + 2;
posY[i] = y - 1;
break;
}
}
}
fflush(stdin);
}
if (check == 1 && x > 0)
{
eraseShip(x, y);
drawShip(--x, y);
}
if (check == 2 && x < 80)
{
eraseShip(x, y);
drawShip(++x, y);
}
if (check == 0)
{
eraseShip(x, y);
drawShip(x, y);
}
for (int i = 0; i < 5; i++)
{
if (bullet[i] == 1)
{
eraseBullet(posX[i], posY[i]);
drawBullet(posX[i], --posY[i]);
if (posY[i] <= 0)
{
bullet[i] = 0;
eraseBullet(posX[i], posY[i]);
}
}
}
Sleep(50);
} while (ch != 'x');
return 0;
}