We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Can someone help me, i need to make it restart after the game has ended, basically draw the game from scratch every time i press a button(in my case Shift). Also any help on making enemies randomly appear for like 4-5 seconds and then disapear would be greatly appriciated. -Cheers
THE.CODE
//add power ups,random enemies spawn,confusion(reverse controls), border
snake sn;
food food1;
enemy enemy1;
int score;
PImage bkg;
PImage img;
PImage imgg;
boolean redo=true;
boolean stopgame=false;
void setup(){
size(700, 500);
frameRate(15);
sn = new snake();
food1 = new food();
enemy1 = new enemy();
rectMode(CENTER);
textAlign(CENTER, CENTER);
bkg = loadImage("bkg.jpg");
img = loadImage("gover.jpg");
score =0;
reset();
}
void draw(){
if(stopgame){
size(700, 500);
background(img);
noFill();
rect(350, 390, 200, 100, 10);
fill(0);
textSize(18);
text("Snake Lenght: "+sn.len+" ",350,378);
textSize(15);
text("To exit, press Esc.",350,420);
return;
}
else{
playGame();
}
}
void playGame() {
score = sn.len;
stopgame=false;
background(bkg);
drawScoreboard();
sn.move();
sn.display();
food1.display();
enemy1.display();
if( dist(food1.xpos, food1.ypos, sn.xpos.get(0), sn.ypos.get(0)) < sn.slen ){
food1.reset();
sn.addLink();
}
if( dist(enemy1.xpos, enemy1.ypos, sn.xpos.get(0), sn.ypos.get(0)) < sn.slen ){
enemy1.reset();
sn.removeLink();
}
}
void reset() {
playGame();
}
void drawScoreboard(){
fill(250, 0, 250);
textSize(25);
text( "", width/2, 80);
fill(250, 0, 250);
textSize(20);
text( "", width/2, 140);
stroke(140, 140, 80);
fill(255, 0 ,255);
rect(0, 0, 0, 10);
fill(18, 20, 67);
textSize(25);
text( "Score: " + (sn.len-1), 350, 255);
}
void keyPressed(){
if(key == CODED){
if(keyCode == LEFT && sn.dir!= "right"){
sn.dir = "left";
}
if(keyCode == RIGHT && sn.dir!= "left"){
sn.dir = "right";
}
if(keyCode == UP && sn.dir!= "down"){
sn.dir = "up";
}
if(keyCode == DOWN && sn.dir!= "up"){
sn.dir = "down";
}
if (keyCode == SHIFT) {
playGame();
}
if (keyCode == ESC) {
exit();
}
}
}
class food{
float xpos, ypos;
food(){
xpos = random(10, width - 10);
ypos = random(10, height - 10);
}
void display(){
fill(154, 205 ,50);
ellipse(xpos, ypos,21,21);
}
void reset(){
xpos = random(51, width - 51);
ypos = random(51, height - 51);
}
}
class enemy{
float xpos, ypos;
enemy(){
xpos = random(10, width - 10);
ypos = random(10, height - 10);
}
void display(){
fill(219,112,147);
ellipse(xpos, ypos,21,21);
}
void reset(){
xpos = random(51, width - 51);
ypos = random(51, height - 51);
}
}
class snake{
int len;
int len0;
float slen;
String dir;
ArrayList <Float> xpos, ypos;
snake(){
len = 1;
slen = 20.5;
xpos = new ArrayList();
ypos = new ArrayList();
xpos.add( random(width) );
ypos.add( random(height) );
}
void move(){
for(int i = len - 1; i > 0; i = i -1 ){
xpos.set(i, xpos.get(i - 1));
ypos.set(i, ypos.get(i - 1));
}
if(dir == "left"){
xpos.set(0, xpos.get(0) - slen);
}
if(dir == "right"){
xpos.set(0, xpos.get(0) + slen);
}
if(dir == "up"){
ypos.set(0, ypos.get(0) - slen);
}
if(dir == "down"){
ypos.set(0, ypos.get(0) + slen);
}
xpos.set(0, (xpos.get(0) + width) % width);
ypos.set(0, (ypos.get(0) + height) % height);
// check if hit itself and if so cut off the tail
if( checkHit() == true){
stopgame=true;
}
}
void display(){
for(int i = 0; i <len; i++){
stroke(179, 140, 198);
fill(99 , 184 , 255, map(i-1, 0, len-1, 250, 50));
ellipse(xpos.get(i), ypos.get(i), slen, slen);
}
}
void addLink(){
xpos.add( xpos.get(len-1) + slen);
ypos.add( ypos.get(len-1) + slen);
len++;
}
void removeLink(){
xpos.remove( xpos.get(len-1) + slen);
ypos.remove( ypos.get(len-1) + slen);
len--;
if (sn.len == 0){
stopgame=true;
}
}
boolean checkHit(){
for(int i = 1; i < len; i++){
if( dist(xpos.get(0), ypos.get(0), xpos.get(i), ypos.get(i)) < slen){
return true;
}
}
return false;
}
}
Answers
What your reset() function is doing right now is probably not what you want it to be doing. To reset a sketch, all you have to do is give all your variables the same values they had when the sketch started.
Now, if you made your reset() function give all your variables their initial values... then when you call that function later on... what happens?
Thanks for your input. I'll re-design the methods. I realized I need an initialState method will be called every time before the game starts, which has all the values set to 0.