Frogger

I started a frogger implementation for Armonston Ch2 and left the code where I had to code the logs that flow with the river.
That’s something I worked on last year and will probably not finish.
I’m sharing it here for whoever may have interest for it.

I’ll add a few comments to help whoever might want to pick it up.

[CODE]// NPC made by 2ndwolf v1
if (created) {
message Engine;
//set the constants here
spritewidth = 32;
maxcars = 30;
carstartpos = 16;
truckstartpos = 46;
frogspeedkey = {.5,1,1.5,2,2};
boardwidth = 28; //28
boardheight = 27;//16
startcoords = {18,21};

//These will contain coords
//type,xpos, index
//with -1 for absence;
setarray cars,maxcars*3;

//rowsy
rowsy = {24,26,28,30,32};

//type,state,xpos,ypos,direction,index
setarray waterthings,boardwidth*arraylen(rowsy)*6;
setarray this.key,10;

speeds = {
//yellowspeedsters
-.125,
//rakecar
.125,
//sportcar
-.125,
//grayspeedster
.125,
//truck
-.125
};

cartypeskey = {
//yellowspeedsters
48,
//rakecar
49,
//sportcar
52,
//grayspeedster
53,
//truck
50
};

carposkey = {
//yellowspeedsters
44,
//rakecar
42,
//sportcar
40,
//grayspeedster
38,
//truck
36
};

frogkey = {//up
1,0,0,0,2,
//left
9,8,8,8,10,
//down
4,3,3,3,5,
//right
12,13,13,13,11};

topbushtiling = {64,65,65,66,68,68,
64,65,65,66,68,68,
64,65,65,66,68,68,
64,65,65,66,68,68,
64,65,65,66,
69,62,62,67,68,68,
69,62,62,67,68,68,
69,62,62,67,68,68,
69,62,62,67,68,68,
69,62,62,67,
71,62,62,70,65,65,
71,62,62,70,65,65,
71,62,62,70,65,65,
71,62,62,70,65,65,
71,62,62,70};

//-1 = nothing, 0 = blocked, 1 = points, 2 = logdirleft, 3 = logdirright, 4 = winstate(?), 5 = die
setarray tileseffect,(boardwidth*boardheight);
}
if (playerenters) {
index = 200;
hideimgs 200,saveindex;
showbackground();
reset();
saveindex = index;

timeout = .05;
}

