Hello! I am making the game snake and I am trying to rotate the snakehead, but I am stuck. Should I use the rotate function or should I redraw each eye under each conditional? Right now I have a class that consists of both eyes. Let me know if you guys have any advice. Thanks in advance.
class Eye { //Creates a class of the two eyeballs
float hx;
float hy;
color rcolor;
float size = 25;
Eye(float hx, float hy, color rcolor) {
this.hx=hx;
this.hy=hy;
this.rcolor=rcolor;
}
void keyPressed() { //Creates the controls of the game
if (key==CODED) {
if (keyCode==UP) {
fill(rcolor);
circle(hx-12, hy-25, size);
hy = -edy+ hy;
ty[ty.length-1] -=dy;
}
}
if (key==CODED) {
if (keyCode==DOWN) {
fill(rcolor);
circle(hx-12, hy+25, size);
hy = hy+edy;
ty[ty.length-1] +=dy;
}
}
if (key==CODED) {
if (keyCode==LEFT) {
fill(rcolor);
circle(hx/2, hy+25, size);
hx = hx-edx;
tx[tx.length-1] -= dx;
}
}
if (key==CODED) {
if (keyCode==RIGHT) {
hx = hx+edx;
tx[tx.length-1] += dx;
}
}
}
void edge() {
float edge_x = hx-(size/8);
float edge_y = hy-(size/8);
if (edge_x>width || edge_x<0) {
state = true;
}
if (edge_y>height || edge_y<0) {
state = true;
}
}
void tail(){
for(int i = 0; i<tx.length-1; i++){ //shifts the tail
tx[i] = tx[i+1];
ty[i] = ty[i+1];
}
for(int j= 0; j<tx.length; j++){
fill(rcolor);
rectMode(CENTER);
//noStroke();
ellipse(tx[j], ty[j], 25, 25);
circle(tx[0], ty[0], 25);
}
}
void food(){
PImage img1;
img1 = loadImage("redApple.png");
img1.resize(45,45);
image(img1,appleX,appleY);
if(dist(hx-12,hy-25, appleX,appleY)<20){
tx = (int[]) append(tx,tx[tx.length-1]);
ty = (int[]) append(ty, ty[ty.length-1]);
appleX = random(20,500);
appleY = random(20,500);
score++;
}
if(dist(tx[tx.length-1], ty[ty.length-1], appleX, appleY) <25){
tx = (int[]) append(tx,tx[tx.length-1]);
ty = (int[]) append(ty, ty[ty.length-1]);
appleX = random(20,500);
appleY = random(20,500);
score++;
}
}
void score(){
fill(0);
textSize(30);
text("Score: ", width*.05, height*.05);
text(score,width*.17, height*.05);
}
void run() {
score();
//display();
keyPressed();
edge();
food();
tail();
}
}