Help With Programming My First Game

Hey guys, this is my first post here, and I am fairly new to programming.
I just completed my first semester of CS, and am going to be starting my second one soon.

Anyways, I am interested in programming my first game. I use Visual Studio, and am going to be programming a Win32 Project.

My professor did a few graphics sessions for those of us that were intersted, but he didn't go too much into detail.

At this point I can make a shape bounce around the screen inside an enclosed box.
I am trying to program a game that drops shapes down the screen, and the goal is to dodge them.
I am looking for the simplest way to do this. I am not sure if there are examples that I can look at, or if you guys have any hints.

Thanks for the help!
welcome! im not the best person to answer this, but i know enough to get you started. i wouldnt use the winapi. i hear its a mess. it also makes your software windows only. if you use sfml, sdl, allegro, or opengl. then it can work on any os. i suggest looking into those
Well this project can also count as extra credit going into my class in the summer, but we have to do it using the winapi.

If I had a choice I wouldn't use it. Is it pretty bad to use?
That should be a fairly straightforward task and you should be able to accomplish it with the knowledge you have already.

The game should basically be like this:

The falling block should have a static (random) x position and a dynamic y position(falling and top as initial). The player block should have a static (bottom) y position and a dynamic x position. When the block hits the ground you are going to want to get a new random x position, reset the y position, and do anything else necessary such as change the color/size. If it hits the player you are going to want to either end the game, remove a life, or something along those lines. You can check if it has collision with the player pretty simple with the position and size.

You already know how to move the blocks the only thing I can think of that you may not know is how to get the keyboard input? If you were a little more specific on what exactly you do not understand it would help us.
Last edited on
If I had a choice I wouldn't use it. Is it pretty bad to use?

Probably a good idea. It isn't really bad to use, itself - it has its advantages and disadvantages over everything else. Its disadvantage is mostly its great complexity, while the advantage is the huge amount of control you therefore get.

For something like what you are doing, there isn't much you need to control and lower complexity would be a bonus, which is why something like SFML would be preferable. Nevertheless, there is nothing wrong per se with using it, and if it gives you extra credit, well why not? It is learning something interesting that you may even find useful at some point.
Well thanks for all the replies guys, I appreciate the help.

I am having a tough time finding out where I am supposed to declare the random static x variable.

If I declare it in my function that draws my ball, then it constantly updates, but if I declare it oustide of my function, then it never changes.

Edit: Okay, here is my function that draws the enclosure, and my block, I am not sure where I declare ther xPos so that it is different every time I start the game. Or a new block is created.

http://pastebin.com/vu6J9wqm
Last edited on
Just create a block object. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
class Block
{
    public:
        Block(unsigned const width, unsigned const height); //unless you want the same for all
        bool move(int const xOffset, int const yOffset); //this way ai and player can call
        bool draw(void);
    private:
        unsigned xPosition;
        unsigned yPosition;
        unsigned width;
        unsigned height;
};
Keep in mind it may be missing a few things you need, this is just so you get a general idea.
I am pretty new to objects and classes. I am not sure how I would implement this.
You could just use RECT structures and the Rectangle() function to draw them and IntersectRect() to check collision between them. That's probably as simple as it gets with winapi.
Last edited on
Okay, I thought I had a way figured out to move a rectangle, but I guess not. The rectangle doesn't move when I use the arrow keys. What is wrong?

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
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	srand(time(NULL));
	PAINTSTRUCT ps;
	HDC hdc;
	int move = 15;
	int fall = 5;
	int l = rand() % 500;
	int r = l + 15;
	int t = 35;
	int b = 55;
	static RECT r1 = {};
	static RECT r2 = {};
	
	switch (message)
	{

	case WM_CREATE:
	{
	r1 = { 270, 425, 295, 465 };
	r2 = { l, t, r, b };
	return 0;
	}
	
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		DrawEnclosure(hWnd, hdc);
		RoundRect(hdc, r1.left, r1.top, r1.right, r1.bottom, 2, 2);
		RoundRect(hdc, r2.left, r2.top, r2.right, r2.bottom, 2, 2);
		r2.top += fall;
		r2.bottom += fall;
		EndPaint(hWnd, &ps);
		break;
	
	case WM_TIMER:
		InvalidateRect(hWnd, NULL, true);
		break;
	
	case WM_KEYDOWN:
		switch (wParam)
		{
		case VK_LEFT:
			r1.left -= move;
			r1.right -= move;
			if (r1.left < 35){
				r1.left = 35;
				r1.right = 60;
			}
			break;

		case VK_RIGHT:
			r1.left += move;
			r1.right += move;
			if (r1.right > 465){
				r1.right = 465;
				r1.left = 440;
			}
			break;
		}
		InvalidateRect(hWnd, NULL, TRUE);
		UpdateWindow(hWnd);
		return 0;
	
	case WM_SIZE:
		return 0;
	
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}
Last edited on
How do you draw a retangle on the screen and move it with WinAPI? you just draw a picture, then move it's X and Y ?
Update: I updated the code above, now I have a block I can move side to side with the arrow keys, and I have one block that falls from the top of the screen.

