Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snake game #68

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions snake_game/README.md
Original file line number Diff line number Diff line change
@@ -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).<br>
Click <a href="https://processing.org"> here </a> 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.<br>
If you find any issue just put it in the repository issue section, thank!<br><br>
.سبحَانَكَ اللَّهُمَّ وَبِحَمْدِكَ، أَشْهَدُ أَنْ لا إِلهَ إِلأَ انْتَ أَسْتَغْفِرُكَ وَأَتْوبُ إِلَيْ
139 changes: 139 additions & 0 deletions snake_game/snake_game.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
ArrayList<Integer> x = new ArrayList<Integer>(), y = new ArrayList<Integer>();
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<x.size(); i++){
noStroke();
fill(0,255,0);
rect(x.get(i)*bs, y.get(i)*bs, bs, bs);
}

if(frameCount%5 == 0)
{
x.add(0,x.get(0)+dx[dir]);
y.add(0,y.get(0)+dy[dir]);
if(foodx == x.get(0) && foody == y.get(0)){
score += 5;
foodx = (int)random(0,w);
foody = (int)random(0,h);
}
else{
x.remove(x.size()-1);
y.remove(y.size()-1);
}
}
if(x.get(0) < 0 || x.get(0) >= 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

}