Advertisement

win 32 pong

Started by July 01, 2019 09:49 PM
40 comments, last by phil67rpg 5 years, 5 months ago

I am working on a pong game  using windows and c++ I am trying to get the computer paddle to move  up and  down by itself, here is my movement code,


		comp_velocity = (comp_move < screen_width&&flag == 0.0f) ? (comp_velocity = 0.1f) : (comp_velocity = -0.1f);

		comp_move = (comp_velocity == 0.1f) ? (comp_move += 1.0f) : (comp_move -= 1.0f);

		if (comp_move >= screen_width)
		{
			flag = 1;
		}

		if (comp_move <= -screen_width)
		{
			flag = 0;
		}

and here is my overall code


#ifndef UNICODE
#define UNICODE
#endif 

#include <windows.h>

float comp_move = 0.0f, comp_velocity = 1.0f, flag = 0.0f, screen_width = 30.0f;

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
	const wchar_t CLASS_NAME[] = L"Sample Window Class";

	WNDCLASS wc = {};

	wc.lpfnWndProc = WindowProc;
	wc.hInstance = hInstance;
	wc.lpszClassName = CLASS_NAME;

	RegisterClass(&wc);

	// Create the window.

	HWND hwnd = CreateWindowEx(
		0,                              // Optional window styles.
		CLASS_NAME,                     // Window class
		L"Pong",    // Window text
		WS_OVERLAPPEDWINDOW,            // Window style

										// Size and position
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

		NULL,       // Parent window    
		NULL,       // Menu
		hInstance,  // Instance handle
		NULL        // Additional application data
	);

	if (hwnd == NULL)
	{
		return 0;
	}

	ShowWindow(hwnd, nCmdShow);

	// Run the message loop.

	MSG msg = {};
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return 0;
}
int y = 0;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	
	case WM_KEYDOWN:
		switch (wParam)
		{
		case VK_UP:
			y-=5;
			if (y <= -300)
			{
				y = -300;
			}
			break;
		case VK_DOWN:
			if (y >= 310)
			{
				y = 310;
			}
			y+=5;
			break;
		}
		InvalidateRect(hwnd, NULL, TRUE);

	case WM_PAINT:
	{
		PAINTSTRUCT ps;
		HDC hdc = BeginPaint(hwnd, &ps);

		FillRect(hdc, &ps.rcPaint, (HBRUSH)(2));

		RECT rect = { 0,300+comp_move,30,400+comp_move };
		HBRUSH brush = CreateSolidBrush(RGB(0, 0, 255));
		FillRect(hdc, &rect, brush);

		RECT rect_one = { 1395,300+y,1425,400+y };
		HBRUSH brush_one = CreateSolidBrush(RGB(255, 0, 0));
		FillRect(hdc, &rect_one, brush_one);

		RECT rect_two = { 702,340,722,360 };
		HBRUSH brush_two = CreateSolidBrush(RGB(255, 255, 255));
		FillRect(hdc, &rect_two, brush_two);
		EndPaint(hwnd, &ps);
	}
	return 0;

	}
	return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

I don't know where to put my movement code into my overall code?

36 minutes ago, phil67rpg said:

I don't know where to put my movement code into my overall code?

That code is for demonstrating how to get a window on the screen and process input. You need to refactor most of it into functions/classes and have it callable from a game loop. Most of this code would be put into the loop as something like processInput and updateWindow. You would have half of it just in createWindow.

I don't know why you insist on going so low level and refuse to use a simple game engine or at least a framework. SFML would do you wonders and it's C++. It could get you past all this aggravating muck but you just are too hardheaded to take good advice.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

Advertisement

so is my movement code good?

Do yourself a favor and try not to use the ternary operator. Break the code in cleanly readable snippets. Make the intended logic obvious and add enough parenthesis so there is no guessing on operator priority required. 

 

Breaking up the first two lines yields the following. This removes the syntax errors, the mixing of types with flag (is it an int or a float?) and the (for me at least) unclear preference of && vs. < in the first comparison.

Also, you better not compare floats with direct values (comp_velocity == 0.1f). This may work if you never do any arithmethic with floats, but once you do start it'll fall apart (rounding errors will creep in).


if ( ( comp_move < screen_width ) 
&&   ( flag == 0 ) )
{
  comp_velocity = 0.1f;
}
else
{
  comp_velocity = -0.1f;
}

if ( comp_velocity == 0.1f )
{
  comp_move += 1.0f;
}
else
{
  comp_move -= 1.0;
}

 

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

where in overall code is the game  loop?

1 hour ago, phil67rpg said:

where in overall code is the game  loop?

For simple games in C++ it is near the bottom of main()

I thought you had made games in C++ before...?

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

Advertisement

I am trying to use keyup message and vk_space and that will poll the space bar, I want to move the computer paddle when the space key is up, however when I press the space bar the computer paddle moves up and down. 

At the very least you need to convert GetMessage to PeekMessage so that the loop will run even if no messages are available.

I tried PeekMessage but it blinks the screen on and off

This topic is closed to new replies.

Advertisement