Hello everyone!
I have to prepare a collage project and for this project I have to create Sokoban (the game where you have to move blocks on specific places in order to win). I worked on it for a bit but now I am stuck for already 3 days. I tried a few things but nothing worked. My problem is that after creating the main box (that is the player) and creating the boxes that I have to move it seems like anything I tried won’t make the player box push the other boxes. At first I tried to match coordinates. So what I did wast to use an “if” and tell the program to move the objects together if theirs coordinates are equal. After 2 days of not understanding why this works I found out that their coordinates would never be equal because my program apparently things that the player box has one coordinate 0.4000006 and the other coordinate (the one it is supposed to be equal to) is 0.1000001. So even if I move the player box it will never be equal as there will always be a difference.
Now I managed to make it push the box but again I run into a problem: it pushes it if they have the same Y but it doesn’t push it only when it is next to it, but also when it is far away.
Here is the code:
I honestly hope someone is going to help me and explain me what I am supposed to do.
#include "glut.h"
#include <stdio.h>
int t = 0;
//float xX = 0.05, yY = -0.05;
float xX = 0, yY = 0;
//float OxA = 0.4, OyA = -0.45, OxB = -0.45, OyB = -0.45, OxC = 0.45, OyC = -0.4, OxD = 0.4, OyD = -0.4;
//float CXA = 0.4, CYA = -0.4, CXB = 0.6, CYB = -0.4, CXC = 0.6, CYC = -0.6, CXD = 0.4, CYD = -0.6;
float CXA = 0.4, CYA = -0.45, CXB = -0.45, CYB = -0.45, CXC = 0.45, CYC = -0.4, CXD = 0.4, CYD = -0.4;
float C1xA = 0.05, C1yA = -0.25, C1xB = 0.1, C1yB = -0.25, C1xC = 0.1, C1yC = -0.2, C1xD = 0.05, C1yD = -0.2;
float C2xA = 0.15, C2yA = -0.25, C2xB = 0.2, C2yB = -0.25, C2xC = 0.2, C2yC = -0.2, C2xD = 0.15, C2yD = -0.2;
float C3xA = 0.25, C3yA = -0.25, C3xB = 0.3, C3yB = -0.25, C3xC = 0.3, C3yC = -0.2, C3xD = 0.25, C3yD = -0.2;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
}
void miscarea(float &x1, float &y1, float &x2, float &y2, float &x3, float &y3, float &x4, float &y4, float q, float w)
{
x1 = x1 + q; // q este pasul pe x
x2 = x3 + q;
x3 = x3 + q;
x4 = x4 + q;
y1 = y1 + w; //w pasul pe y
y2 = y2 + w;
y3 = y3 + w;
y4 = y4 + w;
}
void taste()
{
switch (t)
{
case 1:
xX = -0.05;
break;
case 2:
xX = 0.05;
break;
case 3:
yY = 0.05;
break;
case 4:
yY = -0.05;
break;
}
}
void verificare();
void k(unsigned char key, int x, int y)
{
switch (key)
{
case 'w':
t = 3;
glutPostRedisplay();
break;
case 'a':
t = 1;
glutPostRedisplay();
break;
case's':
t = 4;
glutPostRedisplay();
break;
case'd':
t = 2;
glutPostRedisplay();
break;
}
} //comenzi de miscare
void display(void)
{
xX = 0;
yY = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON); //asta e patratul de pe drepta
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0.45, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glVertex2f(0.45, -0.5);
glEnd();
glBegin(GL_POLYGON); //patratul de pe Ox
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.5, 0);
glVertex2f(-0.1, 0);
glVertex2f(-0.1, -0.05);
glVertex2f(-0.5, -0.05);
glEnd();
glBegin(GL_POLYGON); //patratul de sus
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.5, 0.5);
glVertex2f(-0.5, 0.45);
glVertex2f(0.5, 0.45);
glVertex2f(0.5, 0.5);
glEnd();
glBegin(GL_POLYGON);// patratul din stanga sus
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.5, 0.5);
glVertex2f(-0.45, 0.5);
glVertex2f(-0.45, 0);
glVertex2f(-0.5, 0);
glEnd();
glBegin(GL_POLYGON); //patrat stanga jos
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.15, -0.5);
glVertex2f(-0.1, -0.5);
glVertex2f(-0.1, 0);
glVertex2f(-0.15, 0);
glEnd();
glBegin(GL_POLYGON); //patrat jos
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0.5, -0.5);
glVertex2f(0.5, -0.45);
glVertex2f(-0.15, -0.45);
glVertex2f(-0.15, -0.5);
glEnd();
glBegin(GL_POLYGON); //patratul din dreapta sus mijloc
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0.5, 0.3);
glVertex2f(0.5, 0.25);
glVertex2f(0.1, 0.25);
glVertex2f(0.1, 0.3);
glEnd();
glBegin(GL_POLYGON); //p1
glColor3f(0.0, 1.0, 1.0);
glVertex2f(-0.45, 0.4);
glVertex2f(-0.45, 0.35);
glVertex2f(-0.4, 0.35);
glVertex2f(-0.4, 0.4);
glEnd();
glBegin(GL_POLYGON); //p2
glColor3f(0.0, 1.0, 1.0);
glVertex2f(0.25, 0.3);
glVertex2f(0.25, 0.35);
glVertex2f(0.3, 0.35);
glVertex2f(0.3, 0.3);
glEnd();
glBegin(GL_POLYGON); //p3
glColor3f(0.0, 1.0, 1.0);
glVertex2f(-0.1, 0.0);
glVertex2f(-0.1, -0.05);
glVertex2f(-0.05, -0.05);
glVertex2f(-0.05, 0.0);
glEnd();
taste();
miscarea(CXA, CYA, CXB, CYB, CXC, CYC, CXD, CYD, xX, yY);
//Conditii verif C1
/*1ST ATTEMPT: */ /*if (((((CXA - C1xB) > -0.00005) && (CXA - C1xB) < 0) || ((CXA - C1xB)<0.00005) && (CXA - C1xB) > 0) && (((CYA - C1yB) > -0.00005) && (CYA - C1yB) < 0) || (((CYA - C1yB)<0.00005) && (CYA - C1yB) > 0))
{
miscarea(C1xA, C1yA, C1xB, C1yB, C1xC, C1yC, C1xD, C1yD, xX, yY);
}*/
/*2ND ATTEMPT: */ if ((((((CXA - C1xB) > -0.00005) && (CXA - C1xB) < 0) || ((CXA - C1xB)<0.00005) && (CXA - C1xB) > 0) && (((CYA - C1yB) > -0.00005) && (CYA - C1yB) < 0) || (((CYA - C1yB)<0.00005) && (CYA - C1yB) > 0)) && (((((CXD - C1xC) > -0.00002) && (CXD - C1xC) < 0) || ((CXD - C1xC)<0.00002) && (CXD - C1xC) > 0) && (((CYD - C1yC) > -0.00002) && (CYD - C1yC) < 0) || (((CYD - C1yC)<0.00002) && (CYD - C1yC) > 0) && t==2) )
{
miscarea(C1xA, C1yA, C1xB, C1yB, C1xC, C1yC, C1xD, C1yD, xX, yY);
}
//(((((CXD - C1xC) > -0.00005) && (CXD - C1xC) < 0) || ((CXD - C1xC)<0.00005) && (CXD - C1xC) > 0) && (((CYC - C1yD) > -0.00005) && (CYC - C1yD) < 0) || (((CYC - C1yD)<0.00005) && (CYC - C1yD) > 0))
/*3RD ATTEMPT: */ /* if ((((((CXB - C1xA) > -0.00005) && (CXB - C1xA) < 0) || ((CXB - C1xA)<0.00005) && (CXB - C1xA) > 0) && (((CYB - C1yA) > -0.00005) && (CYB - C1yA) < 0) || (((CYB - C1yA)<0.00005) && (CYB - C1yA) > 0)) && (((((CXC - C1xD) > -0.00002) && (CXC - C1xD) < 0) || ((CXC - C1xD)<0.00002) && (CXC - C1xD) > 0) && (((CYC - C1yD) > -0.00002) && (CYC - C1yD) < 0) || (((CYC - C1yD)<0.00002) && (CYC - C1yD) > 0) && t == 1))
{
miscarea(C1xA, C1yA, C1xB, C1yB, C1xC, C1yC, C1xD, C1yD, xX, yY); //Misca si stanga si dreapta dar si de la distata
}
/*3RD ATTEMPT: */ /* if ((((((CXD - C1xA) > -0.00005) && (CXD - C1xA) < 0) || ((CXD - C1xA)<0.00005) && (CXD - C1xA) > 0) && (((CYD - C1yA) > -0.00005) && (CYD - C1yA) < 0) || (((CYD - C1yA)<0.00005) && (CYD - C1yA) > 0)) && (((((CXC - C1xB) > -0.00002) && (CXC - C1xB) < 0) || ((CXC - C1xB)<0.00002) && (CXC - C1xB) > 0) && (((CYC - C1yB) > -0.00002) && (CYC - C1yB) < 0) || (((CYC - C1yB)<0.00002) && (CYC - C1yB) > 0) && t==1))
{
miscarea(C1xA, C1yA, C1xB, C1yB, C1xC, C1yC, C1xD, C1yD, xX, yY); //Misca si stanga si dreapta dar si de la distata
}
/*3RD ATTEMPT: */ /* if ((((((CXA - C1xD) > -0.00005) && (CXA - C1xD) < 0) || ((CXA - C1xD)<0.00005) && (CXA - C1xD) > 0) && (((CYA - C1yD) > -0.00005) && (CYA - C1yD) < 0) || (((CYA - C1yD)<0.00005) && (CYA - C1yD) > 0)) && (((((CXB - C1xC) > -0.00002) && (CXB - C1xC) < 0) || ((CXB - C1xC)<0.00002) && (CXB - C1xC) > 0) && (((CYB - C1yC) > -0.00002) && (CYB - C1yC) < 0) || (((CYB - C1yC)<0.00002) && (CYB - C1yC) > 0) && t == 4))
{
miscarea(C1xA, C1yA, C1xB, C1yB, C1xC, C1yC, C1xD, C1yD, xX, yY); //Misca si stanga si dreapta dar si de la distata
} */
//DOLAN, PLS D_D
//CREEARE CUB PRINCIPAL
glBegin(GL_POLYGON);
glColor3f(1.0, 0.5, 0.0);
glVertex2f(CXA, CYA);
glVertex2f(CXB, CYB);
glVertex2f(CXC, CYC);
glVertex2f(CXD, CYD);
glEnd();
//SF CUB PRINCIPAL
//CREEARE CUTIA 1
glColor3f(0.0, 0.5, 0.0);
glBegin(GL_POLYGON);
glVertex2f(C1xA, C1yA);
glVertex2f(C1xB, C1yB);
glVertex2f(C1xC, C1yC);
glVertex2f(C1xD, C1yD);
glEnd();
//SF CUTIA 1
//CREEARE CUTIA 2
glColor3f(0.0, 0.5, 0.0);
glBegin(GL_POLYGON);
glVertex2f(C2xA, C2yA);
glVertex2f(C2xB, C2yB);
glVertex2f(C2xC, C2yC);
glVertex2f(C2xD, C2yD);
glEnd();
//SF CUTIA 2
//CREEARE CUTIA 3
glColor3f(0.0, 0.5, 0.0);
glBegin(GL_POLYGON);
glVertex2f(C3xA, C3yA);
glVertex2f(C3xB, C3yB);
glVertex2f(C3xC, C3yC);
glVertex2f(C3xD, C3yD);
glEnd();
//SF CUTIA 3
glDisable(GL_TEXTURE_2D); //sa nu faca backround-ul de la png o poza
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(700, 700);
glutInitWindowPosition(100, 100);
glutCreateWindow("Sokoban");
init();
glutDisplayFunc(display);
glutKeyboardFunc(k);
glutMainLoop();
return 0;
}
Best regards,
Mihai Ilie