Skip to content

Instantly share code, notes, and snippets.

@roy4801
Created October 2, 2018 07:59
Show Gist options
  • Save roy4801/30c01ec6b1585a0fbf48f501e1722a2f to your computer and use it in GitHub Desktop.
Save roy4801/30c01ec6b1585a0fbf48f501e1722a2f to your computer and use it in GitHub Desktop.
2018-10-2
PImage cat, mouse;
boolean mouseLive = false;
boolean endGame = false;
int catPosX, catPosY, catSize;
int totalScore = 0;
int dir = 0;
final int numOfMouse = 50;
CharMouse []m = new CharMouse[numOfMouse];
class CharMouse
{
int x, y;
boolean live;
CharMouse()
{
x = y = 0;
live = false;
}
}
// 1 => up
// 2 => down
// 3 => left
// 4 => right
int offsetX = 10, offsetY = 10;
final int WIDTH = 800;
final int HEIGHT = 600;
void setup()
{
size(800, 600);
cat = loadImage("nyan.png");
mouse = loadImage("mouse.png");
frameRate(20);
randomSeed(0);
catSize = 64;
}
void spawnMouse()
{
for(int i = 0; i < numOfMouse; i++)
{
m[i] = new CharMouse();
m[i].x = (int)random(WIDTH);
m[i].y = (int)random(HEIGHT);
m[i].live = true;
}
mouseLive = true;
}
void draw()
{
background(255);
// Spawn the mouse
if(!mouseLive)
spawnMouse();
// print(catPosX + " " + catPosY + " // " + mousePosX + " " + mousePosY + "\n");
print(mouseLive ? "T" : "F" + "\n");
// Processing input
if (keyPressed == true)
{
if(key == CODED)
{
switch(keyCode)
{
case UP:
catPosY -= offsetY;
dir = 1;
break;
case DOWN:
catPosY += offsetY;
dir = 2;
break;
case LEFT:
catPosX -= offsetX;
dir = 3;
break;
case RIGHT:
catPosX += offsetX;
dir = 4;
break;
}
}
}
//----------------------------------------------------------
// Game Logic
for(int i = 0; i < numOfMouse; i++)
{
if(m[i].live)
{
int catCX = catPosX + catSize / 2, catCY = catPosY + catSize / 2;
int mouseCX = m[i].x + 64 / 2, mouseCY = m[i].y + 64 / 2;
println("Cat: " + catCX + " " + catCY + "Mouse: " + mouseCX + " " + mouseCY + "\n");
println("Dis: " + sqrt( pow((catCX - mouseCX), 2.0) + pow((catCY - mouseCY), 2.0)));
if(sqrt( pow((catCX - mouseCX), 2.0) + pow((catCY - mouseCY), 2.0)) <= 16 + catSize/2)
{
m[i].live = false;
totalScore++;
catSize += 4;
}
}
}
// End Game
if(totalScore == numOfMouse)
endGame = true;
//----------------------------------------------------------
// Drawing
for(int i = 0; i < numOfMouse; i++)
if(m[i].live)
image(mouse, m[i].x, m[i].y, 64, 64);
if(catPosX + cat.width >= WIDTH)
{
image(cat, catPosX - WIDTH, catPosY, catSize, catSize);
}
else if(catPosX < 0)
{
image(cat, catPosX + WIDTH, catPosY, catSize, catSize);
}
if(catPosY + cat.height >= HEIGHT)
{
image(cat, catPosX, catPosY - HEIGHT, catSize, catSize);
}
else if(catPosY < 0)
{
image(cat, catPosX, catPosY + HEIGHT, catSize, catSize);
}
image(cat, catPosX, catPosY, catSize, catSize);
if(catPosX >= WIDTH || catPosY >= HEIGHT)
{
catPosX %= WIDTH;
catPosY %= HEIGHT;
}
if(catPosY <= -catSize)
catPosY = HEIGHT + catPosY;
if(catPosX <= -catSize)
catPosX = WIDTH + catPosX;
if(endGame)
{
textSize(64);
fill(0, 0, 0);
text("Victory", WIDTH / 2 - 100, HEIGHT / 2 - 20);
}
// print score board
textSize(16);
fill(0, 0, 0);
text("Score: " + str(totalScore), 0, 20);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment