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?