Last active
December 23, 2016 12:30
-
-
Save josejuansanchez/740d54819490c8afe156399aaedf6d31 to your computer and use it in GitHub Desktop.
Snake. Processing Game. Raspberry Hackers, 23 December, 2016.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This is the first game developed by the Raspberry Hackers team. | |
| // 23 December, 2016. Las Norias (Almería). | |
| // Reference: https://www.youtube.com/watch?v=JGW5ecDOjjk | |
| int w; | |
| int h; | |
| int bs = 20; | |
| ArrayList<Integer> x = new ArrayList<Integer>(); | |
| ArrayList<Integer> y = new ArrayList<Integer>(); | |
| // 0: Down | |
| // 1: Up | |
| // 2: Right | |
| // 3: Left | |
| int dir; | |
| int [] dx = {0, 0, 1, -1}; | |
| int [] dy = {1, -1, 0, 0}; | |
| boolean gameover = false; | |
| int ax; | |
| int ay; | |
| int score; | |
| //************************************* | |
| void setup() { | |
| //size(600, 600); | |
| fullScreen(); | |
| orientation(LANDSCAPE); | |
| h = (int) height / bs; | |
| w = (int) width / bs; | |
| intializeNewGame(); | |
| } | |
| //************************************* | |
| void draw() { | |
| printBoard(); | |
| if (gameover == false) { | |
| printSnake(); | |
| printFood(); | |
| if (frameCount % 5 == 0) { | |
| x.add(0, x.get(0) + dx[dir]); | |
| y.add(0, y.get(0) + dy[dir]); | |
| //checkSnakeCollision(); | |
| checkBordersCollision(); | |
| if (x.get(0)==ax && y.get(0)==ay) { | |
| ax = (int)random(0, w); | |
| ay = (int)random(0, h); | |
| score = score + 10; | |
| } else { | |
| x.remove(x.size()-1); | |
| y.remove(y.size()-1); | |
| } | |
| } | |
| } else { | |
| showGameOver(); | |
| } | |
| } | |
| //************************************* | |
| void keyPressed() { | |
| if (key == ' ') { | |
| intializeNewGame(); | |
| } | |
| switch(keyCode) { | |
| case DOWN: | |
| dir = 0; | |
| break; | |
| case UP: | |
| dir = 1; | |
| break; | |
| case RIGHT: | |
| dir = 2; | |
| break; | |
| case LEFT: | |
| dir = 3; | |
| break; | |
| } | |
| } | |
| //************************************* | |
| void mousePressed() { | |
| float zoneSize = width / 3; | |
| // First Zone: Action = Left | |
| if (mouseX <= zoneSize) { | |
| dir = 3; | |
| } else if (mouseX <= 2*zoneSize) { | |
| if (mouseY <= height/2) { | |
| // Second Zone: Action = Up | |
| dir = 1; | |
| } else { | |
| // Second Zone: Action = Down | |
| dir = 0; | |
| } | |
| } else { | |
| // Third Zone: Action = Right | |
| dir = 2; | |
| } | |
| if (gameover == true) { | |
| intializeNewGame(); | |
| } | |
| } | |
| //************************************* | |
| void printBoard() { | |
| background(255); | |
| for(int i = 0; i < w; i = i + 1) { | |
| line(i*bs, 0, i*bs, height); | |
| } | |
| for(int i = 0; i < h; i = i + 1) { | |
| line(0, i*bs, width, i*bs); | |
| } | |
| } | |
| //************************************* | |
| void printSnake() { | |
| for(int i = 0; i < x.size(); i = i + 1) { | |
| fill(0, 255, 0); | |
| rect(x.get(i)*bs, y.get(i)*bs, bs, bs); | |
| } | |
| } | |
| //************************************* | |
| void checkBordersCollision() { | |
| if(x.get(0) < 0 || y.get(0) < 0 || x.get(0) >= w || y.get(0) >= h) { | |
| gameover = true; | |
| } | |
| } | |
| //************************************* | |
| void checkSnakeCollision() { | |
| for(int i=1;i<x.size();i++) if(x.get(0)==x.get(i)&&y.get(0)==y.get(i)) { | |
| gameover=true; | |
| } | |
| } | |
| //************************************* | |
| void showGameOver() { | |
| fill(0); | |
| textSize(50); | |
| textAlign(CENTER); | |
| text("GAME OVER. Press space", width/2, height/2); | |
| text("SCORE: " + score, width/2, height/2 + 100); | |
| } | |
| //************************************* | |
| void printFood() { | |
| fill(255, 0, 0); | |
| rect(ax*bs, ay*bs, bs, bs); | |
| } | |
| //************************************* | |
| void randomPositionForSnake() { | |
| // Random position for snake | |
| x.add((int)random(4, w - 4)); | |
| y.add((int)random(4, h - 4)); | |
| } | |
| //************************************* | |
| void randomPositionForFood() { | |
| // Random position for food | |
| ax = (int)random(0, w); | |
| ay = (int)random(0, h); | |
| } | |
| //************************************* | |
| void randomDirectionForSnake() { | |
| dir = (int)random(0, 4); | |
| println(dir); | |
| } | |
| //************************************* | |
| void intializeNewGame() { | |
| x.clear(); | |
| y.clear(); | |
| randomPositionForSnake(); | |
| randomPositionForFood(); | |
| randomDirectionForSnake(); | |
| score = 0; | |
| gameover = false; | |
| } | |
| //************************************* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment