We are about to switch to a new forum software. Until then we have removed the registration on this forum.
like this....?
ArrayList<Integer> snakeArrayX=new ArrayList();
ArrayList<Integer> snakeArrayY=new ArrayList();
// --------------------------------------------------------------
void setup() {
size(400, 400);
frameRate(60);
}
void draw() {
int x = int(random(width)); // probably gridSize here !!!
int y = int(random(height));
// as long as new apple position x,y is inside snake, the position is not acceptable and gets redefined
while (positionIsInsideSnake(x, y)) {
x = int(random(width)); // probably gridSize here !!!
y = int(random(height));
}//while
//
}//func
boolean positionIsInsideSnake(int x, int y) {
for (int i=0; i<snakeArrayX.size(); i++) {
if (x==snakeArrayX.get(i) && y==snakeArrayY.get(i)) {
return true; // it's inside; we immediately leave the function (because one occurance is enough)
}//if
}//for
// no occurance
return false;
}//func
//
For a snake game, I have the food teleport to a random spot on the screen when you eat it, but it's possible for the food to end up on your tail, and not on an empty space. Basically, I sort of need some way to check if the food's x is in the arrayList for the snake's x, and the smae with y, and if so, to execute an action until that is not true.
Hey Guys, I started my first little project using processing, which turned out to become a snake game (Code below). I finished the head and the moving mechanisms. Now ive come to that point, where i have to define the X and Y positions of each part of the body. I created 2 arrays (tailx[] and tail[]) where i wanna save the locations. Ive also created an algorithm to initialize the position of tailx[n] based on tailx[n - 1], which should move on till n > 0 is false. Then i defined tailx[0] as current X position of the head. After that the Head moves and gets a new X position. But it wont work, instead it says Array index out of bounds. The error is somewhere inside the tail() function from line 127 to line 143. Thankful for every answer.
float gridsize = 20;
float speed = 0.05;
int window = 900;
int score = 0;
int n = score;
float snakex = (int((window/ gridsize)/ 2)) * gridsize;
float snakey = (int((window/ gridsize)/ 2)) * gridsize;
float accelx = 0;
float accely = 0;
float gridnumber = window / gridsize;
float pointa = random(2, gridnumber - 3);
float pointb = random(2, gridnumber - 3);
float pointx = int(pointa) * gridsize;
float pointy = int(pointb) * gridsize;
void setup(){
size(900, 900);
frameRate(100);
}
void draw(){
background(10);
run();
}
void food(){
if(snakex == pointx){
if(snakey == pointy){
pointa = random(2, gridnumber - 3);
pointb = random(2, gridnumber - 3);
score++;
println(score);
}
}
pointx = int(pointa) * gridsize;
pointy = int(pointb) * gridsize;
}
void move(){
if(keyPressed){
if(keyCode == UP){
if(snakex % gridsize == 0){
if(accely != speed){
accelx = 0;
accely = -speed;
}
}
}
if(keyCode == DOWN){
if(snakex % gridsize == 0){
if(accely != -speed){
accelx = 0;
accely = speed;
}
}
}
if(keyCode == RIGHT){
if(snakey % gridsize == 0){
if(accelx != -speed){
accelx = speed;
accely = 0;
}
}
}
if(keyCode == LEFT){
if(snakey % gridsize == 0){
if(accelx != speed){
accelx = -speed;
accely = 0;
}
}
}
}
}
void bordercheck(){
if(snakex > height - gridsize){
snakex = (int((window/ gridsize)/ 2)) * gridsize;
snakey = (int((window/ gridsize)/ 2)) * gridsize;
accelx = 0;
accely = 0;
score = 0;
println("---");
}
if(snakey > width - gridsize){
snakex = (int((window/ gridsize)/ 2)) * gridsize;
snakey = (int((window/ gridsize)/ 2)) * gridsize;
accelx = 0;
accely = 0;
score = 0;
println("---");
}
if(snakex < 0){
snakex = (int((window/ gridsize)/ 2)) * gridsize;
snakey = (int((window/ gridsize)/ 2)) * gridsize;
accelx = 0;
accely = 0;
score = 0;
println("---");
}
if(snakey < 0){
snakex = (int((window/ gridsize)/ 2)) * gridsize;
snakey = (int((window/ gridsize)/ 2)) * gridsize;
accelx = 0;
accely = 0;
score = 0;
println("---");
}
}
void tail(){
n = score;
float[] tailx = new float[n + 1];
float[] taily = new float[n + 1];
while(n > 0){
tailx[n] = tailx[n - 1];
taily[n] = taily[n - 1];
fill(255);
rect(tailx[n], taily[n], gridsize, gridsize);
n--;
}
tailx[0] = snakex;
taily[0] = snakey;
fill(255);
rect(tailx[0], taily[0], gridsize, gridsize);
}
void design(){
fill(255,0,0);
rect(pointx,pointy,gridsize,gridsize);
fill(0,255,0);
rect(snakex,snakey,gridsize,gridsize);
}
void run(){
food();
tail();
move();
snakex = snakex + accelx * gridsize;
snakey = snakey + accely * gridsize;
bordercheck();
design();
}
In general (without knowing what your AI does) it would be easier and more efficient to have a lot of methods/parameters, and configuring different AIs using data as to how they access those methods. So an AI is a class that could in theory do what any AI could do, and it is configured by the constructor to be a specific AI.
If you really need dynamic code generation at runtime embedded inside Processing then you could use Nashhorn -- you would build JavaScript file strings rather than Java, then execute them as code.
Can you give a few examples of what the code of your Snake AIs currently looks like?
I'm using processing to teach an AI how to play snake. As the sketch runs, files are modified, one of which is a pde file. The problem is that the sketch only recognizes the changes made to the pde file once the sketch is closed, and so restarting the program with setup() doesn't work. The only way for the program to work is for me to close it and reopen it every time the game is over. Because I will be dealing with thousands of games, this will get quite tiresome, and I was wondering if there was a way to close and reopen the sketch from within the sketch?
I am currently coding a basic snake game in processing and I want the snake not to move in the opposite direction it is currently moving.
The snake just stops whenever I press the opposite direction but i want the game to ignore the keypress.
if (keyCode == DOWN && !direction.equals("up")){
if (frameCount % framespeed == 0){
ypos += speed;
}
direction = "down";
}
if (keyCode == UP && !direction.equals("down")){
if (frameCount % framespeed == 0){
ypos -= speed;
}
direction = "up";
}
if (keyCode == RIGHT && !direction.equals("left")){
if (frameCount % framespeed == 0){
xpos += speed;
}
direction = "right";
}
if (keyCode == LEFT && !direction.equals("right")){
if (frameCount % framespeed == 0){
xpos -= speed;
}
direction = "left";
}
All that is left to do is draw an arc in the right place, with the right amount of openness. This looks good to me:
PVector target, loc, vel;
PVector[] points;
float ease = 0.3;
boolean easing = true;
int q, w, num = 150;
int[] vals;
void setup() {
size(900, 900, P2D);
noStroke();
points = new PVector[num];
for (int i = 0; i < num; i++) {
points[i] = new PVector(width/2, height/2);
}
}
void draw() {
background(#222C32);
noStroke();
loc = new PVector(width/2, height/2);
vel = new PVector();
target = new PVector(mouseX, mouseY);
for (int i = 0; i < num; i++) {
loc = points[i];
PVector distance = PVector.sub(target, loc);
vel = PVector.mult(distance, ease);
loc.add(vel);
target = loc;
}
for ( int i = num-1; i>=0; i--) {
fill(255-i*2, 220-i*1, 120-i*0.2);
ellipse(points[i].x, points[i].y, num-i, num-i);
}
target = new PVector(mouseX, mouseY);
PVector mouthd = PVector.sub(target, points[0]);
mouthd.mult(3);
translate(points[0].x, points[0].y);
rotate(atan2(mouthd.y, mouthd.x));
noStroke();//stroke(255,220,120);
//strokeWeight(3);
//fill(0);
fill(128,110,60);
float amt = map(dist(0,0,mouthd.x,mouthd.y),0,100,0,QUARTER_PI/2.0);
arc(0,0,num,num,-amt,amt);
// line(0, 0, mouthd.x, mouthd.y);
}
Notice that the darker yellow color for the back of the mouth is actually drawn in front of the head. I would suggest you leave it like this, because then it looks like a mouth without us having to draw what would be behind it.
You could also try filling this small mouth arc with the background color - this gives the illusion that the mouth is actually opening, but this effect is ruined when the snake tries to bite its own tail. Try it.
A solid black mouth arc looks pretty good too. It's up to you.
Now we can put the face on him!
PVector target, loc, vel;
PVector[] points;
float ease = 0.3;
boolean easing = true;
int q, w, num = 150;
int[] vals;
void setup() {
size(900, 900, P2D);
noStroke();
points = new PVector[num];
for (int i = 0; i < num; i++) {
points[i] = new PVector(width/2, height/2);
}
}
void draw() {
background(#222C32);
noStroke();
loc = new PVector(width/2, height/2);
vel = new PVector();
target = new PVector(mouseX, mouseY);
for (int i = 0; i < num; i++) {
loc = points[i];
PVector distance = PVector.sub(target, loc);
vel = PVector.mult(distance, ease);
loc.add(vel);
target = loc;
}
for ( int i = num-1; i>=0; i--) {
fill(255-i*2, 220-i*1, 120-i*0.2);
ellipse(points[i].x, points[i].y, num-i, num-i);
}
fill(0);
stroke(0);
translate(points[0].x,points[0].y);
ellipse(30, -10, 20, 20);
ellipse(-30, -10, 20, 20);
rect(-30, 20, 60, 10);
}
Looks silly, yes, but this is a KEY STEP in being able to give him a proper Pacman mouth: We have shown that we can properly draw something on the head of the snake without it looking awful.
"Wait!" your inner thought process yells, "Why don;t we draw the TAIL first, then the head, THEN the mouth?"
YES! That will work. But this is the "serious changes to the way he is being drawn" I mentioned before. Additionally, it also puts the head "in front" of the tail, whereas before, the TAIL would appear in front of the head.
PVector target, loc, vel;
PVector[] points;
float ease = 0.3;
boolean easing = true;
int q, w, num = 150;
int[] vals;
void setup() {
size(900, 900, P2D);
noStroke();
points = new PVector[num];
for (int i = 0; i < num; i++) {
points[i] = new PVector(width/2, height/2);
}
}
void draw() {
background(#222C32);
noStroke();
loc = new PVector(width/2, height/2);
vel = new PVector();
target = new PVector(mouseX, mouseY);
for (int i = 0; i < num; i++) {
loc = points[i];
PVector distance = PVector.sub(target, loc);
vel = PVector.mult(distance, ease);
loc.add(vel);
target = loc;
}
for( int i = num-1; i>=0; i--){
fill(255-i*2, 220-i*1, 120-i*0.2);
ellipse(points[i].x, points[i].y, num-i, num-i);
}
}
I encourage you to compare this with your original code! Notice that the simulation of each point's movement is now done first, and only after that is done are any points drawn - starting with the end of the tail first and moving up to drawing the head last. Thus the head is now in front of all parts of the tail.
Hopefully you can accept this subtle but necessary change to your Pac-snake.
Pac-snake is going to be a pain to put a mouth on... unless you make some serious changes to the way he is being drawn. Here, I tried to draw a face on him to demonstrate what I mean:
PVector target, loc, vel;
PVector[] points;
float ease = 0.3;
boolean easing = true;
int q, w, num = 150;
int[] vals;
void setup() {
size(900, 900, P2D);
noStroke();
points = new PVector[num];
for (int i = 0; i < num; i++) {
points[i] = new PVector(width/2, height/2);
}
}
void draw() {
background(#222C32);
noStroke();
loc = new PVector(width/2,height/2);
vel = new PVector();
target = new PVector(mouseX,mouseY);
for(int i = 0; i < num; i++) {
loc = points[i];
PVector distance = PVector.sub(target, loc);
vel = PVector.mult(distance, ease);
loc.add(vel);
target = loc;
fill(255-i*2, 220-i*1, 120-i*0.2);
ellipse(loc.x, loc.y, num-i, num-i);
}
fill(0);
stroke(0);
ellipse(mouseX+30,mouseY-10, 20, 20);
ellipse(mouseX-30,mouseY-10, 20, 20);
rect(mouseX-30,mouseY+20,60,10);
}
Notice how when the head moves behind the tail, the face shows through the tail? This is because you draw the head first, then each segment of the tail in order. If you then try to put a mouth on him, well, it will show on top of the tail - probably not what you want.
I started making a variation of Pacman, with some changes to the original idea. My Pacman is controlled by mouse and can move in any direction, and it's a snake.
My only problem is a mouth. I don't know, how to make it work. I want it either be active all the time or only when snake moves, any solution will work. Can you help me?
Here is my code:https://gist.github.com/anonymous/d002d99b90b2288ee6f8f3a16ee49c2e
Thank you very much my code works flawlessly now, the only thing I had to add was after the background(0) I had to add background(150) otherwise the window is black, other than that a perfect solution. I understand my mistake now, idk why I didn't realize sooner.
this is the working code if anyone is interested;
int Roll;
int Dice1;
int Dice2;
void setup() {
size(200, 200);
println(Dice1);
noLoop();
fill(0);
textAlign(CENTER);
Roll = 1;
}
void draw() {
background(0);
background(150);
text("Roll", width/6, height/6);
text(Roll, width/6, height/4);
text("Dice 1", width/3, height/6);
text(Dice1, width/3, height/4);
text("Dice 2", width/2, height/6);
text(Dice2, width/2, height/4);
if (Dice1 == 1 && Dice2 == 1) {
text("snake eyes!", width/2, height/2);
}
else {
text("no snake eyes", width /2, height/2);
}
}
void mousePressed() {
Dice1 = int(random(1, 6));
Dice2 = int(random(1, 6));
Roll +=1;
redraw();
}
I'm still a newbie so go easy, basically I'm writing a small piece of code that rolls 2 hypothetical die and will display "snake eyes!" when two 1's are rolled and "no snake eyes" for every other combination. I want to have it so the die re-roll on an input such as mouse pressed but 'redraw()' and 'loop()' do not generate new die rolls, nor do they increment the "Roll" variable itself, not sure what I'm doing wrong. What other ways are there of looping this block of code on mousePressed? Can't seem to get anything working.
int Roll;
int Dice1 = int(random(1, 6));
int Dice2 = int(random(1, 6));
void setup() {
size(200, 200);
println(Dice1);
noLoop();
fill(0);
textAlign(CENTER);
Roll = 1;
}
void draw() {
text("Roll", width/6, height/6);
text(Roll, width/6, height/4);
text("Dice 1", width/3, height/6);
text(Dice1, width/3, height/4);
text("Dice 2", width/2, height/6);
text(Dice2, width/2, height/4);
if (Dice1 == 1 && Dice2 == 1) {
text("snake eyes!", width/2, height/2);
}
else {
text("no snake eyes", width /2, height/2);
}
}
void mousePressed() {
Roll =+1;
redraw();
}
The key question is, is it the same project that people helping you need to understand? If it is, and you need to explain about the timing and the snake and the keys all over again... then ask your follow-up question in this thread as a follow-up.
Your separate question looks fine, but if at some point you need to share your complete code in each (a question about input, and about sound, etc.) then it is easier and less confusing to share it all in one place and update it as the conversation continues.
Helllllo there! I've some problems with the sound. This is a Snake game code and I added a sound for the eaten food and one for the Game Over. While the first one works, the sound of the Game Over plays firstly correctly and afterwards horribly distorted :( What do I do wrong?
ArrayList x = new ArrayList(), y = new ArrayList();
int w = 27, h = 27, bs = 28, dir = 2, foodx = 12, foody = 10;
int[] dx = {
0, 0, 1, -1
}
, dy = {
1, -1, 0, 0
};
boolean gameover = false;
PFont font;
import processing.sound.*;
SoundFile file;
SoundFile file2;
void setup() {
size (756, 756);
smooth();
x.add(5);
y.add(5);
}
void draw() {
background(38);
for (int i = 0; i < w; i++) line(i*bs, 0, i*bs, height);
for (int i = 0; i < h; i++) line(0, i*bs, width, i*bs);
for (int i = 0; i < x.size (); i++) {
fill(0, 255, 255);
noStroke();
rect(x.get(i)*bs, y.get(i)*bs, bs, bs);
}
if (!gameover) {
fill(255, 0, 0);
rect(foodx*bs, foody*bs, bs, bs);
if (frameCount%5==0) {
x.add(0, x.get(0) + dx[dir]);
y.add(0, y.get(0) + dy[dir]);
if (x.get(0) < 0) {
x.set(0, w);
}
if (y.get(0) < 0) {
y.set(0, h);
}
if (x.get(0) > w) {
x.set(0, 0);
}
if (y.get(0) > h) {
y.set(0, 0);
}
for (int i = 1; i < x.size (); i++) if (x.get(0)==x.get(i) && y.get(0) == y.get(i)) gameover = true;
if (x.get(0)==foodx && y.get(0)==foody) {
foodx = (int)random(0, w);
foody = (int)random(0, h);
file = new SoundFile(this, "Eat food.mp3");
file.play();
} else {
x.remove(x.size()-1);
y.remove(y.size()-1);
}
}
} else {
x.clear();
y.clear();
x.add(5);
y.add(5);
gameover = false;
}
int curTimeMs = millis();
if (curTimeMs > 20000) {
gameover = true;
file2 = new SoundFile(this, "game over arcade.mp3");
file2.play();
background(0);
font = createFont("Joystix", 50);
textFont(font);
fill(0, 255, 255);
textAlign(CENTER);
text("Game" + "\nOver", width/2, height/2);
}
}
void keyPressed() {
int newdir = keyCode==DOWN ? 0 : (keyCode==UP ? 1 : (keyCode==RIGHT ? 2 : (keyCode==LEFT ? 3 : -1)));
if (newdir != -1 && (x.size()
hello, it's good that you have the boolean keys_broken
use it in keyPressed()
to
either receive key as it is now OR
receive key to store in a new array of char that you define before setup()
:
char[] charList;
or use an arrayList
in the moment keys_broken
goes false, fire the array charList
to your snake and then empty the array charList
again
I'm really sorry you're right. I'm a beginner and sometimes I forget the proper language. I'm doing a research about frustration in human-computer interaction and I'm using Snake as an investigation tool. So I've modified the game adding some bugs aiming to get the users frustrated while they're playing. I made the input of the keyboard stop working for a few seconds at determined slots of time. Afterwards, the keyboard starts working normally and the user keeps playing.
I'd like to emulate what happens when the computer gets stuck and the user furiously keeps trying to open, for instance, a file: when the computer starts working again the file in question got open as many times as the user tried to open it during the stuck moment. So, in the matter of my case, I'm trying to build this behaviour when the user keeps pushing the keys, that are momentarily not operating, recording somehow which keys he presses, in what order and make them working with delay once the functions of the keyboard start working normally. This is part of the code, the one about the keyboard. I hope it's clearer!
boolean keys_broken;
int x, y;
int time1 = 5000;
int time2 = 8000;
void setup() {
size(600, 600);
x = 100;
y = 100;
keys_broken = false;
}
void draw() {
background(0);
fill(0, 200, 0);
ellipse(x, y, 20, 20);
int curTimeMs = millis();
if (curTimeMs > time1) {
keys_broken = true;
}
if (curTimeMs > time2) {
keys_broken = false;
}
}
void keyPressed() {
if (keys_broken) {
return;
}
if ( keyCode == LEFT ) {
x -= 5;
}
if ( keyCode == RIGHT ) {
x += 5;
}
if ( keyCode == UP ) {
y -= 5;
}
if ( keyCode == DOWN ) {
y += 5;
}
}
For debugging, you can use the debugging features in the Processing IDE or use something like the following lines, which needs to be added in your keyPressed() function:
if (key=='p')
looping=!looping;
if (key=='c') {
println("Snake "+snakesX[0]+", "+snakesY[0]);
println("\tFood "+foodX+", "+foodY);
}
Where p is to pause/resume your game and c is for "checking". I notice your values are right even when you wrapped around the screen. Then I notice you have this:
snakeX
and snakesX[]
I guess your bug is in drawfood(). Give it a try and see if this solves your problem.
Kf
Hello guys! I have troubles with this code of Snake. I was trying to teleport the snake to the other side and actually, the code I wrote mostly works (line 114). The problem is once the snake goes through the wall and is teleported to the other side, it doesn't eat the food anymore. I'm a beginner so I don't have any clue of why. Any suggestion? :( Thanks in advance!
color col=color(0);
color foodColor = color(0,255,255);
float speed = 100;
int cx, cy;
int moveX = 0;
int moveY = 0;
int snakeX = 0;
int snakeY = 0;
int foodX = -1;
int foodY = -1;
boolean check = true;
int []snakesX;
int []snakesY;
int snakeSize = 1;
int windowSize = 600;
boolean gameOver = false;
PFont Font = createFont("Arial",20, true);
void setup(){
size(int(windowSize), int(windowSize),P3D);
background(255);
noStroke();
speed = 100;
speed=speed/frameRate;
snakesX = new int[100];
snakesY = new int[100];
cx = width/2;
cy = height/2;
snakeX = cx-5;
snakeY = cy-5;
foodX = -1;
foodY = -1;
gameOver = false;
check = true;
snakeSize =1;
}
void draw(){
if(speed%5 == 0){
background(255);
runGame();}
speed++;
}
void reset(){
snakeX = cx-5;
snakeY = cy-5;
gameOver = false;
check = true;
snakeSize =1;
moveY = 0;
moveX = 0;
}
void runGame(){
if(gameOver== false){
drawfood();
drawSnake();
snakeMove();
ateFood();
checkHitSelf();
}else{
String modelString = "game over";
textAlign (CENTER);
textFont(Font);
text(modelString,100,100,40);}
}
void checkHitSelf(){
for(int i = 1; i < snakeSize; i++){
if(snakeX == snakesX[i] && snakeY== snakesY[i]){
gameOver = true;}
}
}
void ateFood(){
if(foodX == snakeX && foodY == snakeY){
check = true;
snakeSize++;
}
}
void drawfood(){
fill(foodColor);
while(check){
int x = (int)random(1,windowSize/10);
int y = (int)random(1,windowSize/10);
foodX = 5+x*10;
foodY = 5+y*10;
for(int i = 0; i < snakeSize; i++){
if(x == snakesX[i] && y == snakesY[i]){
check = true;
i = snakeSize;
}else{
check = false; }}
}
rect(foodX-5, foodY-5, 10, 10);
}
void drawSnake(){
fill(col);
for(int i = 0; i < snakeSize; i++) {
int X = snakesX[i];
int Y = snakesY[i];
rect(X-5,Y-5,10,10);
}
for(int i = snakeSize; i > 0; i--){
snakesX[i] = snakesX[i-1];
snakesY[i] = snakesY[i-1];
}
}
void snakeMove(){
snakeX += moveX;
snakeY += moveY;
if (snakeX > windowSize){snakeX = 0;}
if (snakeX < 0){snakeX = windowSize;}
if (snakeY < 0){snakeY = windowSize;}
if (snakeY > windowSize){snakeY = 0;}
snakesX[0] = snakeX;
snakesY[0] = snakeY;
}
void keyPressed() {
if(keyCode == UP){
if(snakesY[1] != snakesY[0]-10){moveY = -10; moveX = 0;}}
if(keyCode == DOWN){
if(snakesY[1] != snakesY[0]+10){moveY = 10; moveX = 0;}}
if(keyCode == LEFT){
if(snakesX[1] != snakesX[0]-10){moveX = -10; moveY = 0;}}
if(keyCode == RIGHT){
if(snakesX[1] != snakesX[0]+10){moveX = 10; moveY = 0;}}
if(keyCode == 'R') {reset();}
}
Instead of making the snake longer, you should focus in working on the snake in the grid. Then, you could add the extra code. The trick is to shuffle the positions of the body of the array by one "backwards" and then update the head.
Please also study for loops... your conditions were wrong which leads to bugs in your programs.
also check in the reference: rectMode()
Kf
final int kUP=0;
final int kDOWN=2;
final int kLEFT=3;
final int kRIGHT=1;
int swidth = 20; //snake width
int sheight = 20; //snake height
int xpos = 200; //Snake position
int ypos = 200;
int dir = 0; //direction: 0 = up, 1 = right, 2 = down, 3 = left
int slength = 5; //snake length
int deltax=0;
int deltay=0;
//Arrays
int[] xSchlange = new int[50]; //X Position Snake
int[] ySchlange = new int[50]; //Y Position Snake
void setup() {
size(400, 400);
frameRate(8);
background(255);
stroke(0);
rectMode(CENTER);
xpos = ypos = 200;
for (int i = 0; i < slength; i++) {
xSchlange[i] = xpos-swidth/2;
ySchlange[i] = ypos+i*sheight-sheight/2;
}
fill(0);
key='w';
keyPressed();
}
void draw() {
zeichneGitter();
steuerung();
schlangeZeichnen();
}
void zeichneGitter() { //draw grid
background(255);
stroke(0);
for (int i = 0; i < 20; i++) {
line(0, i*20, 400, i*20);
line(i*20, 0, i*20, 400);
}
}
void steuerung() { //controls
for (int i=slength-1;i>0; i--) {
xSchlange[i]=xSchlange[i-1];
ySchlange[i]=ySchlange[i-1]+sheight*0;
}
xSchlange[0] = xSchlange[0] + deltax;
ySchlange[0] = ySchlange[0] + deltay;
}
void schlangeZeichnen() { //draw snake
for (int i = 0; i < slength; i++) {
rect(xSchlange[i], ySchlange[i], swidth, sheight);
}
}
void keyPressed() {
deltax=deltay=0;
if (key == 'W' || key == 'w') { //WASD controls
dir = kUP;
deltay=-sheight;
} else if (key == 'S' || key == 's') {
dir = kDOWN;
deltay=sheight;
} else if (key == 'A' || key == 'a') {
dir = kLEFT;
deltax=-swidth;
} else if (key == 'D' || key == 'd') {
dir = kRIGHT;
deltax=swidth;
}
}