Está en la página 1de 10

Game Project

Name: Nicholas Stepanov


Ann Tung

Link: https://github.com/sfsu-csc-413-spring-2018/term-project-team-d.git

Project Introduction:

Create game program with java graphics and GUI. The first game is tank game which involves
two tanks with fire power to break down destructible wall and to wear down the opponent. The
second game is Galactic game which reuses components created from in the first game.

Summary of Technical work:

1. Develop game objects with Sprite and Animation classes to support game components.
2. Develop collision property for game objects not to cross each other and outside game
buffer.
3. Develop game buffer to store game objects for updates and repaints.
4. Develop game clock as an observable to run a thread with to notify game objects
observers for constant updates.
5. Develop game views to draw game buffer images per requirements.
6. Develop control listener to control tank action according to the keyboard input.
7. Develop game map with destructible and indestructible walls.
8. Develop network socket connection with main console input.
9. Develop control messages to be sending through network socket.
10. Develop network listener running a thread to take in control messages for tank actions.
11. Develop Galactic Game main program with background image and initial game objects
with upgrade condition.
12. Develop Galactic game view with buffer image.
13. Develop constant flying objects of rocket and its behavior with stations and asteroids.
14. Develop launch listener to control the landing behavior of the rocket.

Execution and development environment

Terminal shell with Github repository command including javac and java command for
compiling and execution. Locally edited in JDK NetBeans IDE 8.2.

Scope of work described

1. Develop game objects with Sprite and Animation, store the pitch frame and related image
to be painted for destructible and indestructible walls, tanks, bullets, and explosions.
2. Develop intersect methods to determine if an object got hit and needs to be disappear
while repaint.
3. Develop collision property for game objects not to cross each other and outside game
buffer.
4. Develop game buffer to store game objects for updates and repaints. It repaints the
background tile before repainting updated objects.
5. Develop game clock as an observable to run a thread with to notify game objects
observers for constant updates.
6. Develop game views to draw game buffer images per requirements: Two focused game
view to center around the tank in focus with health bar and life score, and one scaled
game view to scale down the entire buffer image into minimap at the lower center of the
game view.
7. Develop control listener to control tank action according to the keyboard input, ADWS
Space for tank one and JLIK Return for tank two.
8. Develop game map with destructible and indestructible walls. Indestructible walls
surround the map to prevent the tank from crossing outside game buffer.
9. Develop network socket connection with main console input, where one input argument
initiates the server connection command, two input arguments initiates the client
connection command, and others as well as no arguments initiates simple local tank
game.
10. Develop control messages extending control listener to store and construct control
message to be sent through network socket.
11. Develop network listener running a thread to take in control messages to be stored and
read for tank actions.
12. Develop terminate flag to end the game with a dialog box.
13. Develop Galactic Game main program with background image and initial game objects
with upgrade condition where more asteroids are added for game going in higher level.
14. Develop Galactic game view which takes in game buffer frame for buffer image.
15. Develop constant flying objects of rocket and its behavior with stations and asteroids.
When intersecting with the station it can deliver through space bar and the station would
disappear. When intersecting with the asteroid it will end the game.

Command line instructions to compile and execute

javac tank_network/NetworkMain.java
java tank_network/NetworkMain (simple tank game on local computer)
java tank_network/NetworkMain 9191 (network tank game server start)
java tank_network/NetworkMain IP_Address 9191 (network tank game client join)

javac mail/GalacticMailMain.java
java mail/GalacticMainMain

Assumption made

For the network tank game version the control message needs to act consistently to the same tank
on two end terminals so that the end users would see the same result when in the same game.

Class diagram with hierarchy


package common:

class Animation:
-animation_length: int
-animation_sprite: Sprite
-current_frame: BufferedImage
-frame_counter: int
-active: boolean
+setSprite(my_sprite: Sprite): void
-nextImage(): void
+update(): void
+getImage(): BufferedImage
+play(): void
+setFrame(frame: int)
+isActive(): boolean

class Explosion extends GameObject


-clip: Clip
+Explosion(b: Bullet)
+Explosion(x:int, y:int)
+update: void

class GameBuffer extends JPanel implements Observable, runnable


