This repository was archived by the owner on Sep 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLeagueInvadersPt.2GameLoop.html
More file actions
1 lines (1 loc) · 6.88 KB
/
Copy pathLeagueInvadersPt.2GameLoop.html
File metadata and controls
1 lines (1 loc) · 6.88 KB
1
<html><head><meta content="text/html; charset=UTF-8" http-equiv="content-type"><style type="text/css">ol{margin:0;padding:0}table td,table th{padding:0}.c6{color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:14pt;font-family:"Arial";font-style:normal}.c3{color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:"Arial";font-style:normal}.c7{padding-top:0pt;padding-bottom:0pt;line-height:1.15;orphans:2;widows:2;text-align:center}.c4{font-weight:400;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:"Arial";font-style:normal}.c1{padding-top:0pt;padding-bottom:0pt;line-height:1.15;orphans:2;widows:2;text-align:left}.c11{background-color:#ffffff;max-width:468pt;padding:72pt 72pt 72pt 72pt}.c10{font-weight:700}.c0{color:#0000ff}.c9{color:#ff0000}.c8{color:#980000}.c5{text-indent:36pt}.c2{height:11pt}.title{padding-top:0pt;color:#000000;font-size:26pt;padding-bottom:3pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}.subtitle{padding-top:0pt;color:#666666;font-size:15pt;padding-bottom:16pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}li{color:#000000;font-size:11pt;font-family:"Arial"}p{margin:0;color:#000000;font-size:11pt;font-family:"Arial"}h1{padding-top:20pt;color:#000000;font-size:20pt;padding-bottom:6pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h2{padding-top:18pt;color:#000000;font-size:16pt;padding-bottom:6pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h3{padding-top:16pt;color:#434343;font-size:14pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h4{padding-top:14pt;color:#666666;font-size:12pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h5{padding-top:12pt;color:#666666;font-size:11pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h6{padding-top:12pt;color:#666666;font-size:11pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;font-style:italic;orphans:2;widows:2;text-align:left}</style></head><body class="c11"><div><p class="c1 c2"><span class="c3"></span></p></div><p class="c7"><span class="c6">League Invader Pt.2</span></p><p class="c7"><span class="c6">Game Loop</span></p><p class="c7 c2"><span class="c6"></span></p><p class="c1"><span class="c3">1. Create a new class called GamePanel. This class will draw our game onto our window.</span></p><p class="c1 c2"><span class="c3"></span></p><p class="c1"><span>2. Add </span><span class="c9">extends JPanel </span><span class="c3">to the end of your class declaration (see below).</span></p><p class="c1"><span> </span><span class="c4 c0">public class GamePanel extends JPanel{ }</span></p><p class="c1 c2"><span class="c4 c0"></span></p><p class="c1"><span class="c3">This turns your GamePanel class into a JPanel. However, by creating our own class instead of using the JPanel class, we can customize the JPanel code to better suit our game.</span></p><p class="c1 c2"><span class="c3"></span></p><p class="c1"><span>3. Add a member variable of a Timer object. This is a new class that we haven't used yet. When importing the Timer code, make sure you import the package that reads </span><span class="c0">javax.swing.Timer. </span><span class="c3">This timer will control the frame rate (or speed) of our game.</span></p><p class="c1 c2"><span class="c3"></span></p><p class="c1"><span class="c3">4. The timer is going to use the ActionListener interface to make stuff happen when it expires. Make your class implement the ActionListener interface.</span></p><p class="c1 c2"><span class="c3"></span></p><p class="c1 c5"><span class="c4 c0">public class GamePanel extends JPanel implements ActionListener</span></p><p class="c1 c5"><span class="c4 c0">{ }</span></p><p class="c1 c5 c2"><span class="c4 c0"></span></p><p class="c1"><span class="c3">Add the unimplemented method. You should now have an ActionPerformed method in your class.</span></p><p class="c1 c2"><span class="c3"></span></p><p class="c1"><span class="c3">5. Inside the ActionPerformed method, add a line of code to print a message to the console.</span></p><p class="c1 c2"><span class="c3"></span></p><p class="c1"><span>6. In your constructor, initialize the timer object. The constructor of the timer object takes two parameters. The first parameter is an int for how fast your want the timer to repeat. This is in milliseconds so 1000 is equal to 1 second. We want our game to run at 60 frames per second. So in the first parameter, put </span><span class="c8">1000 / 60</span><span>. The second parameter takes a reference to an ActionListener. Since we implemented it in our class, we can put </span><span class="c8">this</span><span>.</span></p><p class="c1 c2"><span class="c3"></span></p><p class="c1"><span class="c3">7. Create a void method called startGame.</span></p><p class="c1 c2"><span class="c3"></span></p><p class="c1"><span class="c3">8. In the startGame method use your timer object to call the start method.</span></p><p class="c1"><span> </span><span class="c4 c0">timer.start();</span></p><p class="c1 c2"><span class="c4 c0"></span></p><p class="c1"><span class="c3">9. Go back to your LeagueInvaders class.</span></p><p class="c1 c2"><span class="c3"></span></p><p class="c1"><span class="c3">10. Create an object of the GamePanel class in your field variables. Initialize it in your constructor.</span></p><p class="c1 c2"><span class="c3"></span></p><p class="c1"><span class="c3">11. At the top of your setup method, add the GamePanel to your JFrame</span></p><p class="c1 c2"><span class="c3"></span></p><p class="c1"><span class="c3">12. Add these lines of code to resize your window and game panel:</span></p><p class="c1 c5"><span class="c4 c0">window.getContentPane().setPreferredSize(new Dimension(WIDTH, HEIGHT));</span></p><p class="c1"><span class="c0"> window.pack();</span></p><p class="c1"><span>Make sure to use </span><span class="c10">YOUR</span><span class="c3"> variables names. You will also need to import the Dimension class.</span></p><p class="c1"><span class="c3"> </span></p><p class="c1 c2"><span class="c3"></span></p><p class="c1"><span class="c3">13. At the bottom of your setup method, use your GamePanel object to call the startGame method.</span></p><p class="c1 c2"><span class="c0 c4"></span></p><p class="c1"><span class="c3">14. Run your program. Does your message show up repeatedly in the console?</span></p><p class="c1 c2"><span class="c3"></span></p></body></html>