激情六月丁香婷婷|亚洲色图AV二区|丝袜AV日韩AV|久草视频在线分类|伊人九九精品视频|国产精品一级电影|久草视频在线99|在线看的av网址|伊人99精品无码|午夜无码视频在线

高校合作1:010-59833514 ?咨詢電話:400-810-1418 服務(wù)與監(jiān)督電話:400-810-1418轉(zhuǎn)接2

c++游戲編程:創(chuàng)建3d游戲(虛幻引擎c++代碼實(shí)現(xiàn)3D魔方游戲,這效果太逼真了)

發(fā)布時間:2023-11-27 18:56:30 瀏覽量:270次

?虛幻引擎c++代碼實(shí)現(xiàn)3D魔方游戲,這效果太逼真了

c++游戲編程:創(chuàng)建3d游戲(虛幻引擎c++代碼實(shí)現(xiàn)3D魔方游戲,這效果太逼真了)

UE5編寫的3D魔方益智小游戲。游戲界面和游戲控制是很值得學(xué)習(xí)的。借助本游戲源碼你可了解一些用UE5編寫游戲的基礎(chǔ)技巧。


虛幻5c++生成魔方



一、繪制魔方

我們需要繪制一個3x3的魔方,共9個,每個方塊的大小為1x1x1,并且魔方的中心點(diǎn)在坐標(biāo)(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;

//設(shè)置六面材質(zhì)

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)

c++游戲編程:創(chuàng)建3d游戲(虛幻引擎c++代碼實(shí)現(xiàn)3D魔方游戲,這效果太逼真了)

{

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;

}

c++游戲編程:創(chuàng)建3d游戲(虛幻引擎c++代碼實(shí)現(xiàn)3D魔方游戲,這效果太逼真了)

熱門課程推薦

熱門資訊

請綁定手機(jī)號

x

同學(xué)您好!

您已成功報名0元試學(xué)活動,老師會在第一時間與您取得聯(lián)系,請保持電話暢通!
確定