在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Java语言基础 → 是男人就撑过30秒 游戏源码下载

是男人就撑过30秒 游戏源码下载

Java语言基础

下载此实例
  • 开发语言:Java
  • 实例大小:0.09M
  • 下载次数:12
  • 浏览次数:660
  • 发布时间:2016-06-19
  • 实例类别:Java语言基础
  • 发 布 人:simaxingjie
  • 文件格式:.zip
  • 所需积分:5
 相关标签: 游戏 java

实例介绍

【实例简介】源码在 game.java文件,店家 startgame.bat 可查看效果

【实例截图】

【核心代码】

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Date;

public class Game extends JFrame {
	/**
	 * This method initializes jButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(478, 361, 164, 51));
			jButton1.setText("重新开始");
			jButton1.setVisible(false);
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jButton1.setVisible(false);
					jLabel.setVisible(false);
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e1) {
							// TODO 自动生成 catch 块
							e1.printStackTrace();
						}
					reset();

				}
			});
		}
		return jButton1;
	}

	public static void main(String[] args) {
		Game game = new Game();
		game.start();
		game.reset();
		game.gogo();
	}

	public void reset() {
		kup = false;
		kdown = false;
		kleft = false;
		kright = false;
		int chushihua = 0;
		while (chushihua < zidanshu) {
			((JButton) buttonal.get(chushihua)).setBounds(new Rectangle(-50,
					-50, 10, 10));
			chushihua  ;
		}
		gamexunhuan = true;
		jButton.setIcon(new ImageIcon(fileLoc));
		jButton.setLocation(320, 320);
		p = jButton.getLocation();
		x=p.getX();
		y=p.getY();
		firsttime=new Date().getTime();
	}

	public void start() {
		int chushihua = 0;
		while (chushihua < zidanshu) {
			JButton jb = new JButton();
			jb.setBounds(new Rectangle(-50, -50, 10, 10));
			jb.setEnabled(false);
			Threads ths = new Threads(jb);
			Thread th = new Thread(ths);
			buttonal.add(jb);
			threadal.add(th);
			chushihua  ;
		}
		Game.Move move = new Move();
		Thread tm = new Thread(move);
		tm.start();
	}

	public void gogo() {
		int chushihua = 0;
		while (chushihua < zidanshu) {
			((Thread) threadal.get(chushihua)).start();
			chushihua  ;
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
	}

	// private static Game game;
	private long firsttime;
	private long lasttime;
	private static final long serialVersionUID = 1L;

	private JPanel jPanel = null;

	private JButton jButton = null;

	private boolean kup ;

	private boolean kdown ;

	private boolean kleft ;

	private boolean kright ;

	// 定义玩家的行走步伐,数值越大,移动速度越快
	private int step = 3;

	Point p; // @jve:decl-index=0:

	double x = 0.0;

	double y = 0.0;

	// 定义了子弹的个数
	int zidanshu = 70;

	// 定义子弹初始值,这个是不变的
	// int chushihua = 0;
	// 定义控制子弹行走的循环false就不走了
	private boolean gamexunhuan = true;

	private JLabel jLabel = null;

	private JButton jButton1 = null;

	private ArrayList buttonal = new ArrayList();

	private ArrayList threadal = new ArrayList();
	URLClassLoader urlLoader = (URLClassLoader)this.getClass().getClassLoader();
	URL fileLoc = urlLoader.findResource("MyGameIcons/gwl1.gif");  //  @jve:decl-index=0:
	URL fileLoc1 = urlLoader.findResource("MyGameIcons/gwls1.gif");

	/**
	 * This is the default constructor
	 */
	public Game() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(700, 700);
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				System.exit(1);
			}
		});
		this.setResizable(false);
		this.setContentPane(getJPanel());
		this.setTitle("范传奇的小游戏!(模拟撑过30秒的小DEMO)");
		this.setVisible(true);
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(42, -33, 595, 308));
			jLabel.setFont(new Font("Dialog", Font.BOLD, 24));
			jLabel.setForeground(new Color(250, 2, 2));
			jLabel.setEnabled(true);
			jLabel.setVisible(false);
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.add(getJButton(), null);
			jPanel.setForeground(new Color(1, 1, 1));
			jPanel.setBackground(new Color(1, 1, 1));
			jPanel.setVisible(true);
			jPanel.add(jLabel, null);
			jPanel.add(getJButton1(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	class Move implements Runnable {
		public void run() {
			while(true){
				while (gamexunhuan) {
					p = jButton.getLocation();
					if (kup) {
						if (kleft) {
							x = p.getX();
							y = p.getY();
							if (x > 0 && y > 0) {
								jButton.setLocation((int) x - step, (int) y
										- step);
							}
						} else if (kright) {
							x = p.getX();
							y = p.getY();
							if (x   40 < 700 && y > 0) {
								jButton.setLocation((int) x   step, (int) y
										- step);
							}
						} else {
							x = p.getX();
							y = p.getY();
							if (y > 0) {
								jButton.setLocation((int) x, (int) y - step);
							}
						}
					}
					if (kdown) {
						if (kleft) {
							x = p.getX();
							y = p.getY();
							if (y   60 < 700 && x > 0) {
								jButton.setLocation((int) x - step, (int) y
										  step);
							}
						} else if (kright) {
							x = p.getX();
							y = p.getY();
							if (x   40 < 700 && y   60 < 700) {
								jButton.setLocation((int) x   step, (int) y
										  step);
							}
						} else {
							x = p.getX();
							y = p.getY();
							if (y   60 < 700) {
								jButton.setLocation((int) x, (int) y   step);
							}
						}
					}
					if (kleft) {
						if (kup) {
							x = p.getX();
							y = p.getY();
							if (x > 0 && y > 0) {
								jButton.setLocation((int) x - step, (int) y
										- step);
							}
						} else if (kdown) {
							x = p.getX();
							y = p.getY();
							if (y   60 < 700 && x > 0) {
								jButton.setLocation((int) x - step, (int) y
										  step);
							}
						} else {
							x = p.getX();
							y = p.getY();
							if (x > 0) {
								jButton.setLocation((int) x - step, (int) y);
							}
						}
					}
					if (kright) {
						if (kup) {
							x = p.getX();
							y = p.getY();
							if (x   40 < 700 && y > 0) {
								jButton.setLocation((int) x   step, (int) y
										- step);
							}
						} else if (kdown) {
							x = p.getX();
							y = p.getY();
							if (x   40 < 700 && y   60 < 700) {
								jButton.setLocation((int) x   step, (int) y
										  step);
							}
						} else {
							x = p.getX();
							y = p.getY();
							if (x   40 < 700) {
								jButton.setLocation((int) x   step, (int) y);
							}
						}
					}
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO 自动生成 catch 块
						e.printStackTrace();
					}
				}
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					// TODO 自动生成 catch 块
					e.printStackTrace();
				}
			}
		}
	}

	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(320, 320, 30, 30));
			jButton.setBackground(new Color(1, 1, 1));
			p = jButton.getLocation();
			x = p.getX();
			y = p.getY();
			jButton.setIcon(new ImageIcon(fileLoc));
			jButton.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyReleased(java.awt.event.KeyEvent e) {
					if(e.getKeyCode()==10){
						if(!gamexunhuan){
							jButton1.setVisible(false);
							jLabel.setVisible(false);
							reset();
						}
					}
					if (e.getKeyCode() == 37) {
						kleft = false;
					}
					if (e.getKeyCode() == 38) {
						kup = false;
					}
					if (e.getKeyCode() == 39) {
						kright = false;
					}
					if (e.getKeyCode() == 40) {
						kdown = false;
					}
				}

				public void keyPressed(java.awt.event.KeyEvent e) {
					if (e.getKeyCode() == 37) {
						kleft = true;

					}
					if (e.getKeyCode() == 38) {
						kup = true;

					}
					// 触发按右键
					if (e.getKeyCode() == 39) {
						kright = true;

					}
					if (e.getKeyCode() == 40) {
						kdown = true;

					}
				}
			});
		}
		return jButton;
	}

	class Threads implements Runnable {
		public Threads(JButton jjb) {
			jb = jjb;
		}

		JButton jb = null;

		private boolean first = true;

		public void run() {
			while (gamexunhuan) {
				go();
			}
		}

		public void go() {
			int zzx = 0;
			int zzy = 0;
			int zx = 0;
			int zy = 0;
			while (true) {
				if(gamexunhuan){
				int fangxiang = (int) (Math.random() * 4   1);
				// 四个if随即从四个边发射子弹
				if (fangxiang == 1) {
					zx = 0;
					zy = (int) (Math.random() * 701);
				}
				if (fangxiang == 2) {
					zx = (int) (Math.random() * 701);
					zy = 0;
				}
				if (fangxiang == 3) {
					zx = 700;
					zy = (int) (Math.random() * 701);
				}
				if (fangxiang == 4) {
					zx = (int) (Math.random() * 701);
					zy = 700;
				}
				// 初始化子弹,有了就不在加了
				if (first) {
					jPanel.add(jb, null);
					first = false;
				}
				jb.setBounds(new Rectangle(zx, zy, 10, 10));
				// 定义子弹与物体之间的步长
				zzx = (int) (((x   15) - zx) / 30);
				zzy = (int) (((y   15) - zy) / 30);
				}
				while (gamexunhuan) {
					try {
						zx  = zzx;
						zy  = zzy;
						jb.setLocation(zx, zy);
						if (zx   5 > x & zx   5 < x   30 & zy   5 > y
								& zy   5 < y   30) {
							jButton.setIcon(new ImageIcon(fileLoc1));
							gamexunhuan = false;
							first = true;
							jButton1.setVisible(true);
							jLabel.setVisible(true);
							lasttime = new Date().getTime();
							Date gametime = new Date(lasttime-firsttime);
							int min =0; 
							int sec =0;
							min = gametime.getMinutes();
							sec = gametime.getSeconds();
							String endtime = "";
							if(min!=0){
									endtime=min   "分  "   sec   "秒";
							}else{
								endtime=sec   "秒";
							}
							jLabel.setText("                          GAME OVER!!! \n用时:"   endtime);
							break;
						}
						// 超出边线停止循环
						if (zx > 700 | zy > 700 | zx < 0 | zy < 0) {
							break;
						}
						Thread.sleep(60);
					} catch (InterruptedException e) {
						// TODO 自动生成 catch 块
						e.printStackTrace();
					}
				}
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					// TODO 自动生成 catch 块
					e.printStackTrace();
				}
			}
		}
	}

} // @jve:decl-index=0:visual-constraint="10,10"

标签: 游戏 java

实例下载地址

是男人就撑过30秒 游戏源码下载

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警