swing - Java Game General Design - Pong -
i'm making pong in java fun. finished tetris started off building game incorrectly, poor design followed throughout program limited things do.
this time around, wanted start off right. want to:
- create game following mvc model
- have sensible class organization , decoupling
- make proper use of encapsulation
- separate gui game rules
that being said, proposed class structure make proper use of classes?
main class: extends jframe responsible adding jpanels jframe , initalizing game
- adds gamepanel , scorepanel jframe
- sets sizes , locations, etc... (using frame.pack()...)
- initializes game-
public static void main(string[] args) {...
gamepanel class: extends jpanel: responsible knowing own boundaries , whether or not object breaking rules...
- paintcomponent() draws shapes (paddles , ball) screen
- has boundary , collision (ball collides paddle) rules
- keylisteners (not sure if in correct place)
- game timer sets beat of game
gamerules class: responsible getting data gamepanel (like, if ball has collided paddle), , calculating score , other info based on that...
- game scoring, game end rules, leveling
- angles when ball bounces (not sure if in correct place)
scorepanel class: gui responsible displaying score, level, , other relevant data player... data gamerules class.
- paintcomponent() draws scores , other data screen
paddle class: extends rectangle2d (awt) responsible knowing own (x,y) coordinates... having own color, setting new location
- getx(), gety()
- getcolor()
- setlocation(x, y)
ball class: extends ellipse2d (awt) responsible knowing own (x,y) coords... paddle
questions:
- unsure put math calculating angles ball should bounce at
- where should keylistener go?
- how use "game loop" instead of having game run through
paintcomponent()? - trying follow mvc model... achieve it?
thank you!
edit

unsure put math calculating angles ball should bounce at
gamerules class any. if math more elaborate, create gamemath class.
where should keylistener go?
my first guess gamepanel class.
how use "game loop" instead of having game run through paintcomponent()?
your gamepanel paintcomponent method responsible drawing current state of paddles , ball. helps if paddles , ball have own draw method, paintcomponent merely calls paddles , ball draw methods.
your game loop updates values in paddles , ball classes. can call paintcomponent in it's own loop, steady frame rate, , call paddles , ball position updates in different loop.
trying follow mvc model... achieve it?
java swing can have controller code in view, in form of anonymous action classes. said put drawing code in model classes.
while code isn't separated, functionality is. drawing code in model classes executed drawing jpanel. control code in view classes executed part of control action.
take @ article, sudoku solver swing gui, see how put moderately complicated swing gui. don't have examples of video game show you.
Comments
Post a Comment