Recreational programming

This is a quick gamesketch for a snake-like game, designed to be mostly unbeatable in the oldskool gaming sense (think pong or pacman). I’m sure there are many games like this out there, but this one is mine. Although, somewhat hurriedly hacked together

 

It was written with Processing(Java), and is based on a 2D array, where numerical values run the game. All values start at zero (except difficulty modifiers/ prefilled blocks), and the snake travels around, upping the value by 1 for each turn:

 

	if((gameGridArr[i][j] == 2)  || (gameGridArr[i][j] == 7)){	//dead
	}else if(gameGridArr[i][j] == 1){	//snake
	}else if(gameGridArr[i][j] > 2){	//prefilled blocks (difficulty)

 

To make the Processing-sketch available in this post, without rewriting it all as javascript, I’ve used Processing.js, which runs the code through <canvas>.

 

To be honest, I have not played it past level 4 or five, and the array goes up to 50, giving about fourtyfive levels of fun. I’d guess that the difficulty system, and the scaling of the grid makes it unplayable beyond level 15-20. But please, prove me wrong.

 

Use W,A,S,D keys to play
The goal is to fill whole map

 

Might not run on Opera-browsers, due to problems with catching key presses.

 

 

 

 

Sourcecode:

 

// Snak-like puzzle game version 2
// www.oostring.com

//imports
PFont helvBig;
PFont helvSmall;


//global variables
int boardSizeX = 755;
int boardSizeY = 755;
int [] [] gameGridArr = new int [50] [50];
int boardXsquares = 4;
int boardYsquares = 4;
int clockCounter = 0;
int clockSpeed = 8;
int	blocksize;

boolean startup = true;
boolean running = false;
boolean halted = false;
boolean paused = false;
boolean win = false;
boolean dead = false;
boolean noPause = false;
boolean endGame = false;


int pauseNumber = 5;
int counterX = 0;
int counterY = 0;
int direction = 0;
int score = 0;
int highScore = 0;

int blockerX = 0;
int blockerY = 0;

int difficulty = 0;


int progress = 0;


void setup() {	//setup

	size(boardSizeX, boardSizeY, JAVA2D);
	frameRate(24);
	background(150);
	helvBig = loadFont("Helvetica-Bold-48.vlw"); 
	helvSmall = loadFont("Helvetica-22.vlw"); 
	blocksize = boardSizeX/ boardXsquares;
	
	resetBoard();
	drawBoard();
	loadingScreen();
}


void draw() {	//draw, main loop

	if(running){
		runGame();
	}else{
		loadingScreen(); 
	}

}


void drawBoard(){ // draw permanent graphics
	background(150);
	stroke(80);
	noFill();
	for(int i = 1; i < boardXsquares; i++){
			line((boardSizeX/boardXsquares)*i, 0, (boardSizeX/boardXsquares)*i, height);
		for(int j = 1; j < boardYsquares; j++){
			line(0, (boardSizeY/boardYsquares)*i, width, (boardSizeY/boardYsquares)*i);
		}
	}
	if(noPause){
		fill(255);
		textFont(helvSmall, 22);
		//textSize(22);
		text("YOU HAVE NO PAUSES LEFT" , 22, boardSizeY-22);			
	}
}


void loadingScreen(){ //textboxes
	noStroke();
	fill(150, 150, 150, 20);
	rect(0, 0, width, height);
	drawGame();

	fill(80, 80, 80, 210);
	rect(boardSizeX/5, boardSizeY/3, boardSizeX - (boardSizeX/5)*2, boardSizeY - (boardSizeY/3)*2);
	
	if(startup){
		fill(255);
		textFont(helvBig, 48);
		//textSize(48);
		text("SNAKE", boardSizeX/5 + 20, boardSizeY/3 + 50);
		textFont(helvSmall, 22);
		//textSize(22);
		text("(W, A, S, D) KEY TO CONTINUE: " , boardSizeX/5 + 22, boardSizeY/3 + 75);				
		halted = true;
	}else if(paused){
		fill(255);
		textFont(helvBig, 48);
		//textSize(48);
		text("GAME PAUSED", boardSizeX/5 + 20, boardSizeY/3 + 50);
		textFont(helvSmall, 22);
		//textSize(22);
		text("YOU HAVE " + pauseNumber + " PAUSES LEFT" , boardSizeX/5 + 22, boardSizeY/3 + 75);
		text("YOUR SCORE SO FAR:  " + score, boardSizeX/5 + 22, boardSizeY/3 + 100);				
		halted = true;
	}else if(win){
		fill(255);
		textFont(helvBig, 48);
		//textSize(48);
		text("CLEARED", boardSizeX/5 + 20, boardSizeY/3 + 50);
		textFont(helvSmall, 22);
		//textSize(22);
		text("LEVEL " + difficulty, boardSizeX/5 + 22, boardSizeY/3 + 75);	
		text("YOUR SCORE SO FAR:  " + score, boardSizeX/5 + 22, boardSizeY/3 + 100);									
		halted = true;
	}else if(dead){
		fill(255);
		textFont(helvBig, 48);
		//textSize(48);
		text("DEAD", boardSizeX/5 + 20, boardSizeY/3 + 50);
		textFont(helvSmall, 22);
		//textSize(22);
		text("TOTAL SCORE:  " + score, boardSizeX/5 + 22, boardSizeY/3 + 75);				
		text("PERSONAL BEST:  " + highScore, boardSizeX/5 + 22, boardSizeY/3 + 100);				
		halted = true;
	}else if(endGame){
		fill(255);
		textFont(helvBig, 48);
		//textSize(48);
		text("YOU FINISHED", boardSizeX/5 + 20, boardSizeY/3 + 50);
		textFont(helvSmall, 22);
		//textSize(22);
		text("TOTAL SCORE:  " + score, boardSizeX/5 + 22, boardSizeY/3 + 75);				
		text("PERSONAL BEST:  " + highScore, boardSizeX/5 + 22, boardSizeY/3 + 100);				
	}
}

void keyPressed(){ //keypress actions
	if(halted == true){
		if ((key == 'w') || (key == 'a') || (key == 's') || (key == 'd')){
			halted = false;
			paused = false;
			running = true;
			startup = false;
			if(win == true){
				win = false;
			}else if (dead == true){
				dead = false;
				score = 0;
			}
		}
	}else if((key=='p') && (pauseNumber > 0) && (paused != true)){
		paused = true;
		halted = true;
		running = false;
		pauseNumber --;
	}else if((key=='p') && (pauseNumber < 1)){
		noPause = true;
	}

}


void runGame(){ //running the game, record actions, update state of game
	if (keyPressed) {
		//if (key == CODED) {
			if (key == 's') {
        		direction = 1;
        	}
        	else if (key == 'w') {
        		direction = 2;
        	}
        	else if (key == 'd') {
        		direction = 3;
        	}
        	else if (key == 'a') {
        		direction = 4;
        	}
       // }
	}

	clockCounter ++;
	if (clockCounter == clockSpeed){
		//record player input
		if ((direction == 1) && (counterY + 1 < boardYsquares)){	
			counterY += 1;
		}else if ((direction ==2)  &&  (counterY - 1 >= 0)){
			counterY -= 1;
		}else if ((direction ==3) && (counterX + 1 < boardXsquares)){
			counterX += 1;
		}else if ((direction ==4) && (counterX - 1 >= 0)){
			counterX -= 1;
		}		
		
		//check current game status
		score += 1;
		
		for(int i = 0; i < boardXsquares; i++){
			for(int j = 0; j < boardYsquares; j++){
				if((gameGridArr[i][j] == 2) || (gameGridArr[i][j] == 7)){
					dead = true;
					running = false;
					resetBoard();
					gameGridArr[counterX][counterY] -= 1;	
					if(score > highScore){
						highScore = score;
					}
				}
				if((gameGridArr[i][j] == 1) || (gameGridArr[i][j] == 6)){
					progress ++;
					if( progress == boardXsquares * boardYsquares){
						progress = 0;
						win = true;
						running = false;
						difficulty ++;	
						boardXsquares++;
						boardYsquares++;
						blocksize = boardSizeX/ boardXsquares;
						resetBoard();
						gameGridArr[counterX][counterY] -= 1;	
						if(score > highScore){
							highScore = score;
						}
					}
				}
			}
		}
		gameGridArr[counterX][counterY] += 1;	
		drawGame();
		clockCounter = 0;
		progress = 0;
	}
}

void drawGame(){  //draw snake
	drawBoard();

	for(int i = 0; i < boardXsquares; i++){
		for(int j = 0; j < boardYsquares; j++){
			if((gameGridArr[i][j] == 2)  || (gameGridArr[i][j] == 7)){		//dead
				noStroke();
				fill(150,40,40);
			}else if(gameGridArr[i][j] == 1){	//snake
				noStroke();
				fill(40);
			}else if(gameGridArr[i][j] > 2){	//blokers
				noStroke();
				fill(0);		
			}		
			rect(i*blocksize,j*blocksize, blocksize,blocksize);

			stroke(130);
			fill(150);
			ellipse((counterX*blocksize) +blocksize/2, (counterY*blocksize) + blocksize/2, blocksize/3, blocksize/3);

		}
	}
}


void resetBoard(){  //reset game array and counters
	clockCounter = 0;
	progress = 0;
	
	//reset gameboard
	for(int i = 0; i < boardXsquares; i++){
		for(int j = 0; j < boardYsquares; j++){
			gameGridArr[i][j] = 0;
		}
	}
	
	//adjust diifficulty
	blockerX = int(random( 0 ,boardXsquares));
	blockerY = int(random( 0, boardYsquares));
				
	for (int i = 1; i < difficulty; i += 2 ){
		blockerX = int(random( 0 ,boardXsquares));
		blockerY = int(random( 0, boardYsquares));
		gameGridArr[blockerX][blockerY] = 6;
	}
				
	counterX  = int(random((boardXsquares/4),boardXsquares-(boardXsquares/4)));
	counterY = int(random((boardYsquares/4),boardYsquares-(boardYsquares/4)));
	
	gameGridArr[counterX][counterY] = 1;	
	
	if(boardXsquares > 49){
		running = false;
		endGame = true;
	}		
}

Leave a Reply

Your email address will not be published. Required fields are marked *