Player 1 keys: WSAD, Player 2 keys: Arrow up, down, right and left. The black circle is the puck (/ball).
A fork of Air Hockey by Andreas
xxxxxxxxxx
// Air Hockey Spil
Player player = new Player();
Player player2 = new Player();
Puck puck = new Puck();
float linjerum = 0;
int score = 0;
int score2 = 0;
int state = 0;
PImage bg;
void setup()
{
size(700, 324);
player.setup();
player.xBat=width/4;
player.yBat=height/2;
player2.setup();
player.xBat=525;
player.yBat=height/2;
puck.setup();
bg = loadImage("Screenshot 2020-09-30 at 12.50.14.png");
}
void draw()
{
if(state == 0)
{
startUp();
}
else if(state == 1)
{
game();
}
}
void startUp()
{
background (255);
textSize (40);
fill(0);
text("Air Hockey", width-220, 39);
fill(0);
textSize (15);
text("Press B to begin", width/2, 299);
textSize(15);
text("Press R if the puck is out of the screen or stuck", width/2, 314);
text("Keys for player1: WSAD", width/2-335, 15);
text("Keys for player2: Arrowkeys", width/2-335, 30);
}
void game()
{
// Banens udseende
background (255);
//Linjen i midten
for (linjerum=0; linjerum < height; linjerum = linjerum + 20)
{
fill(255, 0, 0);
rect(width/2 -1.5, -3 + linjerum, 3, 10);
}
//Målene og cirklen i midten
fill(255, 0, 0);
ellipse(width/2, height/2, 60, 60);
fill(255);
ellipse(width/2, height/2, 52, 52);
fill(255, 0, 0);
ellipse(width/2, height/2, 10, 10);
ellipse(0, height/2, 60, 150);
ellipse(width, height/2, 60, 150);
fill(255);
ellipse(0, height/2, 52, 142);
ellipse(width, height/2, 52, 142);
player.draw();
player2.draw();
puck.draw();
player.collision(puck);
player2.collision(puck);
// Score
textSize(30);
text(score,width*0.25,30);
text(score2,505,30);
//Afgrænsning af skærmen
if (player2.xBat >= width/2)
{
player2.xBatFart = -1;
}
if (player2.xBat <= 0)
{
player2.xBatFart = 1;
}
if (player2.yBat >= height)
{
player2.yBatFart = -1;
}
if (player2.yBat <= 0)
{
player2.yBatFart = 1;
}
//Afgrænsning af skærmen
if (player.xBat <= width/2)
{
player.xBatFart = 1;
}
if (player.xBat >= width)
{
player.xBatFart = -1;
}
if (player.yBat >= height)
{
player.yBatFart = -1;
}
if (player.yBat <= 0)
{
player.yBatFart = 1;
}
}