#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Creating a Game!

edited November 2016

Hello everyone! I would really appreciate any help that could be given with this project.

So for my final in a few weeks, I am creating a game in Processing. It is a spin on the snake game where instead of the traditional snake and red square I want to make it where I will have two png images in their place. Specifically, I will be having the Blackhawks logo chasing the Stanley Cup! I began my code with a welcome screen with instructions that I've gotten to work perfectly. My problem is figuring out how to use the reference code (the one I will post below) to adapt it for the two images. Another issue I have is that I'm unsure how to incorporate the game to the welcome screen code. Would I just add it at the end or would I need to use ArrayList?

Thank you again in advance for any help you can give! Below is the reference code that I'm using for the game aspect. (Credit to OpenProcessing - Ian151)

`````` int angle=0;
int snakesize=5;
int time=0;
int applex=(round(random(47))+1)*8;
int appley=(round(random(47))+1)*8;
boolean redo=true;
boolean stopgame=false;
void setup()
{
restart();
size(400,400);
textAlign(CENTER);
}
void draw()
{
if (stopgame)
{
//do nothing because of game over (stop playing)
}
else
{
//draw stationary stuff
time+=1;
fill(255,0,0);
stroke(0);
rect(applex,appley,8,8);
fill(0);
stroke(0);
rect(0,0,width,8);
rect(0,height-8,width,8);
rect(0,0,8,height);
rect(width-8,0,8,height);
//my modulating time by 5, we create artificial frames each 5 frames
//(otherwise the game would go WAY too fast!)
if ((time % 5)==0)
{
travel();
display();
}
}
}
//controls:
void keyPressed()
{
if (key == CODED)
{
{
angle=90;
}
{
angle=270;
{
angle=180;
{
angle=0;
}
if (keyCode == SHIFT)
{
//restart the game by pressing shift
restart();
}
}
}
void travel()
{
for(int i=snakesize;i>0;i--)
{
if (i!=1)
{
//shift all the coordinates back one array
}
else
{
//move the new spot for the head of the snake, which is
switch(angle)
{
case 0:
break;
case 90:
break;
case 180:
break;
case 270:
break;
}
}
}

}
void display()
{
//is the head of the snake eating the apple?
{
//grow and spawn the apple somewhere away from the snake
//(currently some of the code below might not be working, but the game still works.)
snakesize+=round(random(3)+1);
redo=true;
while(redo)
{
applex=(round(random(47))+1)*8;
appley=(round(random(47))+1)*8;
for(int i=1;i<snakesize;i++)
{

{
redo=true;
}
else
{
redo=false;
i=1000;
}
}
}
}
//draw the new head of the snake...
stroke(sinecolor(1),sinecolor(0),sinecolor(.5));
fill(0);
//...then erase the back end of the snake.
fill(255);

}
{
for(int i=2;i<=snakesize;i++)
{
//is the head of the snake occupying the same spot as any of the snake chunks?
{
fill(255);
rect(125,125,160,100);
fill(0);
text("GAME OVER",200,150);
text("Score:  "+str(snakesize-1)+" units long",200,175);
text("To restart, press Shift.",200,200);
stopgame=true;
}
//is the head of the snake hitting the walls?
{
fill(255);
rect(125,125,160,100);
fill(0);
text("GAME OVER",200,150);
text("Score:  "+str(snakesize-1)+" units long",200,175);
text("To restart, press Shift.",200,200);
stopgame=true;
}
}
}
void restart()
{
//by pressing shift, all of the main variables reset to their defaults.
background(255);
for(int i=2;i<1000;i++)
{
}
stopgame=false;
applex=(round(random(47))+1)*8;
appley=(round(random(47))+1)*8;
snakesize=5;
time=0;
angle=0;
redo=true;
}
float sinecolor(float percent)
{
return slime;
}
``````
Tagged:

• edited November 2016 Answer ✓

Hello!

I'd like to comment on the welcome screen.

You can typically use `state` to manage welcome screen versus regular game.

The idea is that a a variable `state` tells you whether the program is in state welcome screen or in the game

I show a small sketch to demonstrate this; this sketch has a pause screen instead of a welcome screen but the principle is almost the same.

You start in welcome screen. `state` has the value `statePause`. Now when a certain time has passed, you set `state` to `stateGame`. The function `draw()` acts accordingly.

GAME OVER is a third state you want to have. (This is instead of `if (stopgame)` in `draw()`...)

Best, Chrisir ;-)

``````//
// states
final int stateGame  = 0;  // consts
final int statePause = 1;
int state = statePause;    // current state

// ---------------------------------------------------------------

void setup()
{
// init (runs only once)
size(800, 600);
} // func

void draw()
{
// draw() runs on and on

switch (state) {

case stateGame:
// Game
handleStateGame();  // GAME HERE !!!!!!!!!!!
break;

case statePause:
// Pause
handleStatePause();
break;

default:
// error
println("Error number 939; unknown state : "
+ state
+ ".");
exit();
break;
} // switch
//
} // func

// ------------------------------------------------------------

// functions for states - called from draw()

void handleStateGame() {
// Game
background(11);
fill(244, 3, 3); // red
text ("Game....", 210, 313);
signHome(10, 100);
} // func

void handleStatePause() {
// Pause
background(255);
fill(244, 3, 3); // red
text ("Pause.... ", 210, 313);
text ("Hit p to start ", 210, 385);
signHome(width-70, 100);
} // func

// ----------------------------------------
// keyboard input

void keyPressed() {

switch (state) {

case stateGame:
// Game
keyPressedForStateGame();
break;

case statePause:
// Pause
keyPressedForStatePause();
break;

default:
// error
println("Error number 1039; unknown state : "
+ state
+ ".");
exit();
break;
} // switch
} // func

// ----

void keyPressedForStateGame() {
if (key == CODED)
{
if (keyCode == UP) {
//
} else if (keyCode == DOWN) {
//
}

if (keyCode == LEFT) {
//
} else if (keyCode == RIGHT) {
//
} else {
// do nothing
} // else
} //  if (key == CODED) {
else
{
// not CODED ----------------------
if (key == 'p') {
// Pause
state = statePause;
} else {
// do nothing
} // else
} // else not CODED
} // func

void keyPressedForStatePause() {
if (key == CODED)
{
if (keyCode == UP) {
//
} else if (keyCode == DOWN) {
// none
}

if (keyCode == LEFT) {
//
} else if (keyCode == RIGHT) {
//
} else {
// do nothing
} // else
} //  if (key == CODED) {
else
{
// not CODED ----------------------
if (key == 'p') {
//
state = stateGame;
} else {
// do nothing
} // else
} // else not CODED
} // func

// -------------------------------------------------------
// Mouse input

void mousePressed() {
//
switch (state) {

case stateGame:
// Game
mousePressedForStateGame();
break;

case statePause:
// Pause
mousePressedForStatePause();
break;

default:
// error
println("Error number 1139; unknown state : " + state+".");
exit();
break;
} // switch
} // func

void mousePressedForStateGame() {
if (dist(mouseX, mouseY, 10, 100) < 30 ) {
println ("Hit Game.");
}
} // func

void mousePressedForStatePause() {
if (dist(mouseX, mouseY, width-70, 100) < 30 ) {
println ("Hit Pause.");
}
} // func

// -------------------------------------------------------
// Misc

void signHome(int x, int y) {
// gives a sign for a house / home sign

final int width1=6;
final int height1=8;

fill( 2, 255, 0 );
strokeWeight(1);
stroke ( 2, 255, 0 );
noFill();
x+=21;
y+=14;
triangle ( x-width1, y,
x, y-height1,
x+width1, y );
rect(x-width1/2, y, width1, width1);
rect(x-width1/4+1, y+height1/4+1, width1/4, width1/3);
strokeWeight(1);
}

// ===============================
``````
• edited November 2016 Answer ✓

As for the 2nd issue, adapting the code you found (.......) on Openprocessing to your idea:

in line 135 the head of the snake is painted; so load the Blackhawks logo in setup() and display it in line 135.

the apple itself is drawn in line 28, so use `image()` to display Stanley Cup there.

• Thank you for your help! I'm pretty new to Processing. Below I put the code I have for the welcome screen, if it's alright I would really appreciate your advice on it! Thank you again!

``````    PFont Silom;
boolean buttonpressed;
int buttonA, buttonB, buttonC, buttonD;
PImage Rink;

void setup() {
size(684, 361);

buttonpressed = false;
buttonC = 215;
buttonD = 100;
buttonA = (width-450);
buttonB = (height-360);
}
void draw() {
background(Rink);
textFont(Silom, 70);
if (buttonpressed) {
println("Begin");
} else {
fill(255);
rect(buttonA, buttonB, buttonC, buttonD);
fill (0);
text("START", buttonA+6, buttonB+buttonD-25);
}
{

if (buttonpressed) {
fill(255, 255, 255, 0);
} else {
textFont(Silom, 20);
text("Press the arrow keys to move around", 160, 245);
textFont(Silom, 60);
text("Catch the cup!", 130, 200);
}
}

void mousePressed() {
if (mouseX>buttonA && mouseX<buttonA+buttonC && mouseY>buttonB && mouseY<buttonB+buttonC)
buttonpressed = true;
}
``````
• edited November 2016

Also, I attempted to play around with the way you explained the adapt the code and I havent been able to get it to work. UPDATE: I see where I went wrong and this comment doesnt matter anymore!

• Try to implement the state thing I've shown you

Then there's a line to delete the end of the snake/ old head. Here you need to place a black rect to delete the logo. But as I see it, the code is doing this already