parent
c6a4207675
commit
00e9bfe27b
@ -0,0 +1,192 @@ |
|||||||
|
#define OLC_PGE_APPLICATION |
||||||
|
#include "olcPixelGameEngine.h" |
||||||
|
|
||||||
|
struct Rect{ |
||||||
|
float x,y,w,h; |
||||||
|
}; |
||||||
|
|
||||||
|
struct Enemy{ |
||||||
|
olc::vf2d pos; |
||||||
|
Rect alienSpr; |
||||||
|
int health=1; |
||||||
|
bool alive=true; |
||||||
|
//Returns true if the enemy has died.
|
||||||
|
void Hurt(int damage=1){ |
||||||
|
health-=damage; |
||||||
|
if(health<=0){ |
||||||
|
alive=false; |
||||||
|
} |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
struct Bullet{ |
||||||
|
olc::vf2d pos; |
||||||
|
bool alive=true; |
||||||
|
void Update(float fElapsedTime){ |
||||||
|
pos.y-=96*fElapsedTime; |
||||||
|
} |
||||||
|
void Kill(){ |
||||||
|
alive=false; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
class ShooterGame : public olc::PixelGameEngine |
||||||
|
{ |
||||||
|
Rect alien1={5,22, 11,9}; |
||||||
|
Rect alien2={19,22, 12,6}; |
||||||
|
Rect alien3={34,20, 11,8}; |
||||||
|
Rect ship={35,6, 11,7}; |
||||||
|
|
||||||
|
olc::Decal*tilesheet; |
||||||
|
|
||||||
|
std::vector<Enemy>enemies; |
||||||
|
std::vector<Bullet>bullets; |
||||||
|
olc::vf2d playerPos={128,200}; |
||||||
|
float playerShotTimer=0; |
||||||
|
int enemyMoveAmount=0; |
||||||
|
bool movingRight=true; |
||||||
|
float enemyMoveTimer=0; |
||||||
|
|
||||||
|
void DrawSubSprite(olc::vi2d pos,Rect sprite,olc::Pixel color=olc::WHITE){ |
||||||
|
DrawPartialDecal(pos-olc::vf2d{sprite.w,sprite.h}/2,{sprite.w,sprite.h},tilesheet,{sprite.x,sprite.y},{sprite.w,sprite.h},color); |
||||||
|
} |
||||||
|
|
||||||
|
public: |
||||||
|
ShooterGame() |
||||||
|
{ |
||||||
|
// Name your application
|
||||||
|
sAppName = "Shooter Game"; |
||||||
|
} |
||||||
|
|
||||||
|
public: |
||||||
|
bool OnUserCreate() override |
||||||
|
{ |
||||||
|
tilesheet = new olc::Decal(new olc::Sprite("tilesheet.png")); |
||||||
|
|
||||||
|
for(int y=0;y<4;y++){ |
||||||
|
for(int x=0;x<12;x++){ |
||||||
|
int alienType = rand()%3; //Choose an alien number between 0-2
|
||||||
|
Rect alienSpr; |
||||||
|
int alienHealth=1; |
||||||
|
switch(alienType){ |
||||||
|
case 0:{ |
||||||
|
alienSpr=alien1; |
||||||
|
}break; |
||||||
|
case 1:{ |
||||||
|
alienSpr=alien2; |
||||||
|
}break; |
||||||
|
case 2:{ |
||||||
|
alienSpr=alien3; |
||||||
|
alienHealth=3; |
||||||
|
}break; |
||||||
|
} |
||||||
|
enemies.push_back({{24.f+16*x,24.f+12*y},alienSpr,alienHealth}); |
||||||
|
} |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
bool OnUserUpdate(float fElapsedTime) override |
||||||
|
{ |
||||||
|
Clear(olc::BLACK); |
||||||
|
|
||||||
|
if(enemyMoveTimer==0){ |
||||||
|
|
||||||
|
if(movingRight){ |
||||||
|
|
||||||
|
if(enemyMoveAmount>=3){ |
||||||
|
|
||||||
|
movingRight=false; |
||||||
|
for(Enemy&enemy:enemies){ |
||||||
|
enemy.pos.y+=12; |
||||||
|
} |
||||||
|
|
||||||
|
} else { |
||||||
|
|
||||||
|
enemyMoveAmount++; |
||||||
|
for(Enemy&enemy:enemies){ |
||||||
|
enemy.pos.x+=16; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ |
||||||
|
|
||||||
|
if(enemyMoveAmount<0){ |
||||||
|
|
||||||
|
movingRight=true; |
||||||
|
for(Enemy&enemy:enemies){ |
||||||
|
enemy.pos.y+=12; |
||||||
|
} |
||||||
|
|
||||||
|
} else { |
||||||
|
|
||||||
|
enemyMoveAmount--; |
||||||
|
for(Enemy&enemy:enemies){ |
||||||
|
enemy.pos.x-=16; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
enemyMoveTimer=0.8; |
||||||
|
} |
||||||
|
enemyMoveTimer=std::max(enemyMoveTimer-fElapsedTime,0.f); |
||||||
|
|
||||||
|
for(Bullet&bullet:bullets){ |
||||||
|
bullet.Update(fElapsedTime); |
||||||
|
for(Enemy&enemy:enemies){ |
||||||
|
float distanceFromEnemy=sqrt(pow(bullet.pos.x-enemy.pos.x,2)+pow(bullet.pos.y-enemy.pos.y,2)); |
||||||
|
if(distanceFromEnemy<=7){ |
||||||
|
enemy.Hurt(1); |
||||||
|
bullet.Kill(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
enemies.erase(std::remove_if(enemies.begin(),enemies.end(),[](Enemy&enemy){return !enemy.alive;}),enemies.end()); |
||||||
|
bullets.erase(std::remove_if(bullets.begin(),bullets.end(),[](Bullet&bullet){return !bullet.alive;}),bullets.end()); |
||||||
|
|
||||||
|
playerShotTimer=std::max(playerShotTimer-fElapsedTime,0.f); |
||||||
|
|
||||||
|
if(GetKey(olc::A).bHeld){ |
||||||
|
playerPos.x-=48*fElapsedTime; |
||||||
|
} |
||||||
|
if(GetKey(olc::D).bHeld){ |
||||||
|
playerPos.x+=48*fElapsedTime; |
||||||
|
} |
||||||
|
if(GetKey(olc::SPACE).bHeld){ |
||||||
|
if(playerShotTimer==0){ |
||||||
|
bullets.push_back({playerPos}); |
||||||
|
playerShotTimer=0.4; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
for(Enemy&enemy:enemies){ |
||||||
|
if(enemy.health>2){ |
||||||
|
DrawSubSprite(enemy.pos,enemy.alienSpr,olc::RED); |
||||||
|
}else |
||||||
|
if(enemy.health>1){ |
||||||
|
DrawSubSprite(enemy.pos,enemy.alienSpr,olc::YELLOW); |
||||||
|
} else { |
||||||
|
DrawSubSprite(enemy.pos,enemy.alienSpr,olc::GREEN); |
||||||
|
} |
||||||
|
} |
||||||
|
for(Bullet&bullet:bullets){ |
||||||
|
DrawLine(bullet.pos,bullet.pos+olc::vi2d{0,-4}); |
||||||
|
} |
||||||
|
DrawSubSprite(playerPos,ship); |
||||||
|
return true; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
int main() |
||||||
|
{ |
||||||
|
ShooterGame demo; |
||||||
|
if (demo.Construct(256, 240, 4, 4)) |
||||||
|
demo.Start(); |
||||||
|
return 0; |
||||||
|
} |
@ -0,0 +1,138 @@ |
|||||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||||
|
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
||||||
|
<ItemGroup Label="ProjectConfigurations"> |
||||||
|
<ProjectConfiguration Include="Debug|Win32"> |
||||||
|
<Configuration>Debug</Configuration> |
||||||
|
<Platform>Win32</Platform> |
||||||
|
</ProjectConfiguration> |
||||||
|
<ProjectConfiguration Include="Release|Win32"> |
||||||
|
<Configuration>Release</Configuration> |
||||||
|
<Platform>Win32</Platform> |
||||||
|
</ProjectConfiguration> |
||||||
|
<ProjectConfiguration Include="Debug|x64"> |
||||||
|
<Configuration>Debug</Configuration> |
||||||
|
<Platform>x64</Platform> |
||||||
|
</ProjectConfiguration> |
||||||
|
<ProjectConfiguration Include="Release|x64"> |
||||||
|
<Configuration>Release</Configuration> |
||||||
|
<Platform>x64</Platform> |
||||||
|
</ProjectConfiguration> |
||||||
|
</ItemGroup> |
||||||
|
<PropertyGroup Label="Globals"> |
||||||
|
<VCProjectVersion>16.0</VCProjectVersion> |
||||||
|
<Keyword>Win32Proj</Keyword> |
||||||
|
<ProjectGuid>{0c2682a1-988b-4c60-8ba1-157cd1e2637a}</ProjectGuid> |
||||||
|
<RootNamespace>ShooterGame</RootNamespace> |
||||||
|
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion> |
||||||
|
</PropertyGroup> |
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> |
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> |
||||||
|
<ConfigurationType>Application</ConfigurationType> |
||||||
|
<UseDebugLibraries>true</UseDebugLibraries> |
||||||
|
<PlatformToolset>v143</PlatformToolset> |
||||||
|
<CharacterSet>Unicode</CharacterSet> |
||||||
|
</PropertyGroup> |
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> |
||||||
|
<ConfigurationType>Application</ConfigurationType> |
||||||
|
<UseDebugLibraries>false</UseDebugLibraries> |
||||||
|
<PlatformToolset>v143</PlatformToolset> |
||||||
|
<WholeProgramOptimization>true</WholeProgramOptimization> |
||||||
|
<CharacterSet>Unicode</CharacterSet> |
||||||
|
</PropertyGroup> |
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> |
||||||
|
<ConfigurationType>Application</ConfigurationType> |
||||||
|
<UseDebugLibraries>true</UseDebugLibraries> |
||||||
|
<PlatformToolset>v143</PlatformToolset> |
||||||
|
<CharacterSet>Unicode</CharacterSet> |
||||||
|
</PropertyGroup> |
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> |
||||||
|
<ConfigurationType>Application</ConfigurationType> |
||||||
|
<UseDebugLibraries>false</UseDebugLibraries> |
||||||
|
<PlatformToolset>v143</PlatformToolset> |
||||||
|
<WholeProgramOptimization>true</WholeProgramOptimization> |
||||||
|
<CharacterSet>Unicode</CharacterSet> |
||||||
|
</PropertyGroup> |
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> |
||||||
|
<ImportGroup Label="ExtensionSettings"> |
||||||
|
</ImportGroup> |
||||||
|
<ImportGroup Label="Shared"> |
||||||
|
</ImportGroup> |
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> |
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> |
||||||
|
</ImportGroup> |
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> |
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> |
||||||
|
</ImportGroup> |
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> |
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> |
||||||
|
</ImportGroup> |
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> |
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> |
||||||
|
</ImportGroup> |
||||||
|
<PropertyGroup Label="UserMacros" /> |
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> |
||||||
|
<ClCompile> |
||||||
|
<WarningLevel>Level3</WarningLevel> |
||||||
|
<SDLCheck>true</SDLCheck> |
||||||
|
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> |
||||||
|
<ConformanceMode>true</ConformanceMode> |
||||||
|
</ClCompile> |
||||||
|
<Link> |
||||||
|
<SubSystem>Console</SubSystem> |
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation> |
||||||
|
</Link> |
||||||
|
</ItemDefinitionGroup> |
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> |
||||||
|
<ClCompile> |
||||||
|
<WarningLevel>Level3</WarningLevel> |
||||||
|
<FunctionLevelLinking>true</FunctionLevelLinking> |
||||||
|
<IntrinsicFunctions>true</IntrinsicFunctions> |
||||||
|
<SDLCheck>true</SDLCheck> |
||||||
|
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> |
||||||
|
<ConformanceMode>true</ConformanceMode> |
||||||
|
</ClCompile> |
||||||
|
<Link> |
||||||
|
<SubSystem>Console</SubSystem> |
||||||
|
<EnableCOMDATFolding>true</EnableCOMDATFolding> |
||||||
|
<OptimizeReferences>true</OptimizeReferences> |
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation> |
||||||
|
</Link> |
||||||
|
</ItemDefinitionGroup> |
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> |
||||||
|
<ClCompile> |
||||||
|
<WarningLevel>Level3</WarningLevel> |
||||||
|
<SDLCheck>true</SDLCheck> |
||||||
|
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> |
||||||
|
<ConformanceMode>true</ConformanceMode> |
||||||
|
</ClCompile> |
||||||
|
<Link> |
||||||
|
<SubSystem>Console</SubSystem> |
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation> |
||||||
|
</Link> |
||||||
|
</ItemDefinitionGroup> |
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> |
||||||
|
<ClCompile> |
||||||
|
<WarningLevel>Level3</WarningLevel> |
||||||
|
<FunctionLevelLinking>true</FunctionLevelLinking> |
||||||
|
<IntrinsicFunctions>true</IntrinsicFunctions> |
||||||
|
<SDLCheck>true</SDLCheck> |
||||||
|
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> |
||||||
|
<ConformanceMode>true</ConformanceMode> |
||||||
|
</ClCompile> |
||||||
|
<Link> |
||||||
|
<SubSystem>Console</SubSystem> |
||||||
|
<EnableCOMDATFolding>true</EnableCOMDATFolding> |
||||||
|
<OptimizeReferences>true</OptimizeReferences> |
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation> |
||||||
|
</Link> |
||||||
|
</ItemDefinitionGroup> |
||||||
|
<ItemGroup> |
||||||
|
<ClInclude Include="olcPixelGameEngine.h" /> |
||||||
|
</ItemGroup> |
||||||
|
<ItemGroup> |
||||||
|
<ClCompile Include="ShooterGame.cpp" /> |
||||||
|
</ItemGroup> |
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> |
||||||
|
<ImportGroup Label="ExtensionTargets"> |
||||||
|
</ImportGroup> |
||||||
|
</Project> |
File diff suppressed because it is too large
Load Diff
After Width: | Height: | Size: 15 KiB |
Loading…
Reference in new issue