精美逼真的3D魔方游戏实现,基于虚幻引擎的C++代码

发布时间:2023-12-19 16:29:50 浏览量:164次

UE5编写的3D魔方益智小游戏。游戏界面和游戏控制是很值得学习的。借助本游戏源码你可了解一些用UE5编写游戏的基础技巧。


虚幻5c++生成魔方



一、绘制魔方

我们需要绘制一个3x3的魔方,共9个,每个方块的大小为1x1x1,并且魔方的中心点在坐标(0, 0, 0)。

代码如下:

class MOFANG2_API AMoFangActor : public AActor

{

GENERATED_BODY()


public:

// Sets default values for this actor's properties

AMoFangActor();

int CubeColor[6][10][10][10];

UPROPERTY(EditAnywhere)

int Jie =3;

UPROPERTY(VisibleAnywhere)

TArray<UStaticMeshComponent*> CubeComponent;

UPROPERTY(EditAnywhere)

UStaticMesh* CubePlaneStaticMesh;

//设置六面材质

UPROPERTY(EditAnywhere)

TArray<UMaterialInterface*> MediaTextureMat;

protected:

// Called when the game starts or when spawned

virtual void BeginPlay() override;


public:

// Called every frame

virtual void Tick(float DeltaTime) override;


virtual void Create3DMofang();

};


AMoFangActor::AMoFangActor()

{

// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.

PrimaryActorTick.bCanEverTick = true;

//三维魔方生成

for (int i = 0; i < Jie * Jie * Jie; i++)

{

CubeComponent.Add(CreateDefaultSubobject<UStaticMeshComponent>(FName(FString::Printf(TEXT("Mesh 1%d"), (i)))));

CubeComponent.Add(CreateDefaultSubobject<UStaticMeshComponent>(FName(FString::Printf(TEXT("Mesh 2%d"), (i)))));

CubeComponent.Add(CreateDefaultSubobject<UStaticMeshComponent>(FName(FString::Printf(TEXT("Mesh 3%d"), (i)))));

CubeComponent.Add(CreateDefaultSubobject<UStaticMeshComponent>(FName(FString::Printf(TEXT("Mesh 4%d"), (i)))));

CubeComponent.Add(CreateDefaultSubobject<UStaticMeshComponent>(FName(FString::Printf(TEXT("Mesh 5%d"), (i)))));

CubeComponent.Add(CreateDefaultSubobject<UStaticMeshComponent>(FName(FString::Printf(TEXT("Mesh 6%d"), (i)))));

}

}


// Called when the game starts or when spawned

void AMoFangActor::BeginPlay()

