generated from sigonasr2/CPlusPlusProjectTemplate
The good old Neggs are back.
https://sigonasr2.itch.io/meerca-chase-clone
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
274 lines
8.1 KiB
274 lines
8.1 KiB
#define OLC_PGE_APPLICATION
|
|
#include "pixelGameEngine.h"
|
|
#include <math.h>
|
|
|
|
#define boolean char
|
|
#define true 1
|
|
#define false 0
|
|
|
|
using namespace std;
|
|
|
|
#define UP 0
|
|
#define RIGHT 1
|
|
#define DOWN 2
|
|
#define LEFT 3
|
|
|
|
#define WINDOW_WIDTH 480
|
|
#define WINDOW_HEIGHT 240
|
|
|
|
#define TAIL_DISTANCE 4
|
|
|
|
struct TailData{
|
|
char direction = 0;
|
|
char*future_direction;
|
|
int*pos;
|
|
int*frameCount;
|
|
int waitTime = 0;
|
|
char futureIndex=0;
|
|
};
|
|
//Yellow = 1
|
|
//Blue = 2
|
|
//Green = 3
|
|
//Grey = 4
|
|
//Brown = 5
|
|
//Aqua = 6
|
|
//Red = 7
|
|
//Orange = 8
|
|
//Golden = 9
|
|
//Rainbow = 10
|
|
|
|
class MeercaChase : public olc::PixelGameEngine
|
|
{
|
|
public:
|
|
olc::Decal*meerca,*negg,*badnegg,*goldennegg,*rainbownegg;
|
|
const float UPDATE_RATE = 0.016666667f;
|
|
const int NEGG_BOUNDARY=6;
|
|
const int WALL_BOUNDARY=-8;
|
|
float accumulatedTime = 0.0f;
|
|
int frameCount=0;
|
|
int moveSpd=4;
|
|
int neggType=1;
|
|
float meercaSpd[2] = {0,0};
|
|
float meercaPos[2] = {WINDOW_WIDTH/2,WINDOW_HEIGHT/2};
|
|
float scorePos[2] = {-50,-50};
|
|
int scoreTransparency=255;
|
|
int scoreValue=1;
|
|
float*meercaPreviousPos=NULL;
|
|
float neggPos[2] = {};
|
|
float*badNeggs=NULL;
|
|
int badNeggCount=0;
|
|
int tailSize=0;
|
|
int score=0;
|
|
|
|
void assignNeggRandomPos() {
|
|
do {
|
|
neggPos[0]=(float)rand()/(float)RAND_MAX*(WINDOW_WIDTH-NEGG_BOUNDARY*2)+NEGG_BOUNDARY;
|
|
neggPos[1]=(float)rand()/(float)RAND_MAX*(WINDOW_HEIGHT-NEGG_BOUNDARY*2)+NEGG_BOUNDARY;
|
|
} while(!positionFree(neggPos));
|
|
}
|
|
void spawnBadNegg() {
|
|
badNeggs=(float*)realloc(badNeggs,sizeof(float)*(++badNeggCount*2));
|
|
do {
|
|
badNeggs[badNeggCount*2-2] = (float)rand()/(float)RAND_MAX*(WINDOW_WIDTH-NEGG_BOUNDARY*2)+NEGG_BOUNDARY;
|
|
badNeggs[badNeggCount*2-1] = (float)rand()/(float)RAND_MAX*(WINDOW_HEIGHT-NEGG_BOUNDARY*2)+NEGG_BOUNDARY;
|
|
} while(!positionFree(badNeggs+(badNeggCount*2-2),true));
|
|
}
|
|
boolean positionFree(float*pos) {
|
|
return positionFree(pos,false);
|
|
}
|
|
boolean positionFree(float*pos,boolean badNegg) {
|
|
if (badNegg&&abs(pos[0]-neggPos[0])<4&&abs(pos[1]-neggPos[1])<4) {
|
|
return false;
|
|
}
|
|
if (abs(pos[0]-meercaPos[0])<64&&abs(pos[1]-meercaPos[1])<64) {
|
|
return false;
|
|
}
|
|
for (int i=0;i<((badNegg)?badNeggCount-1:badNeggCount);i++) {
|
|
if (abs(pos[0]-badNeggs[i*2])<4&&abs(pos[1]-badNeggs[i*2+1])<4) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
MeercaChase()
|
|
{
|
|
sAppName = "Meerca Chase";
|
|
init();
|
|
}
|
|
void init() {
|
|
srand(432189);
|
|
assignNeggRandomPos();
|
|
if (badNeggs!=NULL) {free(badNeggs);}
|
|
if (meercaPreviousPos!=NULL) {
|
|
free(meercaPreviousPos);
|
|
}
|
|
badNeggs=(float*)malloc(sizeof(float)*0);
|
|
meercaPreviousPos=(float*)malloc(sizeof(float)*0);
|
|
badNeggCount=0;
|
|
tailSize=0;
|
|
score=0;
|
|
meercaPos[0]=WINDOW_WIDTH/2;
|
|
meercaPos[1]=WINDOW_HEIGHT/2;
|
|
meercaSpd[0]=0;
|
|
meercaSpd[1]=0;
|
|
}
|
|
bool OnUserCreate() override
|
|
{
|
|
meerca = new olc::Decal(new olc::Sprite("assets/meerca.png"));
|
|
negg = new olc::Decal(new olc::Sprite("assets/negg.png"));
|
|
badnegg = new olc::Decal(new olc::Sprite("assets/badnegg.png"));
|
|
goldennegg = new olc::Decal(new olc::Sprite("assets/golden_negg.png"));
|
|
rainbownegg = new olc::Decal(new olc::Sprite("assets/rainbow_negg.png"));
|
|
SetPixelMode(olc::Pixel::ALPHA);
|
|
ConsoleCaptureStdOut(true);
|
|
Clear(olc::DARK_CYAN);
|
|
|
|
// Called once at the start, so create things here
|
|
return true;
|
|
}
|
|
|
|
bool OnUserUpdate(float fElapsedTime) override
|
|
{
|
|
Clear(olc::DARK_CYAN);
|
|
accumulatedTime+=fElapsedTime;
|
|
while (accumulatedTime>=UPDATE_RATE) {
|
|
accumulatedTime-=UPDATE_RATE;
|
|
updateGame(); //DO NOT ADD THINGS HERE. USE updateGame()!
|
|
}
|
|
if (GetKey(olc::F1).bPressed) {
|
|
ConsoleShow(olc::F1,false);
|
|
}
|
|
if ((GetKey(olc::W).bPressed||(GetMouse(0).bPressed&&(GetMouseY()-meercaPos[1]<0&&(meercaSpd[0]!=0||abs(GetMouseX()-meercaPos[0])<abs(GetMouseY()-meercaPos[1])))))&&(meercaSpd[0]+meercaSpd[1]==0||meercaSpd[0]!=0)) {
|
|
meercaSpd[0]=0;
|
|
meercaSpd[1]=-moveSpd;
|
|
} else
|
|
if ((GetKey(olc::A).bPressed||(GetMouse(0).bPressed&&(GetMouseX()-meercaPos[0]<0&&(meercaSpd[1]!=0||abs(GetMouseY()-meercaPos[1])<abs(GetMouseX()-meercaPos[0])))))&&(meercaSpd[0]+meercaSpd[1]==0||meercaSpd[1]!=0)) {
|
|
meercaSpd[0]=-moveSpd;
|
|
meercaSpd[1]=0;
|
|
} else
|
|
if ((GetKey(olc::D).bPressed||(GetMouse(0).bPressed&&(GetMouseX()-meercaPos[0]>0&&(meercaSpd[1]!=0||abs(GetMouseY()-meercaPos[1])<abs(GetMouseX()-meercaPos[0])))))&&(meercaSpd[0]+meercaSpd[1]==0||meercaSpd[1]!=0)) {
|
|
meercaSpd[0]=moveSpd;
|
|
meercaSpd[1]=0;
|
|
} else
|
|
if ((GetKey(olc::S).bPressed||(GetMouse(0).bPressed&&(GetMouseY()-meercaPos[1]>0&&(meercaSpd[0]!=0||abs(GetMouseX()-meercaPos[0])<abs(GetMouseY()-meercaPos[1])))))&&(meercaSpd[0]+meercaSpd[1]==0||meercaSpd[0]!=0)) {
|
|
meercaSpd[0]=0;
|
|
meercaSpd[1]=moveSpd;
|
|
}
|
|
// called once per frame
|
|
if (meercaSpd[0]==moveSpd) {
|
|
DrawRotatedDecal({meercaPos[0],meercaPos[1]},meerca,M_PI_2,{16,16});
|
|
} else
|
|
if (meercaSpd[0]==-moveSpd) {
|
|
DrawRotatedDecal({meercaPos[0],meercaPos[1]},meerca,M_PI_2*3,{16,16});
|
|
} else
|
|
if (meercaSpd[1]==moveSpd) {
|
|
DrawRotatedDecal({meercaPos[0],meercaPos[1]},meerca,M_PI,{16,16});
|
|
} else {
|
|
DrawRotatedDecal({meercaPos[0],meercaPos[1]},meerca,0,{16,16});
|
|
}
|
|
switch (neggType) {
|
|
case 1:{
|
|
DrawRotatedDecal({neggPos[0],neggPos[1]},negg,0,{16,16},{1.5,1.5},olc::YELLOW);
|
|
}break;
|
|
case 2:{
|
|
DrawRotatedDecal({neggPos[0],neggPos[1]},negg,0,{16,16},{1.5,1.5},olc::BLUE);
|
|
}break;
|
|
case 3:{
|
|
DrawRotatedDecal({neggPos[0],neggPos[1]},negg,0,{16,16},{1.5,1.5},olc::GREEN);
|
|
}break;
|
|
case 4:{
|
|
DrawRotatedDecal({neggPos[0],neggPos[1]},negg,0,{16,16},{1.5,1.5},olc::GREY);
|
|
}break;
|
|
case 5:{
|
|
DrawRotatedDecal({neggPos[0],neggPos[1]},negg,0,{16,16},{1.5,1.5},olc::Pixel(71,36,20));
|
|
}break;
|
|
case 6:{
|
|
DrawRotatedDecal({neggPos[0],neggPos[1]},negg,0,{16,16},{1.5,1.5},olc::Pixel(0,251,255));
|
|
}break;
|
|
case 7:{
|
|
DrawRotatedDecal({neggPos[0],neggPos[1]},negg,0,{16,16},{1.5,1.5},olc::MAGENTA);
|
|
}break;
|
|
case 8:{
|
|
DrawRotatedDecal({neggPos[0],neggPos[1]},negg,0,{16,16},{1.5,1.5},olc::Pixel(255,128,0));
|
|
}break;
|
|
case 9:{
|
|
DrawRotatedDecal({neggPos[0],neggPos[1]},goldennegg,0,{16,16},{1.5,1.5},olc::WHITE);
|
|
}break;
|
|
case 10:{
|
|
DrawRotatedDecal({neggPos[0],neggPos[1]},rainbownegg,0,{16,16},{1.5,1.5},olc::WHITE);
|
|
}break;
|
|
}
|
|
for (int i=0;i<badNeggCount;i++) {
|
|
DrawRotatedDecal({badNeggs[i*2],badNeggs[i*2+1]},badnegg,0,{16,16},{1,1});
|
|
}
|
|
DrawString({16,16},"Score: "+to_string(score),olc::VERY_DARK_BLUE);
|
|
for (int i=0;i<tailSize;i++) {
|
|
if (i!=tailSize-1&&abs(meercaPos[0]-meercaPreviousPos[(i*8+6)])<8&&abs(meercaPos[1]-meercaPreviousPos[(i*8+7)])<8) {
|
|
init();
|
|
}
|
|
DrawCircle({(int)meercaPreviousPos[(i*8+6)],(int)meercaPreviousPos[(i*8+7)]},8);
|
|
}
|
|
if (scoreTransparency>0) {
|
|
DrawString({(int)scorePos[0],(int)scorePos[1]},"+"+to_string(scoreValue),olc::Pixel(0,0,255,scoreTransparency),2);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void updateGame() {
|
|
for (int i=tailSize*8-2;i>=0;i-=2) {
|
|
if (i==0) {
|
|
meercaPreviousPos[i]=meercaPos[0];
|
|
meercaPreviousPos[i+1]=meercaPos[1];
|
|
} else {
|
|
meercaPreviousPos[i]=meercaPreviousPos[i-2];
|
|
meercaPreviousPos[i+1]=meercaPreviousPos[i-1];
|
|
}
|
|
}
|
|
meercaPos[0]+=meercaSpd[0];
|
|
meercaPos[1]+=meercaSpd[1];
|
|
|
|
if (abs(meercaPos[0]-neggPos[0])<8&&abs(meercaPos[1]-neggPos[1])<8) {
|
|
assignNeggRandomPos();
|
|
tailSize++;
|
|
score+=neggType;
|
|
scorePos[0]=meercaPos[0];
|
|
scorePos[1]=meercaPos[1]-12;
|
|
scoreTransparency=255;
|
|
scoreValue=neggType;
|
|
neggType=1;
|
|
while (rand()%2==0&&neggType<10) {
|
|
neggType++;
|
|
}
|
|
meercaPreviousPos=(float*)realloc(meercaPreviousPos,sizeof(float)*tailSize*8);
|
|
for (int i=0;i<8;i+=2) {
|
|
meercaPreviousPos[(tailSize-1)*8+i]=meercaPos[0];
|
|
meercaPreviousPos[(tailSize-1)*8+i+1]=meercaPos[1];
|
|
}
|
|
if ((float)rand()/(float)RAND_MAX<0.6) {
|
|
spawnBadNegg();
|
|
}
|
|
}
|
|
if (meercaPos[0]<WALL_BOUNDARY||meercaPos[0]>WINDOW_WIDTH-WALL_BOUNDARY||meercaPos[1]<WALL_BOUNDARY||meercaPos[1]>WINDOW_HEIGHT-WALL_BOUNDARY) {
|
|
init();
|
|
}
|
|
for (int i=0;i<badNeggCount;i++) {
|
|
if (abs(meercaPos[0]-badNeggs[i*2])<6&&abs(meercaPos[1]-badNeggs[i*2+1])<6) {
|
|
init();
|
|
}
|
|
}
|
|
scorePos[1]-=0.2;
|
|
scoreTransparency-=4;
|
|
}
|
|
};
|
|
|
|
|
|
int main()
|
|
{
|
|
|
|
MeercaChase game;
|
|
if (game.Construct(WINDOW_WIDTH, WINDOW_HEIGHT, 4, 4))
|
|
game.Start();
|
|
|
|
|
|
return 0;
|
|
} |