However, the position is not randomized each time I start the program, which is weird.

Also I want to have multiple blocks falling at a time, how would I do that?
Last edited on
Shakenbake158 wrote:
However, the position is not randomized each time I start the program, which is weird.

Are you creating a new seed each time? try putting this at the top of your main function srand(time(NULL));

Also I want to have multiple blocks falling at a time, how would I do that?

Just create multiple objects. Possibly a vector of the objects then just iterate over the vector and draw(update) each one.

So I would just have to create multiple falling objects like this:

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
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

	PAINTSTRUCT ps;
	HDC hdc;
	int xL = 225, xR = 250;
	int yT = 430, yB = 450;
	int move = 30;
	int fall = 10;
	int left2 = rand() % 200 + 50;
	int right2 = left2 + 20;
	int left3 = rand() % 200 + 50;
	int right3 = left3 + 20;
	int left4 = rand() % 200 + 50;
	int right4 = left4 + 20;
	int left5 = rand() % 200 + 50;
	int right5 = left5 + 20;
	int left6 = rand() % 200 + 50;
	int right6 = left6 + 20;
	int t = 35;
	int b = 60;
	static RECT r1 = {};
	static RECT r2 = {};
	static RECT r3 = {};
	static RECT r4 = {};
	static RECT r5 = {};
	static RECT r6 = {};
	
	switch (message)
	{
	case WM_CREATE:
	{
	srand(time(NULL));
	
	r1 = { 270, 445, 295, 470 };
	r2 = { left2, t, right2, b };
	r3 = { left3, t, right3, b };
	r4 = { left4, t, right4, b };
	r5 = { left5, t, right5, b };
	r6 = { left6, t, right6, b };
	return 0;
	}
	
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		DrawEnclosure(hWnd, hdc);
		RoundRect(hdc, r1.left, r1.top, r1.right, r1.bottom, 2, 2);
		RoundRect(hdc, r2.left, r2.top, r2.right, r2.bottom, 3, 3);
		r2.top += fall;
		r2.bottom += fall;
		if (r2.bottom > 470){
			r2.top = 35;
			r2.bottom = 60;
			r2.left = left2;
			r2.right = right2;
		}
		RoundRect(hdc, r3.left, r3.top, r3.right, r3.bottom, 3, 3);
			r3.top += fall;
			r3.bottom += fall;
			if (r3.bottom > 470){
				r3.top = 35;
				r3.bottom = 60;
				r3.left = left3;
				r3.right = right3;
		}
		RoundRect(hdc, r4.left, r4.top, r4.right, r4.bottom, 3, 3);
			r4.top += fall;
			r4.bottom += fall;
			if (r4.bottom > 470){
				r4.top = 35;
				r4.bottom = 60;
				r4.left = left4;
				r4.right = right4;
		}
		RoundRect(hdc, r5.left, r5.top, r5.right, r5.bottom, 3, 3);
			r5.top += fall;
			r5.bottom += fall;
			if (r5.bottom > 470){
				r5.top = 35;
				r5.bottom = 60;
				r5.left = left5;
				r5.right = right5;
		}
		RoundRect(hdc, r6.left, r6.top, r6.right, r6.bottom, 3, 3);
			r6.top += fall;
			r6.bottom += fall;
			if (r6.bottom > 470){
				r6.top = 35;
				r6.bottom = 60;
				r6.left = left6;
				r6.right = right6;
		}
		EndPaint(hWnd, &ps);
		break;
	
	case WM_TIMER:
		InvalidateRect(hWnd, NULL, true);
		break;
	
	case WM_KEYDOWN:
		switch (wParam)
		{
		case VK_LEFT:
			r1.left -= move;
			r1.right -= move;
			if (r1.left < 20){
				r1.left = 20;
				r1.right = 45;
			}
			break;

		case VK_RIGHT:
			r1.left += move;
			r1.right += move;
			if (r1.right > 295){
				r1.right = 295;
				r1.left = 270;
			}
			break;
		}
		InvalidateRect(hWnd, NULL, TRUE);
		UpdateWindow(hWnd);
		return 0;
	
	case WM_SIZE:
		return 0;
	
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}


I got the random part to work. So next I need to be able to tell when a falling object collides with the object the player is controlling.

How do I do that?
Last edited on
Topic archived. No new replies allowed.