From 174c0cad2aa5d1aa45bc4c80375ce344e55dd6e9 Mon Sep 17 00:00:00 2001 From: SidaDan Date: Thu, 3 Oct 2019 10:24:07 +0200 Subject: [PATCH 1/2] Added to count yummys and displaying on Title as points --- src/ThreadsController.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ThreadsController.java b/src/ThreadsController.java index 3df2d3d..9a2e6ee 100644 --- a/src/ThreadsController.java +++ b/src/ThreadsController.java @@ -7,16 +7,19 @@ public class ThreadsController extends Thread { Tuple headSnakePos; int sizeSnake=3; long speed = 50; + int points = 0; + Window frame; public static int directionSnake ; ArrayList positions = new ArrayList(); Tuple foodPosition; //Constructor of ControlleurThread - ThreadsController(Tuple positionDepart){ + ThreadsController(Tuple positionDepart,Window parmFrame){ //Get all the threads Squares=Window.Grid; - + frame = parmFrame; + headSnakePos=new Tuple(positionDepart.x,positionDepart.y); directionSnake = 1; @@ -31,6 +34,7 @@ public class ThreadsController extends Thread { //Important part : public void run() { + while(true){ moveInterne(directionSnake); checkCollision(); @@ -61,7 +65,8 @@ private void checkCollision() { boolean eatingFood = posCritique.getX()==foodPosition.y && posCritique.getY()==foodPosition.x; if(eatingFood){ - System.out.println("Yummy!"); + frame.setpoints(++points); + System.out.println("Yummy!"); sizeSnake=sizeSnake+1; foodPosition = getValAleaNotInSnake(); From 9940c32fefe646865ba1ffa0e30afb43b122022c Mon Sep 17 00:00:00 2001 From: SidaDan Date: Thu, 3 Oct 2019 10:27:36 +0200 Subject: [PATCH 2/2] Implemented new method to show points in Title bar --- src/Window.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Window.java b/src/Window.java index 518fa9e..91cb95d 100644 --- a/src/Window.java +++ b/src/Window.java @@ -12,7 +12,7 @@ class Window extends JFrame{ public static int height = 20; public Window(){ - + this.setResizable(false); // Creates the arraylist that'll contain the threads Grid = new ArrayList>(); ArrayList data; @@ -40,7 +40,7 @@ public Window(){ // initial position of the snake Tuple position = new Tuple(10,10); // passing this value to the controller - ThreadsController c = new ThreadsController(position); + ThreadsController c = new ThreadsController(position,this); //Let's start the game now.. c.start(); @@ -52,6 +52,11 @@ public Window(){ //Tuple position2 = new Tuple(13,13); //ControlleurThreads c2 = new ControlleurThreads(position2); //c2.start(); - + } + + public void setpoints(int points) { + setTitle("Snake [Points: " + points + " ]"); + } + }