/**
* Description of PvH
* May 25, 2017
*/
import java.util.Scanner;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.util.ArrayList;
import java.awt.event.*;
import javax.swing.*;
public class Game {
public void newGame(String command) {
boolean diagon = false;
double hx = 100.0, hy = 150.0, px = 15.0, py = 150.0;
if (command.equals("play")) {
// set canvas boundaries and graphics
StdDraw.setXscale(0, 300); // set x-axis
StdDraw.setYscale(0, 300); // set y-axis
//double penrad = 0.005;
boolean isPlaying = true;
int hs = 0; // hs = hades' score,
int ps = 0; // ps = persephone's score
// Figure h = new Figure("h"); //initialize characters
// Figure p = new Figure("p");
double d = 0.3;
// initialize Flower objects
int numOfFlowers = 100;
// ArrayList<Double[][]> flowers = new ArrayList<Double[][]>(numOfFlowers);
double[][] flowers = new double[numOfFlowers][2];
double flwidth = 3.0;
for (int i = 0; i < numOfFlowers; i++)
{
Flower f = new Flower();
f.setxy();
flowers[i][0] = f.getx(f);
flowers[i][1] = f.gety(f);
}
boolean[] keyStates = new boolean[8]; // first four for persephone, next four for hades
while (isPlaying) // game loop
{
if (diagon) System.out.println("isPlaying");
// if ((h.getx(h,"r") <= p.getx(p,"l")) | (p.getx(p,"r") <= h.getx(h,"l")) | (p.gety(p,"b") <= (h.gety(h,"t"))) | (h.gety(h,"b") <= (p.gety(p,"t")))) isPlaying = false;
if (StdDraw.hasNextKeyTyped()) { // allow user to hold and move at the same time; now you have to press repeatedly
int keyTyped = StdDraw.nextKeyTyped();
// PERSEPHONE CONTROL = WASD, HADES CONTROL = IJKL
// keyStates indices: 0 = pu, 1 = pd, 2 = pl, 3 = pr
// 4 = hu, 5 = hd, 6 = hl, 7 = hr
// fix directions and hitting wall, maKE IT STOP
if (keyTyped == 119) keyStates[0] = true; // p moves up
else keyStates[0] = false;
if (keyTyped == 115) keyStates[1] = true; // p moves down
else keyStates[1] = false;
if (keyTyped == 97) keyStates[2] = true; // p moves left
else keyStates[2] = false;
if (keyTyped == 100) keyStates[3] = true; // p moves right
else keyStates[3] = false;
if (keyTyped == 105) keyStates[4] = true; // h moves up
else keyStates[4] = false;
if (keyTyped == 107) keyStates[5] = true; // h moves down
else keyStates[5] = false;
if (keyTyped == 106) keyStates[6] = true; // h moves left
else keyStates[6] = false;
if (keyTyped == 108) keyStates[7] = true; // h moves right
else keyStates[7] = false;
}
// double nd = d/10;
if (keyStates[0] && (py+5 < 300-d)) py += d; // pu
if (keyStates[1] && (py-5 > d)) py -= d; // pd
if (keyStates[2] && (px-5 > d)) px -= d; // pl
if (keyStates[3] && (px+5 < 300-d)) px += d; // pr
if (keyStates[4] && (hy+5 < 300-d)) hy += d; // hu
if (keyStates[5] && (hy-5 > d)) hy -= d; // hd
if (keyStates[6] && (hx-5 > d)) hx -= d; // hl
if (keyStates[7] && (hx+5 < 300-d)) hx += d; // hr
// write cases for characters intercepting each other
// wall bounce back function should be refined
// draw characters
StdDraw.clear(StdDraw.WHITE);
//StdDraw.setPenColor(StdDraw.BLACK);
for (int i = 0; i < numOfFlowers; i++)
{ //draw flowers
// if hx and hy touch, then dont draw the flower. draw it otherwise
double fx = flowers[i][0];
double fy = flowers[i][1];
if ( (hx-5<=fx+3) | (hx+5 >= fx-3) | (hy+5 >= fy-3) | (hy-5 <= fy+3) ){
fx = -5;
fy = -5; //get rid of flowers. was going to use arraylist but this might be the most straightfoward way
hs++;
}
if ( (px-5<=fx+3) | (px+5 >= fx-3) | (py+5 >= fy-3) | (py-5 <= fy+3) ){
fx = -5;
fy = -5;
ps++;
}
StdDraw.setPenColor(StdDraw.FLOWER_COLOR);
StdDraw.circle(flowers[i][0],flowers[i][1],flwidth);
}
StdDraw.setPenColor(StdDraw.HADES_COLOR); // draw hades
StdDraw.filledSquare(hx,hy,5);
StdDraw.setPenColor(StdDraw.PERSEPHONE_COLOR); // draw persephone
StdDraw.filledSquare(px,py,5);
// display characters on screen; animate
StdDraw.show(1);
}
}
}
/**
* empty constructor for Game class
*/
public Game()
{
}
}