Starter code for a Space Invaders game. Intended for Fun3a final project.
The showcase player uses a modified version of Processing.js in combination with jsweet to let students program their apps in Java code while still allowing for browser support.
Content created by students is scaled to fit the showcase frame while maintaining aspect ratio and cursor position. This is why some projects may appear blurry in fullscreen, or why some small details may not be visible on a small screen
<iframe width='500px' height='400px' src='https://nest.ktbyte.com/nest#299621' allowfullscreen></iframe>
int[][] board = new int[20][20]; int gridSize = 30; int EMPTY = 0; int SNAKE = 1; int APPLE = 2; int direction = LEFT; int MAXLENGTH = 400; int snakeLength = 1; int[] snakeX = new int[MAXLENGTH]; int[] snakeY = new int[MAXLENGTH]; int tailX, tailY; int snakeDirection = LEFT; int appleX = (int)random(0,20); int appleY = (int)random(0,20); int snakeSpeed = 15; boolean grow = false; int slide = 1; int buttonX = 300; int buttonY = 400; int speed1X = 150; int speed1Y = 300; int speed2X = 300; int speed2Y = 300; int speed3X = 450; int speed3Y = 300; void setup(){ size(600,600); snakeX[0] = 10; snakeY[0] = 10; textAlign(CENTER,CENTER); } void draw(){ if(slide == 1){ startScreen(); } if(slide == 2){ if(frameCount % snakeSpeed == 0){ moveSnake(); drawSnake(); checkCollision(); } playerControls(); drawApple(); drawBoard(); } if(slide == 3){ chooseSpeed(); } } void drawBoard(){ for(int i = 0; i < 20; i++){ for(int j = 0; j < 20; j++){ if(board[i][j] == EMPTY){ if(i == 0 || j == 0 || i == 19 || j == 19){ fill(0,0,255); }else{ fill(255); } strokeWeight(3); rect(i*gridSize,j*gridSize,gridSize,gridSize); } if(board[i][j] == SNAKE){ fill(0,255,0); strokeWeight(3); rect(i*gridSize,j*gridSize,gridSize,gridSize); } if(board[i][j] == APPLE){ fill(255,0,0); strokeWeight(3); rect(i*gridSize,j*gridSize,gridSize,gridSize); } } } } void keyPressed(){ if(keyCode == LEFT){ snakeDirection = LEFT; } if(keyCode == RIGHT){ snakeDirection = RIGHT; } if(keyCode == DOWN){ snakeDirection = DOWN; } if(keyCode == UP){ snakeDirection = UP; } } void moveSnake(){ tailX = snakeX[snakeLength - 1]; tailY = snakeY[snakeLength - 1]; for(int i = snakeLength; i > 0; i--){ snakeX[i] = snakeX[i - 1]; snakeY[i] = snakeY[i - 1]; } if(direction == LEFT){ snakeY[0]-=1; }else if(direction == RIGHT){ snakeY[0]++; }else if(direction == DOWN){ snakeX[0]++; }else if(direction == UP){ snakeX[0]--; } } void drawSnake() { for(int i=0; i<snakeLength; i++) { board[snakeY[i]][snakeX[i]] = SNAKE; } if(grow){ snakeX[snakeLength] = tailX; snakeY[snakeLength] = tailY; grow = false; snakeLength++; }else{ board[tailY][tailX] = EMPTY; } } void playerControls(){ if(direction != RIGHT){ if(snakeDirection == LEFT){ direction = LEFT; } } if(direction != LEFT){ if(snakeDirection == RIGHT){ direction = RIGHT; } } if(direction != DOWN){ if(snakeDirection == UP){ direction = UP; } } if(direction != UP){ if(snakeDirection == DOWN){ direction = DOWN; } } } void drawApple(){ board[appleY][appleX] = APPLE; if(appleY == 0 || appleX == 0 || appleY == 19 || appleX == 19){ board[appleY][appleX] = EMPTY; appleY = (int)random(0,20); appleX = (int)random(0,20); } if(snakeY[0]*gridSize == appleY * gridSize && snakeX[0] * gridSize == appleX * gridSize){ board[appleY][appleX] = SNAKE; grow = true; appleY = (int)random(0,20); appleX = (int)random(0,20); } } void checkCollision(){ for(int i = 1; i < snakeLength; i++){ if(snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]){ reset(); } } for(int i = 0; i < snakeLength; i++){ if(snakeY[i] == 0){ slide = 1; } if(snakeY[i] == 19){ slide = 1; } if(snakeX[i] == 0){ slide = 1; } if(snakeX[i] == 19){ slide = 1; } } } void reset(){ for(int i = 0; i < 20; i++){ board[snakeY[i]][snakeX[i]] = EMPTY; } snakeLength = 1; snakeX[0] = 10; snakeY[0] = 10; } void startScreen(){ textAlign(CENTER,CENTER); fill(0,0,255); textSize(45); text("Snake",300,150); textSize(25); text("By: Pierce Zhao",300,200); fill(255); ellipse(buttonX,buttonY,200,200); fill(0,0,255); text("PLAY",buttonX,buttonY); } void mousePressed(){ if (slide == 1){ if(dist(mouseX,mouseY,buttonX,buttonY) <= 100){ reset(); slide = 3; } } } void chooseSpeed(){ background(200); fill(255); ellipse(speed1X,speed1Y,100,100); ellipse(speed2X,speed2Y,100,100); ellipse(speed3X,speed3Y,100,100); fill(0,0,255); textSize(20); text("Slow",speed1X,speed1Y); text("Normal",speed2X,speed2Y); }