{

Super::BeginPlay();

Create3DMofang();

for (int i = 0; i < Jie * Jie * Jie; i++)

{

FVector center = FVector((i % Jie) * 100 - 100, ((i / Jie) % Jie) * 100 - 100, ((i / Jie / Jie) % Jie) * 100 - 100);

//上

if (CubeColor[0][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie] != -1)

{

CubeComponent[i * 6 + 0]->SetupAttachment(RootComponent);

CubeComponent[i * 6 + 0]->SetStaticMesh(CubePlaneStaticMesh);

CubeComponent[i * 6 + 0]->SetMaterial(0, MediaTextureMat[CubeColor[0][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie]]);

CubeComponent[i * 6 + 0]->SetRelativeLocationAndRotation(center + FVector(0, 0, 50), FRotator(0, 0, 0));

CubeComponent[i * 6 + 0]->RegisterComponent();

}

//下

if (CubeColor[1][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie] != -1)

{

CubeComponent[i * 6 + 1]->SetupAttachment(RootComponent);

CubeComponent[i * 6 + 1]->SetStaticMesh(CubePlaneStaticMesh);

CubeComponent[i * 6 + 1]->SetMaterial(0, MediaTextureMat[CubeColor[1][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie]]);


CubeComponent[i * 6 + 1]->SetRelativeLocationAndRotation(center + FVector(0, 0, -50), FRotator(180, 0, 0));

CubeComponent[i * 6 + 1]->RegisterComponent();

}

//←

if (CubeColor[2][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie] != -1)

{

CubeComponent[i * 6 + 2]->SetupAttachment(RootComponent);

CubeComponent[i * 6 + 2]->SetStaticMesh(CubePlaneStaticMesh);

CubeComponent[i * 6 + 2]->SetMaterial(0, MediaTextureMat[CubeColor[2][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie]]);

CubeComponent[i * 6 + 2]->SetRelativeLocationAndRotation(center + FVector(0, -50, 0), FRotator(0, 0, 270));

CubeComponent[i * 6 + 2]->RegisterComponent();

}

//→

if (CubeColor[3][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie] != -1)

{

CubeComponent[i * 6 + 3]->SetupAttachment(RootComponent);

CubeComponent[i * 6 + 3]->SetStaticMesh(CubePlaneStaticMesh);

CubeComponent[i * 6 + 3]->SetMaterial(0, MediaTextureMat[CubeColor[3][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie]]);

CubeComponent[i * 6 + 3]->SetRelativeLocationAndRotation(center + FVector(0, 50, 0), FRotator(0, 0, 90));

CubeComponent[i * 6 + 3]->RegisterComponent();

}

//前

if (CubeColor[4][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie] != -1)

{

CubeComponent[i * 6 + 4]->SetupAttachment(RootComponent);

CubeComponent[i * 6 + 4]->SetStaticMesh(CubePlaneStaticMesh);

CubeComponent[i * 6 + 4]->SetMaterial(0, MediaTextureMat[CubeColor[4][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie]]);

CubeComponent[i * 6 + 4]->SetRelativeLocationAndRotation(center + FVector(-50, 0, 0), FRotator(90, 0, 0));

CubeComponent[i * 6 + 4]->RegisterComponent();

}

//后

if (CubeColor[5][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie] != -1)

{

CubeComponent[i * 6 + 5]->SetupAttachment(RootComponent);

CubeComponent[i * 6 + 5]->SetStaticMesh(CubePlaneStaticMesh);

CubeComponent[i * 6 + 5]->SetMaterial(0, MediaTextureMat[CubeColor[5][i % Jie][(i / Jie) % Jie][(i / Jie / Jie) % Jie]]);

CubeComponent[i * 6 + 5]->SetRelativeLocationAndRotation(center + FVector(50, 0, 0), FRotator(270, 0, 0));

CubeComponent[i * 6 + 5]->RegisterComponent();

}

}

}


// Called every frame

void AMoFangActor::Tick(float DeltaTime)

{

Super::Tick(DeltaTime);


}



void AMoFangActor::Create3DMofang()

{

for (int col = 0; col < 6; col++)

for (int i = 0; i < Jie; i++)

for (int j = 0; j < Jie; j++)

for (int k = 0; k< Jie; k++)

CubeColor[col][i][j][k] = -1;

//上九个面

for (int i = 0; i < Jie; i++)

for (int j = 0; j < Jie; j++)

CubeColor[0][i][j][2] = 0;

//下九个面

for (int i = 0; i < Jie; i++)

for (int j = 0; j < Jie; j++)

CubeColor[1][i][j][0] = 1;

//左九个面

for (int i = 0; i < Jie; i++)

for (int j = 0; j < Jie; j++)

CubeColor[2][i][0][j] = 2;

//右九个面

for (int i = 0; i < Jie; i++)

for (int j = 0; j < Jie; j++)

CubeColor[3][i][2][j] = 3;

//九个面

for (int i = 0; i < Jie; i++)

for (int j = 0; j < Jie; j++)

CubeColor[4][0][i][j] = 4;

//九个面

for (int i = 0; i < Jie; i++)

for (int j = 0; j < Jie; j++)

CubeColor[5][2][i][j] = 5;

}

热门课程推荐

热门资讯

请绑定手机号

x

微信扫码在线答疑

扫码领福利1V1在线答疑

点击咨询
添加老师微信,马上领取免费课程资源

1. 打开微信扫一扫,扫描左侧二维码

2. 添加老师微信,马上领取免费课程资源

同学您好!

您已成功报名0元试学活动,老师会在第一时间与您取得联系,请保持电话畅通!
确定