C零基础视频-31-二维数组应用之游戏中的碰撞检测

目录

  • 没有碰撞检测的版本
  • 碰撞检测

没有碰撞检测的版本

#include <windows.h>
#include <conio.h>
#include <stdio.h>

void MoveCursorTo(int nRow, int nCol)
{
    COORD crdLocation;
    crdLocation.X = 2*nCol;
    crdLocation.Y = nRow;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), crdLocation);
}

char g_chBackground[20][20] = { 0 };

void InitBackGround()
{
    for (size_t nRow = 0; nRow < 20; nRow++)
    {
        for (size_t nCol = 0; nCol < 20; nCol++)
        {
            if (nRow == 0
                || nCol == 0
                || nRow == 19
                || nCol == 19)
            {
                g_chBackground[nRow][nCol] = 1;
            }
            else
            {
                g_chBackground[nRow][nCol] = 0;
            }
        }
    }
}

void ShowBackGround()
{
    for (size_t nRow = 0; nRow < 20; nRow++)
    {
        for (size_t nCol = 0; nCol < 20; nCol++)
        {
            if (g_chBackground[nRow][nCol] == 1)
            {
                MoveCursorTo(nRow, nCol);
                printf("■");
            }
            else
            {

            }
        }
    }
}

void ClearPlayer(int nRow, int nCol)
{
    MoveCursorTo(nRow, nCol);
    printf(" ");
}
void ShowPlayer(int nRow, int nCol)
{
    MoveCursorTo(nRow, nCol);
    printf("×");
}

int main(int argc, char* argv[])
{
    char chInput = 0;

    InitBackGround();
    ShowBackGround();

    int nRow = 10;
    int nCol = 10;

    ShowPlayer(nRow, nCol);

    while (1)
    {
        if (_kbhit() != 0)
        {
            chInput = _getch();
            switch (chInput)
            {
            case 'a':
                ClearPlayer(nRow, nCol);
                nCol -= 1;
                ShowPlayer(nRow, nCol);
                break;
            case 'w':
                ClearPlayer(nRow, nCol);
                nRow -= 1;
                ShowPlayer(nRow, nCol);
                break;
            case 's':
                ClearPlayer(nRow, nCol);
                nRow += 1;
                ShowPlayer(nRow, nCol);
                break;
            case 'd':
                ClearPlayer(nRow, nCol);
                //改变坐标并移动、打印
                nCol += 1;
                ShowPlayer(nRow, nCol);
                break;
            default:
                break;
            }
        }

    }

    return 0;
}

碰撞检测

#include <windows.h>
#include <conio.h>
#include <stdio.h>

void MoveCursorTo(int nRow, int nCol)
{
    COORD crdLocation;
    crdLocation.X = 2*nCol;
    crdLocation.Y = nRow;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), crdLocation);
}

char g_chBackground[20][20] = { 0 };

