Java项目:星际争霸游戏(java+swing+awt界面编程+IO输入输出流+socket+udp网络通信)
Posted qq_1334611189
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java项目:星际争霸游戏(java+swing+awt界面编程+IO输入输出流+socket+udp网络通信)相关的知识,希望对你有一定的参考价值。
功能简介:
星际争霸游戏项目,该项目实现了单人模式和多人合作模式,可记录游戏进度,新建游戏,载入历史记录等功能,多人模式下可以创建一个区,然后邀请玩家加入一起玩
游戏界面:
@SuppressWarnings( "unused", "serial" )
public class GameGUI extends JPanel
//卡片布局器
private CardLayout cardLayout = new CardLayout();
//进度条面板
private ProgressPanel progressPanel;
//菜单面板
private MainMenuPanel menuPanel;
//游戏面板
private GamePanel gamePanel;
//需要update
private Abstractpanel currentPanel;
//单人游戏面板
private SingleGamePanel singleGamePanel;
//多人能游戏面板
private MutilPlayerPanel mutilPlayerPanel;
NewGamePanel newGamePanel;
NetWorkManager netWorkManager;
GridMapRender mapRender;
JFrame frame;
public GameGUI(JFrame frame)
this.frame = frame;
setLayout(cardLayout);
getBorder();
frame.getContentPane().add(this);
setSize(frame.getWidth(), frame.getHeight());
//这里有个次序,最先加入的显示
currentPanel = menuPanel = new MainMenuPanel(this, "mainMenu");
progressPanel = new ProgressPanel(this, "progress");
gamePanel = new GamePanel(this, "game");
singleGamePanel = new SingleGamePanel(this,"singleGame");
mutilPlayerPanel = new MutilPlayerPanel(this,"mutilGame");
newGamePanel = new NewGamePanel(this,"newGame");
public void switchProgress(final int type,final List<Integer> types)
cardLayout.show(this, "progress");
currentPanel = progressPanel;
Thread t = new Thread()
public void run()
mapRender = ResourceManager.load(type,types);
mapRender.setNetWorkManager(netWorkManager);
;
t.start();
public void switchGame()
gamePanel.init();
cardLayout.show(this, "game");
currentPanel = gamePanel;
public void switchSingleGame()
cardLayout.show(this, "singleGame");
singleGamePanel.reset();
currentPanel = singleGamePanel;
public void switchMutilGame()
cardLayout.show(this, "mutilGame");
mutilPlayerPanel.reset();
currentPanel = mutilPlayerPanel;
public void switchMainMenu()
cardLayout.show(this, "mainMenu");
menuPanel.reset();
currentPanel = menuPanel;
public void switchNewGame()
cardLayout.show(this, "newGame");
newGamePanel.reset();
currentPanel = newGamePanel;
public void update(long elapsedTime)
if(currentPanel!=null)
currentPanel.update(elapsedTime);
游戏控制面板:
/**
* 游戏控制面板
*/
public class GamePanel extends Abstractpanel
GridMap gridmap;
GridMapRender gridMapRender;
Control control;
ConsolePanel controlPanel;
Robot robot;
public void init()
initControl();
gridmap = ResourceManager.resourceManager.getGridMap();
gridMapRender = gridmap.getTileMapRender();
gridMapRender.screenWidth = getWidth();
gridMapRender.screenHeight = getHeight();
controlPanel = new ConsolePanel(gridMapRender);
controlPanel.setLocation(getWidth() - controlPanel.getWidth(), 0);
add(controlPanel);
try
robot = new Robot();
catch (AWTException e)
e.printStackTrace();
private void initControl()
GameGUI gui = (GameGUI) this.getParent();
control = new Control(this, gui.frame);
control.addKeyPressListener(new KeyPressListener()
public void press(int keyCode)
if(keyCode==KeyEvent.VK_1)
gridMapRender.blur();
for(Tile tile:gridmap.getTiles())
boolean isScv = (tile.getType()==gridMapRender.getCurrentType())
&&(tile instanceof Scv);
if(isScv)
tile.focus();
gridMapRender.addFocusSprite((Sprite)tile);
else if(keyCode==KeyEvent.VK_2)
gridMapRender.blur();
for(Tile tile:gridmap.getTiles())
boolean isMarine = tile.getType()==gridMapRender.getCurrentType()
&&tile instanceof Marine;
if(isMarine)
tile.focus();
gridMapRender.addFocusSprite((Sprite)tile);
);
control.addDragListener(new DragListener()
public void drag(int x, int y, int dx, int dy)
gridMapRender.focus(x, y, dx, dy);
);
control.addLeftPressListener(new LeftPressListener()
public void press(int x, int y)
//gridMapRender.focus(x, y);
gridMapRender.operate(x, y);
);
control.addRightPressListener(new RightPressListener()
public void press(int x, int y)
gridMapRender.move(x, y);
);
control.addMoveListener(new MoveListener()
public void move(int x, int y)
int offsetX = gridMapRender.offsetX;
int screenX = getWidth()-GridMapRender.TILE_WIDTH;
if(x>screenX&&offsetX<GridMapRender.tileXToPx(107))
gridMapRender.offsetX=offsetX+GridMapRender.TILE_WIDTH;
robot.mouseMove(screenX, y);
++controlPanel.map_panel.x;
if(x<GridMapRender.TILE_WIDTH&&offsetX>0)
gridMapRender.offsetX=offsetX-GridMapRender.TILE_WIDTH;
robot.mouseMove(GridMapRender.TILE_WIDTH, y);
--controlPanel.map_panel.x;
int offsetY = gridMapRender.offsetY;
int screenY = getHeight()-GridMapRender.TILE_HEIGHT;
if(y>screenY&&offsetY<GridMapRender.tileXToPx(110))
gridMapRender.offsetY=offsetY+GridMapRender.TILE_HEIGHT;
robot.mouseMove(x, screenY);
++controlPanel.map_panel.y;
if(y<GridMapRender.TILE_HEIGHT&&offsetY>0)
gridMapRender.offsetY=offsetY-GridMapRender.TILE_WIDTH;
robot.mouseMove(x, GridMapRender.TILE_HEIGHT);
--controlPanel.map_panel.y;
gridMapRender.moveX = x;
gridMapRender.moveY = y;
);
public GamePanel(GameGUI parent, String name)
super(parent, name);
public void paintComponent(Graphics g)
gridMapRender.draw((Graphics2D)g);
control.drag(g);
public void update(long elapsedTime)
gridMapRender.update(elapsedTime);
以上是关于Java项目:星际争霸游戏(java+swing+awt界面编程+IO输入输出流+socket+udp网络通信)的主要内容,如果未能解决你的问题,请参考以下文章
java swing 飞机大战游戏 github 免费 开源 公开 源码