Commit d5c59ff5 authored by Nguyen Nguyen's avatar Nguyen Nguyen
Browse files

Basic game with 3 lvls (use the MVC version)

parent 5d4e15e1
/* *****************************************
* CSCI205 - Software Engineering and Design
* Spring 2021
* Instructor: Prof. Chris Dancy
*
* Name: Nguyen Nguyen
* Section: Section 02 - 1:50-2:42am
* Date: 5/13/2021
* Time: 10:11 PM
*
* Project: CSCI205SP21finalproject
* Package: PACKAGE_NAME
* Class: BabaIsYouGameGUI
*
* Description: THIS IS A DESCRIPTION Y’ALL AND I NEED TO CHANGE THIS!
*
* ****************************************
*/
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
......
/* *****************************************
* CSCI205 - Software Engineering and Design
* Spring 2021
* Instructor: Prof. Chris Dancy
*
* Name: Nguyen Nguyen
* Section: Section 02 - 1:50-2:42am
* Date: 5/12/2021
* Time: 7:33 PM
*
* Project: CSCI205SP21finalproject
* Package: PACKAGE_NAME
* Class: BabaIsYouGameGUIController
*
* Description: THIS IS A DESCRIPTION Y’ALL AND I NEED TO CHANGE THIS!
*
* ****************************************
*/
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.awt.*;
import java.util.ArrayList;
public class BabaIsYouGameGUIController {
/** The width of the board */
private static final int WIDTH = 800;
/** The height of the board */
private static final int HEIGHT = 800;
/** The number of rows of the board */
private static final int ROWS = 40;
/** The number of columns of the board */
private static final int COLUMNS = 40;
/** The size of a square */
private static final int SQUARE_SIZE = WIDTH/ROWS;
/** For character movement */
private int moveUp = 1;
private int moveDown = 1;
private int moveRight = 1;
private int moveLeft = 1;
private int countLvl2 = 0;
private int countLvl3 = 0;
/** Character color */
private KeyCode code;
/** Setting up theModel and theView */
private BabaIsYouGameGUIModel theModel;
private BabaIsYouGameGUIView theView;
public BabaIsYouGameGUIController(BabaIsYouGameGUIView theView, BabaIsYouGameGUIModel theModel){
this.theModel = new BabaIsYouGameGUIModel();
this.theView = new BabaIsYouGameGUIView(theModel);
//Timeline for character animation
Timeline timeline = new Timeline(new KeyFrame(new Duration(10), e -> run()));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
}
private void run(){
if (theModel.getCurrentLvl() == 1) {
theModel.setObjs(generateObjLvl1());
//generateKeyObjsLvl1();
}
else if (theModel.getCurrentLvl() == 2) {
countLvl2+= 1;
theModel.setObjs(generateObjLvl2());
if (countLvl2 <= 1) {
BabaIsYouGameGUIModel.setBabaObj(new Point(600, 400));
BabaIsYouGameGUIModel.setIsObj(new Point(80, 200));
BabaIsYouGameGUIModel.setPassObj(new Point(700, 200));
BabaIsYouGameGUIModel.setWinObj(new Point(100, 100));
}
}
else if (theModel.getCurrentLvl() == 3){
countLvl3 += 1;
theModel.setObjs(generateObjLvl3());
if (countLvl3 <= 1) {
BabaIsYouGameGUIModel.setBabaObj(new Point(600, 400));
BabaIsYouGameGUIModel.setIsObj(new Point(80, 200));
BabaIsYouGameGUIModel.setPassObj(new Point(700, 200));
BabaIsYouGameGUIModel.setWinObj(new Point(100, 100));
}
}
keyboardInput();
colorBackground();
colorCharacter(theModel.getCurrentColor());
drawObj();
drawPassObj();
drawBaBaObj();
drawIsObj();
drawWinObj();
checkInteraction();
nearKeyObject(BabaIsYouGameGUIModel.getBabaObj());
nearKeyObject(BabaIsYouGameGUIModel.getIsObj());
nearKeyObject(BabaIsYouGameGUIModel.getPassObj());
nearKeyObject(BabaIsYouGameGUIModel.getWinObj());
checkWinCondition();
}
/**
* Initializes a list of objects with coordinates
* @return list of objects
*/
private ArrayList<Point> generateObjLvl3(){
//ArrayList<Point> objs = new ArrayList<Point>();
ArrayList<Point> coordinates = new ArrayList<Point>();
coordinates.add(new Point(5,5));
coordinates.add(new Point(6,5));
coordinates.add(new Point(7,5));
coordinates.add(new Point(8,5));
coordinates.add(new Point(9,5));
coordinates.add(new Point(10,5));
coordinates.add(new Point(11,5));
coordinates.add(new Point(5,6));
coordinates.add(new Point(5,7));
coordinates.add(new Point(5,8));
coordinates.add(new Point(5,9));
coordinates.add(new Point(5,10));
coordinates.add(new Point(5,11));
coordinates.add(new Point(9,12));
for (int i=0; i< coordinates.size(); i++){
coordinates.get(i).x *= SQUARE_SIZE;
coordinates.get(i).y *= SQUARE_SIZE;
}
return coordinates;
}
/**
* Initializes a list of objects with coordinates
* @return list of objects
*/
private ArrayList<Point> generateObjLvl2(){
//ArrayList<Point> objs = new ArrayList<Point>();
ArrayList<Point> coordinates = new ArrayList<Point>();
coordinates.add(new Point(5,5));
coordinates.add(new Point(6,5));
coordinates.add(new Point(7,5));
coordinates.add(new Point(8,5));
coordinates.add(new Point(9,5));
coordinates.add(new Point(10,5));
coordinates.add(new Point(11,5));
for (int i=0; i< coordinates.size(); i++){
coordinates.get(i).x *= SQUARE_SIZE;
coordinates.get(i).y *= SQUARE_SIZE;
}
return coordinates;
}
/**
* Initializes a list of objects with coordinates
* @return list of objects
*/
private ArrayList<Point> generateObjLvl1(){
ArrayList<Point> objs = new ArrayList<Point>();
for (int i = 10; i< 21; i++) {
int x = (i) * SQUARE_SIZE;
int y = (i) * SQUARE_SIZE;
objs.add(new Point(x, y));
}
return objs;
}
private void generateKeyObjsLvl1(){
theModel.setBabaObj(new Point(100, 100));
theModel.setIsObj(new Point(80, 200));
theModel.setPassObj(new Point(200, 200));
theModel.setWinObj(new Point(400, 400));
}
//
// private void generateKeyObjsLvl1(){
// theModel.setBabaObj(new Point(2* SQUARE_SIZE,2* SQUARE_SIZE)) ;
// theModel.setIsObj(new Point(3* SQUARE_SIZE,3* SQUARE_SIZE)) ;
// theModel.setPassObj(new Point(4* SQUARE_SIZE,4* SQUARE_SIZE)) ;
// theModel.setWinObj(new Point(5* SQUARE_SIZE,5* SQUARE_SIZE)) ;
// }
//
// private void generateKeyObjsLvl2(){
// theModel.setBabaObj(new Point(2* SQUARE_SIZE,5* SQUARE_SIZE)) ;
// theModel.setIsObj(new Point(4* SQUARE_SIZE,9* SQUARE_SIZE)) ;
// theModel.setPassObj(new Point(8* SQUARE_SIZE,3* SQUARE_SIZE)) ;
// theModel.setWinObj(new Point(12* SQUARE_SIZE,10* SQUARE_SIZE)) ;
// }
//
// private void generateKeyObjsLvl3(){
// theModel.setBabaObj(new Point(10* SQUARE_SIZE,4* SQUARE_SIZE)) ;
// theModel.setIsObj(new Point(12* SQUARE_SIZE,9* SQUARE_SIZE)) ;
// theModel.setPassObj(new Point(15* SQUARE_SIZE,8* SQUARE_SIZE)) ;
// theModel.setWinObj(new Point(2* SQUARE_SIZE,19* SQUARE_SIZE)) ;
// }
private void keyboardInput() {
BabaIsYouGameGUIMain.getScene().setOnKeyPressed(event -> {
code = event.getCode();
if (code == KeyCode.RIGHT){
theModel.getCharacter().x += moveRight;
} else if (code == KeyCode.LEFT){
theModel.getCharacter().x -= moveLeft;
} else if (code == KeyCode.UP){
theModel.getCharacter().y -= moveUp;
} else if (code == KeyCode.DOWN){
theModel.getCharacter().y+= moveDown;
} else if (code == KeyCode.ESCAPE){
goToInGameMenu();
}
});
}
private void checkWinCondition(){
int checker = theModel.getCurrentLvl();
if (theModel.isNextLvl() && (theModel.getCurrentLvl() == checker))
theModel.goToNextLvl();
}
/**
* Check the interaction and the current state of the character and the normal objects
*/
private void checkInteraction(){
if (!theModel.isPassable()) {
if (theModel.nearObjectLeft())
moveRight = 0;
else {
moveRight = 1;
}
if (theModel.nearObjectRight())
moveLeft = 0;
else {
moveLeft = 1;
}
if (theModel.nearObjectDown())
moveUp = 0;
else {
moveUp = 1;
}
if (theModel.nearObjectUp())
moveDown = 0;
else {
moveDown = 1;
}
}
}
/**
* Go to in-game menu by calling In-Game Menu methods
*/
private void goToInGameMenu(){
InGameMenu.generateInGameMenu();
InGameMenu.generateHint();
InGameMenu.generateCustomizeTheme();
BabaIsYouGameGUIMain.getStage().setScene(InGameMenu.getMenuScene());
}
/**
* colors the background
*/
private void colorBackground(){
theView.getGc().setFill(Color.BLACK);
theView.getGc().fillRect(0, 0, 800,800);
}
/**
* colors the character
* @param color - The desired color
*/
private void colorCharacter(Color color){
theView.getGc().setFill(color);
theView.getGc().fillRect(theModel.getCharacter().getX()*SQUARE_SIZE, theModel.getCharacter().getY()*SQUARE_SIZE, SQUARE_SIZE-1, SQUARE_SIZE-1);
}
/**
* Colors the objects based on its coordinate
*/
private void drawObj(){
for (Point obj: theModel.getObjs()) {
theView.getGc().setFill(Color.ORANGE);
theView.getGc().fillRect(obj.getX(), obj.getY(), SQUARE_SIZE - 1, SQUARE_SIZE - 1);
}
}
private void drawPassObj(){
theView.getGc().setFill(Color.RED);
theView.getGc().fillRect(BabaIsYouGameGUIModel.getPassObj().getX(), BabaIsYouGameGUIModel.getPassObj().getY(), SQUARE_SIZE - 1, SQUARE_SIZE - 1);
}
private void drawIsObj(){
theView.getGc().setFill(Color.BLUE);
theView.getGc().fillRect(BabaIsYouGameGUIModel.getIsObj().getX(), BabaIsYouGameGUIModel.getIsObj().getY(), SQUARE_SIZE - 1, SQUARE_SIZE - 1);
}
private void drawBaBaObj(){
theView.getGc().setFill(Color.PURPLE);
theView.getGc().fillRect(BabaIsYouGameGUIModel.getBabaObj().getX(), BabaIsYouGameGUIModel.getBabaObj().getY(), SQUARE_SIZE - 1, SQUARE_SIZE - 1);
}
private void drawWinObj(){
theView.getGc().setFill(Color.PINK);
theView.getGc().fillRect(BabaIsYouGameGUIModel.getWinObj().getX(), BabaIsYouGameGUIModel.getWinObj().getY(), SQUARE_SIZE - 1, SQUARE_SIZE - 1);
}
/**
* Check if the character is near the key objects (baba-is-pass)
* @param keyObj baba-is-pass objects
*/
private void nearKeyObject(Point keyObj){
if (theModel.getCharacter().getX()*SQUARE_SIZE == (keyObj.getX()) && theModel.getCharacter().getY()*SQUARE_SIZE == keyObj.getY()) {
if (code == KeyCode.RIGHT) {
keyObj.x += SQUARE_SIZE;
}
else if (code == KeyCode.LEFT)
keyObj.x -= SQUARE_SIZE;
else if (code == KeyCode.UP)
keyObj.y -= SQUARE_SIZE;
else if (code == KeyCode.DOWN)
keyObj.y+= SQUARE_SIZE;
}
}
}
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.awt.*;
import java.util.ArrayList;
/**
* The BabaIsYouGameGUI, with the game board, character and objects
*
* Source of reference:
* @see
* <a href=" https://github.com/mahmoudhamwi/Snake/blob/main/Snake/src/sample/Main.java">
* https://github.com/mahmoudhamwi/Snake/blob/main/Snake/src/sample/Main.java</a>
*/
public class BabaIsYouGameGUIMain extends Application {
private static Stage stage;
/** The in-game scene */
private static Scene scene;
/** Setting up theModel, theView and theController */
private BabaIsYouGameGUIModel theModel;
private BabaIsYouGameGUIView theView;
private BabaIsYouGameGUIController theController;
public static void main(String[] args) {
launch(args);
}
/**
* Initializing the model and the view
* @throws Exception
*/
@Override
public void init() throws Exception{
super.init();
this.theModel = new BabaIsYouGameGUIModel();
this.theView = new BabaIsYouGameGUIView(theModel);
}
@Override
public void start(Stage primaryStage) {
this.theController = new BabaIsYouGameGUIController(theView, theModel);
//The stage
stage = primaryStage;
scene = new Scene(theView.getRoot());
//Setting the scene
stage.setScene(scene);
stage.setTitle("Purple-Blue-RedPass-PinkWin");
stage.show();
}
public static Stage getStage() {
return stage;
}
public static Scene getScene() {
return scene;
}
}
/* *****************************************
* CSCI205 - Software Engineering and Design
* Spring 2021
* Instructor: Prof. Chris Dancy
*
* Name: Nguyen Nguyen
* Section: Section 02 - 1:50-2:42am
* Date: 5/12/2021
* Time: 6:43 PM
*
* Project: CSCI205SP21finalproject
* Package: PACKAGE_NAME
* Class: BabaIsYouGameGUIModel
*
* Description: THIS IS A DESCRIPTION Y’ALL AND I NEED TO CHANGE THIS!
*
* ****************************************
*/
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import java.awt.*;
import java.util.ArrayList;
public class BabaIsYouGameGUIModel {
/** The width of the board */
private static final int WIDTH = 800;
/** The height of the board */
private static final int HEIGHT = 800;
/** The number of rows of the board */
private static final int ROWS = 40;
/** The number of columns of the board */
private static final int COLUMNS = 40;
/** The size of a square */
private static final int SQUARE_SIZE = WIDTH/ROWS;
/** The character as a Point object */
private Point character = new Point(0,0);
/** The objects */
private Point obj;
private int currentLvl = 1;
/** Key objects */
private static Point babaObj = new Point(2* SQUARE_SIZE,2* SQUARE_SIZE);
private static Point isObj = new Point(3* SQUARE_SIZE,3* SQUARE_SIZE);
private static Point passObj = new Point(4* SQUARE_SIZE,4* SQUARE_SIZE);
private static Point winObj = new Point(5 * SQUARE_SIZE, 5 * SQUARE_SIZE);
/** Character color */
protected static Color currentColor = defaultColor();
/** List of objects */
private ArrayList<Point> objs;
// private ArrayList<GeneralObject> entityList;
// private ArrayList<GeneralObject> nameWordList;
// private ArrayList<GeneralObject> connectorWordList = new ArrayList<GeneralObject>();
// private ArrayList<GeneralObject> ruleWordList = new ArrayList<GeneralObject>();
/** The in-game scene */
protected static Scene scene;
public BabaIsYouGameGUIModel(){
}
// private void generateObjsFromList(GeneralObject[] objList) {
// for (int i=0; i<objList.length; i++){
// if (objList[i].getType().equals("Entity"))
// entityList.add(objList[i]);
// else if (objList[i].getType().equals("NameWord"))
// nameWordList.add(objList[i]);
// else if (objList[i].getType().equals("RuleWord"))
// ruleWordList.add(objList[i]);
// else if (objList[i].getType().equals("ConnectorWord"))
// connectorWordList.add(objList[i]);
//
// }
// }
/**
* Check if the character is on the left a normal object
* @return true/false
*/
public boolean nearObjectLeft(){
for (Point obj: objs) {
if (character.getX()*SQUARE_SIZE == (obj.getX() - 1*SQUARE_SIZE) && character.getY()*SQUARE_SIZE == obj.getY()) {
return true;
}
}
return false;
}
/**
* Check if the character is on the right a normal object
* @return true/false
*/
public boolean nearObjectRight(){
for (Point obj: objs) {
if (character.getX()*SQUARE_SIZE == (obj.getX() + 1*SQUARE_SIZE) && character.getY()*SQUARE_SIZE == obj.getY())
return true;
}
return false;
}
/**
* Check if the character is above a normal object
* @return true/false
*/
public boolean nearObjectUp(){
for (Point obj: objs) {
if (character.getX()*SQUARE_SIZE == (obj.getX()) && character.getY()*SQUARE_SIZE == obj.getY() - 1*SQUARE_SIZE )
return true;
}
return false;
}
/**
* Check if the character is below a normal object
* @return true/false
*/
public boolean nearObjectDown(){
for (Point obj: objs) {
if (character.getX()*SQUARE_SIZE == (obj.getX()) && character.getY()*SQUARE_SIZE == obj.getY() + 1*SQUARE_SIZE )
return true;
}
return false;
}
/**
* Check if Baba-Is-Pass is viable. Return true if so.
* @return true if Baba-Is-Pass, false otherwise
*/
public boolean isPassable(){
if (babaObj.getX() == (isObj.getX()-SQUARE_SIZE) && (isObj.getX() == (passObj.getX()-SQUARE_SIZE))
&& (babaObj.getY() == (isObj.getY())) && (isObj.getY() == (passObj.getY()))){
return true;
}
return false;
}
/**
* Check if Baba-Is-Win is viable. Return true if so.
* @return true if Baba-Is-Pass, false otherwise
*/
public boolean isNextLvl(){
if (babaObj.getX() == (isObj.getX()-SQUARE_SIZE) && (isObj.getX() == (winObj.getX()-SQUARE_SIZE))