【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《积木游戏程序源代码》,欢迎阅读!
积木游戏程序源代码
一、HTML网页文件名:JMYX6.html
积木游戏JMYX6
二、源代码文件名:JMYX6.java import java.applet.*; import java.awt.*;
import java.awt.event.*;
public class JMYX6 extends Applet implements KeyListener,ActionListener { Label label=new Label("鼠标点击任一积木,然后用光标键移动它。"); Button b[ ]=new Button[40]; int x[ ]=new int[40]; int y[ ]=new int[40];
String s[ ]=new String[40];
public void init( ) { setLayout(null); add(label);
label.setBounds(0,0,250,30); int k1=70,k2=70,k3=70,k4=70; for(int i=0;i<40;i++)
{ b[i]=new Button(String.valueOf(i));
if(i%3==0) b[i].setBackground(Color.red); if(i%3==1) b[i].setBackground(Color.blue); if(i%3==2) b[i].setBackground(Color.yellow);
b[i].addKeyListener(this); //按钮获得键盘事件监视器 b[i].addActionListener(this); add(b[i]);
if(i%4==0) { b[i].setBounds(k1,40,30,30);k1=k1+31; } else if(i%4==1) { b[i].setBounds(k2,71,30,30);k2=k2+31; } else if(i%4==2) { b[i].setBounds(k3,102,30,30);k3=k3+31; } else if(i%4==3) { b[i].setBounds(k4,133,30,30);k4=k4+31; } }
for(int i=0;i<40;i++)
{ x[i]=b[i].getBounds( ).x;
y[i]=b[i].getBounds( ).y; //获得按钮左上角的x,y 坐标 } }
public void keyTyped(KeyEvent e) { }
public void keyPressed(KeyEvent e)
{ if(e.getKeyCode( )==KeyEvent.VK_UP) { for(int i=0;i<=39;i++)
{ if(s[i].equals(String.valueOf(i))) { y[i]=y[i]-2;
if(y[i]<=0) y[i]=0;
b[i].setLocation(x[i],y[i]); } } }
else if(e.getKeyCode( )==KeyEvent.VK_DOWN) { for(int i=0;i<=39;i++)
{ if(s[i].equals(String.valueOf(i))) { y[i]=y[i]+2;
if(y[i]>=300) y[i]=300; b[i].setLocation(x[i],y[i]); } } }
else if(e.getKeyCode( )==KeyEvent.VK_LEFT) { for(int i=0;i<=39;i++)
{ if(s[i].equals(String.valueOf(i))) { x[i]=x[i]-2;
if(x[i]<=0) x[i]=0;
b[i].setLocation(x[i],y[i]); } } }
else if(e.getKeyCode( )==KeyEvent.VK_RIGHT) { for(int i=0;i<=39;i++)
{ if(s[i].equals(String.valueOf(i))) { x[i]=x[i]+2;
if(x[i]>=300) x[i]=300; b[i].setLocation(x[i],y[i]); } } } }
public void keyReleased(KeyEvent e) { }
public void actionPerformed(ActionEvent e) { for(int i=0;i<40;i++)
{ if(e.getSource( )==b[i]) { for(int j=0;j<=39;j++) s[j]=b[i].getLabel( ); } } } }
本文来源:https://www.wddqxz.cn/6a8071fcfab069dc502201f1.html