C++ GAME HELP!

Pages: 123
so i am making a shooter game.
I am facing some problem that is i want my character to shoot ^ when space is pressed here is the code:

1
2
3
4
5
6
7
8
if(hit == ' '){
//should i add loop here?
    gotoxy(x,y);
      printf(" ");
x2; y2 = lasty + 1;  // lastx and lasty are the positions of the character
 printf("^");
 Sleep(200);
 }


please help i need it urgently
please some one?? its urgent~!
You realize that it would take more time for you to get this to work then it would to learn a library like SFML or SDL right? Here is some sample code I wrote for a space shooter, it's about two years old so it uses and older version of SFML but it still illustrates how simple this is:

1
2
3
4
if(Event.Key.Code == sf::Key::Space) // <- LOOK HERE
{
                Shot.Fire(MyShip.Pos);
}


This was one of my early attempts at using SFML and I'm not that awesome of a programmer so it isn't the great, but this shows how easy this stuff is with the proper library. The part I tagged with LOOK HERE is all you need to detect a keystroke.
i am sorry, but we are not allowed to use this. My teacher says i have to make it very basic no matter how big it is.

sorry :( though thanks

if u could tell me the problem in my code?
I would have to see more of your "code" to be sure, but my guess is that the problem is in your overall control flow. Programs use what is sometimes called a "Running Loop" during the course of their lifetime, an over simplification of this is: 1.) Look for input 2.) update status 3.) draw to screen. All three of these steps should be contained in the running loop, the section of code you are asking about should be part of step 1.

Also, if you want to draw a carrot, then the variable you pass to "printf()" on Line 4 should be a carrot, and "gotoxy()" is a terrible function to use for what I think you mean to use it for.

I honestly don't believe that your teacher would assign you a shooter game and then not allow you to use any graphic libraries. This would be like a math teacher telling you to do geometry without either a compass or a protractor.
Acually our teacher told us to make a text based shooter,
if u need the code i can give u but its pretty long, try to compile it. Do u want the code?


and for the graphics i am using symbols like bricks.
Last edited on
the code is too long to be posted here so i poted it here:

link:-
http://codeviewer.org/view/code:3fc4
Acually our teacher told us to make a text based shooter


Someone should remind your teacher that there is not, nor has there ever been, such thing as a "text based shooter".

As for your code goes; I have to say that your dedication is unbelievable, I have honestly never seen anyone demonstrate this kind of determination to get their code to work. That being said, the skill level shown here is simply non-existent. I'm sorry but in order to finish this project you would need a lot more help then I can provide on a forum.
I think what the teacher is getting at is frames in text like launching a text rocket.

^
||
||
||
/ \

The idea being print a window, clear screen, print a window, etc. Here you overwrite every screen.
So yes you would loop and modify where your ^ function is called in the screen. (lucky its only one char cout << "^" )
He is probably trying to get you to think in a modular mentality like calling the print_the_rocket( ) at the appropriate height within the loop of clearing and posting the image to the device context, which in your case is the console screen. So if row ==?? and col==?? print ("^"); and refine from there might be easier for ya.
As in graphics it can be quite fast so you would need a method of wait or sleep/idle states between updates/prints.
Last edited on
please just help me with the shooting part, i tried loop but it showed error


I have to say that your dedication is unbelievable


thanks.

Plus I am in a group of 5 and only I know how to programme( begginer ), and i am the only one doing. Thats why i need help


Thanks though
Last edited on

It is possible to do a shooter in the console, although it wont be perfect and wont look the best.

I noticed your post and thought I'd lend you a hand, take a look at this example.

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

#include <iostream>
#include <Windows.h>   // for gotoxy
#include <conio.h>   // for _kbhit()
using namespace std;

#define BULLET_SPEED 10;

struct Bullet
{
	int x;
	int y;
	int timer;  // simple timer to slow bullet a bit
	bool alive;

	Bullet() { alive = false; x = 0; y = 0; timer = BULLET_SPEED; }

	void Update()
	{
		timer--;
		if (timer == 0) {
			if (alive) {
				y--;
				if (y < 1)
					alive = false;
			}
			timer = BULLET_SPEED;  // 10 seemed fine on this machine
		}
	}

};

void gotoxy(int x, int y);
void shootBullet(Bullet bullets[], int shipX, int shipY);
void updateBullet(Bullet bullets[]);


int main()
{

	// for simplicity, max 10 bullets at a time
	Bullet bullets[10];

	bool playing = true;
	char ch;

	int shipXPos = 0, shipYPos = 40;
	int bulletXPos = 0, bulletYPos = 0;
	int bulletAlive = false;

	do
	{
		
		// update the bullets pos
		updateBullet(bullets);

		// have i sensed a key press? - we need to do this
		// because _getch would normally wait and therefore
		// stop the updating of bullets etc.

		if (_kbhit())
		{
			// yes, what was it?
			ch = _getch();

			switch (ch)
			{
			case 'z':
				shipXPos--;
				if (shipXPos < 0) shipXPos = 0;
				break;
			case 'x':
				shipXPos++;
				if (shipXPos > 50) shipXPos = 50;
				break;
			case ' ':
				shootBullet(bullets, shipXPos, shipYPos-1);
				break;

			}		
			system("cls");
		}
		// draw the ship
		gotoxy(shipXPos, shipYPos);
		cout << " * ";
		gotoxy(shipXPos, shipYPos + 1);
		cout << "***";

	} while (playing);

	return 0;

}

