etukeni
Ex Member
Re: Snake game
Reply #7 - Jan 16th , 2010, 6:28pm
Check this version. class Snake { int x; int y; int h; Snake(int xc, int yc) { x=xc; y=yc; h=14; } Snake(int xc, int yc, int s) { x=xc; y=yc; h=s; } void createSnake() { ellipse(x,y,h,h); } void createSnake(int a,int b) { ellipse(a,b,h,h); } void moveSnakeUp() { if(gameEnd == false){ y= y - h; if(y<0){ end(); y=height; gameEnd = true; newSession(); } } } void moveSnakeDown() { if(gameEnd == false){ y = y + h; if(y>height){ end(); y=0; gameEnd = true; newSession(); } } } void moveSnakeRight() { if(gameEnd == false){ x = x + h; if(x>width){ end(); x=0; gameEnd = true; newSession(); } } } void moveSnakeLeft() { if(gameEnd == false){ x = x - h; if(x<0){ x=width; end(); gameEnd = true; newSession(); } } } int getXc() { return x; } int getYc() { return y; } int getH() { return h; } } import pitaru.sonia_v2_9.*; Sample backMusic; ArrayList snakeSegments; Snake head; int SEGMENT_SIZE = 0; int direction; int prevPosX; int prevPosY; int timer; int timeLength = 1000; PFont font; PFont fontt; String welcomeMessage=""; String gameOver = ""; String exitGame = ""; String startAllOver = ""; String message = ""; boolean isOk=false; boolean newGame = false; boolean gameEnd = false; boolean stopGame = false; boolean changeLevel; int score = 0; int speed = (int)random(1,5); int xMove = 0; Snake food; int gameLevel = 0; PImage backImage; PImage inBack; void setup() { size(500, 500); backImage = loadImage("back.jpg"); backImage.resize(500,500); font=loadFont("Broadway-28.vlw"); fontt=loadFont("Chiller-Regular-28.vlw"); textFont(font); snakeSegments = new ArrayList(); head = new Snake(width/8, height/20); snakeSegments.add(head); direction=0; Sonia.start(this); backMusic = new Sample("backgroundM.wav"); snakeSegments.add(new Snake(width, height)); addFood(); welcomeMessage="CLICK THE SCREEN TO START!" + "\n "; message = "B or b = Background Music" + "\nC or c = Cancel Music."; } void draw() { fill(random(0,255),random(0,255),random(0,255)); background(backImage); text(welcomeMessage,10,height/2); text(gameOver,150,height/2); text(exitGame,60,400); text(message, 1,460); if(mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) { cursor(HAND); } if(isOk ) { background(0); frameRate(5); food.createSnake(); tryToEatAndMove(snakeSegments.size()); cursor(ARROW); } newSession(); } boolean tryToEatAndMove(int p) { int x = head.getXc(); int y = head.getYc(); if (dist(x, y, food.getXc(), food.getYc()) < 12) { move(); Snake s1 = (Snake) snakeSegments.get(snakeSegments.size() - 1); Snake s2 = (Snake) snakeSegments.get(snakeSegments.size() - 2); int dX = s1.getXc() - s2.getXc(); int dY = s1.getYc() - s2.getYc(); snakeSegments.add(new Snake(s1.getXc() + dX, s1.getYc() + dY)); addFood(); return true; } move(); return false; } void addFood() { food = new Snake(int(random(10, 390)), int(random(10, 390)),14); } void move() { scoreLogic(); prevPosX = head.getXc(); prevPosY = head.getYc(); switch(direction) { case 0: head.moveSnakeUp(); break; case 1: head.moveSnakeDown(); break; case 2: head.moveSnakeRight(); break; case 3: head.moveSnakeLeft(); break; } followHead(); } void followHead() { fill(200,25,0); head.createSnake(); fill(0,255,0); for (int i = 1; i < snakeSegments.size(); i++) { Snake s = (Snake) snakeSegments.get(i); int ppX = s.getXc(); int ppY = s.getYc(); s.x = prevPosX; s.y = prevPosY; prevPosX = ppX; prevPosY = ppY; s.createSnake(); } } void mousePressed() { exitGame = ""; welcomeMessage=""; startAllOver = ""; isOk=true; newGame = true; stopGame= true; direction=2; } void keyPressed() { if(key==CODED) { if(keyCode==UP) { direction=0; } if(keyCode==DOWN) { direction=1; } if(keyCode==RIGHT) { direction=2; } if(keyCode==LEFT) { direction=3; } } if(key == 'b' || key == 'B') { backMusic.play(); } if(key == 'c' || key == 'C') { backMusic.stop(); } } void scoreLogic(){ stroke(4); score = snakeSegments.size()*10-20; textFont(fontt); fill(255,0,300); rect(190, 460 , 200, 20 ); fill(0,255,255); rect(190 + score, 460 , 4, 20 ); text("Score:", 80, 480); fill(100,255,255); text(score, 400, 480); score++; } void end(){ textFont(font); gameOver = "Game Over!!!" + "\n "; exitGame = "Click Me to Exit the Game"; backMusic.stop(); isOk = false; } void newSession(){ if(mouseX > 0 && mouseX < width && mouseY > height/2 && mouseY < height && mousePressed){ exit(); } if(mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height/2 && gameEnd == true && mousePressed){ snakeSegments = new ArrayList(); head = new Snake(width/8, height/20); snakeSegments.add(head); followHead(); move(); tryToEatAndMove(snakeSegments.size()); } }