CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Sakka Mouid
Level 19
Hannover
  • 29.04.2022
  • 169views
  • 2comments

the exceution is correct . I don't know why the requirements are not met

Question about the task Snake (Part 11/20)
Games,  Level 0,  Lesson 1
Under discussion


Let's add the ability to control our snake. To handle key presses, override the Game parent class's onKeyPress(Key) method. Depending on which key is pressed, set the appropriate direction of snake movement.

Bear in mind that a snake cannot instantly turn 180 degrees.

Requirements:
  • The SnakeGame class must override the Game parent class's onKeyPress(Key) method.
  • If Key.LEFT is passed to the onKeyPress(Key) method, you need to call the setDirection(Direction) method on the snake with Direction.LEFT as the argument.
  • If Key.RIGHT is passed to the onKeyPress(Key) method, you need to call the setDirection(Direction) method on the snake with Direction.RIGHT as the argument.
  • If Key.UP is passed to the onKeyPress(Key) method, you need to call the setDirection(Direction) method on the snake with Direction.UP as the argument.
  • If Key.DOWN is passed to the onKeyPress(Key) method, you need to call the setDirection(Direction) method on the snake with Direction.DOWN as the argument.
  • The Snake class's setDirection(Direction) method must not change the direction of snake movement if the method parameter is the opposite of the current direction.
package com.codegym.games.snake; import com.codegym.engine.cell.*; public class SnakeGame extends Game { public static final int WIDTH = 15; public static final int HEIGHT = 15; private Snake snake ; private int turnDelay; @Override public void onKeyPress(Key key){ switch (key){ case LEFT ://my mistake is that i used == to compare two object , i should //use equals() instead ! if (this.snake.getDirection().equals(Direction.RIGHT)){ break; } snake.setDirection(Direction.LEFT); break; case RIGHT : if (this.snake.getDirection().equals(Direction.LEFT)){ break; } snake.setDirection(Direction.RIGHT); break; case UP: if (this.snake.getDirection().equals(Direction.DOWN)){ break; } snake.setDirection(Direction.UP); break; case DOWN: if (this.snake.getDirection().equals(Direction.UP)){ break; } snake.setDirection(Direction.DOWN); break; } } @Override public void initialize() { setScreenSize(WIDTH, HEIGHT); createGame(); } private void createGame(){ turnDelay=300; snake= new Snake(WIDTH/2,HEIGHT/2);//why should the snake be intialized //before drawing the Scene ? drawScene(); //Apple apple = new Apple(7,7); //apple.draw(this);//this tarje3 3al class mta3 apple (Apple) setTurnTimer(turnDelay); } private void drawScene(){ for (int i = 0; i < WIDTH; i++) { for (int j = 0; j < HEIGHT; j++) { //setCellColor(i, j, Color.GRAY);replaced setCellValueEx(i,j,Color.GRAY , "");//n7ot chaine vide fi kol cell?? } } snake.draw(this);//this here refers to the snake class(Snake) } //the requirements said : this method should be overriden but the compiler said //there is no such function in super class @Override public void onTurn(int step){ //Everything that should happen in the game during one turn is described here snake.move(); drawScene();// drawScene() after every move ? } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Lisa L
Level 47 , Nuremberg, Germany
29 April 2022, 16:59useful
from reading the requirements I'd say you need to call the setDirection(Direction) in any case... no matter if the current direction is the opposite of the key... 'If Key.LEFT is passed to the onKeyPress(Key) method, you need to call the setDirection(Direction) method on the snake with Direction.LEFT as the argument.' and then in the setDirection method you check if currentDirection is opposite of the passed direction and if so you return When you read the last requirement it becomes totally clear: 'The Snake class's setDirection(Direction) method must not change the direction of snake movement if the method parameter is the opposite of the current direction'
+2
Sakka Mouid
Level 19 , Hannover, Deutschland
29 April 2022, 21:47
i think it's logically to disallow the 180 degrees turn of the snake inside the OnKeyPress() instead of setDirection() , that´s why i got a correct execution . CodeGym validation system just wants it to be inside setDirection()
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.