推箱子游戏2

#include <iostream>
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
using namespace std;

#define width 800
#define height 800

//e:empty w:wall t:target b:box p:player
char maps[8][8] = { {'w','w','w','w','w','w','w','w'},
    {'w','w','w','t','b','e','e','w'},
    {'w','e','e','e','e','e','e','w'},
    {'w','e','e','e','e','e','e','w'},
    {'w','e','e','e','e','e','e','w'},
    {'w','e','p','b','t','e','e','w'},
    {'w','e','e','e','w','w','w','w'},
    {'w','w','w','w','w','w','w','w'}

};
int block_size = 100;
int player_x = 250;
int player_y = 550;
bool running = true;
int box1_x = 450;
int box1_y = 150;
int box2_x = 450;
int box2_y = 50;
int m1 = 1; int n1 = 4;
int m2 = 5; int n2 = 3;

void startup()
{
    initgraph(width, height);
    setbkcolor(RGB(150, 150, 150));
    cleardevice();

}

int next_step_x = 250;
int next_step_y = 550;
void PlayerMove()
{
    
    if (maps[next_step_y / block_size][next_step_x / block_size] == 'e'|| maps[next_step_y / block_size][next_step_x / block_size] == 'p')
    {
        player_x = next_step_x;
        player_y = next_step_y;
        setfillcolor(RED);
        setlinecolor(RED);
        fillcircle(player_x, player_y, 45);
        setfillcolor(BLACK);
        fillcircle(player_x - 20, player_y - 10, 10);
        fillcircle(player_x + 20, player_y - 10, 10);
        fillrectangle(player_x - 15, player_y + 15, player_x + 15, player_y + 20);
    }
    else
    {
        next_step_x = player_x;
        next_step_y = player_y;
    }
    
    if (_kbhit)
    {
        char input = _getch();
        if (input == 'w' || input == 'a' || input == 's' || input == 'd')
        {
            if (input == 'd')
            {
                next_step_x += 100;
            }
            else if (input == 'w')
            {
                next_step_y -= 100;
            }
            else if (input == 's')
            {
                next_step_y += 100;
            }
            else if (input == 'a')
            {
                next_step_x -= 100;
            }
            if (maps[next_step_y / block_size][next_step_x / block_size] == 'b')
            {
                if ((player_y / block_size - m2) * (player_y / block_size - m2) + (player_x / block_size - n2) * (player_x / block_size - n2) == 1)
                {
                    if (input == 'd' && maps[m2][n2 + 1] != 'w')
                    {
                        n2 += 1;
                        maps[m2][n2] = 'b';
                        maps[m2][n2 - 1] = 'e';

                    }
                    else if (input == 'w' && maps[m2 - 1][n2] != 'w')
                    {
                        m2 -= 1;
                        maps[m2][n2] = 'b';
                        maps[m2 + 1][n2] = 'e';
                    }
                    else if (input == 's' && maps[m2 + 1][n2] != 'w')
                    {
                        m2 += 1;
                        maps[m2][n2] = 'b';
                        maps[m2 - 1][n2] = 'e';
                    }
                    else if (input == 'a' && maps[m2][n2 - 1] != 'w')
                    {
                        n2 -= 1;
                        maps[m2][n2] = 'b';
                        maps[m2][n2 + 1] = 'e';
                    }

                }
                if ((player_y / block_size - m1) * (player_y / block_size - m1) + (player_x / block_size - n1) * (player_x / block_size - n1) == 1)
                {
                    if (input == 'd' && maps[m1][n1 + 1] != 'w')
                    {
                        n1 += 1;
                        maps[m1][n1] = 'b';
                        maps[m1][n1 - 1] = 'e';

                    }
                    else if (input == 'w' && maps[m1 - 1][n1] != 'w')
                    {
                        m1 -= 1;
                        maps[m1][n1] = 'b';
                        maps[m1 + 1][n1] = 'e';
                    }
                    else if (input == 's' && maps[m1 + 1][n1] != 'w')
                    {
                        m1 += 1;
                        maps[m1][n1] = 'b';
                        maps[m1 - 1][n1] = 'e';
                    }
                    else if (input == 'a' && maps[m1][n1 - 1] != 'w')
                    {
                        n1 -= 1;
                        maps[m1][n1] = 'b';
                        maps[m1][n1 + 1] = 'e';
                    }
                }
                
            }

        }
        if (maps[next_step_y / block_size][next_step_x / block_size] == 'e'|| maps[next_step_y / block_size][next_step_x / block_size] == 'p')
        {
            cleardevice();
        }


    }

    
}
void UpdateWithoutInput()
{
    
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            if (maps[i][j] == 'w')
            {
                setfillcolor(RED);
                fillrectangle(j * block_size, i * block_size, (j + 1) * block_size, (i + 1) * block_size);
                setlinecolor(WHITE);
                rectangle(j * block_size, i * block_size, (j + 1) * block_size, (i + 1) * block_size);
            }
            else if (maps[i][j] == 't')
            {
                setfillcolor(WHITE);
                fillrectangle(j * block_size + 30, i * block_size + 30, (j + 1) * block_size - 30, (i + 1) * block_size - 30);
            }
            else if (maps[i][j] == 'b')
            {
                setfillcolor(YELLOW);
                fillrectangle(j * block_size, i * block_size, (j + 1) * block_size, (i + 1) * block_size);
            }
        }
    }
}


int main()
{
    startup();
    while (running)
    {
        
        UpdateWithoutInput();
        PlayerMove();
        int count = 0;
        for(int i=0;i<8;i++)
            for (int j = 0; j < 8; j++)
            {

                count++;
                if (maps[i][j] == 't')
                {
                    i = 8;
                }
                else if(count==63)
                {
                    running = false;
                    UpdateWithoutInput();
                    MessageBox(GetHWnd(), _T("你赢了!"), _T("游戏结束"), MB_OK);
                }
            }

    }
    
    

    _getch();
    closegraph();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值