课程设计之坦克大战
程序员文章站
2022-04-11 12:52:53
...
- 运用知识:
TCP通信实现客户端对于服务端的服务通信 - 效果图
- 主要功能
3. 创建项目
3.1 创建基地类
public void draw( Graphics g )
{
g.drawImage( base, xPos - 12, yPos - 12, null );
}
public void paintComponent( Graphics g )
{
Graphics offScreenGraphics;
if ( offScreenImage == null )
{
offScreenImage = createImage( 640, 550 );
}
offScreenGraphics = offScreenImage.getGraphics();
myPaint( offScreenGraphics );
g.drawImage( offScreenImage, 0, 0, this );
}
- 2 坦克类
interval = (int) (Math.random() * 200);
direction = (int) (Math.random() * 4);
if ( type == args1 )
{
firePosibility = args2;
speed = args3;
textures = new Image[args4]; }
public void draw( Graphics g )
{
if ( flashing && gameModel.gameFlow % 10 > 4 )
g.drawImage( textures[textures.length - 4 + direction], xPos - size, yPos - size, null );
else
g.drawImage( textures[direction], xPos - size, yPos - size, null );
}
- 3河道、草坪类
public grass( int a, int b )
{
xPos = a;
yPos = b;
border = new Rectangle( 0, 0, 0, 0 );
}
public void draw( Graphics g )
{
g.setColor( new Color( 0, 225, 0 ) );
for ( int i = yPos - 11; i <= yPos + 12; i += 5 )
g.drawLine( xPos - 12, i, xPos + 12, i );
for ( int i = xPos - 11; i <= xPos + 12; i += 5 )
g.drawLine( i, yPos - 12, i, yPos + 12 );
g.setColor( new Color( 0, 128, 0 ) );
for ( int i = yPos - 10; i <= yPos + 12; i += 5 )
g.drawLine( xPos - 12, i, xPos + 12, i );
for ( int i = xPos - 10; i <= xPos + 12; i += 5 )
g.drawLine( i, yPos - 12, i, yPos + 12 );
}
- 4 墙与钢板类
generalBorder = new Rectangle(xPos - 12, yPos - 12, 25, 25);
border[0] = new Rectangle(xPos - 11, yPos - 11, 11, 11);
border[1] = new Rectangle(xPos + 1, yPos - 11, 11, 11);
border[2] = new Rectangle(xPos - 11, yPos + 1, 11, 11);
border[3] = new Rectangle(xPos + 1, yPos + 1, 11, 11);
public wall(int a, int b, int orientation, ServerModel gameModel){
xPos = a;
yPos = b;
this.gameModel = gameModel;
wall = gameModel.textures[70];
generalBorder = new Rectangle(xPos - 12, yPos - 12, 25, 25);}
3.5 服务端控制类
注:此处设置的攻击键为"s",上下左右移动
public class ServerControler{
public ServerView view;
public ServerModel model;
public int helpMessageCount = 1;
//一个玩家坦克的参考
public ServerControler(ServerView thisview, ServerModel thismodel){
view = thisview;
model = thismodel;
//操作发送消息按钮的动作
view.sendMessage.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(!model.gameStarted){
model.addMessage("还没有和别的玩家联上, 无法发送对话");
return;
}
if(!view.messageField.getText().equals("")){
model.addMessage("主机端玩家说:" + view.messageField.getText());
model.playerTypedMessage += "m" + view.messageField.getText() + ";";
view.messageField.setText("");
}else{
model.addMessage("对话内容不能为空");
}
}
}
);
//操作建立主机按钮的动作
view.createServer.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(!model.serverCreated)
model.t.start();
}
}
);
//操作暂停/继续按钮的动作
view.pauseAndResume.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
model.pausePressed = true;;
if(!model.gameOver && model.gameStarted){
if(!model.gamePaused){
model.gamePaused = true;
model.addMessage("主机端玩家暂停了游戏");
}else{
model.gamePaused = false;
model.addMessage("主机端玩家取消了暂停");
}
}
}
}
);
//操作帮助按钮的动作
view.help.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
model.addMessage("帮助: 按 s 键开火, 按键盘的方向键来控制坦克的移动");
model.addMessage("如果按键没有反应请 1. 关闭大写功能; 2. 用 tab键切换 ");
model.addMessage("到控制界面如果您在使用对话界面.");
}
}
);
//操作退出按钮的动作
view.exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
);
//操作输入按钮的动作
view.messageField.addKeyListener( new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(helpMessageCount > 0){
model.addMessage("提示:用\"tab\"键可以*切换于控制界面和对话界面");
model.addMessage("提示:按回车键可以直接发送您的对话");
helpMessageCount--;
}
if(e.getKeyCode()==e.VK_ENTER){
if(!view.messageField.getText().equals("")){
model.addMessage("主机端玩家说:" + view.messageField.getText());
model.playerTypedMessage += "m" + view.messageField.getText() + ";";
view.messageField.setText("");
}else{
model.addMessage("对话内容不能为空");
}
}
}
});
JPanel temp = view.mainPanel;
temp.addKeyListener( new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(model.P1 != null){
if(e.getKeyCode() == KeyEvent.VK_UP){
model.P1.moveUp = true;
model.P1.moveDown = false;
model.P1.moveLeft = false;
model.P1.moveRight = false;
}
if(e.getKeyCode() == KeyEvent.VK_DOWN ){
model.P1.moveDown = true;
model.P1.moveUp = false;
model.P1.moveLeft = false;
model.P1.moveRight = false;
}
if(e.getKeyCode() == KeyEvent.VK_LEFT ){
model.P1.moveLeft = true;
model.P1.moveUp = false;
model.P1.moveDown = false;
model.P1.moveRight = false;
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT ){
model.P1.moveLeft = false;
model.P1.moveUp = false;
model.P1.moveDown = false;
model.P1.moveRight = true;
}
if(e.getKeyChar() == 's')
model.P1.fire = true;
if(e.getKeyCode()==e.VK_ENTER){
if(!view.messageField.getText().equals("")){
model.addMessage("主机端玩家说:" + view.messageField.getText());
model.playerTypedMessage += "m" + view.messageField.getText() + ";";
view.messageField.setText("");
}
}
if(e.getKeyChar() == 'y' && model.gameOver && !model.serverVoteYes){
model.serverVoteYes = true;
model.addMessage("等待用户端玩家的回应...");
}
if(e.getKeyChar() == 'n' && model.gameOver)
model.serverVoteNo = true;
}
}
public void keyReleased(KeyEvent e){
if(model.P1 != null){
if(e.getKeyCode() == KeyEvent.VK_UP)
model.P1.moveUp = false;
if(e.getKeyCode() == KeyEvent.VK_DOWN )
model.P1.moveDown = false;
if(e.getKeyCode() == KeyEvent.VK_LEFT )
model.P1.moveLeft = false;
if(e.getKeyCode() == KeyEvent.VK_RIGHT )
model.P1.moveRight = false;
if(e.getKeyChar() == 's')
model.P1.fire = false;
}
}
}
);
}
}
上一篇: linux之如何显示世界地图