1. 程式人生 > >java小遊戲基礎設計參考程式碼

java小遊戲基礎設計參考程式碼

{
        
this.setTitle("射擊遊戲");
        
this.getContentPane().setLayout(new BorderLayout());
        
        
this.getContentPane().add(panel1,"Center");
        
this.getContentPane().add(panel2,"South");
        
        imgBack1
=Toolkit.getDefaultToolkit().getImage("thread/background.png");
        imgBack2
=Toolkit.getDefaultToolkit().getImage("thread/background.png");
        imgHawk
=Toolkit.getDefaultToolkit().getImage("thread/hawk.png");
        imgBullet
=Toolkit.getDefaultToolkit().getImage("thread/bullet.png");
        
        mc.setSize(
400,320);
        panel1.add(mc);
        panel2.add(btnStart);
        panel2.add(btnStop);
        
        
this.setSize(400,300);
        
this.setResizable(false);
        
this.setLocationRelativeTo(null);
        
this.setVisible(true);
        
this.pack();        
        
        
//註冊監聽器
        
        
this.addWindowListener(new WindowAdapter(){
            
publicvoid windowClosing(WindowEvent e) 
{
                System.exit(
0);
            }

        }
);
        
        btnStart.addActionListener(
new ActionListener(){
            
publicvoid actionPerformed(ActionEvent e) {                
                started
=true;
                mc.requestFocus();
                start();            
            }
            
        }
);
        
        btnStop.addActionListener(
new ActionListener(){
            
publicvoid actionPerformed(ActionEvent e) {
                started
=false;                
            }
            
        }
);
        
        mc.addKeyListener(
new KeyAdapter(){
            
publicvoid keyPressed(KeyEvent e) {                
                
switch(e.getKeyCode()){
                    
case37//按左方向鍵
                        hawkX=hawkX-StepLength;
                        
break;
                    
case100://按數字小鍵盤區數字4鍵
                        hawkX=hawkX-StepLength;
                        
break;
                    
case38//按上方向鍵
                        hawkY=hawkY-StepLength;
                        
break;
                    
case104//按數字小鍵盤區數字8鍵
                        hawkY=hawkY-StepLength;
                        
break;
                    
case39://按右方向鍵
                        hawkX=hawkX+StepLength;
                        
break;
                    
case102://按數字小鍵盤區數字6鍵
                        hawkX=hawkX+StepLength;
                        
break;
                    
case40://按下方向鍵
                        hawkY=hawkY+StepLength;
                        
break;
                    
case98://按數字小鍵盤區數字2鍵
                        hawkY=hawkY+StepLength;
                        
break;
                    
case10://按回車鍵
                        fire=true;                        
                        
break;        
                    
case101://按數字小鍵盤區數字5鍵
                        fire=true;                        
                        
break;
                }

                    
                
if(hawkX<0)//碰左壁
                    hawkX=0;                    
                }

                
                
if(hawkX>=mc.getWidth()-hawkWidth)//碰右壁
                    hawkX=mc.getWidth()-hawkWidth;                    
                }

                
                
if(hawkY<0)//碰上壁
                    hawkY=0;                    
                }

                
                
if(hawkY>=mc.getHeight()-hawkHeight)//碰下壁
                    hawkY=mc.getHeight()-hawkHeight;                        
                }

                repaint();            
            }
                
        }
);        
    }