generated from sigonasr2/CPlusPlusProjectTemplate
Draw tail
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
13fd3be0f7
commit
92bd237f30
BIN
MeercaChase
BIN
MeercaChase
Binary file not shown.
108
MeercaChase.cpp
108
MeercaChase.cpp
@ -35,10 +35,10 @@ public:
|
|||||||
int moveSpd=4;
|
int moveSpd=4;
|
||||||
float meercaSpd[2] = {0,0};
|
float meercaSpd[2] = {0,0};
|
||||||
float meercaPos[2] = {240,240};
|
float meercaPos[2] = {240,240};
|
||||||
|
float*meercaPreviousPos=NULL;
|
||||||
float neggPos[2] = {};
|
float neggPos[2] = {};
|
||||||
float*badNeggs=NULL;
|
float*badNeggs=NULL;
|
||||||
int badNeggCount=0;
|
int badNeggCount=0;
|
||||||
TailData*tail=NULL;
|
|
||||||
int tailSize=0;
|
int tailSize=0;
|
||||||
|
|
||||||
void assignNeggRandomPos() {
|
void assignNeggRandomPos() {
|
||||||
@ -80,15 +80,11 @@ public:
|
|||||||
srand(432189);
|
srand(432189);
|
||||||
assignNeggRandomPos();
|
assignNeggRandomPos();
|
||||||
if (badNeggs!=NULL) {free(badNeggs);}
|
if (badNeggs!=NULL) {free(badNeggs);}
|
||||||
if (tail!=NULL) {
|
if (meercaPreviousPos!=NULL) {
|
||||||
for (int i=0;i<tailSize;i++) {
|
free(meercaPreviousPos);
|
||||||
free(tail[i].future_direction);
|
|
||||||
free(tail[i].pos);
|
|
||||||
free(tail[i].frameCount);
|
|
||||||
}
|
}
|
||||||
free(tail);}
|
|
||||||
badNeggs=(float*)malloc(sizeof(float)*0);
|
badNeggs=(float*)malloc(sizeof(float)*0);
|
||||||
tail=(TailData*)malloc(sizeof(TailData)*0);
|
meercaPreviousPos=(float*)malloc(sizeof(float)*0);
|
||||||
badNeggCount=0;
|
badNeggCount=0;
|
||||||
tailSize=0;
|
tailSize=0;
|
||||||
meercaPos[0]=240;
|
meercaPos[0]=240;
|
||||||
@ -123,38 +119,18 @@ public:
|
|||||||
if (GetKey(olc::W).bPressed&&(meercaSpd[0]+meercaSpd[1]==0||meercaSpd[0]!=0)) {
|
if (GetKey(olc::W).bPressed&&(meercaSpd[0]+meercaSpd[1]==0||meercaSpd[0]!=0)) {
|
||||||
meercaSpd[0]=0;
|
meercaSpd[0]=0;
|
||||||
meercaSpd[1]=-moveSpd;
|
meercaSpd[1]=-moveSpd;
|
||||||
if (tailSize>0) {
|
|
||||||
tail[0].future_direction[tail[0].futureIndex]=UP;
|
|
||||||
tail[0].frameCount[tail[0].futureIndex]=TAIL_DISTANCE;
|
|
||||||
tail[0].futureIndex=(tail[0].futureIndex+1)%TAIL_DISTANCE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (GetKey(olc::A).bPressed&&(meercaSpd[0]+meercaSpd[1]==0||meercaSpd[1]!=0)) {
|
if (GetKey(olc::A).bPressed&&(meercaSpd[0]+meercaSpd[1]==0||meercaSpd[1]!=0)) {
|
||||||
meercaSpd[0]=-moveSpd;
|
meercaSpd[0]=-moveSpd;
|
||||||
meercaSpd[1]=0;
|
meercaSpd[1]=0;
|
||||||
if (tailSize>0) {
|
|
||||||
tail[0].future_direction[tail[0].futureIndex]=LEFT;
|
|
||||||
tail[0].frameCount[tail[0].futureIndex]=TAIL_DISTANCE;
|
|
||||||
tail[0].futureIndex=(tail[0].futureIndex+1)%TAIL_DISTANCE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (GetKey(olc::D).bPressed&&(meercaSpd[0]+meercaSpd[1]==0||meercaSpd[1]!=0)) {
|
if (GetKey(olc::D).bPressed&&(meercaSpd[0]+meercaSpd[1]==0||meercaSpd[1]!=0)) {
|
||||||
meercaSpd[0]=moveSpd;
|
meercaSpd[0]=moveSpd;
|
||||||
meercaSpd[1]=0;
|
meercaSpd[1]=0;
|
||||||
if (tailSize>0) {
|
|
||||||
tail[0].future_direction[tail[0].futureIndex]=RIGHT;
|
|
||||||
tail[0].frameCount[tail[0].futureIndex]=TAIL_DISTANCE;
|
|
||||||
tail[0].futureIndex=(tail[0].futureIndex+1)%TAIL_DISTANCE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (GetKey(olc::S).bPressed&&(meercaSpd[0]+meercaSpd[1]==0||meercaSpd[0]!=0)) {
|
if (GetKey(olc::S).bPressed&&(meercaSpd[0]+meercaSpd[1]==0||meercaSpd[0]!=0)) {
|
||||||
meercaSpd[0]=0;
|
meercaSpd[0]=0;
|
||||||
meercaSpd[1]=moveSpd;
|
meercaSpd[1]=moveSpd;
|
||||||
if (tailSize>0) {
|
|
||||||
tail[0].future_direction[tail[0].futureIndex]=DOWN;
|
|
||||||
tail[0].frameCount[tail[0].futureIndex]=TAIL_DISTANCE;
|
|
||||||
tail[0].futureIndex=(tail[0].futureIndex+1)%TAIL_DISTANCE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// called once per frame
|
// called once per frame
|
||||||
if (meercaSpd[0]==moveSpd) {
|
if (meercaSpd[0]==moveSpd) {
|
||||||
@ -172,43 +148,34 @@ public:
|
|||||||
for (int i=0;i<badNeggCount;i++) {
|
for (int i=0;i<badNeggCount;i++) {
|
||||||
DrawRotatedDecal({badNeggs[i*2],badNeggs[i*2+1]},badnegg,0,{16,16},{1,1});
|
DrawRotatedDecal({badNeggs[i*2],badNeggs[i*2+1]},badnegg,0,{16,16},{1,1});
|
||||||
}
|
}
|
||||||
|
if (tailSize>=1) {
|
||||||
|
DrawString({16,16},to_string(meercaPreviousPos[3])+" "+to_string(meercaPreviousPos[2])+" "+to_string(meercaPreviousPos[1])+" "+to_string(meercaPreviousPos[0]));
|
||||||
|
}
|
||||||
for (int i=0;i<tailSize;i++) {
|
for (int i=0;i<tailSize;i++) {
|
||||||
TailData t = tail[i];
|
if (meercaPreviousPos[(i*8+6)]!=0||meercaPreviousPos[(i*8+7)]!=0) {
|
||||||
DrawCircle({t.pos[0],t.pos[1]},8);
|
DrawCircle({(float)meercaPreviousPos[(i*8+6)],(float)meercaPreviousPos[(i*8+7)]},8);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateGame() {
|
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[0]+=meercaSpd[0];
|
||||||
meercaPos[1]+=meercaSpd[1];
|
meercaPos[1]+=meercaSpd[1];
|
||||||
|
|
||||||
if (abs(meercaPos[0]-neggPos[0])<8&&abs(meercaPos[1]-neggPos[1])<8) {
|
if (abs(meercaPos[0]-neggPos[0])<8&&abs(meercaPos[1]-neggPos[1])<8) {
|
||||||
assignNeggRandomPos();
|
assignNeggRandomPos();
|
||||||
tail=(TailData*)realloc(tail,sizeof(TailData)*++tailSize);
|
tailSize++;
|
||||||
tail[tailSize-1].future_direction = (char*)malloc(sizeof(char)*TAIL_DISTANCE);
|
meercaPreviousPos=(float*)realloc(meercaPreviousPos,sizeof(float)*tailSize*8);
|
||||||
tail[tailSize-1].pos = (int*)malloc(sizeof(int)*2);
|
|
||||||
tail[tailSize-1].frameCount = (int*)malloc(sizeof(int)*TAIL_DISTANCE);
|
|
||||||
if (meercaSpd[0]==moveSpd) {
|
|
||||||
tail[tailSize-1].direction=RIGHT;
|
|
||||||
} else
|
|
||||||
if (meercaSpd[0]==-moveSpd) {
|
|
||||||
tail[tailSize-1].direction=LEFT;
|
|
||||||
} else
|
|
||||||
if (meercaSpd[1]==moveSpd) {
|
|
||||||
tail[tailSize-1].direction=DOWN;
|
|
||||||
} else
|
|
||||||
if (meercaSpd[1]==-moveSpd) {
|
|
||||||
tail[tailSize-1].direction=UP;
|
|
||||||
}
|
|
||||||
tail[tailSize-1].pos[0]=meercaPos[0];
|
|
||||||
tail[tailSize-1].pos[1]=meercaPos[1];
|
|
||||||
for (int i=0;i<TAIL_DISTANCE;i++) {
|
|
||||||
tail[tailSize-1].frameCount[i]=-1;
|
|
||||||
tail[tailSize-1].future_direction[i]=-1;
|
|
||||||
}
|
|
||||||
tail[tailSize-1].waitTime=TAIL_DISTANCE*tailSize;
|
|
||||||
tail[tailSize-1].futureIndex=0;
|
|
||||||
if ((float)rand()/(float)RAND_MAX<0.6) {
|
if ((float)rand()/(float)RAND_MAX<0.6) {
|
||||||
spawnBadNegg();
|
spawnBadNegg();
|
||||||
}
|
}
|
||||||
@ -218,39 +185,6 @@ public:
|
|||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i=0;i<tailSize;i++) {
|
|
||||||
if (tail[i].waitTime>0) {
|
|
||||||
tail[i].waitTime--;
|
|
||||||
} else {
|
|
||||||
for (int j=0;j<TAIL_DISTANCE;j++) {
|
|
||||||
if (tail[i].frameCount[j]>=0) {
|
|
||||||
tail[i].frameCount[j]--;
|
|
||||||
if (tail[i].frameCount[j]==-1) {
|
|
||||||
tail[i].direction=tail[i].future_direction[j];
|
|
||||||
if (i+1<tailSize) {
|
|
||||||
tail[i+1].future_direction[tail[i+1].futureIndex]=tail[i].future_direction[j];
|
|
||||||
tail[i+1].frameCount[tail[i+1].futureIndex]=TAIL_DISTANCE;
|
|
||||||
tail[i+1].futureIndex=(tail[i+1].futureIndex+1)%TAIL_DISTANCE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
switch (tail[i].direction) {
|
|
||||||
case UP:{
|
|
||||||
tail[i].pos[1]-=moveSpd;
|
|
||||||
}break;
|
|
||||||
case RIGHT:{
|
|
||||||
tail[i].pos[0]+=moveSpd;
|
|
||||||
}break;
|
|
||||||
case LEFT:{
|
|
||||||
tail[i].pos[0]-=moveSpd;
|
|
||||||
}break;
|
|
||||||
case DOWN:{
|
|
||||||
tail[i].pos[1]+=moveSpd;
|
|
||||||
}break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user