Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import javax.swing.*; /** * * ========================================================================== * CODING INSTRUCTIONS! * ========================================================================== * * * * Task 0: * compile

import java.awt.*;

import java.awt.event.*;

import java.util.ArrayList;

import javax.swing.*;

/**

*

* ==========================================================================

* CODING INSTRUCTIONS!

* ==========================================================================

*

*

<code><pre></p> <p> *</p> <p> * Task 0:</p> <p> * compile and run SnakeGame as a Java Application.</p> <p> *</p> <p> * Task 1: (in class Snake)</p> <p> * create a class called Snake in a separate Java source file.</p> <p> * import java.awt.*;</p> <p> *</p> <p> * required fields (or instance variables):</p> <p> * int size, direction, bodyAdd;</p> <p> * Rectangle head;</p> <p> * ArrayList<rectangle>body;</p> <p> * ArrayList<integer>directionQ;</p> <p> * (note: look up Rectangle in the Java Documentation for more information)</p> <p> * write a default constructor for the Snake Object</p> <p> * size is 20 (the number of pixels for each Snake piece)</p> <p> * direction is 8 (8 is up, 2 down, 4 left, and 6 right.. look at numpad)</p> <p> * bodyAdd is 4 (4 body pieces will be initially added to the Snake</p> <p> * head is a Rectangle that is 20x20 and</p> <p> * is located in the approximate center of the window (i.e. coordinates (300,300))</p> <p> * remember to initialize body and directionQ to new ArrayLists</p> <p> * write method: public void draw(Graphics2D g)</p> <p> * that will render the head of Snake ..use g.draw(head)</p> <p> * and the body of Snake (use a for-loop)</p> <p> *</p> <p> * Task 1a: (in class SnakeGame)</p> <p> * uncomment the snake instance variable</p> <p> * in method initRound()</p> <p> * initialize variable snake</p> <p> * in method draw()</p> <p> * render snake by invoking its draw method</p> <p> * YOU SHOULD SEE snake APPEAR IN THE CENTER OF THE WINDOW!</p> <p> *</p> <p> *</p> <p> * Task 3: (in class Snake)</p> <p> * write method: public int getDirection() that returns the direction</p> <p> * write method: public Rectangle getHead() that returns the head</p> <p> * write method: public void grow() that adds 5 to variable bodyAdd</p> <p> * write method: public void setDirection(int d)</p> <p> * (note: d will be either 2,4,6, or 8)</p> <p> * add d to the end of directionQ</p> <p> * (note: it is ok to add an int value to an ArrayList<integer>)</p> <p> * write method: public void move()</p> <p> * if the directionQ is not empty,</p> <p> * remove the first element of directionQ and assign it to direction</p> <p> * add a copy of head to the body .. body.add(0,new Rectangle(head));</p> <p> * there are 4 exclusive cases for the value of direction:</p> <p> * .. direction is 4 (i.e. left)</p> <p> * subtract size from head.x (this will change its x position)</p> <p> * .. direction is 2 (i.e. down)</p> <p> * add size to head.y</p> <p> * .. etc.</p> <p> * </p> <p> * Task 3a: (in class SnakeGame)</p> <p> * uncomment the lines that invoke setDirection and move methods on snake</p> <p> * and complete method keyPressed()</p> <p> * YOU SHOULD BE ABLE TO SEE snake MOVE AND CONTROL IT WITH THE ARROW KEYS</p> <p> *</p> <p> * Task 3a (in class Snake)</p> <p> * continue in method: public void move()</p> <p> *</p> <p> * if bodyAdd is positive, simply decrement bodyAdd,</p> <p> * otherwise remove the last Rectangle in ArrayList body</p> <p> * YOU SHOULD BE ABLE TO SEE snake GROW!</p> <p> *</p> <p> * Task 4: (in class SnakeGame)</p> <p> * uncomment nibble field and initialize it</p> <p> * to a 20x20 Rectangle with random coordinates</p> <p> * (note: the coordinates should be multiples of 20 .. the snake size)</p> <p> * render nibble in method draw .. g.fill(nibble);</p> <p> * in method actionPerformed()</p> <p> * (use the intersects method of Rectangle nibble</p> <p> * to invoke the grow method of snake)</p> <p> * (i.e. if the snake head intersects the nibble)</p> <p> * also generate new random coordinates for nibble</p> <p> * YOU SHOULD BE ABLE TO PLAY THE SNAKE GAME AS AN INVINCIBLE snake!</p> <p> *</p> <p> * Task 5: (in class Snake)</p> <p> * write method: public boolean isDead()</p> <p> * returns true if head intersects any Rectangle in body or</p> <p> * head has passed any window boundaries</p> <p> * (example: head.x < 0 , head.y > 580, etc)</p> <p> * otherwise, returns false</p> <p> * </p> <p> * Task 5a: (in class SnakeGame)</p> <p> * in method actionPerformed()</p> <p> * write an if statement that stops the Timer clock if the snake isDead</p> <p> </p> </div> </section> <section class="answerHolder"> <div class="answerHolderHeader"> <div class="answer-heading"> <h2>Step by Step Solution</h2> </div> <div class="answerReviews"> <div class="starReview"> <div class="starIcon"> </div> <div class="starText"> </div> </div> </div> </div> <div class="answerSteps"> <p>There are <span>3</span> Steps involved in it</p> <div class="step"> <h3>Step: 1</h3> <img src="https://dsd5zvtm8ll6.cloudfront.net/includes/images/document_product_info/blur-text-image.webp" width="759" height="271" alt="blur-text-image" loading="lazy" decoding="async" fetchpriority="low"> <div class="step1Popup"> <h3>Get Instant Access to Expert-Tailored Solutions</h3> <p>See step-by-step solutions with expert insights and AI powered tools for academic success</p> <button class="view_solution_btn step1PopupButton">View Solution</button> </div> </div> <div class="step"> <h3 class="accordion">Step: 2</h3> <div class="panel"> <img src="https://dsd5zvtm8ll6.cloudfront.net/includes/images/document_product_info/blur-subtext-image.webp" width="975" height="120" alt="blur-text-image" loading="lazy" decoding="async" fetchpriority="low"> <button class="view_solution_btn stepPopupButton">Sign Up to view</button> </div> </div> <div class="step"> <h3 class="accordion">Step: 3</h3> <div class="panel"> <img src="https://dsd5zvtm8ll6.cloudfront.net/includes/images/document_product_info/blur-subtext-image.webp" width="975" height="120" alt="blur-text-image" loading="lazy" decoding="async" fetchpriority="low"> <button class="view_solution_btn stepPopupButton">Sign Up to view</button> </div> </div> </div> </section> </div> <div class="expertRight"> <section class="AIRedirect"> <div class="AIHolder"> <h2>Ace Your Homework with AI</h2> <p>Get the answers you need in no time with our AI-driven, step-by-step assistance</p> <a class="AILink" href="/ask_ai">Get Started</a> </div> </section> <section class="relatedBook"> <div class="bookHolder" > <div class="relatedBookHeading" > <h2>Recommended Textbook for</h2> <object class="freeTagImage" type="image/svg+xml" data="https://dsd5zvtm8ll6.cloudfront.net/includes/images/rewamp/document_product_info/free.svg" name="free-book-icon"></object> </div> <div class="bookMainInfo" > <div class="bookImage" > <a href="/textbooks/sql-database-programming-1st-edition-9781937842314"> <img src="https://dsd5zvtm8ll6.cloudfront.net/si.question.images/book_images/64ed965ac9c9e_47872.jpg" width="100" height="131" alt="SQL Database Programming" loading="lazy"> </a> </div> <div class="bookInfo" > <h3 class="bookTitle"> <a href="/textbooks/sql-database-programming-1st-edition-9781937842314"> SQL Database Programming </a> </h3> <div class="bookMetaInfo" > <p class="bookAuthor"> <b>Authors:</b> <span>Chris Fehily</span> </p> <p class="bookEdition"> 1st Edition </p> <p class="bookEdition"> 1937842312, 978-1937842314 </p> </div></div></div> <a href="/textbooks/computer-science-coffeescript-2447" class="viewMoreBooks">More Books</a> </div> </section> </div> </div> <section class="relatedQuestion"> <div class="relatedQuestionHolder"> <h4>Students also viewed these Databases questions</h4> <div class="relatedQuestionSliderHolder"> <div class="relatedQuestionCart "> <div class="relatedQuestionCartHeader"> <h3>Question</h3> <div class="relatedStarRating"> <span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star">&#9733;</span><span class="star">&#9733;</span> </div> </div> <a class="relatedQuestionText" href="/describe-the-types-of-inventories-used-by-manufacturers-and-merchandisers-129986" > Describe the types of inventories used by manufacturers and merchandisers. </a> <div class="relatedQuestionCartFooter"> <div class="relatedHistory"> <p> Answered: <span>1 week ago</span> </p> </div> </div> </div> <div class="relatedQuestionCart "> <div class="relatedQuestionCartHeader"> <h3>Question</h3> <div class="relatedStarRating"> <span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star">&#9733;</span><span class="star">&#9733;</span> </div> </div> <a class="relatedQuestionText" href="/study-help/business-statistics-in-practice/3-if-we-pick-three-respondents-at-random-whats-the-1964405" > =+3. If we pick three respondents at random, whats the probability that at least one preferred purple? </a> <div class="relatedQuestionCartFooter"> <div class="relatedHistory"> <p> Answered: <span>1 week ago</span> </p> </div> </div> </div> <div class="relatedQuestionCart "> <div class="relatedQuestionCartHeader"> <h3>Question</h3> <div class="relatedStarRating"> <span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star">&#9733;</span><span class="star">&#9733;</span> </div> </div> <a class="relatedQuestionText" href="/study-help/human-resources-management/what-does-john-jacak-in-the-hrm-closeup-say-are-2109250" > What does John Jacak in the HRM Close-up say are the most important aspects in a privacy policy? </a> <div class="relatedQuestionCartFooter"> <div class="relatedHistory"> <p> Answered: <span>1 week ago</span> </p> </div> </div> </div> <div class="relatedQuestionCart "> <div class="relatedQuestionCartHeader"> <h3>Question</h3> <div class="relatedStarRating"> <span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star">&#9733;</span><span class="star">&#9733;</span> </div> </div> <a class="relatedQuestionText" href="/kellys-collectibles-sells-nearly-half-its-merchandise-on-credit-during-130400" > Kelly's Collectibles sells nearly half its merchandise on credit. During the past four years, the following data were developed for credit sales and losses from uncollectible accounts: In 2010,... </a> <div class="relatedQuestionCartFooter"> <div class="relatedHistory"> <p> Answered: <span>1 week ago</span> </p> </div> </div> </div> <div class="relatedQuestionCart "> <div class="relatedQuestionCartHeader"> <h3>Question</h3> <div class="relatedStarRating"> <span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star">&#9733;</span><span class="star">&#9733;</span> </div> </div> <a class="relatedQuestionText" href="/study-help/questions/6-suppose-that-e-is-a-java-enumeration-declared-as-13193288" > 6. Suppose that E is a Java enumeration declared as follows: enum E.) where the enumeration constants are not shown. Also assume that a is an array of integers whose length is equal to the number of... </a> <div class="relatedQuestionCartFooter"> <div class="relatedHistory"> <p> Answered: <span>1 week ago</span> </p> </div> </div> </div> <div class="relatedQuestionCart "> <div class="relatedQuestionCartHeader"> <h3>Question</h3> <div class="relatedStarRating"> <span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star">&#9733;</span><span class="star">&#9733;</span> </div> </div> <a class="relatedQuestionText" href="/study-help/questions/mrs-johnson-is-considering-buying-a-property-for-2900000-with-9397266" > Mrs. Johnson is considering buying a property for $2,900,000 with estimated acquisition costs of $37,000. She plans to use $2,000,000 in debt financing which has loan costs of $40,000 and the annual... </a> <div class="relatedQuestionCartFooter"> <div class="relatedHistory"> <p> Answered: <span>1 week ago</span> </p> </div> </div> </div> <div class="relatedQuestionCart "> <div class="relatedQuestionCartHeader"> <h3>Question</h3> <div class="relatedStarRating"> <span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star">&#9733;</span><span class="star">&#9733;</span> </div> </div> <a class="relatedQuestionText" href="/study-help/questions/examine-the-summary-of-the-balance-of-payments-of-the-838101" > Examine the summary of the balance of payments of the United States for 2021. Compute and discuss the balance on the current account and its subaccounts. A Summary of the U.S. Balance of Payments for... </a> <div class="relatedQuestionCartFooter"> <div class="relatedHistory"> <p> Answered: <span>1 week ago</span> </p> </div> </div> </div> <div class="relatedQuestionCart "> <div class="relatedQuestionCartHeader"> <h3>Question</h3> <div class="relatedStarRating"> <span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star">&#9733;</span><span class="star">&#9733;</span> </div> </div> <a class="relatedQuestionText" href="/study-help/questions/2-share-what-interested-you-in-taking-this-course-and-838165" > 2) Share what interested you in taking this course and what you hope to get from taking the course. There have been college-wide discussions regarding concerns with grading in online courses.... </a> <div class="relatedQuestionCartFooter"> <div class="relatedHistory"> <p> Answered: <span>1 week ago</span> </p> </div> </div> </div> <div class="relatedQuestionCart "> <div class="relatedQuestionCartHeader"> <h3>Question</h3> <div class="relatedStarRating"> <span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star">&#9733;</span><span class="star">&#9733;</span> </div> </div> <a class="relatedQuestionText" href="/study-help/questions/victory-company-uses-weighted-average-process-costing-the-company-has-853511" > Victory Company uses weighted average process costing. The company has two production processes. Conversion cost is added evenly throughout each process. Direct materials are added at the beginning... </a> <div class="relatedQuestionCartFooter"> <div class="relatedHistory"> <p> Answered: <span>1 week ago</span> </p> </div> </div> </div> <div class="relatedQuestionCart "> <div class="relatedQuestionCartHeader"> <h3>Question</h3> <div class="relatedStarRating"> <span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star">&#9733;</span><span class="star">&#9733;</span> </div> </div> <a class="relatedQuestionText" href="/study-help/questions/cost-of-goods-manufactured-slapshot-company-makes-ice-hockey-sticks-853546" > Cost of Goods Manufactured Slapshot Company makes ice hockey sticks. During the month of June, the company purchased $127,000 of materials. Also during the month of June, Slapshot Company incurred... </a> <div class="relatedQuestionCartFooter"> <div class="relatedHistory"> <p> Answered: <span>1 week ago</span> </p> </div> </div> </div> <div class="relatedQuestionCart "> <div class="relatedQuestionCartHeader"> <h3>Question</h3> <div class="relatedStarRating"> <span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star">&#9733;</span><span class="star">&#9733;</span> </div> </div> <a class="relatedQuestionText" href="/study-help/questions/finally-21st-century-is-also-considering-project-z-project-z-854501" > Finally, 21st Century is also considering Project Z Project Z has an up-front after-tax cost of $500,000, and it is expected to produce after-tax cash flows of $100,000 at the end of each of the next... </a> <div class="relatedQuestionCartFooter"> <div class="relatedHistory"> <p> Answered: <span>1 week ago</span> </p> </div> </div> </div> <div class="relatedQuestionCart "> <div class="relatedQuestionCartHeader"> <h3>Question</h3> <div class="relatedStarRating"> <span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star">&#9733;</span><span class="star">&#9733;</span> </div> </div> <a class="relatedQuestionText" href="/study-help/managing-human-behavior-in-public/what-was-it-you-did-in-this-situation-that-caused-2125433" > What was it you did in this situation that caused others to follow you? </a> <div class="relatedQuestionCartFooter"> <div class="relatedHistory"> <p> Answered: <span>1 week ago</span> </p> </div> </div> </div> <div class="relatedQuestionCart "> <div class="relatedQuestionCartHeader"> <h3>Question</h3> <div class="relatedStarRating"> <span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star">&#9733;</span><span class="star">&#9733;</span> </div> </div> <a class="relatedQuestionText" href="/study-help/human-resource-information/4-considering-the-experience-gained-from-all-their-current-parks-2125430" > 4. Considering the experience gained from all their current parks as well as the external forces including increasing competition, market regulations, diversity of potential new markets, and... </a> <div class="relatedQuestionCartFooter"> <div class="relatedHistory"> <p> Answered: <span>1 week ago</span> </p> </div> </div> </div> <div class="relatedQuestionCart "> <div class="relatedQuestionCartHeader"> <h3>Question</h3> <div class="relatedStarRating"> <span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star active">&#9733;</span><span class="star">&#9733;</span><span class="star">&#9733;</span> </div> </div> <a class="relatedQuestionText" href="/study-help/managing-human-behavior-in-public/3-you-can-gain-power-by-making-others-feel-important-2125438" > 3. You can gain power by making others feel important. </a> <div class="relatedQuestionCartFooter"> <div class="relatedHistory"> <p> Answered: <span>1 week ago</span> </p> </div> </div> </div> </div> </div> </section> <hr class="expert-separator"> <div class="next-previous-button"> <div class="navigationButtons"> <a class="previousQuestionButton" href="/study-help/questions/the-first-modern-transaction-processing-system-was-multiple-choice-the-13193286">Previous Question</a> <a class="nextQuestionButton" href="/study-help/questions/6-suppose-that-e-is-a-java-enumeration-declared-as-13193288">Next Question</a> </div> </div> </div> <div class="promo items-center justify-center hidden" style="margin-left:-15px;"> <div class="app_promo"> <div class="app_promo_headline"> <img class="app_promo_icon" alt="Mobile App Logo" width="40" height="40" loading="lazy" src="includes/images/mobile/finalLogo.png"> <p class="app_promo_title font-sans items-center">View Answer in SolutionInn App</p> </div> <button class="app_promo_action redirection question_open_url='q_id=13193287&q_type=2'"> Download on the App Store </button> <button class="btn btn-default app_promo_dismiss"> Continue with the mobile website </button> </div> </div> </main> </div> <div class="blank-portion"></div> <footer> <div class="container footerHolder"> <div class="footerLinksFlex"> <div class="footerLinksCol col-md-3 col-lg-3 col-sm-6 col-6"> <p>Services</p> <ul> <li><a href="/site-map">Sitemap</a></li> <li><a href="/fun/">Fun</a></li> <li><a href="/study-help/definitions">Definitions</a></li> <li><a href="/tutors/become-a-tutor">Become Tutor</a></li> <li><a href="/study-help/categories">Study Help Categories</a></li> <li><a href="/study-help/latest-questions">Recent Questions</a></li> <li><a href="/study-help/questions-and-answers">Expert Questions</a></li> </ul> </div> <div class="footerLinksCol col-md-3 col-lg-3 col-sm-6 col-6"> <p>Company Info</p> <ul> <li><a href="/security">Security</a></li> <li><a href="/copyrights">Copyrights</a></li> <li><a href="/privacy">Privacy Policy</a></li> <li><a href="/conditions">Terms & Conditions</a></li> <li><a href="/solutioninn-fee">SolutionInn Fee</a></li> <li><a href="/scholarships">Scholarship</a></li> </ul> </div> <div class="footerLinksCol col-md-3 col-lg-3 col-sm-6 col-6"> <p>Get In Touch</p> <ul> <li><a href="/about-us">About Us</a></li> <li><a href="/support">Contact Us</a></li> <li><a href="/career">Career</a></li> <li><a href="/jobs">Jobs</a></li> <li><a href="/support">FAQ</a></li> <li><a href="/campus-ambassador-program">Campus Ambassador</a></li> </ul> </div> <div class="footerLinksCol col-md-3 col-lg-3 col-sm-6 col-12"> <p>Secure Payment</p> <div class="footerAppDownloadRow"> <div class="downloadLinkHolder"> <img src="https://dsd5zvtm8ll6.cloudfront.net/includes/images/rewamp/common/footer/secure_payment_method.png" class="img-fluid mb-3" width="243" height="28" alt="payment-verified-icon" loading="lazy"> </div> </div> <p>Download Our App</p> <div class="footerAppDownloadRow"> <div class="downloadLinkHolder mobileAppDownload col-md-6 col-lg-6 col-sm-6 col-6 redirection" data-id="1"> <img style="cursor:pointer;" src="https://dsd5zvtm8ll6.cloudfront.net/includes/images/rewamp/home_page/google-play-svg.svg" alt="SolutionInn - Study Help App for Android" width="116" height="40" class="img-fluid mb-3 " loading="lazy"> </div> <div class="downloadLinkHolder mobileAppDownload col-md-6 col-lg-6 col-sm-6 col-6 redirection" data-id="2"> <img style="cursor:pointer;" src="https://dsd5zvtm8ll6.cloudfront.net/includes/images/rewamp/home_page/apple-store-download-icon.svg" alt="SolutionInn - Study Help App for iOS" width="116" height="40" class="img-fluid mb-3" loading="lazy"> </div> </div> </div> </div> </div> <div class="footer-bottom"> <p>&copy; 2024 SolutionInn. All Rights Reserved</p> </div></footer> <script> window.addEventListener("load",function(){jQuery(document).ready(function(t){t.ajax({type:"POST",url:"/",data:{trackUserActivity:!0,reqUri:document.URL,referer:document.referrer},success:function(t){}})})},!1),window.addEventListener("load",function(){jQuery(document).ready(function(t){t.ajax({type:"POST",url:"/",data:{insertCrawler:!0,reqUri:document.URL,parseTime:"0.056",queryTime:"0.01654768548584",queryCount:"30"},success:function(t){}})})},!1),window.addEventListener("load",function(){jQuery(document).ready(function(){function t(t="",n=!1){var i="itms-apps://itunes.apple.com/app/id6462455425",e="openApp://action?"+t;isAndroid()?(setTimeout(function(){return window.location="market://details?id=com.solutioninn.studyhelp",!1},25),window.location=e):isIOS()?(setTimeout(function(){return window.location=i,!1},25),window.location=e):(i="https://apps.apple.com/in/app/id6462455425",n&&(i="https://play.google.com/store/apps/details?id=com.solutioninn.studyhelp"),window.open("about:blank","_blank").location.href=i)}jQuery("#appModal").modal("show"),jQuery(".download-app-btn").click(function(){t(jQuery(this).attr("question_open_url"))}),jQuery(".redirection").click(function(){var n=jQuery(this).attr("question_open_url"),i=jQuery(this).attr("data-id");void 0!=n?1==i?t(n,!0):t(n,!1):1==i?t("",!0):t("",!1)}),jQuery(".app-notification-close").click(function(){jQuery(".app-notification-section").css("visibility","hidden");var t=new FormData;t.append("hide_notification",!0),jQuery.ajax({type:"POST",url:"/",data:t,cache:!1,contentType:!1,processData:!1,beforeSend:function(){},success:function(t){location.reload()}})})})},!1); </script> </body> </html>