检查冲突
Checking for collision
我正在使用 Allegro 为我们专业的最终项目编写游戏,但我遇到了一些碰撞问题。我的游戏基本上是战舰,我所做的是在加载屏幕后,会出现一个屏幕,您可以在其中使用箭头键选择船只的坐标并使用空格键锁定它。如果您选择的坐标已被占用,则空格键不起作用。
我的问题是我无法在放置之前检查船舶的长度和宽度是否有碰撞。只有当您已经放置了飞船并且下一艘飞船可以与其他飞船重叠时,才会考虑飞船的长度和宽度。我已经尝试了一切,但我遇到了一些问题。不仅如此,即使网格是空的,也无法选择网格的某些部分。
void shipplacement(){
//something like a cursor for ship placement
shipx = 0;
shipy = 0;
while(!placed)
{
ALLEGRO_EVENT ev;
al_wait_for_event(prepqueue, &ev);
if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
switch(ev.keyboard.keycode) {
case ALLEGRO_KEY_UP:
key[KEY_UP] = true;
break;
case ALLEGRO_KEY_DOWN:
key[KEY_DOWN] = true;
break;
case ALLEGRO_KEY_LEFT:
key[KEY_LEFT] = true;
break;
case ALLEGRO_KEY_RIGHT:
key[KEY_RIGHT] = true;
break;
case ALLEGRO_KEY_SPACE:
xcheck(); // checks the coordinates of the ship controlled
tst(); // checks for collision and if space bar should work
break;
}
}
else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
switch(ev.keyboard.keycode) {
case ALLEGRO_KEY_UP:
key[KEY_UP] = false;
break;
case ALLEGRO_KEY_DOWN:
key[KEY_DOWN] = false;
break;
case ALLEGRO_KEY_LEFT:
key[KEY_LEFT] = false;
break;
case ALLEGRO_KEY_RIGHT:
key[KEY_RIGHT] = false;
break;
}
}
if(redraw && al_is_event_queue_empty(prepqueue)){
if (shippl[1]){
//redraw the background and draw the "selector"
}
else if (shippl[2]){
//redraw the background and draw the new "selector" and the placed ship.
}
else if (shippl[3]){
//redraw the background and draw the new "selector" and the placed ships.
}
else if (shippl[4]){
//redraw the background and draw the new "selector" and the placed ships.
}
else if (shippl[5]){
//redraw the background and draw the new "selector" and the placed ships.
}
redraw = false;
al_flip_display();}
}
}
void tst(){
if(logic == 0){
makeittrue();
shippl[count] = false;
shipfx[count] = shipx;
shipfy[count] = shipy;
count++;
logic++;
shipx = 0;
shipy = 0;
}
else if(logic == 1){
if(!s1[xholder][yholder] && !s1[xholder][y2] && !s1[xholder][y3] && !s1[xholder][y4]){
makeittrue();
shippl[count] = false;
shipfx[count] = shipx;
shipfy[count] = shipy;
count++;
logic++;
shipx = 0;
shipy = 0;
}
}
else if(logic == 2){
if(!s2[xholder][yholder] && !s2[xholder][y2] && !s2[xholder][y3] && !s1[xholder][yholder] && !s1[xholder][y2] && !s1[xholder][y3] && !s1[xholder][y4]){
makeittrue();
shippl[count] = false;
shipfx[count] = shipx;
shipfy[count] = shipy;
count++;
logic++;
shipx = 0;
shipy = 0;
}
}
else if(logic == 3){
if(!s3[xholder][yholder] && !s3[x2][yholder] && !s3[x3][yholder] && !s2[xholder][yholder] && !s2[xholder][y2] && !s2[xholder][y3] && !s1[xholder][yholder] && !s1[xholder][y2] && !s1[xholder][y3] && !s1[xholder][y4]){
makeittrue();
shippl[count] = false;
shipfx[count] = shipx;
shipfy[count] = shipy;
count++;
logic++;
shipx = 0;
shipy = 0;
}
}
else if(logic == 4){
if(!s4[xholder][yholder] && !s4[xholder][y2] && !s3[xholder][yholder] && !s3[x2][yholder] && !s3[x3][yholder] && !s2[xholder][yholder] && !s2[xholder][y2] && !s2[xholder][y3] && !s1[xholder][yholder] && !s1[xholder][y2] && !s1[xholder][y3] && !s1[xholder][y4]){
makeittrue();
shippl[count] = false;
shipfx[count] = shipx;
shipfy[count] = shipy;
count++;
logic++;
shipx = 0;
shipy = 0;
}
}
}
void makeittrue(){
// s1 is ship1 with length of 4 blocks
// s2 is ship2 with length of 3 blocks
// s3 is ship3 with width of 3 blocks
// s4 is ship4 with length of 2 blocks
// s5 is ship5 with width of 2 blocks
if(count == 1){
y2 = yholder + 1;
y3 = yholder + 2;
y4 = yholder + 3;
s1[xholder][yholder] = true;
s1[xholder][y2] = true;
s1[xholder][y3] = true;
s1[xholder][y4] = true;
placeflag[xholder][yholder] = true;
placeflag[xholder][y2] = true;
placeflag[xholder][y3] = true;
placeflag[xholder][y4] = true;
}
else if(count == 2){
y2 = yholder + 1;
y3 = yholder + 2;
s2[xholder][yholder] = true;
s2[xholder][y2] = true;
s2[xholder][y3] = true;
placeflag[xholder][yholder] = true;
placeflag[xholder][y2] = true;
placeflag[xholder][y3] = true;
}
else if(count == 3){
x2 = xholder + 1;
x3 = xholder + 2;
s3[xholder][yholder] = true;
s3[x2][yholder] = true;
s3[x3][yholder] = true;
placeflag[xholder][yholder] = true;
placeflag[x2][yholder] = true;
placeflag[x3][yholder] = true;
}
else if(count == 4){
y2 = yholder + 1;
s4[xholder][yholder] = true;
s4[xholder][y2] = true;
placeflag[xholder][yholder] = true;
placeflag[xholder][y2] = true;
}
else if(count == 5){
x2 = xholder + 1;
s5[xholder][yholder] = true;
s5[x2][yholder] = true;
placeflag[xholder][yholder] = true;
placeflag[x2][yholder] = true;
}
}
请帮助我。我没主意了。
编辑
忘了说网格是10x10。
发生的情况示例
注意:使用过的FF飞船精灵,因为我还没有完成自己的创建。
所以看来我只需要从编码中休息一下。我设法通过为每艘船制作单独的变量来修复它,这样我就可以准确地检查它的每个部分并避免变量过载。
相关文章:
- valgrind-hellgrind与泄漏检查的结果不同
- C++模板来检查友元函数的存在
- 检查输入是否不是整数或数字
- 试图让变量检查数组中的某些内容
- 写入位置0x0000000C时发生访问冲突
- 检查值是否在集合p1和p2中,但不在p3中
- C++概念:如何使用'concept'检查模板化结构的属性?
- 概念TS检查忽略私有访问修饰符
- 检查 std::shared_ptr<> 的当前底层类型是否为 T
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- 如何在C++中检查2D数组中负值的输入验证
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- C++:正在检查LinkedList中的回文-递归方法-错误
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 使用for循环检查数组中的重复项
- 对派生自同一基类的类实现冲突检查
- 检查对象之间的冲突时,列表迭代器不可递增
- 如何检查 cocos2d-x v3.1 中的特定冲突?C++
- 检查冲突
- 检查来自不同类的两个Rect之间的冲突