code isnt working properly?

Jun 28, 2017 at 3:46am
the code runs but the input isnt doing what it is supposed to do, ive used flags to see if it is getting the input, which it is, but it still doesnt work, nad after i press say 'w' it crashses with error 0x0

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
#include "stdafx.h"
#include <iostream>
#include "Console.h"
#include <windows.h>
#include <string>
#include <sstream>
#include <conio.h>

using namespace std;

int inputKey; //key input

bool gamerunning;

Console console;

int main()
{
	while(gamerunning);
	{
		stringstream ss;
		ss << number_of_coins;
		string items = "COINS: ";
		items += ss.str();
		
	system("cls");

		for(int i = 0; i < 10; i++)
			{
					cout <<  map[i] << endl;
			}
		for(int i = 0; i < 1; i++)
			{
					cout <<  items << endl;
			}
		
	system("pause>nul");
		{

		inputKey = _getch();

		if(inputKey == 'w')
				console.Move(-1, 0);

		if(inputKey == 's')
				console.Move(1, 0);

		if(inputKey == 'd')
				console.Move(0, 1);

		if(inputKey == 'a')
				console.Move(0, -1);
		}
	}
}

Last edited on Jun 30, 2017 at 12:35am
Jun 28, 2017 at 3:47am
cosnole.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
#include "stdafx.h"
#include "Console.h"
#include <conio.h>

int x = 1;
int y = 1;

int V = 0;
int H = 0;

int number_of_coins;

char map[10][30] =
{
	"##########       ##########",
	"#@                 #       #                    #",
	"#                    #####                   #",
	"#                                                    #",
	"#  **              #####                   #",
	"#                    #       #                    #",
	"##########       ##########"
};

Console::Console()
{
}


Console::~Console()
{
}

void Console::Move(int V, int H)
{
	int y2 = y + V;
	int x2 = x + H;

	if(map[y2][x2] == '*')
			number_of_coins++;

	if(map[y][x2] == ' ' || map[y][x2] == '*')
	{
		map[y][x] = ' ';
		x += H;
		map[y][x] = '@';
	}

	if(map[y2][x] == ' ' || map[y2][x] == '*')
	{
		map[y][x] = ' ';
		y += V;
		map[y][x] = '@';
	}
}
Last edited on Jun 28, 2017 at 10:32pm
Jun 28, 2017 at 3:49am
console.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef CONSOLE_H
#define CONSOLE_H
#pragma once

class Console
{
public:
	Console();
	~Console();
	void Move(int V, int H);
};

extern char map[10][30];
extern int x;
extern int y;

extern int number_of_coins;

#endif
Last edited on Jun 28, 2017 at 10:32pm
Jun 28, 2017 at 3:51am
the map didnt show up properly on this website
Jun 28, 2017 at 3:52am
debug output

1>------ Build started: Project: 2d_game, Configuration: Debug Win32 ------
1> source.cpp
1> 2d_game.vcxproj -> U:\2d_game\Debug\2d_game.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Jun 28, 2017 at 1:46pm
In the closing tag, the / goes before the word "code", not after it.
Topic archived. No new replies allowed.