Skip to content

Instantly share code, notes, and snippets.

@thcrack
Created March 22, 2019 05:27
Show Gist options
  • Save thcrack/5375d62a0fa9d02c6948c9d8ee93159a to your computer and use it in GitHub Desktop.
Save thcrack/5375d62a0fa9d02c6948c9d8ee93159a to your computer and use it in GitHub Desktop.
PImage bg, start1, start2, lose, win, restart, ship;
final int GAME_START = 0;
final int GAME_RUN = 1;
final int GAME_OVER = 2;
final int GAME_WIN = 3;
int gameState = GAME_START;
final int BUTTON_TOP = 210;
final int BUTTON_BOTTOM = 280;
final int BUTTON_LEFT = 115;
final int BUTTON_RIGHT = 450;
boolean upPressed, downPressed, rightPressed, leftPressed;
float shipX, shipY;
float shipSpeed = 5;
float shipWidth = 50;
float wall1Speed = 1;
float wall2Speed = 2;
float wall3Speed = 3;
float wall1Y = 100;
float wall2Y = 200;
float wall3Y = 300;
float wall1HoleWidth = 300;
float wall2HoleWidth = 200;
float wall3HoleWidth = 100;
float wall1X, wall2X, wall3X;
float winningLineY = 400;
color wallColor;
void setup() {
size(600, 500);
bg=loadImage("img/bg.png");
start1=loadImage("img/start1.png");
start2=loadImage("img/start2.png");
lose=loadImage("img/lose.png");
win=loadImage("img/win.png");
restart=loadImage("img/restart.png");
ship=loadImage("img/ship.png");
shipX = width / 2 - shipWidth / 2;
shipY = 0;
wallColor = color(247, 210, 60);
}
void draw() {
switch(gameState){
case GAME_START:
if(mouseX > BUTTON_LEFT && mouseX < BUTTON_RIGHT
&& mouseY > BUTTON_TOP && mouseY < BUTTON_BOTTOM){
image(start2, 0, 0);
if(mousePressed){
gameState = GAME_RUN;
}
}else{
image(start1, 0, 0);
}
break;
case GAME_RUN:
image(bg, 0, 0);
image(ship, shipX, shipY);
if(upPressed){
shipY -= shipSpeed;
if(shipY < 0) shipY = 0;
}
if(downPressed){
shipY += shipSpeed;
if(shipY + shipWidth > height) shipY = height - shipWidth;
}
if(leftPressed){
shipX -= shipSpeed;
if(shipX < 0) shipX = 0;
}
if(rightPressed){
shipX += shipSpeed;
if(shipX + shipWidth > width) shipX = width - shipWidth;
}
if(shipY + shipWidth > winningLineY) gameState = GAME_WIN;
//walls
stroke(wallColor);
strokeWeight(5);
wall1X += wall1Speed;
if(wall1X <= 0 || wall1X + wall1HoleWidth >= width) wall1Speed *= -1;
line(0, wall1Y, wall1X, wall1Y);
line(wall1X + wall1HoleWidth, wall1Y, width, wall1Y);
if((shipX < wall1X || shipX + shipWidth > wall1X + wall1HoleWidth)
&& wall1Y > shipY && wall1Y < shipY+shipWidth){
gameState = GAME_OVER;
}
wall2X += wall2Speed;
if(wall2X <= 0 || wall2X + wall2HoleWidth >= width) wall2Speed *= -1;
line(0, wall2Y, wall2X, wall2Y);
line(wall2X + wall2HoleWidth, wall2Y, width, wall2Y);
if((shipX < wall2X || shipX + shipWidth > wall2X + wall2HoleWidth)
&& wall2Y > shipY && wall2Y < shipY+shipWidth){
gameState = GAME_OVER;
}
wall3X += wall3Speed;
if(wall3X <= 0 || wall3X + wall3HoleWidth >= width) wall3Speed *= -1;
line(0, wall3Y, wall3X, wall3Y);
line(wall3X + wall3HoleWidth, wall3Y, width, wall3Y);
if((shipX < wall3X || shipX + shipWidth > wall3X + wall3HoleWidth)
&& wall3Y > shipY && wall3Y < shipY+shipWidth){
gameState = GAME_OVER;
}
break;
case GAME_OVER:
if(mouseX > BUTTON_LEFT && mouseX < BUTTON_RIGHT
&& mouseY > BUTTON_TOP && mouseY < BUTTON_BOTTOM){
image(restart, 0, 0);
if(mousePressed){
gameState = GAME_RUN;
shipX = width / 2 - shipWidth / 2;
shipY = 0;
}
}else{
image(lose, 0, 0);
}
break;
case GAME_WIN:
if(mouseX > BUTTON_LEFT && mouseX < BUTTON_RIGHT
&& mouseY > BUTTON_TOP && mouseY < BUTTON_BOTTOM){
image(restart, 0, 0);
if(mousePressed){
gameState = GAME_RUN;
shipX = width / 2 - shipWidth / 2;
shipY = 0;
}
}else{
image(win, 0, 0);
}
break;
}
}
void keyPressed(){
switch(keyCode){
case UP:
upPressed = true;
break;
case DOWN:
downPressed = true;
break;
case RIGHT:
rightPressed = true;
break;
case LEFT:
leftPressed = true;
break;
}
}
void keyReleased(){
switch(keyCode){
case UP:
upPressed = false;
break;
case DOWN:
downPressed = false;
break;
case RIGHT:
rightPressed = false;
break;
case LEFT:
leftPressed = false;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment