Help with AI enemies

I need help with moving my enemies for a top "snake" view type of game. The objective is to get points and avoid enemies but I can't seem to figure out a way to make them move, in sort of a path OR randomly around the screen without disrupting the game.

Sorry if some of it is in Swedish but if there's anything you don't understand, ask.
It's a fairly simple game, but it's something I have to be done with until 7 feb. I'm nearly done with the actual game except dying and be able to lose.

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
void runGame() //The game
{
	system("CLS");

	dY = 0; // "Resetar" x och y värdet för gubben ifall man avslutar spelet till menyn och sen startar det igen
	dX = 0; // Så den inte spårar vidare där den var


	textbackground(BLACK);
	textcolor(LIGHTCYAN);

	int cl = 60;
	int score = 0;
	int l = 0; //räknar antalet loopar
	int r = rand() % 800 + 200; //Random antal loopar som den ska räkna upp till för att spawna supermat

	printmap(); //ritar ut spelplanen
	instruct(); //instruktionerna
	printmap();
	enemyspawn();



	gotoxy(15, 32);
	textcolor(RED);
	cout << "Lives left:" << lives << " " << char(3);
	gotoxy(60, 32);
	textcolor(LIGHTCYAN);
	cout << "Your points:" << score << endl;


	getpoints2(); //Placerar ut maten

	int key = 0;
	while (key != 27)
	{
		/*gotoxy(40, 32);
		textcolor(LIGHTGRAY);
		cout << cl;*/

		if (l >= r)
		{
			getpoints1(); 	 //super
			l = 0;
			r = rand() % 800 + 200;
		}



		gotoxy(x, y); //Sudd
		cout << " ";

		if (_kbhit())	// Tangent nedtryckt
		{
			key = _getch();
			checkKeygame(key);
		}

		//Ändra position
		x = x + dX;
		y = y + dY;

		collision(); //Kollar om gubben tar i väggen.
		gotoxy(x, y);

		if (x == gX2 & y == gY2) //Byter position och uppdaterar poäng om gubbens och matens x y är samma.
		{
			getpoints2();
			score = score + 350;
			textcolor(LIGHTCYAN);
			gotoxy(60, 32);
			cout << "Your points:" << score << endl;

		}
		if (x == gX1 & y == gY1) //Byter position och uppdaterar poäng om gubbens och matens x y är samma.
		{
			getpoints1();
			score = score + 1250;
			textcolor(LIGHTCYAN);
			gotoxy(60, 32);
			cout << "Your points:" << score << endl;
		}

		gotoxy(x, y); // Rita nya positionen
		textcolor(LIGHTCYAN);
		cout << char(190);

		l++;


		Sleep(40); //Hastighet
	}
	return;
}
//------------------------------------------------------
void checkKeygame(int key) //Tangentnedtryckningen i spelet
{
	if (key == 72)		//pil upp
	{
		dY = -1;
		dX = 0;
	}
	else if (key == 80)	//pil ned
	{
		dY = 1;
		dX = 0;
	}
	if (key == 75)		//pil vänster
	{
		dX = -1;
		dY = 0;
	}
	else if (key == 77)	//pil höger
	{
		dX = 1;
		dY = 0;
	}
}







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
void enemyspawn()
{
	gotoxy(en1X, en1Y);
	textcolor(RED);
	cout << char(15);

	gotoxy(en2X, en2Y);
	textcolor(RED);
	cout << char(15);

	gotoxy(en3X, en3Y);
	textcolor(RED);
	cout << char(15);

}
[code]
void enemycollision()
{

	//X koordinater

	//Fiende 1 
	if (en1X<11) //Vänster
	{
		en1X = 77;
	}

	else if (en1X>78) //Höger
	{
		en1X = 11;
	}


	//Fiende 2 
	//-------------------------
	if (en2X<11) //Vänster
	{
		en2X = 77;
	}

	else if (en2X>78) //Höger
	{
		en2X = 11;
	}

	//Fiende 3 
	//-------------------------
	if (en3X<11) //Vänster
	{
		en3X = 77;
	}

	else if (en3X>78) //Höger
	{
		en3X = 11;
	}



	//Y koordinater

	//Fiende 1
	else if (en1Y<1) //Uppe
	{
		en1Y = 29;
	}
	else if (en1Y>29) //Nere
	{
		en1Y = 1;
	}
	//-----------------------

	//Fiende 2
	else if (en2Y<1) //Uppe
	{
		en2Y = 29;
	}
	else if (en2Y>29) //Nere
	{
		en2Y = 1;
	}
	//-----------------------

	//Fiende 3
	else if (en2Y<1) //Uppe
	{
		en2Y = 29;
	}
	else if (en2Y>29) //Nere
	{
		en2Y = 1;
	}
	//-----------------------
}
Last edited on
Why a console game o.O

Also the easiest way would be to make command functions for you and the ai. Ex: Moving would be a command function.

Also is there a reason you are making it a console game? Seems a bit trivial compared to with graphics.
Why a console game o.O

Also the easiest way would be to make command functions for you and the ai. Ex: Moving would be a command function.

Also is there a reason you are making it a console game? Seems a bit trivial compared to with graphics.


Well, it's homework basically, I'm in a programming class, the idea was to create our own game with a menu. :/
Last edited on
Topic archived. No new replies allowed.