Hi,
I'm try to make a small code that when the object moves into a wall it won't move anymore
My code is
Code:
#include <stdio.h>
#include <windows.h>
#include <stdlib.h> //To clear the screen
void drawplayer(int x, int y, WORD color)
{
HANDLE Poutputh;
COORD Ppos = {x,y};
Poutputh = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(Poutputh, color);
SetConsoleCursorPosition(Poutputh, Ppos);
}
int drawblocks(int bx, int by, WORD bcolor)
{
HANDLE boutput;
COORD bpos = {bx,by};
boutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(boutput, bcolor);
SetConsoleCursorPosition(boutput, bpos);
}
x = 7;
y = 8;
int KeyR = 0;
int KeyR2 = 0;
int KeyR3 = 0;
int KeyR4 = 0;
int play = 1;
int c = 0;
int KeyPress(int key)
{
return GetAsyncKeyState(key);
}
int main()
{
{
while(play == 1)
{
c = 0;
drawplayer(x, y, FOREGROUND_RED);
printf("@");
drawblocks(9, 2, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("#");
if(KeyPress(VK_UP))
{
if(KeyR == 0)
{
if(y > 0)
{
system ("cls");
y = y - 1;
KeyR = 1;
}
}
}
if(!KeyPress(VK_UP))
{
if(KeyR == 1)
{
KeyR = 0;
}
}
if(KeyPress(VK_RIGHT))
{
if(KeyR2 == 0)
{
if(x < 79)
{
system ("cls");
x = x + 1;
KeyR2 = 1;
}
}
}
if(!KeyPress(VK_RIGHT))
{
if(KeyR2 == 1)
{
KeyR2 = 0;
}
}
if(KeyPress(VK_LEFT))
{
if(KeyR3 == 0)
{
if(x > 0)
{
system ("cls");
x = x -1;
KeyR3 = 1;
}
}
}
if(!KeyPress(VK_LEFT))
{
if(KeyR3 == 1)
{
KeyR3 = 0;
}
if(y < 24)
{
system ("cls");
}
if(KeyPress(VK_DOWN))
{
if(KeyR4 == 0)
{
y = y + 1;
KeyR4 = 1;
}
}
}
if(!KeyPress(VK_DOWN))
{
if(KeyR4 == 1)
{
KeyR4 = 0;
}
}
}
}
}