Full code if anyone needs it (pretty messy):
int cols, rows;
int scl = 20;
int w = 2000;
int h = 1600;
Ship ship;
Ground ground;
ArrayList<Spikes> spikes;
color g;
color s;
float flying = 0;
float score = 0;
float[][] terrain;
boolean gameStart;
boolean gameOver;
void setup() {
size(600, 600, P3D);
cols = w / scl;
rows = h/ scl;
terrain = new float[cols][rows];
g = color(156,111,81);
s = color(0);
ship = new Ship(1000,1100,200);
ground = new Ground(1000,1100,200);
spikes = new ArrayList<Spikes>();
gameStart = true;
gameOver = false;
}
void draw() {
if( gameStart == true){
flying -= 0.1;
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
terrain[x][y] = flying;
}
}
background(0);
textSize(50);
fill(255);
text(score, 50,50);
stroke(s);
fill(g);
score += 0.01;
translate(width/2, height/2+50);
rotateX(PI/3);
translate(-w/2, -h/2);
ground.render();
for (int y = 0; y < rows-1; y++) {
beginShape(TRIANGLE_STRIP);
for (int x = 0; x < cols; x++) {
vertex(x*scl, y*scl, terrain[x][y]);
vertex(x*scl, (y+1)*scl, terrain[x][y+1]);
}
endShape();
// Idea for terrain using a triangle sector adapted from The Coding Train by Dan Shiffman
}
if(score > 0 && score < 25){
spikes.add(new Spikes(width/2,500,50));
}
for(int i=0; i < spikes.size(); i++){
spikes.get(i).render();
spikes.get(i).move();
}
ship.render();
ship.move();
}
}
class Ship{
float x;
float y;
float z;
Ship(float _x, float _y, float _z){
x = _x;
y = _y;
z = _z;
}
void render(){
translate(x,y,z);
fill(160);
stroke(0);
beginShape(TRIANGLES);
scale(0.6);
vertex(-50,0,25);
vertex(-150,100,25);
vertex(0,100,25);
vertex(50,0,25);
vertex(150,100,25);
vertex(0,100,25);
vertex(-50,0,-25);
vertex(-150,100,-25);
vertex(0,100,-25);
vertex(50,0,-25);
vertex(150,100,-25);
vertex(0,100,-25);
vertex(-50,0,25);
vertex(50,0,25);
vertex(0,-300,25);
vertex(-50,0,25);
vertex(50,0,25);
vertex(0,100,25);
vertex(-150,100,25);
vertex(150,100,25);
vertex(-150,100,-25);
vertex(150,100,25);
vertex(-150,100,-25);
vertex(150,100,-25);
vertex(-150,100,25);
vertex(150,100,25);
vertex(150,100,-25);
vertex(50,0,25);
vertex(50,0,-25);
vertex(0,-300,-25);
vertex(50,0,25);
vertex(0,-300,25);
vertex(0,-300,-25);
vertex(-50,0,25);
vertex(-50,0,-25);
vertex(0,-300,-25);
vertex(-50,0,25);
vertex(0,-300,25);
vertex(0,-300,-25);
vertex(-50,0,25);
vertex(-50,0,-25);
vertex(-150,100,25);
vertex(50,0,25);
vertex(50,0,-25);
vertex(150,100,25);
endShape();
}
void move(){
if(keyPressed && keyCode == LEFT){
x-=3;
}
if(keyPressed && keyCode == RIGHT){
x+=3;
}
if(keyPressed && keyCode == UP){
y-=3;
}
if(keyPressed && keyCode == DOWN){
y+=3;
}
}
}
class Ground{
float x;
float y;
float z;
Ground(float _x,float _y,float _z){
x = _x;
y = _y;
z = _z;
}
void render(){
fill(g);
translate(x,y,z);
beginShape(TRIANGLES);
vertex(100,100,2000);
vertex(100,2000,-150);
vertex(100,100,-150);
translate(-x,-y,-z);
endShape();
}
}
class Spikes{
float x;
float y;
float z;
Spikes(float _x, float _y, float _z){
x = _x;
y = _y;
z = _z;
}
void render(){
translate(x,y,z);
fill(0);
stroke(255);
beginShape(TRIANGLES);
vertex(100,-300,-100);
vertex(150,-350,-100);
vertex(100,-300,200);
vertex(100,-300,-100);
vertex(50,-350,-100);
vertex(100,-300,200);
endShape();
translate(-x,-y,-z);
}
void move(){
y+=4;
if(y>1810){
x = random(300,1500);
y = 600;
}
}
}