if(timeout){
getKeys();
updatecolliders();

debugcolliders();

frogtile = tileseffect[froghitbox[0]+froghitbox[1]*boardwidth];
frogtiletwo = tileseffect[froghitbox[0]+froghitbox[1]*boardwidth]+1; //not sure if the +1 is a mistake

if(frogframes = 0){ // If arrived at destination
if(frogtile == -1) message engine;
else if(frogtile==0) message U block;
else if(frogtile==1) message Got points!;
else if(frogtile==2) message U on a left log :O;
else if(frogtile==3) message Log is going riiiiiight!;
else if(frogtile==4) message You win!;
else if(frogtile==5||frogtiletwo==5) message U die ;
}
////////////////////////////////

////////////////Manage input
if(movdir==-1){
for(this.i=0;this.i<4;this.i++){
if(this.key[this.i]==1){
if(!(froghitbox[0]+vecx(this.i)*4>boardwidth ||
froghitbox[0]+vecx(this.i)*2<0 ||
froghitbox[1]+vecy(this.i)*4>boardheight ||
froghitbox[1]+vecy(this.i)*2<0 ||
tileseffect[froghitbox[0]+froghitbox[1]*boardwidth+vecx(this.i)*2] == 0 || //THIS PART IS FAULTY
tileseffect[froghitbox[0]+froghitbox[1]*boardwidth+(vecy(this.i)*2)*boardwidth] == 0)) {

  movdir = this.i;
}
  }
}

}else if (frogframes == 5){
froghitbox[0] += vecx(movdir)*2;
froghitbox[1] += vecy(movdir)*2;
frogpos[0] = froghitbox[0]+startcoords[0];
frogpos[1] = froghitbox[1]+startcoords[1];
frogframes = 0;
movdir = -1;
}
////////////////////////////

////////////////Animate that frog
if(movdir!=-1){
currfrogsprite = frogkey[movdir*5+frogframes];
frogpos[0] = froghitbox[0] + (frogspeedkey[frogframes])*vecx(movdir) + startcoords[0];
frogpos[1] = froghitbox[1] + (frogspeedkey[frogframes])*vecy(movdir) + startcoords[1];
frogframes++;
}
///////////////

////////////////Render graphics
//Cars
for(this.i=0;this.i<maxcars;this.i++){
if(cars[this.i3+1]!=-1){
if(cars[this.i
3]==4) {
spritewidth = 64;
if(cars[this.i3+1]<16)cars[this.i3+1] = truckstartpos;
} else{
spritewidth = 32;
if(speeds[cars[this.i3]]<0) {
if(cars[this.i
3+1]<18)cars[this.i3+1] = truckstartpos;
} else if(cars[this.i
3+1]>44)cars[this.i3+1] = carstartpos;
}
param.1.int.getsprite = cartypeskey[cars[this.i
3]];
index = cars[this.i3+2];
getsprite();
cars[this.i
3+1] += speeds[cars[this.i3]];
spritecoordsx = cars[this.i
3+1];
spritecoordsy = carposkey[cars[this.i3]];
showsprite();
}
}
param.1.int.getsprite = 0;
index = 1200;
getsprite();
spritecoordsx = startcoords[0];
spritecoordsy = startcoords[1];
showsprite();
//Logs and turtles and alligator
//Do updatecolliders for waterthings
//for(this.i=0;this.i<arraylen(waterthings);this.i++){
//param1 getsprite takes the sprite index from the spritesheet
// this.i
6

param.1.int.getsprite = currfrogsprite;
index = frogindex;
getsprite();
spritecoordsx = int(frogpos[0]);
spritecoordsy = int(frogpos[1]);
showsprite();
changeimgvis index,1;
////////////////////////////////

timeout = .05;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////Functions
///////////////////////////////////////////////////////////////////////////////////////////////////////////Functions
function updatecolliders(){
for(this.i=0;this.i<=arraylen(tileseffect);this.i++){
if(this.i<boardwidth1){//do nothing
}
else if(this.i<boardwidth
3){
if((this.i%boardwidth)%6 in |1,2| ) tileseffect[this.i] = 4;
else tileseffect[this.i] = 0;
}
else if (this.i<boardwidth13-1) tileseffect[this.i] = 5;
else tileseffect[this.i] = -1;
}
for(this.i=0;this.i<arraylen(cars);this.i++){
if(!(cars[this.i
3+1]<startcoords[0]-1||cars[this.i3+1]>startcoords[0]+boardwidth+1)){
/////TODO add a conditional that keeps the colliding from spilling
tileseffect[(cars[this.i
3+1]-startcoords[0])+((carposkey[cars[this.i3]]-startcoords[1]))boardwidth] = 5;
tileseffect[(cars[this.i
3+1]-startcoords[0])+((carposkey[cars[this.i
3]]-startcoords[1]))boardwidth+1] = 5;
if(cars[this.i
3]==4){
tileseffect[(cars[this.i3+1]-startcoords[0])+((carposkey[cars[this.i3]]-startcoords[1]))boardwidth+2] = 5;
tileseffect[(cars[this.i
3+1]-startcoords[0])+((carposkey[cars[this.i*3]]-startcoords[1]))*boardwidth+3] = 5;
}
}
}
}

function debugcolliders(){
index = frogindex;
for(this.i=0;this.i<=arraylen(tileseffect);this.i++){
index++;
if(tileseffect[this.i]==-1){
showimg index,debug32.GIF,(this.i%boardwidth)+startcoords[0],int(this.i/boardwidth)+startcoords[1];
changeimgmode index,0;
changeimgcolors index,1,1,1,.2;
} else if(tileseffect[this.i]==1){
showimg index,debug32.GIF,(this.i%boardwidth)+startcoords[0],int(this.i/boardwidth)+startcoords[1];
changeimgmode index,0;
changeimgcolors index,0,1,1,.2;
} else if(tileseffect[this.i]==2){
showimg index,debug32.GIF,(this.i%boardwidth)+startcoords[0],int(this.i/boardwidth)+startcoords[1];
changeimgmode index,0;
changeimgcolors index,1,0,1,.2;
} else if(tileseffect[this.i]==3){
showimg index,debug32.GIF,(this.i%boardwidth)+startcoords[0],int(this.i/boardwidth)+startcoords[1];
changeimgmode index,0;
changeimgcolors index,1,1,0,.2;
} else hideimg index;
}

index++;
showimg index,debug32.GIF,froghitbox[0]+startcoords[0],froghitbox[1]+startcoords[1];
changeimgmode index,0;
changeimgcolors index,0,1,1,.2;
}

function reset(){
//Delete all cars
for(this.i=0;this.i<maxcars;this.i++){
index++;
cars[this.i3] = -1; //cartype
cars[this.i
3+1] = -1; //carX
cars[this.i3+2] = index;
}
for(this.i = 0; this.i < boardswidth
arraylen(rowsy) ; this.i++){
index++;
waterthings[this.i+5] = index;
}
for(this.i=0;this.i<=arraylen(tileseffect);this.i++){
tileseffect[this.i] = -1;
}
movdir = -1;
frogframes = 0;
index++;
addcars();
addstartlogs();
frogindex = index;
currfrogsprite = 2;
frogpos = {31,46};
froghitbox = {13,25}; //These are from the startcoords

}

function addstartlogs(){
//So this should just place the starting logs
for(this.i = 0; this.i < arraylen(rowsy); this.i++){
logstotwidth = 0;
length = 0;
logtype = -1;
logstotwidth += int(random(1,3)); //beforetrailing
while(logstotwidth < boardwidth){ //
if(this.i==0){
if(int(random(0,10))>8){
logtype = 0;
length = 3;
} else {
logtype = 1;
length = int(random(0,5));
}
} else if (this.i == 2 || this.i == 3){
logtype = 1;
length = int(random(0,5));
} else { //turtles and lillies
logtype = random(0,10)>5? 2 : 3;
length = int(random(0,4));
}
trailing = int(random(1,3));

  for(this.j = 0; this.j < length; this.j++){
waterthings[(boardwidth*this.i)*6+this.j*6] = logtype;
if(logtype < 2)waterthings[(boardwidth*this.i)*6+this.j*6+1] = -1;
else waterthings[(boardwidth*this.i)*6+this.j*6+1] = 2;
waterthings[(boardwidth*this.i)*6+this.j*6+2] = startcoords[0]+logstotwidth*2+this.j*2;
waterthings[(boardwidth*this.i)*6+this.j*6+3] = rowsy[this.i];
waterthings[(boardwidth*this.i)*6+this.j*6+4] = this.i%2; //left = 0 right = 1
//waterthings[(boardwidth*this.i)*6+this.j*6+5] = it's already set in reset
  }
  //Acounts for semilogs
  logstotwidth += length+trailing;
}
//when it wraps, reuse index for the new log parts

}
}

//logtypes -1 = none, 0 = gator, 1 = log, 2 = turtle, 3 = lilly

function placelog(){ //sets waterthings array’s fields
//logindex = 0;
if(initlogs){

}
}

function huhgenlog(){ //repeats at timeout to call placelog if neccesary
//if random(0,10)<5 and/or conditions favorable to add a log run genlog
}

function addcars(){
//trucks
cars[0] = 4;
cars[1] = truckstartpos;

cars[3] = 4;
cars[4] = truckstartpos - 10;

cars[6] = 4;
cars[7] = truckstartpos - 10*2;

//grayspeedsters
cars[9] = 3;
cars[10] = carstartpos;

//grayspeedster
cars[12] = 3;
cars[13] = carstartpos + 4;

cars[15] = 3;
cars[16] = carstartpos + 14;

cars[18] = 3;
cars[19] = carstartpos + 18;

//sportcar
cars[21] = 2;
cars[22] = carstartpos;

cars[24] = 2;
cars[25] = carstartpos+8;

cars[27] = 2;
cars[28] = carstartpos+16;

cars[30] = 2;
cars[31] = carstartpos+24;

//rakecar
cars[33] = 1;
cars[34] = truckstartpos ;

cars[36] = 1;
cars[37] = truckstartpos -9.3;

cars[39] = 1;
cars[40] = truckstartpos -18.6;

//grayspeedster
cars[42] = 0;
cars[43] = carstartpos;

cars[45] = 0;
cars[46] = carstartpos + 15;

}

function getKeys(){
for (this.keyCheck = 0;this.keyCheck < 10; this.keyCheck++){
if (keydown(this.keyCheck))
this.key[this.keyCheck] = (this.key[this.keyCheck] == 0)?1:2;//1 = pressed,2 = held
else
this.key[this.keyCheck] = (this.key[this.keyCheck]>0)?-1:0;//-1 = released, 0 = not held
}
}

function showbackground(){
param.1.int.getsprite = 63;
getsprite();
for(this.i=18;this.i<46;this.i+=2){
spritecoordsx = this.i;
spritecoordsy = 46;
showsprite();
index++;
}
for(this.i=18;this.i<46;this.i+=2){
spritecoordsx = this.i;
spritecoordsy = 34;
showsprite();
index++;
}
this.b = 0;
spritewidth = 32;
for(this.i=0;this.i<3;this.i++){
for(this.j=18;this.j<46;this.j+=1){
param.1.int.getsprite = topbushtiling[this.b];
getsprite();
index++;
spritecoordsx = this.j;
spritecoordsy = 21+this.i;
showsprite();
this.b++;
}
}
}

function getsprite(){
gotsprite = {(param.1.int.getsprite%8)*32,(int(param.1.int.getsprite/8))*32};
}

function showsprite(){
showimg index,froggerspritesheet.png,spritecoordsx,spritecoordsy;
changeimgpart index,gotsprite[0],gotsprite[1],spritewidth,32;
changeimgvis index,0;
}[/CODE]

Her’s a pic of it running, along with the spritesheets.

[ATTACH=JSON]{“data-align”:“none”,“data-size”:“full”,“data-attachmentid”:186460}[/ATTACH]

[ATTACH=JSON]{“data-align”:“none”,“data-size”:“full”,“data-attachmentid”:186458}[/ATTACH][ATTACH=JSON]{“data-align”:“none”,“data-size”:“full”,“data-attachmentid”:186459}[/ATTACH]