發(fā)布時間:2023-11-27 18:56:30 瀏覽量:270次
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)
{
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;
}
熱門資訊
探討游戲引擎的文章,介紹了10款游戲引擎及其代表作品,涵蓋了RAGE Engine、Naughty Dog Game Engine、The Dead Engine、Cry Engine、Avalanche Engine、Anvil Engine、IW Engine、Frostbite Engine、Creation引擎、Unreal Engine等引擎。借此分析引出了游戲設(shè)計領(lǐng)域和數(shù)字藝術(shù)教育的重要性,歡迎點(diǎn)擊咨詢報名。
2. 手機(jī)游戲如何開發(fā)(如何制作傳奇手游,都需要準(zhǔn)備些什么?)
?如何制作傳奇手游,都需要準(zhǔn)備些什么?提到傳奇手游相信大家都不陌生,他是許多80、90后的回憶;從起初的端游到現(xiàn)在的手游,說明時代在進(jìn)步游戲在更新,更趨于方便化移動化。而如果我們想要制作一款傳奇手游的
3. B站視頻剪輯軟件「必剪」:免費(fèi)、炫酷特效,小白必備工具
B站視頻剪輯軟件「必剪」,完全免費(fèi)、一鍵制作炫酷特效,適合新手小白??靵碓囋?!
4. Steam值得入手的武俠游戲盤點(diǎn),各具特色的快意江湖
游戲中玩家將面臨武俠人生的掙扎抉擇,戰(zhàn)或降?殺或放?每個抉定都將觸發(fā)更多愛恨糾葛的精彩奇遇?!短烀嬗肪哂卸嗑€劇情多結(jié)局,不限主線發(fā)展,高自由...
5. Bigtime加密游戲經(jīng)濟(jì)體系揭秘,不同玩家角色的經(jīng)濟(jì)活動
Bigtime加密游戲經(jīng)濟(jì)模型分析,探討游戲經(jīng)濟(jì)特點(diǎn),幫助玩家更全面了解這款GameFi產(chǎn)品。
6. 3D動漫建模全過程,不是一般人能學(xué)的會的,會的多不是人?
步驟01:面部,頸部,身體在一起這次我不準(zhǔn)備設(shè)計圖片,我從雕刻進(jìn)入。這一次,它將是一種純粹關(guān)注建模而非整體繪畫的形式。像往常一樣,我從Sphere創(chuàng)建它...
7. 3D動畫軟件你知道幾個?3ds Max、Blender、Maya、Houdini大比拼
當(dāng)提到3D動畫軟件或動畫工具時,指的是數(shù)字內(nèi)容創(chuàng)建工具。它是用于造型、建模以及繪制3D美術(shù)動畫的軟件程序。但是,在3D動畫軟件中還包含了其他類型的...
?三昧動漫對于著名ARPG游戲《巫師》系列,最近CD Projekt 的高層回應(yīng)并不會推出《巫師4》。因?yàn)椤段讕煛废盗性诓邉澋臅r候一直定位在“三部曲”的故事框架,所以在游戲的出品上不可能出現(xiàn)《巫師4》
9. 3D打印技巧揭秘!Cura設(shè)置讓你的模型更堅固
想讓你的3D打印模型更堅固?不妨嘗試一下Cura參數(shù)設(shè)置和設(shè)計技巧,讓你輕松掌握!
10. Unity3D入門:手把手帶你開發(fā)一款坦克大戰(zhàn)的游戲
Unity工程創(chuàng)建完成后如圖所示: 接下來應(yīng)該導(dǎo)入此項(xiàng)目所需的Unity Package文件,要用到的Unity package文件大家可以去Unity3D的官方網(wǎng)站下載(地址:ht...
最新文章
同學(xué)您好!