

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Instructions for a three-part homework assignment where students build a game called space roundup. The game involves capturing aliens in a specific order and returning them to their planets. The document also introduces mvc programming and explains how to design the view and control for the game. Students are required to create a jframe, a main panel, and a panel for displaying aliens, as well as implement actionlisteners and keylisteners.
Typology: Assignments
1 / 3
This page cannot be seen from the preview
Don't miss anything!


For the next 3 homework assignments, we’ll be building a fully-working game. Each assignment will build upon the last. Because there will be a lot of code to write, we’ve provided you with not only a larger instruction file than normal, but all of the code snippets you’ll need for any of the mathematical operations in the assignment. This instructional file is divided up as follows:
In space roundup, you play the philanthropic pilot of a spaceship who has happened to find him/herself in the middle of a four-planet system. The indigenous creatures have escaped from their respective zoos, and it’s up to you to take them all back to their planets. The only catch is that you must drop off the Aliens in the order they arrive in your ship: If you catch one Krang, then a Tron, then another Krang, you’ll have to visit Cranion, Titanius, and then Cranion again, in that order. The game ends when you’ve dropped off all the aliens back to their homes.
MVC means “Model, View, Control,” and it’s the way that most Windowed applications are designed in Java. The Model is the class hierarchy that represents all the objects in the application. For instance, for a game like pong there might be classes to represent the paddle, ball, and court. For Space Roundup, the model consists of Aliens, Planets, the Ship, and the entire Universe (what you wrote for hw9). The idea is that this Model is completely independent of how it is to be displayed. If you wanted to write a text-based version of Space Roundup, you should be able to use the same Model. The View and Control are the two parts of the GUI, which you’ll be writing for hw10. The View refers to the layout of the window: what components it’s made of, where they’re placed. The Control refers to all the event handlers: it’s what ties the GUI to how it affects the model. For hw11, you’ll be implementing your own data structure to be used in the game.
After homework 9’s deadline has passed, we will make a solution to hw 9 available on the course website, upon which you can build your GUI for this assignment. Feel free to use your own hw9 solution to build upon. We’ve merely included a working solution in case you might’ve had problems with anything on the previous homework. Your grade for this homework will not be affected by the functionality of your past homework assignment. Since the solution will be posted as soon as the grace period is over, you will not be allowed to turn in late submissions for hw9.
Now we’re ready to talk about the actual View and Control design. What are the basic components of the GUI? We need the typical class that creates a JFrame and adds everything to it, a panel for the main part of the game (a star field, with all the Drawables in it), and also a panel for displaying what’s currently in the Ship’s holding tank. As far as ActionListening is concerned, you’ll need an ActionListener for a Timer that ticks every TIMER_TICK_INTERVAL seconds, and a KeyListener for controlling the ship’s flight. In particular, you need to make the arrow keys move the ship around, and space bar to stop the ship.
How do the View, Control, and Model interact with one another? Thanks to two of the methods we wrote in Universe, it’s pretty simple! Firstly, you need to have one instance of a Universe, that all of your GUI classes have a reference to. Every time the timer ticks, call that Universe’s update method, and it’ll handle the rest. The only time the GUI needs information from the Universe is inside all the various paintComponent methods you’ll be writing.
How do you know when the game is over, and what should you do to make it stop? The game is over when both the Universe’s ArrayList of aliens and the Ship’s queue of aliens have a size of zero. Check for this when the timer ticks. To make the game stop, simply stop the timer. You should also display a message to the user letting them know they won, using a JoptionPane.
Here’s a suggested design for the GUI (excluding all the inner class that’ll do the actionlistening). SpaceRoundup.java The basic class with a main method that creates a JFrame and adds a SpaceRoundupPanel to it. SpaceRoundupPanel.java SpaceRoundupPanel is the main panel that gets added to the JFrame. It uses a BorderLayout, with the JLabel displaying this ship’s current security at SOUTH, a SpacePanel in the CENTER, and an AlienDisplayPanel to the EAST. The easiest way to make KeyListening work is to add the KeyListener to this class, and call its setFocusable() method, passing in true. This class could also instantiate the Universe object that it will need to pass to all the other GUI classes (SpacePanel and AlienDisplayPanel). This class should also handle the Timer, since it will have access to the JLabel displaying this ship’s security (you should re-set its text to the appropriate value every time the timer ticks). SpacePanel.java SpacePanel.java will need a reference to the Universe, so it can call its allDrawables method. SpacePanel should have an overridden paintComponent method that draws a random field of stars. The field should be different each time the game is played, but