// update all the bullets
void updateBullet(Bullet bullets[])
{
	for (int i = 0; i < 10; i++)
	{
		if (bullets[i].alive) {
			// bullet it active, before we update
			// its position, we need to clear out
			// the old one.
			gotoxy(bullets[i].x+1, bullets[i].y);
			cout << " ";
			// update its position
			bullets[i].Update();
			// display it
			gotoxy(bullets[i].x+1, bullets[i].y);
			cout << "*";
		}
	}
}

// shoot the bullet
void shootBullet(Bullet bullets[], int shipX, int shipY)
{
	for (int i = 0; i < 10; i++)
	{
		// find a bullet thats expired
		if (!bullets[i].alive)
		{
			// found one, set its position.
			bullets[i].x = shipX;
			bullets[i].y = shipY;
			bullets[i].alive = true;  // flag it as active
		}
	}
}

// gotoxy
void gotoxy(int x, int y)
{
	COORD coord;
	coord.X = x;
	coord.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}



If you have any questions about the code please feel free to ask.

Z, X = left/right and Space bar is shoot.

Well as i told u i am new, could u please explain me this part...

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
#define BULLET_SPEED 10;

struct Bullet
{
	int x;
	int y;
	int timer;  // simple timer to slow bullet a bit
	bool alive;

	Bullet() { alive = false; x = 0; y = 0; timer = BULLET_SPEED; }

	void Update()
	{
		timer--;
		if (timer == 0) {
			if (alive) {
				y--;
				if (y < 1)
					alive = false;
			}
			timer = BULLET_SPEED;  // 10 seemed fine on this machine
		}
	}

};



my teacher will ask me questions based on it.

Thanks a lot!

structures are a group of data elements grouped together under one name. In this case our group name is Bullet. Check here: http://www.cplusplus.com/doc/tutorial/structures/ for detailed info.

Basically, on line 42 I tell the compiler I want 10 instances (10 groups) of type Bullet so I basically have 10 x int x, int y, etc. but they are seperated by the group name and index (like an array)

so here I did a array of type Bullet like you would do a int or char array

Bullet bullets[10];

First bullet is at index 0, last is at 9 (just like arrays)

Structures can also hold functions, here I use a function called Update() which is called at line 107 inside the updateBullets() function. Notice the updateBullets() function looping 10 times through each instance of the structure Bullet and updating their positions.

Inside the update I use the variable alive, this simply is a boolean and when bullet reaches top of the screen this is set to false and the bullet hidden. timer, is just a simple countdown to slow down the bullet.

I hope that made sense, if not fire away with more questions.







Last edited on
i tried doing this
1
2
3
4
5
6
7
8
9
10
11
  if(hit == ' '){
        while(true){
    gotoxy(x,y);
      printf(" ");
x2; y2 = lasty + 1;  //lasty  and lastx are the positions of the character. 
// x2 is = last x so that this '^' shoots from the players x pos.
 printf("^");
 Sleep(200);
        }
 }
   


i tried this stuff but it wont work.

well the code which you softrix gave me is amazing but i am curious why wont my code work?

You could do without the structure but you could only fire 1 bullet at a time, if you prefer not using a structure then let me know.

Sorry not looked at yours properly, and just noticed the link.

Copy and past your whole code into a post here, the link puts annoying line numbers in which doesn't help if I want to copy/paste your code to play around.


my code i s too long to fit here
ill try posting somewhere else..

i called up my friend asking are we allowed to use structures and he told me no. He showed me the Instructions it said basic If and else, switches, loops and anything non complex. As i am 14 years old i am not eligible to learn c++ at school, so, I joined the club.. our teacher alloted us in such a way that the children who know programming are kept with dumb children so we can explain them, thats why we must not use something complex.


here is the new link
http://pastebin.com/K5exJkUh
Last edited on
REFERENCE TO CODE:-
in the code, that i linked..

most people without noicing ask what is <<b<< stuff?(I asked for this shooting problems on other forums too but everyone discouraged me so i tried out here)

<<b<< is a design i have made, i have declared the value of b to full brick in the code.

There are quite a lot of problems in your code, and not directly related to the bullet issue. For example, you use getch() to read a character into ch1, thats a single character which is read but in your switch statement you are testing for 13 (which appears twice, i.e. two cases testing for 13).

You should also avoid global variables, y1 is actually a function in a system header file and will conflict with your global variable.

Also note getch waits until a key is pressed before moving on, so basically a bullet would only update if you moved your player, know what I mean?

There are quite a lot of problems in your code to go further.

I can modify my example code to not use the structure, from there you could modify it with your extra bits and pieces but I really haven't got much time to go right through your code sadly.

If you want me to modify the code let me know.
I'd love too!

THANKS A LOT! YOU SAVED ME!
and i am really sorry about taking your precious time

its fine i knew my code was a bit pathetic though.
Last edited on
Pages: 123