-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
144 lines (121 loc) · 3.8 KB
/
main.cpp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// Created by sean on 14/02/23.
//
// https://chat.openai.com/chat using prompt "generate a c++ program for a simple video game including obstacles and enemies"
//
#include <ncurses.h>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
// Define a struct for the enemy objects
struct Enemy {
int x;
int y;
char symbol;
int color_pair;
};
// Function prototypes
void moveEnemies(Enemy enemies[], int num_enemies, int player_x, int player_y);
int main() {
// Initialize ncurses
initscr();
noecho();
curs_set(0);
keypad(stdscr, true);
start_color();
srand(time(nullptr));
// Define the player object
int player_x = 10;
int player_y = 10;
char player_symbol = '@';
int player_color_pair = 1;
// Define the enemy objects
int num_enemies = 5;
Enemy enemies[num_enemies];
for (int i = 0; i < num_enemies; i++) {
enemies[i].x = rand() % 40;
enemies[i].y = rand() % 20;
enemies[i].symbol = 'E';
enemies[i].color_pair = 2;
}
// Define color pairs
init_pair(player_color_pair, COLOR_GREEN, COLOR_BLACK);
init_pair(2, COLOR_RED, COLOR_BLACK);
// Start the game loop
int ch = 0;
int max_x, max_y;
getmaxyx(stdscr, max_y, max_x);
while (ch != 'q') {
// Clear the screen
clear();
// Draw the player
attron(COLOR_PAIR(player_color_pair));
mvaddch(player_y, player_x, player_symbol);
attroff(COLOR_PAIR(player_color_pair));
// Draw the enemies
for (int i = 0; i < num_enemies; i++) {
attron(COLOR_PAIR(enemies[i].color_pair));
mvaddch(enemies[i].y, enemies[i].x, enemies[i].symbol);
attroff(COLOR_PAIR(enemies[i].color_pair));
}
// Move the enemies
moveEnemies(enemies, num_enemies, player_x, player_y);
// Handle user input
switch(ch) {
case KEY_UP:
if (player_y > 0)
player_y--;
break;
case KEY_DOWN:
if (player_y < max_y - 1)
player_y++;
break;
case KEY_LEFT:
if (player_x > 0)
player_x--;
break;
case KEY_RIGHT:
if (player_x < max_x - 1)
player_x++;
break;
}
// Refresh the screen
refresh();
// Get the maximum x and y coordinates of the screen
getmaxyx(stdscr, max_y, max_x);
// Wait for user input or a short time delay
ch = getch();
napms(50);
}
// Clean up ncurses
endwin();
return 0;
}
void moveEnemies(Enemy enemies[], int num_enemies, int player_x, int player_y) {
for (int i = 0; i < num_enemies; i++) {
// Calculate the distance between the enemy and the player
int dist_x = player_x - enemies[i].x;
int dist_y = player_y - enemies[i].y;
int dist = sqrt(dist_x * dist_x + dist_y * dist_y);
// If the enemy is close enough to the player, move it toward the player
if (dist <= 5) {
// Calculate the direction to move in
int dir_x = 0;
if (player_x > enemies[i].x)
dir_x = 1;
else if (player_x < enemies[i].x)
dir_x = -1;
int dir_y = 0;
if (player_y > enemies[i].y)
dir_y = 1;
else if (player_y < enemies[i].y)
dir_y = -1;
// Move the enemy in the chosen direction
if (enemies[i].x + dir_x >= 0 && enemies[i].x + dir_x < COLS)
enemies[i].x += dir_x;
if (enemies[i].y + dir_y >= 0 && enemies[i].y + dir_y < LINES)
enemies[i].y += dir_y;
}
}
}