SlideShare a Scribd company logo
1 of 2
JAVA: need help with my code!
Need help updating the GUI part of my code to reflect the moves being made by the user. I was
able to set up the game succesfully, but it doesnt do anything when i press the arrow keys. It is
supposed to work like the game 2048. For each arrow key, I need to check if the move can be
made (board.canMove()), then perform the move (board.move(Direction)) and then add a
random tile after (board.addRandomTile()). All the methods mentioned for board work fine.
This is what I have so far:
This is what it looks like when i run it:
@Override public void start (Stage primaryStage) // Process Arguments and Initialize the Game
Board processArgs (getParameters).getRaw().toArray(new String])); // Create the pane that will
hold all of the visual objects pane = new GridPane(); pane.setAlignment (Pos.CENTER);
pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5); pane.setStyle("-fx-background-color: rgb
(187, 173, 16)"); // Set the spacing between the Tiles pane.setHgap (15); pane.setVgap (15);
Add your Code for the GUI Here* Text gameTitle = new Text(0, 0, "2048"); gameTitle.setFont
(Font.font ("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 40));
gameTitle.setFill(COLOR_WALUE_DARK); pane.getChi ldren).add(gameTitle); Text score
new Text(1, 4, "Score: " + board.getScore()); score.setFont (Font.font("Times New Roman",
FontWeight.BOLD, 30)); score.setFiLL(COLOR_VALUE_DARK); pane.add(score,3,); Scene
scene = new Scene(pane); primaryStage. setTit le("Gui 2048" ) ; primaryStage.setScene(scene);
primaryStage.show(); int []] myBoard board.getGrid); for (int row 0; row board.GR ID-SIZE;
row++){ for (int col = 0; col board.GRID-SIZE; col++){ myTileValue-myBoard [row] [col] if
(myTi levalue = 0) { Rectangle myTile new Rectangle(); myTile.setWidth(100);
myTile.setHeight (100); myTi le.setFill (COLOR EMPTY); pane.add(myTile, col, row-1);
scene.setOnKeyPressed (new myKeyHandler));
Solution
instead of handle overrode keypressed
switch(e.getcode()){
case KeyEvent.VK_UP:
//your code

More Related Content

Similar to JAVA- need help with my code! Need help updating the GUI part of my co.docx

ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdfObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
rajkumarm401
 
Hello!This is Java assignment applet.Can someone help me writing.pdf
Hello!This is Java assignment applet.Can someone help me writing.pdfHello!This is Java assignment applet.Can someone help me writing.pdf
Hello!This is Java assignment applet.Can someone help me writing.pdf
jyothimuppasani1
 
Need help writing the code for a basic java tic tac toe game Tic.pdf
Need help writing the code for a basic java tic tac toe game Tic.pdfNeed help writing the code for a basic java tic tac toe game Tic.pdf
Need help writing the code for a basic java tic tac toe game Tic.pdf
hainesburchett26321
 
Need to make a ReversiOthello Board game in JAVAThe board size ca.pdf
Need to make a ReversiOthello Board game in JAVAThe board size ca.pdfNeed to make a ReversiOthello Board game in JAVAThe board size ca.pdf
Need to make a ReversiOthello Board game in JAVAThe board size ca.pdf
flashfashioncasualwe
 

Similar to JAVA- need help with my code! Need help updating the GUI part of my co.docx (20)

XNA coding series
XNA coding seriesXNA coding series
XNA coding series
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
 
School For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine BasicsSchool For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine Basics
 
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdfObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
 
Writing videogames with titanium appcelerator
Writing videogames with titanium appceleratorWriting videogames with titanium appcelerator
Writing videogames with titanium appcelerator
 
Hello!This is Java assignment applet.Can someone help me writing.pdf
Hello!This is Java assignment applet.Can someone help me writing.pdfHello!This is Java assignment applet.Can someone help me writing.pdf
Hello!This is Java assignment applet.Can someone help me writing.pdf
 
Academy PRO: React native - navigation
Academy PRO: React native - navigationAcademy PRO: React native - navigation
Academy PRO: React native - navigation
 
Processing and Processing.js
Processing and Processing.jsProcessing and Processing.js
Processing and Processing.js
 
Unity introduction for programmers
Unity introduction for programmersUnity introduction for programmers
Unity introduction for programmers
 
#Julialang and Computer Shogi (Japanese Chess)
#Julialang and Computer Shogi (Japanese Chess)#Julialang and Computer Shogi (Japanese Chess)
#Julialang and Computer Shogi (Japanese Chess)
 
Unity workshop
Unity workshopUnity workshop
Unity workshop
 
Shoes
ShoesShoes
Shoes
 
CreateJS
CreateJSCreateJS
CreateJS
 
WP7 HUB_XNA
WP7 HUB_XNAWP7 HUB_XNA
WP7 HUB_XNA
 
Programming simple games with a raspberry pi and
Programming simple games with a raspberry pi andProgramming simple games with a raspberry pi and
Programming simple games with a raspberry pi and
 
Need help writing the code for a basic java tic tac toe game Tic.pdf
Need help writing the code for a basic java tic tac toe game Tic.pdfNeed help writing the code for a basic java tic tac toe game Tic.pdf
Need help writing the code for a basic java tic tac toe game Tic.pdf
 
Денис Ковалев «Python в игровой индустрии»
Денис Ковалев «Python в игровой индустрии»Денис Ковалев «Python в игровой индустрии»
Денис Ковалев «Python в игровой индустрии»
 
The Ring programming language version 1.7 book - Part 53 of 196
The Ring programming language version 1.7 book - Part 53 of 196The Ring programming language version 1.7 book - Part 53 of 196
The Ring programming language version 1.7 book - Part 53 of 196
 
Need to make a ReversiOthello Board game in JAVAThe board size ca.pdf
Need to make a ReversiOthello Board game in JAVAThe board size ca.pdfNeed to make a ReversiOthello Board game in JAVAThe board size ca.pdf
Need to make a ReversiOthello Board game in JAVAThe board size ca.pdf
 
Enterprise Tic-Tac-Toe
Enterprise Tic-Tac-ToeEnterprise Tic-Tac-Toe
Enterprise Tic-Tac-Toe
 

More from olsenlinnea427

Assignment- Peg Game Create a web page that allows the user to play th.docx
Assignment- Peg Game Create a web page that allows the user to play th.docxAssignment- Peg Game Create a web page that allows the user to play th.docx
Assignment- Peg Game Create a web page that allows the user to play th.docx
olsenlinnea427
 
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docxAssignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
olsenlinnea427
 

More from olsenlinnea427 (20)

Assume that X is a discrete random variable that can take on the follo.docx
Assume that X is a discrete random variable that can take on the follo.docxAssume that X is a discrete random variable that can take on the follo.docx
Assume that X is a discrete random variable that can take on the follo.docx
 
Assume that in an economy- people hold $1000 of currency and $4000 of.docx
Assume that in an economy- people hold $1000 of currency and $4000 of.docxAssume that in an economy- people hold $1000 of currency and $4000 of.docx
Assume that in an economy- people hold $1000 of currency and $4000 of.docx
 
Assume a Poisson distribution with -4-5- Find the following probabilit.docx
Assume a Poisson distribution with -4-5- Find the following probabilit.docxAssume a Poisson distribution with -4-5- Find the following probabilit.docx
Assume a Poisson distribution with -4-5- Find the following probabilit.docx
 
Assume a member is selected at random trom the population represented.docx
Assume a member is selected at random trom the population represented.docxAssume a member is selected at random trom the population represented.docx
Assume a member is selected at random trom the population represented.docx
 
Assume a list has the following element- write a function to interch.docx
Assume a list has the following element-   write a function to interch.docxAssume a list has the following element-   write a function to interch.docx
Assume a list has the following element- write a function to interch.docx
 
Assume a company had no jobs in progress at the beginning of July and.docx
Assume a company had no jobs in progress at the beginning of July and.docxAssume a company had no jobs in progress at the beginning of July and.docx
Assume a company had no jobs in progress at the beginning of July and.docx
 
Assignment- Peg Game Create a web page that allows the user to play th.docx
Assignment- Peg Game Create a web page that allows the user to play th.docxAssignment- Peg Game Create a web page that allows the user to play th.docx
Assignment- Peg Game Create a web page that allows the user to play th.docx
 
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docxAssignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
 
As part of measures for containing the Covid-19 pandemic- vaccination.docx
As part of measures for containing the Covid-19 pandemic- vaccination.docxAs part of measures for containing the Covid-19 pandemic- vaccination.docx
As part of measures for containing the Covid-19 pandemic- vaccination.docx
 
As hematopoietic stem cells (HSC) mature and become differentiated the.docx
As hematopoietic stem cells (HSC) mature and become differentiated the.docxAs hematopoietic stem cells (HSC) mature and become differentiated the.docx
As hematopoietic stem cells (HSC) mature and become differentiated the.docx
 
As human populations & ecological footprints approach earth's carrying.docx
As human populations & ecological footprints approach earth's carrying.docxAs human populations & ecological footprints approach earth's carrying.docx
As human populations & ecological footprints approach earth's carrying.docx
 
Aries' description of an attitude toward death he called -tame death-.docx
Aries' description of an attitude toward death he called -tame death-.docxAries' description of an attitude toward death he called -tame death-.docx
Aries' description of an attitude toward death he called -tame death-.docx
 
Apple Inc- received $350-000 as dividends last year from its investmen.docx
Apple Inc- received $350-000 as dividends last year from its investmen.docxApple Inc- received $350-000 as dividends last year from its investmen.docx
Apple Inc- received $350-000 as dividends last year from its investmen.docx
 
Anthony has been living in Western WA for 3 years- After moving from C.docx
Anthony has been living in Western WA for 3 years- After moving from C.docxAnthony has been living in Western WA for 3 years- After moving from C.docx
Anthony has been living in Western WA for 3 years- After moving from C.docx
 
answer the questions in Blockchain list 5 point for all of the followi.docx
answer the questions in Blockchain list 5 point for all of the followi.docxanswer the questions in Blockchain list 5 point for all of the followi.docx
answer the questions in Blockchain list 5 point for all of the followi.docx
 
Answer the following questions- Question 1- You have implemented a sec.docx
Answer the following questions- Question 1- You have implemented a sec.docxAnswer the following questions- Question 1- You have implemented a sec.docx
Answer the following questions- Question 1- You have implemented a sec.docx
 
Answer the following based on a Blockchain and Biometrics I- What t.docx
Answer the following based on a Blockchain and Biometrics    I- What t.docxAnswer the following based on a Blockchain and Biometrics    I- What t.docx
Answer the following based on a Blockchain and Biometrics I- What t.docx
 
Anglin Company- a manufacturing firred has supplicd the following info.docx
Anglin Company- a manufacturing firred has supplicd the following info.docxAnglin Company- a manufacturing firred has supplicd the following info.docx
Anglin Company- a manufacturing firred has supplicd the following info.docx
 
Angela Lopez owns and manages a consulting firm called Metrixi which b.docx
Angela Lopez owns and manages a consulting firm called Metrixi which b.docxAngela Lopez owns and manages a consulting firm called Metrixi which b.docx
Angela Lopez owns and manages a consulting firm called Metrixi which b.docx
 
Andrew finds that on his way to work his wait time for the bus is roug.docx
Andrew finds that on his way to work his wait time for the bus is roug.docxAndrew finds that on his way to work his wait time for the bus is roug.docx
Andrew finds that on his way to work his wait time for the bus is roug.docx
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Recently uploaded (20)

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

JAVA- need help with my code! Need help updating the GUI part of my co.docx

  • 1. JAVA: need help with my code! Need help updating the GUI part of my code to reflect the moves being made by the user. I was able to set up the game succesfully, but it doesnt do anything when i press the arrow keys. It is supposed to work like the game 2048. For each arrow key, I need to check if the move can be made (board.canMove()), then perform the move (board.move(Direction)) and then add a random tile after (board.addRandomTile()). All the methods mentioned for board work fine. This is what I have so far: This is what it looks like when i run it: @Override public void start (Stage primaryStage) // Process Arguments and Initialize the Game Board processArgs (getParameters).getRaw().toArray(new String])); // Create the pane that will hold all of the visual objects pane = new GridPane(); pane.setAlignment (Pos.CENTER); pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5); pane.setStyle("-fx-background-color: rgb (187, 173, 16)"); // Set the spacing between the Tiles pane.setHgap (15); pane.setVgap (15); Add your Code for the GUI Here* Text gameTitle = new Text(0, 0, "2048"); gameTitle.setFont (Font.font ("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 40)); gameTitle.setFill(COLOR_WALUE_DARK); pane.getChi ldren).add(gameTitle); Text score new Text(1, 4, "Score: " + board.getScore()); score.setFont (Font.font("Times New Roman", FontWeight.BOLD, 30)); score.setFiLL(COLOR_VALUE_DARK); pane.add(score,3,); Scene scene = new Scene(pane); primaryStage. setTit le("Gui 2048" ) ; primaryStage.setScene(scene); primaryStage.show(); int []] myBoard board.getGrid); for (int row 0; row board.GR ID-SIZE; row++){ for (int col = 0; col board.GRID-SIZE; col++){ myTileValue-myBoard [row] [col] if (myTi levalue = 0) { Rectangle myTile new Rectangle(); myTile.setWidth(100); myTile.setHeight (100); myTi le.setFill (COLOR EMPTY); pane.add(myTile, col, row-1); scene.setOnKeyPressed (new myKeyHandler)); Solution instead of handle overrode keypressed switch(e.getcode()){ case KeyEvent.VK_UP: