打地鼠的代码怎么写啊???

打地鼠的代码怎么写啊???,第1张

package com.lzw

import java.awt.EventQueue

import java.awt.event.MouseAdapter

import java.awt.event.MouseEvent

import javax.swing.ImageIcon

import javax.swing.JFrame

import javax.swing.JLabel

public class Shrewmouse extends JFrame implements Runnable {

private JLabel[] mouses //存放显示底数的标签数组

private ImageIcon imgMouse //地鼠图片对象

public static void main(String args[]){

EventQueue.invokeLater(new Runnable(){

public void run(){

try{

Shrewmouse frame=new Shrewmouse() //创建窗体

frame.setVisible(true)//显示窗体

new Thread(frame).start() //启动线程

}catch(Exception e){

e.printStackTrace()

}

}

})

}

public Shrewmouse(){

super()

this.setResizable(false) //禁止调整窗体大小

this.getContentPane().setLayout(null)//窗体不使用布局管理器

this.setTitle("简易的打地鼠游戏") //设置窗体标题

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

ImageIcon img=new ImageIcon(getClass().getResource("background.jpg"))

//初始化背景图片

imgMouse=new ImageIcon(getClass().getResource("mouse1.jpg"))

//初始化地鼠图片对象

mouses=new JLabel[6] //创建显示地鼠的标签数组

for(int i=0i<6i++){ //遍历数组

mouses[i]=new JLabel() //初始化每一个数组元素

mouses[i].setSize(imgMouse.getIconWidth(),imgMouse.getIconHeight())

//设置标签与地鼠图片相同大小

mouses[i].addMouseListener(new MouseAdapter(){//为标签添加鼠标事件监听适配器

/**

* 处理鼠标单击事件的方法

*/

@Override

public void mouseClicked(MouseEvent e){

Object source=e.getSource() //获取事件源,即地鼠标签

if(source instanceof JLabel){ //如果事件是标签组件

JLabel mouse=(JLabel)source //强制转换为JLabel标签

mouse.setIcon(null) //取消标签图标

}

}

})

this.getContentPane().add(mouses[i]) //添加显示地鼠的标签到窗体

}

mouses[0].setLocation(253, 300) //设置每个标签的位置

mouses[1].setLocation(333, 250)

mouses[2].setLocation(388, 296)

mouses[3].setLocation(362, 364)

mouses[4].setLocation(189, 353)

mouses[5].setLocation(240, 409)

final JLabel backLabel=new JLabel() //创建显示背景的标签

backLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight())

this.setBounds(100,100,img.getIconWidth(),img.getIconHeight())

backLabel.setIcon(img)//添加背景到标签

this.getContentPane().add(backLabel) //添加背景标签到窗体

}

/**

* 线程的核心方法

*/