#frame: Dimension
+clock: GameClock
#background: BufferedImage
#thread: Thread
+imageBuffered: BufferedImage
+terminate: boolean
+upgrade: boolean
+remote: boolean
+level: int
+objects: ArrayList<GameObject>
#views: ArrayList<GameView>
#objectsReserve: ArrayList<GameObject>
+GameBuffer(objects: GameObject[], bg: BufferedImage)
+start(): synchronized void
+myFrame(): Dimension
+bufferFrame(): Dimension
+run(): void
+update(o: Observable, b: Object): void
+gameOver(g: Graphics): void
+paintComponent(g: Graphics): void
+addObject(object: GameObject): void
+addView(view GameView): void
+stopThread(): void

class GameClock extends Observable implements Runnable


#CLOCK_T: int
+run(): void

abstract class GameObject implements Observer


-x: int
-y: int
-pitch: int
#object_visual: Animation
#draw_graphics: Graphics
+is_active: boolean
+a: Rectangle
+myType: char
-offset: int
+GameObject(x: int, y:int, pitch: int, animation:Animation)
+update(o: Observable, a:Object): void
+setGraphics(g: Graphics): void
+repaint(g: Graphics): void
+setX(x: int): void
+getX(): int
+setY(y: int): void
+getY(): int
+getPitch(): int
+setPitch(p: int): void
+setAnimationSprite(s: Sprite): void
+intersects(): boolean
+interact(): void

abstract class GameView extends JPanel implements ImageObserver, ImageProducer


#bOffscreen: GameBuffer
#imageOffscreen: Image
-c: ImageConsumer
-x: int
-y: int
+GameView(bOffscreen: GameBuffer)
+addConsumer(c: imageConsumer): synchronized void
+isConsumer(c: ImageConsumer): synchronized boolean
+removeConsumer(c: ImageConsumer): synchronized void
+startProduction(c: ImageConsumer): void
+requestTopDownLeftRightResend(c: ImageConsumer): void
+render(): synchronized void
+imageUpdate(image: Image, flags: int, x: int, y: int, w: int, h: int): Boolean
+setX(x: int): void
+getX(): int
+setY(y: int): void
+getY(): int

class Sprite
-tile_length: int
-tile_count: int
-SpriteImages: BufferedImage []
+Sprite(image: BufferedImage, tile_count: int)
+getTile(tile: int): BufferedImage
+getTileCount(): int
package mail:

class Asteroid extends GameObject


+asteroidTotal: int
+asteroidCount: int
+asteroidLevel: int
+b: GameBuffer
+Asteroid(x: int, y: int, p: int)
+update(o: Observable, a: Object): void
+upgrade(o: Observable): void

class GalacticMailMain
+drawView: GameView
+rocket: Rocket
+main(args[]: String): void

class GalacticMailView extends GameView


-rocket: Rocket
+GalacticMailView(bOffscreen: GameBuffer, rocket: Rocket)
+draw(g: Graphics): void

class LaunchListener implements KeyListener


#r: Rocket
+LaunchListener(r: Rocket)
+keyPressed(e: KeyEvent): void
+keyTyped(e: KeyEvent): void
+keyReleased(e: KeyEvent): void

class Rocket extends GameObjects


#control: char
#controls: HashMap<Character, Boolean>
+points: int
-state: boolean
-landedAnimation: Animation
-currStation: Station
-clip: Clip
-alive: boolean
+Rocket(x: int, y: int, pitch: int)
+update(o: Observable, a: Object): void
+intersects(): boolean
+repaint(g: Graphics): void

class Station extends GameObject


+baseTotal: int
+baseCount: int
b: GameBuffer
-clip: Clip
+Station(x: int, y: int, p: int)
+update(o: Observable, a: Object): void
+upgrade(o: Observable): void

package tank_network:

class Bullet extends GameObject


-clip: Clip
+Bullet(t: Tank)
+update(o: Observable, a: Object): void
+intersects(): boolean
+interact(): void

class ControlListener implements KeyListener


#t: Tank
#p1: boolean
#ctl: int
+ControlListener(t: Tank)
+keyPressed(e: KeyEvent): void
+setPlayer2(): ControlListener
+keyTyped(e: KeyEvent): void
+keyReleased(e: KeyEvent): void
+setControls(): void

class ControlMessage extends ControlListener


-message: BufferedWriter
+ControlMessage(t: Tank, port: Socket)
+keyPressed(e: keyEvent): void
+setPlayer2: ControlMessage
+keyReleased(e: KeyEvent): void

class DestructableTile extends GameObject


+DestructableTile(x: int, y: int)
+update(o: Observable, a: Object)
+interact(): void

