CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Nikesh
Level 15
Delhi
  • 17.01.2020
  • 929views
  • 12comments

Last two requirements not meeting

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

An object on the playing field can have one of two states: it can be alive (isAlive=true) or dead (isAlive=false).
For example, if the snake hits the edge of the playing field, it dies. The same thing happens to an apple when it is eaten by the snake. To store object state, create a boolean isAlive variable in the Apple and Snake classes. Modify the logic for drawing a snake so that dead snakes are red.

The snake can move in one of four directions. To indicate these directions, create a Direction enum with UP, RIGHT, DOWN, and LEFT fields.

And in the Snake class, create a direction field, which will store the current direction of movement. Let the snake initially move to the left. The snake's direction will be changed by the setDirection(Direction) method. Create it.

Requirements:
  • The Apple class must have a public boolean isAlive field that is initialized to true.
  • The Snake class must have a public boolean isAlive field that is initialized to true.
  • In the Snake class's draw(Game) method, replace all calls to the setCellValue(int, int, String) method with calls to the setCellValueEx(int, int, Color, String, Color, int) method using arguments (x, y, Color.NONE, HEAD_SIGN (or BODY_SIGN), <color of snake>, 75). If the snake is dead, its color must be Color.RED. Otherwise, any other color, for example, Color.BLACK.
  • There must be a public Direction enum with the following values: UP, RIGHT, DOWN, LEFT.
  • The Snake class must have a private Direction direction field that is initialized to Direction.LEFT when it is declared.
  • The Snake class must have a public setter for the direction field: setDirection(Direction), which assigns the passed value to the field.
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; public void initialize() { setScreenSize(WIDTH,HEIGHT); createGame(); } private void createGame() { snake = new Snake(WIDTH/2,HEIGHT/2); drawScene(); //Apple apple = new Apple(7,7); //apple.draw(this); } private void drawScene() { for(int i=0;i<WIDTH;i+=1) { for(int j=0;j<HEIGHT;j+=1) { setCellColor(i,j,Color.DARKSEAGREEN); } } snake.draw(this); } }
0
Comments (12)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Antali András
Level 17 , Budapest, Hungary
17 January 2020, 17:56
Hello, It is maybe just a lack of a semicolon in the Direction class. You have to put a semicolon after the list: public enum Direction { UP, RIGHT, DOWN, LEFT; } I hope this will help, the Snake class seems fine.
0
Guadalupe Gagnon
Level 37 , Tampa, United States
17 January 2020, 19:00
A semi-colon is not needed here
+1
Antali András
Level 17 , Budapest, Hungary
17 January 2020, 20:08
Yes, my mistake. I always use it - it does not seem to be a fault. All right.
0
Guadalupe Gagnon
Level 37 , Tampa, United States
17 January 2020, 15:58
Is the Snake class supposed to extend GameObject?
+1
Antali András
Level 17 , Budapest, Hungary
17 January 2020, 20:36
No, the Snake does not have to inherit any other Class. And I found a further problem with the draw method. It only draws 3 elements in a special location. But later, if the snake turns, and grows, this method will be unable to draw correctly.
0
Guadalupe Gagnon
Level 37 , Tampa, United States
17 January 2020, 21:14
The draw method may change in a later part to the big task. These big tasks are really hard to help people on because I don't know if the differences in code are because they are doing it wrong, or the task hasn't reached the part where it is "final".
0
Antali András
Level 17 , Budapest, Hungary
17 January 2020, 21:43
I have already finished this game, but I do not remember if it would have been a future task at this point. (Was my English correct? I seldom use English language outside of CodeGym...) Just in case, I think it would be better to correct this drawing part now. It could be harder to search for this bug later. Nikes, if you want, I will help you in this. But (back to your original issue) I cannot find any other problems but the failed inheritence we already talked about.
0
Guadalupe Gagnon
Level 37 , Tampa, United States
17 January 2020, 23:27
Your English is good, I wouldnt know that you never used it if you hadn't mentioned it.
0
Antali András
Level 17 , Budapest, Hungary
18 January 2020, 19:45
Good to read this from a native speaker, thank you.
0
Nikesh
Level 15 , Delhi, India
20 January 2020, 09:53
Hi Antali, As snake is also a game object like apple it must inherit GameObject. If I don't inherit it then class will never know the value of x and y in constructor class. But thanks for reply ppl.
0
Antali András
Level 17 , Budapest, Hungary
20 January 2020, 11:52
It is not an IS-A, but a HAS-A relationship. Your 7th row is right: private List<GameObject> snakeParts = new ArrayList<>(); Not the whole snake is a GameObject, but its parts, because a GameObject is only one (x, y) pair. The whole snake cannot be described with an (x, y) coordinate, just the parts of the snake. (Believe me, my game is finished and works. :) ) So I recommend you to delete the ("extends GameObject" part from the class head. And also delete the super(x, y); row from the Snake constructor.
0
Guadalupe Gagnon
Level 37 , Tampa, United States
20 January 2020, 14:15
I agree with Antali. I didn't find a part in this big task for this game that asks you to extend GameObject. The x and y variables in the constructor are specifically for the snakeParts list. The specific task that asks you to create the Snake class is part 5 and continues in part 6: https://codegym.cc/tasks/com.codegym.games.snake.part05 https://codegym.cc/tasks/com.codegym.games.snake.part06
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.