int IsCanMove(int nToRow, int nToCol)
{
    if (g_chBackground[nToRow][nToCol] == 1)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

void InitBackGround()
{
    for (size_t nRow = 0; nRow < 20; nRow++)
    {
        for (size_t nCol = 0; nCol < 20; nCol++)
        {
            if (nRow == 0
                || nCol == 0
                || nRow == 19
                || nCol == 19)
            {
                g_chBackground[nRow][nCol] = 1;
            }
            else
            {
                g_chBackground[nRow][nCol] = 0;
            }
        }
    }
}

void ShowBackGround()
{
    for (size_t nRow = 0; nRow < 20; nRow++)
    {
        for (size_t nCol = 0; nCol < 20; nCol++)
        {
            if (g_chBackground[nRow][nCol] == 1)
            {
                MoveCursorTo(nRow, nCol);
                printf("■");
            }
            else
            {

            }
        }
    }
}

void ClearPlayer(int nRow, int nCol)
{
    MoveCursorTo(nRow, nCol);
    printf(" ");
}
void ShowPlayer(int nRow, int nCol)
{
    MoveCursorTo(nRow, nCol);
    printf("×");
}

int main(int argc, char* argv[])
{
    char chInput = 0;

    InitBackGround();
    ShowBackGround();

    int nRow = 10;
    int nCol = 10;

    ShowPlayer(nRow, nCol);

    while (1)
    {
        if (_kbhit() != 0)
        {
            chInput = _getch();
            switch (chInput)
            {
            case 'a':
                if (IsCanMove(nRow, nCol - 1))
                {
                    ClearPlayer(nRow, nCol);
                    nCol -= 1;
                    ShowPlayer(nRow, nCol);
                }
                break;
            case 'w':
                if (IsCanMove(nRow - 1, nCol))
                {
                    ClearPlayer(nRow, nCol);
                    nRow -= 1;
                    ShowPlayer(nRow, nCol);
                }
                break;
            case 's':
                if (IsCanMove(nRow + 1, nCol))
                {
                    ClearPlayer(nRow, nCol);
                    nRow += 1;
                    ShowPlayer(nRow, nCol);
                }
                break;
            case 'd':
                if (IsCanMove(nRow, nCol + 1))
                {
                    ClearPlayer(nRow, nCol);
                    //改变坐标并移动、打印
                    nCol += 1;
                    ShowPlayer(nRow, nCol);
                }
                break;
            default:
                break;
            }
        }

    }

    return 0;
}

原文地址:https://www.cnblogs.com/shellmad/p/11695586.html

时间: 2024-08-30 01:09:16

C零基础视频-31-二维数组应用之游戏中的碰撞检测的相关文章

19.把1~100存到二维数组a[10][10]中,并按二维矩阵形式输出

#include<iostream>using namespace std; int main(){    int a[10][10];    for(int i=0;i<10;i++)    {        for(int j=0;j<10;j++)        {            a[i][j]=i*10+j+1;//二维数组逻辑上还是一维数组的存储方式        }    }    for(int j=0;j<10;j++)    {        for

44.从键盘输入12个数存入二维数组a[3][4]中,编写程序求出最大元素的值及它所在的行号和列号

//1.建立二维数组 //2.运用循环,将内容输入到数组中 //3.求出最大元素,并输出行号和列号 #include<iostream> using namespace std; int main() { int a[3][4]; int Max=0;//赋值之前需要先置为0 cout<<"please input 12 numbers: "<<endl; for(int i=0;i<3;i++)//嵌套循环,用于向二维数组中输入内容 { fo

8.31 二维数组 字符串和指针

指针+1移动了相当于所指向类型的大小的字节 int *s1[100] 移动了4个字节 int (*s2)[100] 移动了400个字节 char *s3 移动了1 个字节 int *s4 移动了4个字节 ***p2如何理解? int *p0 = &i *p0  = i int **p1 = &p0 **p1 = i int ***p2 = &p1 ***p2 = i *p2 = p1的值 **p2 = p0的值 ***p2 = i的值 所以***p2就是p0的值        而p

Java基础学习第六天——二维数组与面向对象入门

文档版本 开发工具 测试平台 工程名字 日期 作者 备注 V1.0 2016.02.25 lutianfei none 二维数组 格式1(动态初始化) 格式:数据类型[][] 变量名 = new 数据类型[m][n]; m表示这个二维数组有多少个一维数组 n表示每一个一维数组的元素个数 举例: int[][] arr = new int[3][2]; 定义了一个二维数组arr 这个二维数组有3个一维数组,名称是arr[0],arr[1],arr[2] 每个一维数组有2个元素,可以通过arr[m]

C语言二维数组实现扫雷游戏

#include<stdio.h> //使用二维数组实现 扫雷 int main() { char ui[8][8]={ '+','+','+','+','+','+','+','+', '+','+','+','+','+','+','+','+', '+','+','+','+','+','+','+','+', '+','+','+','+','+','+','+','+', '+','+','+','+','+','+','+','+', '+','+','+','+','+','+'

C语言+二维数组实现扫雷游戏(贴过来后无缩进尽请谅解,工程完成度:70%)

#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 10 #define L 10 //打印随机雷的位置,测试代码方便看雷 void sl_print(constint a[L]) { int i=0; printf("地雷的随机位置为:"); for(i=0;i<L;i++) { printf("%02d ",a[i]); } printf(

[java学习笔记]java语言基础概述之数组的定义&amp;常见操作(遍历、排序、查找)&amp;二维数组

1.数组基础 1.什么是数组:           同一类型数据的集合,就是一个容器. 2.数组的好处:           可以自动为数组中的元素从零开始编号,方便操作这些数据. 3.格式:  (一旦创建,必须明确长度)          格式1:              元素类型   [ ]  数组名  =  new  元素类型  [元素个数即数组的长度]:              示例:int[] array = new int[5];          格式2:           

二级指针与二维数组

最近看<Linux C程序设计大全>这本书,虽然书中有一些错误,但整体来说,书写得还算可以. 当看到网络编程[第23.2.4小节 获得主机信息]时,遇到了一段代码,原文如下: “一台主机有许多和网络相关的信息,例如,主机名称.IP地址.主机提供的服务等.这些信息一般都保存在系统中的某个文件里(例如/etc/hosts等),用户程序可以通过系统提供的函数读取这些文件上的内容.Linux环境下使用gethostent函数读取和主机有关的信息,该函数的原型如下: 1 #include <net

二维数组的传输 (host &lt;-&gt; device)

前言 本文的目的很明确:介绍如何将二维数组传递进显存,以及如何将二维数组从显存传递回主机端. 实现步骤 1. 在显存中为二维数组开辟空间 2. 获取该二维数组在显存中的 pitch 值 (cudaMallocPitch 实现) 3. 将二维数组传递进显存 (cudaMemcpy2D 实现) 4. 在显存中对该二维数组进行处理 (目前必须按照 1 维数组的规则进行处理) 5. 将结果传递回内存 (cudaMemcpy2D实现) 重要概念 - pitch 对于内存的存取来说,对准偏移量为2的幂(现在