From fc90a299d9c8adb63ed15c748e7871167c1074fe Mon Sep 17 00:00:00 2001 From: Mohamed Yanis HIOU <61564196+mohamedyanis@users.noreply.github.com> Date: Tue, 12 Oct 2021 23:23:05 +0100 Subject: [PATCH] Snake game --- snake_game/README.md | 28 ++++++++ snake_game/snake_game.pde | 139 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 snake_game/README.md create mode 100644 snake_game/snake_game.pde diff --git a/snake_game/README.md b/snake_game/README.md new file mode 100644 index 0000000..8c22061 --- /dev/null +++ b/snake_game/README.md @@ -0,0 +1,28 @@ +# Snake Game + +![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=102) + +#100daysofcode This simple programme written with processing3 (like JAVA but easier).
+Click here to read more about processing3. + +## Features :dart: +* [x] Free & Open Source +* [x] Very Easy to use + +## Thank _You_! +Please :star: this repo to help us improve the quality. + +## Requirements +* processing3 + +## How To Use It +1. Download Processing3 from this link: https://processing.org/download/ +2. Install this repository. +3. Extract the folder. +4. Open the ```snake_game.pde``` file. +5. Run it :) + +## Contributing 💡 +If you want to contribute to this project and make it better with new ideas, your pull request is very welcomed.
+If you find any issue just put it in the repository issue section, thank!

+.سبحَانَكَ اللَّهُمَّ وَبِحَمْدِكَ، أَشْهَدُ أَنْ لا إِلهَ إِلأَ انْتَ أَسْتَغْفِرُكَ وَأَتْوبُ إِلَيْ diff --git a/snake_game/snake_game.pde b/snake_game/snake_game.pde new file mode 100644 index 0000000..ed44be8 --- /dev/null +++ b/snake_game/snake_game.pde @@ -0,0 +1,139 @@ +ArrayList x = new ArrayList(), y = new ArrayList(); +int w = 30, h = 30, bs = 20, dir = 2, foodx = 10, foody = 10, score = 0; +int[] dx = {0,0,1,-1}, dy = {1,-1,0,0}; +boolean gameOver = false; + +void setup() +{ + size(600, 600); + x.add(5); + y.add(5); +} + + +void draw() +{ + if(!gameOver) + { + background(255); + fill(0); + textAlign(LEFT); + text(score, 70, 20); + rect(foodx*bs, foody*bs, bs, bs); + for(int i = 0; i= w || y.get(0) <0 || y.get(0) >=h) + { + delay(2000); + gameOver = true; + score = 0; + } + for(int i = 1; i < x.size()-1; i++) + { + if(x.get(i) == x.get(0) && y.get(i) == y.get(0)) + { + delay(2000); + gameOver = true; + score = 0; + } + } + } + else{ + background(0); + textSize(20); + textAlign(CENTER); + text("GAME OVER! \nPRESS SPACE TO PLAY AGAIN OR ESC TO EXIT)", width/2, height/3); + if(keyPressed && key == ' ') + { + x.clear(); + y.clear(); + x.add(5); + y.add(5); + gameOver = false; + } + } +} + + +void keyPressed() +{ + int newDir; + if(key == CODED) + { + switch(keyCode){ + case DOWN: newDir = 0; break; + case UP: newDir = 1; break; + case RIGHT: newDir = 2; break; + case LEFT: newDir = 3; break; + default: newDir = -1; break; + }//end switch + }//end if + else + { + switch(key){ + case 's': newDir = 0; break; + case 'w': newDir = 1; break; + case 'd': newDir = 2; break; + case 'a': newDir = 3; break; + default: newDir = -1; break; + }//end switch + }//end else + + switch(newDir){ + case 0: { + if(dy[dir] == -1) + { + break; + }else { + dir = newDir; + break; + }//end if/else + }//end case0 + case 1: { + if(dy[dir] == 1) + { + break; + }else { + dir = newDir; + break; + }//end if/else + }//end case1 + case 2: { + if(dx[dir] == -1) + { + break; + }else { + dir = newDir; + break; + }//end if/else + }//end case2 + case 3: { + if(dx[dir] == 1) + { + break; + }else { + dir = newDir; + break; + }//end if/else + }//end case3 + }//end switch + +}