class Explosion extends GameObject


-clip: Clip
+Explosion(b: Bullet)
+Explosion(x:int, y:int)
+update: void

class FocusedGameView extends GameView


-focus: Tank
+FocusedGameView(bOffscreen: GameBuffer, focus: Tank, ID: int)
+draw(g: Graphics): void

class IndestructableTile extends GameObject


+IndestructableTile(x: int, y: int)
+update(o: Observable, a: Object): void

class NetworkListener implements Runnable


-t: Tank
-p1: boolean
-oldMsg: int
-stream: BufferedReader
-thread: Thread
+NetworkListener(t: Tank, source: Socket)
+run(): void
+start(): synchronized void
+msgRead(msg: int): void
+setPlayer2(): NetworkListener

class NetworkMain
+PATH: int
+tank1: Tank
+tank2: Tank
+drawView: GameView
+main(args[]: String): void

class ScaledGameView extends GameView


+ScaledGameView(bOffscreen: GameBuffer)
+draw(g: Graphics): void

class Tank extends GameObject


#control: char
+controls: HashMap<Character, Boolean>
+health: int
+fire_counter: int
+life: int
-spawnX: int
-spawnT: int
+Tank(x: int, y: int, pitch: int)
+update(o: Observable, a: Object): void
+interact(): void

Implementation decisions

1. Instead of implementing game scene altogether as game objects inside game buffer, game
view is adapted as an observer for images created from game buffer.
2. It uses game clock as an observable constantly updating all the game objects.
3. In network tank game a control message is saved locally in order to clear tank control
message for communication purpose. It loses the benefit of constant movement from the
same keyboard status.

Code Organization

common/

GameBuffer: Holds all the game objects and views while running clock thread

GameClock: Observable. Provides updates to its observers at a constant rate

GameObject: Super class for items in both games

GameView: Used by GameBuffer to create an appropriate image for the game

Sprite: Breaks a Sprite into small images and gives access to them

Animation: Performs Sprite update

Explosion: Subclass of GameObject. Plays the Explosion Animation and deletes itself

mail/
GalacticMailMain: Instantiates GameObjects and adds them to GameBuffer. Instantiates
LaunchListener for the Rocket GameObject. Starts the thread inside the GameBuffer and
runs the GameClock.
GalacticMailView: Subclass of GameView. Displays the original GameBuffer image
with game points.
LaunchListener: Modifies Rocket GameObject based on keyboard input
Rocket: Subclass of GameObject. Updates location based on controls. Intersects with
Asteroid will end the game. Intersects with Station will station the rocket until launch.
Station: Subclass of GameObject. Disappears after the Rocket launches from the
station.
Asteroid: Constantly moves in a direction and bounces with Stations and other Asteroids

tank_network/
FocusedGameView: Subclass of GameView. Crops the image provided by GameBuffer
around specified Tank location.
ScaledGameView: Subclass of GameView. Scales down the entire image provided by
GameBuffer and show it at specified location.
ControlListener: Modifies Tank GameObject based on keyboard input
Tank: Subclass of GameObject. Updates location based on controls. Shoots bullet, and
decrease health index when shot, respawns upon death, and terminates when no life left.
IndestructableTile: Subclass of GameObject. It can’t be moved or destroyed.
DestructableTile: Subclass of GameObject. It can’t be moved but can be destroyed.
Bullet: Subclass of GameObject. Moves in a direction and creates Explosion when
hitting objects.
NetworkMain: Instantiates GameObjects and adds them to GameBuffer. Instantiates
ControlMesssage and NetworkListener for the Tank GameObject. Starts the thread inside
the GameBuffer and runs the GameClock.
ControlMessage: Subclass of ControlListener. Used to send control message over the
network to be recceived by NetworkListener.
NetworkListener: Modifies Tank GameObject based on the control message received
from ControlMessage through specified network socket.

Explosion: Subclass of GameObject. Plays the Explosion Animation and deletes itself

Results/Conclusion

The main issues encountered is how and when the observers update their status. In network tank
game, a control message might be updated multiple times while the other end updates different
times. The solution is to clear the control status so that only one action is performed based on
one control message. In Galactic mail game, different orders in updates might affect the
appearance of the view, say, the rocket might be painted over by the station. Furthermore, the
status would lose its update track count when it was marked as inactive to be deleted during the
object array updates. A solution to it is to remove the observer of the rocket and to read it back
into observer queue.

También podría gustarte