siki学院的飞机大作战UE4.26代码
上一篇文章写了整体框架,这篇文章来贴出我自己跟着siki学院的飞机大作战视频所制作的代码,编译环境为VS2019,UE4.26完美运行。
SpaceShip.cpp
#include "SpaceShip.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SphereComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/PlayerController.h"
#include "Kismet/KismetMathLibrary.h"
#include "GameFramework/SpringArmComponent.h"
#include "Misc/App.h"
#include "Engine/World.h"
#include "Bullet.h"
#include "TimerManager.h"
#include "Enemy.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"
#include "Particles/ParticleSystemComponent.h"
#include "Particles/ParticleSystem.h"// Sets default values
ASpaceShip::ASpaceShip()
{// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.PrimaryActorTick.bCanEverTick = true;CollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("CollisionComp")); RootComponent = CollisionComp;ShipSM = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ShipSM"));ShipSM->SetupAttachment(RootComponent);SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComp"));SpringArmComp->SetupAttachment(RootComponent);CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));CameraComp->SetupAttachment(SpringArmComp);SpawnPoint = CreateDefaultSubobject<USceneComponent>(TEXT("SpawnPoint"));SpawnPoint->SetupAttachment(ShipSM);ThrusterParticleComp = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("ThrusterParticleComp"));ThrusterParticleComp->SetupAttachment(RootComponent);Speed = 2500.0f;TimeBetweenShot = 0.2f;bDead = false;
}// Called when the game starts or when spawned
void ASpaceShip::BeginPlay()
{Super::BeginPlay();PC = Cast<APlayerController>(GetController());PC->bShowMouseCursor = true;}void ASpaceShip::LookAtCursor()
{FVector MouseLocation, MouseDirection;PC->DeprojectMousePositionToWorld(MouseLocation, MouseDirection);FVector TargetLocation = FVector(MouseLocation.X, MouseLocation.Y, GetActorLocation().Z);FRotator Rotator = UKismetMathLibrary::FindLookAtRotation(GetActorLocation(), TargetLocation);SetActorRotation(Rotator);
}void ASpaceShip::MoveUp(float Value)
{if (Value != 0) {bUpMove = true;}else {bUpMove = false;}AddMovementInput(FVector::ForwardVector, Value);
}void ASpaceShip::MoveRight(float Value)
{if (Value != 0) {bRightMove = true;}else {bRightMove = false;}AddMovementInput(FVector::RightVector, Value);
}void ASpaceShip::Move()
{AddActorWorldOffset(ConsumeMovementInputVector() * Speed * FApp::GetDeltaTime(), true);
}void ASpaceShip::Fire()
{if (Bullet && !bDead){FActorSpawnParameters SpawnParams;GetWorld()->SpawnActor<ABullet>(Bullet, SpawnPoint->GetComponentLocation(), SpawnPoint->GetComponentRotation(), SpawnParams);if (ShootCue) UGameplayStatics::PlaySoundAtLocation(this, ShootCue, GetActorLocation());}
}void ASpaceShip::StartFire()
{GetWorldTimerManager().SetTimer(TimerHandle_BetweenShot, this, &ASpaceShip::Fire, TimeBetweenShot, true, 0.0f);
}void ASpaceShip::EndFire()
{GetWorldTimerManager().ClearTimer(TimerHandle_BetweenShot);
}void ASpaceShip::RestartLevel()
{UGameplayStatics::OpenLevel(this, "MainMap");
}void ASpaceShip::OnDeath()
{bDead = true;CollisionComp->SetVisibility(false, true);if (GameOverCue) UGameplayStatics::PlaySoundAtLocation(this, GameOverCue, GetActorLocation());if (ExplosionParticle) UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ExplosionParticle, GetActorLocation(), FRotator::ZeroRotator, true);GetWorldTimerManager().SetTimer(TimerHandle_Restart, this, &ASpaceShip::RestartLevel, 2.0f, false);
}// Called every frame
void ASpaceShip::Tick(float DeltaTime)
{Super::Tick(DeltaTime);if (!bDead) {if (bRightMove || bUpMove) {ThrusterParticleComp->Activate();}else {ThrusterParticleComp->Deactivate();}LookAtCursor();Move();}else {ThrusterParticleComp->Deactivate();}
}// Called to bind functionality to input
void ASpaceShip::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{Super::SetupPlayerInputComponent(PlayerInputComponent);PlayerInputComponent->BindAxis("MoveUp", this, &ASpaceShip::MoveUp);PlayerInputComponent->BindAxis("MoveRight", this, &ASpaceShip::MoveRight);PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ASpaceShip::StartFire);PlayerInputComponent->BindAction("Fire", IE_Released, this, &ASpaceShip::EndFire);
}void ASpaceShip::NotifyActorBeginOverlap(AActor* OtherActor)
{Super::NotifyActorBeginOverlap(OtherActor);AEnemy* Enemy = Cast<AEnemy>(OtherActor);if (Enemy) {Enemy->Destroy();UE_LOG(LogTemp, Warning, TEXT("Player is Dead"));OnDeath();//Destroy();}
}
SpaceShip.h
#pragma once#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "SpaceShip.generated.h"class USphereComponent;
class UCameraComponent;
class USpringArmComponent;
class ABullet;
class USoundCue;UCLASS()
class SPACESHIPBATTLE_API ASpaceShip : public APawn
{GENERATED_BODY()public:// Sets default values for this pawn's propertiesASpaceShip();protected:UPROPERTY(VisibleAnywhere, Category = "Component")USphereComponent* CollisionComp;UPROPERTY(VisibleAnywhere, Category = "Component")UStaticMeshComponent* ShipSM;UPROPERTY(VisibleAnywhere, Category = "Component")UCameraComponent* CameraComp;UPROPERTY(VisibleAnywhere, Category = "Component")USpringArmComponent* SpringArmComp;APlayerController* PC;UPROPERTY(EditAnywhere, Category = "Fire")TSubclassOf<ABullet> Bullet;UPROPERTY(VisibleAnywhere, Category = "Component")USceneComponent* SpawnPoint;UPROPERTY(EditAnywhere, Category = "Move")float Speed;FTimerHandle TimerHandle_BetweenShot;FTimerHandle TimerHandle_Restart;UPROPERTY(EditAnywhere, Category = "Fire")float TimeBetweenShot;UPROPERTY(EditAnywhere, Category = "Souned")USoundCue* GameOverCue;UPROPERTY(EditAnywhere, Category = "Souned")USoundCue* ShootCue;UPROPERTY(VisibleAnywhere, Category = "Component")UParticleSystemComponent* ThrusterParticleComp;UPROPERTY(EditAnywhere, Category = "Particle")UParticleSystem* ExplosionParticle;bool bDead;bool bUpMove;bool bRightMove;// Called when the game starts or when spawnedvirtual void BeginPlay() override;void LookAtCursor();void MoveUp(float Value);void MoveRight(float Value);void Move();void Fire();void StartFire();void EndFire();void RestartLevel();void OnDeath();public: // Called every framevirtual void Tick(float DeltaTime) override;// Called to bind functionality to inputvirtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;virtual void NotifyActorBeginOverlap(AActor* OtherActor) override;FORCEINLINE bool GetBDead() {return bDead;}
};
Enemy.cpp
#include "Enemy.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SphereComponent.h"
#include "Kismet/GameplayStatics.h"
#include "SpaceShip.h"
#include "Kismet/KismetMathLibrary.h"
#include "ShipGameMode.h"
#include "EnemySpawner.h"
#include "Particles/ParticleSystem.h"// Sets default values
AEnemy::AEnemy()
{// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.PrimaryActorTick.bCanEverTick = true;CollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("CollisionComp"));RootComponent = CollisionComp;ShipSM = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ShipSM"));ShipSM->SetupAttachment(RootComponent);
}// Called when the game starts or when spawned
void AEnemy::BeginPlay()
{Super::BeginPlay();SpaceShip = Cast<ASpaceShip>(UGameplayStatics::GetPlayerPawn(this, 0));SetColor();MyGameMode = Cast<AShipGameMode>(UGameplayStatics::GetGameMode(this));TArray<AActor*> EnemySpawnerArray;UGameplayStatics::GetAllActorsOfClass(this, AEnemySpawner::StaticClass(), EnemySpawnerArray);EnemySpawner = Cast<AEnemySpawner>(EnemySpawnerArray[0]);
}void AEnemy::MoveTowardsPlayer(float DeltaTime)
{FVector Direction = (SpaceShip->GetActorLocation() - GetActorLocation()).GetSafeNormal();AddActorWorldOffset(Direction * Speed * DeltaTime, true);SetActorRotation(UKismetMathLibrary::FindLookAtRotation(GetActorLocation(), SpaceShip->GetActorLocation()));
}void AEnemy::OnDeath()
{MyGameMode->IncreaseScore();EnemySpawner->DecreaseEnemyCount();SpawnExplosion();Destroy();
}// Called every frame
void AEnemy::Tick(float DeltaTime)
{Super::Tick(DeltaTime);if (SpaceShip->GetBDead() == false) MoveTowardsPlayer(DeltaTime);
}// Called to bind functionality to input
void AEnemy::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{Super::SetupPlayerInputComponent(PlayerInputComponent);}
Enemy.h
#pragma once#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Enemy.generated.h"class USphereComponent;
class ASpaceShip;
class AShipGameMode;
class AEnemySpawner;UCLASS()
class SPACESHIPBATTLE_API AEnemy : public APawn
{GENERATED_BODY()public:// Sets default values for this pawn's propertiesAEnemy();protected:UPROPERTY(VisibleAnywhere, Category = "Component")USphereComponent* CollisionComp;UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Component")UStaticMeshComponent* ShipSM;// Called when the game starts or when spawnedvirtual void BeginPlay() override;void MoveTowardsPlayer(float DeltaTime);ASpaceShip* SpaceShip;AShipGameMode* MyGameMode;float Speed = 300.0f;AEnemySpawner* EnemySpawner;UFUNCTION(BlueprintImplementableEvent)void SetColor();UFUNCTION(BlueprintImplementableEvent)void SpawnExplosion();UPROPERTY(EditAnywhere, Category = "Particle")UParticleSystem* ExplosionParticle;public: // Called every framevirtual void Tick(float DeltaTime) override;// Called to bind functionality to inputvirtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;void OnDeath();
};
Bullet.cpp
#include "Bullet.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SceneComponent.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Enemy.h"
#include "Engine/BlockingVolume.h"
#include "Kismet/GameplayStatics.h"
#include "EnemySpawner.h"// Sets default values
ABullet::ABullet()
{// 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;RootComp = CreateDefaultSubobject<USceneComponent>(TEXT("RootComp"));RootComponent = RootComp;BulletSM = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BulletSM"));BulletSM->SetupAttachment(RootComponent);ProjectileMovementComp = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovementComp"));
}// Called when the game starts or when spawned
void ABullet::BeginPlay()
{Super::BeginPlay();}// Called every frame
void ABullet::Tick(float DeltaTime)
{Super::Tick(DeltaTime);}void ABullet::NotifyActorBeginOverlap(AActor* OtherActor)
{Super::NotifyActorBeginOverlap(OtherActor);AEnemy* Enemy = Cast<AEnemy>(OtherActor);if (Enemy) {Enemy->OnDeath();Destroy();}else if (Cast<ABlockingVolume>(OtherActor)) {Destroy();}
}
Bullet.h
#pragma once#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Bullet.generated.h"class UProjectileMovementComponent;UCLASS()
class SPACESHIPBATTLE_API ABullet : public AActor
{GENERATED_BODY()public: // Sets default values for this actor's propertiesABullet();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;UPROPERTY(VisibleAnywhere, Category = "Component")USceneComponent* RootComp;UPROPERTY(VisibleAnywhere, Category = "Component")UStaticMeshComponent* BulletSM;UPROPERTY(VisibleAnywhere, Category = "Component")UProjectileMovementComponent* ProjectileMovementComp;public: // Called every framevirtual void Tick(float DeltaTime) override;virtual void NotifyActorBeginOverlap(AActor* OtherActor) override;
};
ShipGameMode.cpp
#include "ShipGameMode.h"AShipGameMode::AShipGameMode() {Score = 0;
}void AShipGameMode::IncreaseScore()
{Score++;
}
ShipGameMode.h
#pragma once#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "ShipGameMode.generated.h"/*** */
UCLASS()
class SPACESHIPBATTLE_API AShipGameMode : public AGameModeBase
{GENERATED_BODY()protected:AShipGameMode();UPROPERTY(BlueprintReadOnly)int Score;public:void IncreaseScore();};
EnemySpawner.cpp
#include "EnemySpawner.h"
#include "Components/BoxComponent.h"
#include "Kismet/KismetMathLibrary.h"
#include "Kismet/GameplayStatics.h"
#include "SpaceShip.h"
#include "Engine/World.h"
#include "Enemy.h"// Sets default values
AEnemySpawner::AEnemySpawner()
{// 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;SpawnArea = CreateDefaultSubobject<UBoxComponent>(TEXT("SpawnArea"));RootComponent = SpawnArea;SpawnInterval = 2.0f;MaxEnemyNum = 30;CurrentEnemyCount = 0;
}// Called when the game starts or when spawned
void AEnemySpawner::BeginPlay()
{Super::BeginPlay();SpaceShip = Cast<ASpaceShip>(UGameplayStatics::GetPlayerPawn(this, 0));GetWorldTimerManager().SetTimer(TimerHandle_Spawn, this, &AEnemySpawner::SpawnEnemy, SpawnInterval, true, 0.0f);
}FVector AEnemySpawner::GetGenerateLocation()
{float Distance = 0;FVector Location;while (Distance < MininumDistanceToPlayer) {Location = UKismetMathLibrary::RandomPointInBoundingBox(SpawnArea->Bounds.Origin, SpawnArea->Bounds.BoxExtent);Distance = (Location - SpaceShip->GetActorLocation()).Size();}return Location;
}void AEnemySpawner::SpawnEnemy()
{if (SpaceShip->GetBDead() == false && CurrentEnemyCount < MaxEnemyNum) {FActorSpawnParameters SpawnParameters;GetWorld()->SpawnActor<AEnemy>(Enemy, GetGenerateLocation(), FRotator::ZeroRotator, SpawnParameters);CurrentEnemyCount++;}
}// Called every frame
void AEnemySpawner::Tick(float DeltaTime)
{Super::Tick(DeltaTime);}void AEnemySpawner::DecreaseEnemyCount()
{if (CurrentEnemyCount > 0) {CurrentEnemyCount--;UE_LOG(LogTemp, Warning, TEXT("%s"), *FString::SanitizeFloat(CurrentEnemyCount));}
}
EnemySpawner.h
#pragma once#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "EnemySpawner.generated.h"class AEnemy;
class UBoxComponent;
class ASpaceShip;UCLASS()
class SPACESHIPBATTLE_API AEnemySpawner : public AActor
{GENERATED_BODY()public: // Sets default values for this actor's propertiesAEnemySpawner();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;UPROPERTY(EditAnywhere, Category = "Enemy")TSubclassOf<AEnemy> Enemy;UPROPERTY(VisibleAnywhere, Category = "Component")UBoxComponent* SpawnArea;FVector GetGenerateLocation();float MininumDistanceToPlayer = 1200.0f;ASpaceShip* SpaceShip;void SpawnEnemy();FTimerHandle TimerHandle_Spawn;float SpawnInterval;UPROPERTY(EditAnywhere, Category = "Spawn")int MaxEnemyNum;int CurrentEnemyCount;public: // Called every framevirtual void Tick(float DeltaTime) override;void DecreaseEnemyCount();};
以上就是编写的全部代码了,相关的在UE4中需要进行设置的在此就不再赘述,如有任何问题欢迎与我讨论。整体工程较大(将近1GB),如果你想要的话私聊我,我可以通过邮箱发给你。
siki学院的飞机大作战UE4.26代码相关推荐
- 飞机大作战java源代码_java实现抖音飞机大作战
本文实例为大家分享了java抖音飞机大作战的具体代码,供大家参考,具体内容如下 Airplane.java package zmf.game.shoot; import java.util.Rando ...
- 基于LabVIEW的飞机大作战小游戏(可做毕设)
一.前言 Python是目前相当流行的一门编程语言,网上有人用Python做了一个<飞机大作战>的小游戏,并且出了一份视频教程,很有意思."基于Python的飞机大作战小游戏&q ...
- python小项目之飞机大作战
** 第一步:导入pygame 模块 ** 飞机大作战image数据集:https://pan.baidu.com/s/1pMM0beb (windows环境+python3.7) win+R调出命令 ...
- Python—飞机大作战游戏(附源代码及素材)
目录 过程说明: 主函数 键盘控制 创建类 01.飞机基类 02.子弹基类 03. Hero飞机类 04.enemy飞机类 源代码及素材 过程说明: 应用到的库: import pyg ...
- [知了堂学习笔记]_用JS制作《飞机大作战》游戏_第2讲(四大界面之间的跳转与玩家飞机的移动)
一.通过点击按钮事件,实现四大界面之间的跳转: (一)跳转的思路: 1.打开软件,只显示登录界面(隐藏游戏界面.暂停界面.玩家死亡界面) 2.点击微信登录(QQ登录)跳转到游戏界面,隐藏登录界面 3. ...
- scratch飞机大作战
西瓜编程课又开始了!本课我们要做一个游戏--飞机大作战 游戏效果:1.用上下左右键来操控飞机,空格键按下后能发射子弹. 2.敌机各式各样,打败蓝敌机获得升级子弹的黄能量豆 ...
- python进阶应用:飞机大作战小游戏完整代码实现(初级)
飞机大作战(初级) 完整代码如下: 注意,需要在对应同一个文件中放入相应的游戏需要用的图片及音效.否则可能报错 # 导入pygame库 import pygame import random# 设置常 ...
- python基于pygame的飞机大作战小游戏
基于pygame的飞机大作战小游戏,适合新手,不能直接运行,只能在命令行进入当前游戏目录,输入python game.py才能够运行,尚不知道是什么原因 游戏截图如下,我们用黄色的圆圈代表敌机, 代码 ...
- Java窗体小游戏开发飞机大作战Java小游戏开发源码
Java窗体小游戏开发飞机大作战Java小游戏开发源码
- 球球大作战简易版代码(含简单人机)
不废话,直接上代码: #include<stdio.h> #include<easyx.h> #include<stdlib.h> #include<time ...
最新文章
- java数独中数独空格初始化,java高手近解决数独问题,看你是不是高手!
- python day two,while
- 阿里员工都是这样排查Java问题的,附工具单
- web页面--前端明水印
- HTTP/2 in GO(二)
- ubuntu 设置php开机启动
- python窗体处理access数据库_用Python操作MS Access数据库
- 20190829:(leetcode习题)环形链表
- centOS下开机自启动apache、mysql、samba、svn等服务的最简单方法
- HTML5学习笔记 二:article和section
- java 单点_java实现单点登录的两种方式
- 图的拓补排序(TopologicalSort)算法在邻接表与邻接矩阵结构下实现
- 应届生比老员工更吃香?为什么大厂都在抢应届生
- 指纹识别、图形识别、aliOCR 识别
- 百度云平台BAE空间申请
- SICP中关于兑换零钱的练习
- 碎片化时间学习,这几个在线视频学习网站值得拥有!
- 怎么释放gpu内存占用
- android 启动画面广告,浅谈APP启动界面广告
- 闭关修炼21天,“啃完”283页pdf,我终于4面拿下字节跳动offer