public void run(){

while(true){//使用无限循环

try{

Thread.sleep(3000)//使线程休眠3秒

int index=(int)(Math.random()*6) //生成随机的地鼠索引

if(mouses[index].getIcon()==null){ //如果地鼠标签没有设置图片

mouses[index].setIcon(imgMouse) //为该标签添加地鼠图片

}

}catch(InterruptedException e){

e.printStackTrace()

}

}

}

}

 public void mouseClicked(MouseEvent e){\x0d\x0aObject source=e.getSource() //获取事件源,即地鼠标签\x0d\x0aif(source instanceof JLabel){ //如果事件是标签组件\x0d\x0aJLabel mouse=(JLabel)source //强制转换为JLabel标签\x0d\x0amouse.setIcon(null) //取消标签图标\x0d\x0a}\x0d\x0a}\x0d\x0a})\x0d\x0athis.getContentPane().add(mouses[i]) //添加显示地鼠的标签到窗体\x0d\x0a}\x0d\x0a\x0d\x0amouses[0].setLocation(253, 300) //设置每个标签的位置\x0d\x0amouses[1].setLocation(333, 250)\x0d\x0amouses[2].setLocation(388, 296)\x0d\x0amouses[3].setLocation(362, 364)\x0d\x0amouses[4].setLocation(189, 353)\x0d\x0amouses[5].setLocation(240, 409)\x0d\x0a\x0d\x0afinal JLabel backLabel=new JLabel() //创建显示背景的标签\x0d\x0abackLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight())\x0d\x0athis.setBounds(100,100,img.getIconWidth(),img.getIconHeight())\x0d\x0abackLabel.setIcon(img)//添加背景到标签\x0d\x0athis.getContentPane().add(backLabel) //添加背景标签到窗体\x0d\x0a}\x0d\x0a/**\x0d\x0a* 线程的核心方法\x0d\x0a*/\x0d\x0a\x0d\x0apublic void run(){\x0d\x0awhile(true){//使用无限循环\x0d\x0atry{\x0d\x0aThread.sleep(3000)//使线程休眠3秒\x0d\x0aint index=(int)(Math.random()*6) //生成随机的地鼠索引\x0d\x0aif(mouses[index].getIcon()==null){ //如果地鼠标签没有设置图片\x0d\x0amouses[index].setIcon(imgMouse) //为该标签添加地鼠图片\x0d\x0a}\x0d\x0a}catch(InterruptedException e){\x0d\x0ae.printStackTrace()\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a}

下面是源代码:

Form1:

Private Sub Form_Load() Dim temp As Integer Randomize

temp = Int(Rnd * 3) + 1

Form1.Picture = LoadPicture("C:\Users\Administrator\Desktop\打地鼠\picture" &temp &".jpg") WindowsMediaPlayer1.URL = "C:\Users\Administrator\Desktop\打地鼠\Heaven's Devils.wma" End Sub

Private Sub Label1_Click() Form2.Show Form2.num = -1 Form1.Hide

WindowsMediaPlayer1.URL = ""

Open "C:\Users\Administrator\Desktop\打地鼠\primary.dat" For Append As #1 '写 *** 作 Close #1

Open "C:\Users\Administrator\Desktop\打地鼠\intermediate.dat" For Append As #2 Close #2

Open "C:\Users\Administrator\Desktop\打地鼠\advanced.dat" For Append As #3 Close #3 End Sub

Private Sub Label2_Click() End End Sub

Form2:

Dim allnum As Integer, oknum As Integer '定义变化次数 打中次数 Public num As Integer '判别是否第一次开始游戏 Dim flags1 As Boolean '判别文件是否保存 Public flags As Boolean '判别是否播放声音 Private Sub countine_Click() pause.Enabled = True Timer1.Enabled = True countine.Enabled = False WLXZ.Enabled = True End Sub

Private Sub exit_Click() Dim X As Integer Dim tt As Integer

X = MsgBox("是否真的退出?", vbYesNo, "退出游戏框")

If X = 6 Then

tt = MsgBox("退出游戏之前,是否保存", vbYesNo, "保存提示") If tt = 6 Then

If flags1 = True Then EndElse

If Timer1.Interval = 1000 Then

Open "C:\Users\Administrator\Desktop\打地鼠\primary.dat" For Append As #1'写 *** 作

Print #1, Text1.Text + " " + Format(Date, "M/d/yy") Close #1

ElseIf Timer1.Interval = 500 Then

Open "C:\Users\Administrator\Desktop\打地鼠\intermediate.dat" For Append As #2

Print #2, Text1.Text + " " + Format(Date, "M/d/yy") Close #2Else

Open "C:\Users\Administrator\Desktop\打地鼠\advanced.dat" For Append As #3

Print #3, Text1.Text + " " + Format(Date, "M/d/yy") Close #3End If End If End If End End If End Sub

Private Sub Form_Load() Form4.Check1.Value = 1

WindowsMediaPlayer1.Controls.stop countine.Enabled = False pause.Enabled = False WLXZ.Enabled = False

Form5.Top = Form2.Top + 700 Form5.Left = Form2.Left flags1 = False flags = True End Sub

Private Sub help_Click() Form3.Show End Sub

Private Sub new_game_Click() num = num + 1 Dim t As Integer

If (num >0) Then

t = MsgBox("新游戏开始之前,是否保存", vbYesNo, "保存提示") If t = 6 Then

flags1 = True

If Timer1.Interval = 1000 Then

Open "C:\Users\Administrator\Desktop\打地鼠\primary.dat" For Append As #1'写 *** 作

Print #1, Text1.Text + " " + Format(Date, "M/d/yy") Close #1

ElseIf Timer1.Interval = 500 Then

Open "C:\Users\Administrator\Desktop\打地鼠\intermediate.dat" For Append As #2

Print #2, Text1.Text + " " + Format(Date, "M/d/yy") Close #2 Else

Open "C:\Users\Administrator\Desktop\打地鼠\advanced.dat" For Append As #3

Print #3, Text1.Text + " " + Format(Date, "M/d/yy") Close #3 End If End If End If

Call Form_Load

allnum = 0 '变化次数初始为0 oknum = 0 '打中次数初始为0 Timer1.Enabled = True pause.Enabled = True WLXZ.Enabled = True End Sub

Private Sub options_Click() Timer1.Enabled = False Form4.Show

WLXZ.Enabled = False countine.Enabled = True End Sub

Private Sub pause_Click() Dim r, g, b As Integer Timer1.Enabled = False countine.Enabled = True pause.Enabled = False WLXZ.Enabled = False

Form5.Top = Form2.Top + 700 Form5.Left = Form2.Left Form5.Show


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7200102.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-02
下一篇 2023-04-02

发表评论

登录后才能评论

评论列表(0条)

保存