understanding some code

Hey so I've been asked to create a fairly simple zombie survival game for my university... being as what they've taught me so far has no help what so ever in making this game (except maybe while/for loops... alittle) I've decided to do some research find some code and learn the code myself so I don't fail the course..

so I find this really good youtube video of a guy making a simple game of him moving a character '@' when I was looking through his code I noticed he was doing things I've never seen before and as such I thought I would ask you beautiful people on Cplusplus if you could perhaps explain why he used the lines of code that he did

In the code below is my current attempt at a game, the top is totally done by me alone but the code is what I've taken from the youtube video myself in hope it will better educate me in what I have to write for my program.

anyways the key points I'm struggling to understand is the code line "switch" onwards... was wondering if people could explain why he was using a function called "Switch" what it does ect.. how does it fit into this bit of code all that sexy stuff

obviously this isn't the whole code he wrote just what he wrote to make his character '@' to go up

probably should mention I got no clue what "system("cls");" means either

Kind regards x

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
  #include <iostream>
#include <Windows.h>
#include <ctime>

using namespace std;

char Map[30][53] = {

"####################################################",
"#                                                  #",
"#                 O                                #",
"#                                    Z    O        #",
"#       Z                                          #",
"#                  O                               #",
"#                              Z                   #",
"#                                                  #",
"#            Z                      O              #",
"#                                                  #",
"#                                                  #",
"#      O                       Z                   #",
"#                                       O          #",
"#                 O                                #",
"#                                                  #",
"#    O                                             #",
"#         Z                            M           #",
"####################################################" };



bool running = true;
int Gamespeed = 100;

nt main() {

	while (running == true)
	{
		system("cls");
		for (int y = 0; y < 30; y++)
		{
			cout << Map[y] << endl;
		}
		for (int y = 0; y < 19; y++)
		{
			for (int x = 0; x < 53; x++)
			{
				switch (Map[y][x])
				{
				case 'M':
				{
					if (GetAsyncKeyState(VK_UP) != 0)
					{
						int y2 = (y - 1);
						
						switch (Map[y2][x])
						{
						case ' ':
						{
							Map[y][x] = ' ';
							y -= 1;
							Map[y2][x] = 'M';
						}
						}
					}
				}
				}
			}
			Sleep(Gamespeed);
		}
	}

	return 0;
}
Last edited on
hiya,
it's not a function, it's a statement:
http://www.cplusplus.com/doc/tutorial/control/

Take a look towards the end of that page.

it's a bit like an if..else if...else if... else if type o' thing.
Last edited on
@mutexe, yeah I didn't mean function function I just couldn't think of the proper word off the top of my head to put in there haha :)
But you get what it does now? That's the important thing.
@mutexe yeah thanks bro that linked really helped me understand the reason he used "switched" although the code following that

mainly

" int y2 = (y - 1);

switch (Map[y2][x])
{
case ' ':
{
Map[y][x] = ' ';
y -= 1;
Map[y2][x] = 'M';
}
}
"
I'm struggling to get. I'm curious as to why he made a new int called y2 and why he made a brand new switch for y2 and how he was able to edit the array adding y2 to it instead of "y"
The code is saying, for each element in my 2d array:
1. move the character up one (second switch statement)
2. BUT only if the character is an M (first switch statement)

the y2 stuff is converting between a zero-based scheme and a one based scheme. I think :)
@mutexe uhmmm Okey, I'm understanding most of it, some of it i'm still a little fuzzy on, but better than when I first came across this code :)
In this case y2 is the line above the current line. And by the way it will crash if y is 0. Don't know if checking for 'M' on line 48 will prevent this.
@coder777 that made some sense to me, I've kinda just gone to the mind frame of "if your doing this type of code that's the line of code you use" sort of mentality which is probably incorrect to have.

It hasn't crashed for me yet. but I thought I would give ago writing the rest of the code to make 'M' go down left and right on my own without looking at the youtube video, and I've come across a snag which may be relating to the game "crashing" that you mentioned.. I've managed to get the 'M' to move up right and down, but for some reason I cannot get 'M' to move left... I've sure I have entered the code correctly.. but for educational sake see if you can spot the error I may have done

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
#include <iostream>
#include <windows.h>
#include <ctime>

using namespace std;

char Map[30][53] = {

"####################################################",
"#                                                  #",
"#                 O                                #",
"#                                    Z    O        #",
"#       Z                                          #",
"#                  O                               #",
"#                              Z                   #",
"#                                                  #",
"#            Z                      O              #",
"#                                                  #",
"#                                                  #",
"#      O                       Z                   #",
"#                                       O          #",
"#                 O                                #",
"#                                                  #",
"#    O                                             #",
"#         Z                            M           #",
"####################################################" };



bool running = true;
int Gamespeed = 100;

int main() {

	while (running == true)
	{
		system("cls");
		for (int y = 0; y < 30; y++)
		{
			cout << Map[y] << endl;
		}
		for (int y = 0; y < 19; y++)
		{
			for (int x = 0; x < 53; x++)
			{
				switch (Map[y][x])
				{
					case 'M':
					{
						if (GetAsyncKeyState(VK_UP) != 0)
						{
							int y2 = (y - 1);
						
							switch (Map[y2][x])
							{
								case ' ':
								{
									Map[y][x] = ' ';
									y -= 1;
									Map[y2][x] = 'M';
								}break;
							}
						}

						if (GetAsyncKeyState(VK_DOWN) != 0)
						{
							int y2 = (y + 1);

							switch (Map[y2][x])
							{
								case ' ':
								{
									Map[y][x] = ' ';
									y += 1;
									Map[y2][x] = 'M';
								}break;
							}
						}

						if (GetAsyncKeyState(VK_RIGHT) != 0)
						{
							int x2 = (x + 1);

							switch (Map[y][x2])
							{
								case ' ':
								{
									Map[y][x] = ' ';
									x += 1;
									Map[y][x2] = 'M';
								}break;
							}
						}
						if (GetAsyncKeyState(VK_LEFT) != 0)
						{
							int x2 = (x - 1);

							switch (Map[y][x2])
							{
								case ' ':
								{
									Map[y][x] = ' ';
									x -= 1;
									Map[y][x2] = 'M';
								}
							}
						}
					}
				}break;
			}
		}
			Sleep(Gamespeed);
	}
    }

	return 0;
}
Almost overlooked that...

The problem might be GetAsyncKeyState(...). For me the don't work at all. In your case they might not always work as expected. I'd say that GetAsyncKeyState isn't meant for this scenario
Topic archived. No new replies allowed.