
正文
简易射击游戏java代码,编程射击小游戏
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
求一个简单的Java小游戏的代码
连连看的小源码
package Lianliankan;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组
JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮
JLabel fractionLable=new JLabel("0"); //分数标签
JButton firstButton,secondButton; //分别记录两次被选中的按钮
int grid[][] = new int[8][7];//储存游戏按钮位置
static boolean pressInformation=false; //判断是否有按钮被选中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标
int i,j,k,n;//消除方法控制
public void init(){
mainFrame=new JFrame("JKJ连连看");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel=new JPanel();
southPanel=new JPanel();
northPanel=new JPanel();
thisContainer.add(centerPanel,"Center");
thisContainer.add(southPanel,"South");
thisContainer.add(northPanel,"North");
centerPanel.setLayout(new GridLayout(6,5));
for(int cols = 0;cols 6;cols++){
for(int rows = 0;rows 5;rows++ ){
diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1]));
diamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton=new JButton("退出");
exitButton.addActionListener(this);
resetButton=new JButton("重列");
resetButton.addActionListener(this);
newlyButton=new JButton("再来一局");
newlyButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
southPanel.add(newlyButton);
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));
northPanel.add(fractionLable);
mainFrame.setBounds(280,100,500,450);
mainFrame.setVisible(true);
}
public void randomBuild() {
int randoms,cols,rows;
for(int twins=1;twins=15;twins++) {
randoms=(int)(Math.random()*25+1);
for(int alike=1;alike=2;alike++) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=randoms;
}
}
}
public void fraction(){
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));
}
public void reload() {
int save[] = new int[30];
int n=0,cols,rows;
int grid[][]= new int[8][7];
for(int i=0;i=6;i++) {
for(int j=0;j=5;j++) {
if(this.grid[i][j]!=0) {
save[n]=this.grid[i][j];
n++;
}
}
}
n=n-1;
this.grid=grid;
while(n=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=save[n];
n--;
}
mainFrame.setVisible(false);
pressInformation=false; //这里一定要将按钮点击信息归为初始
init();
for(int i = 0;i 6;i++){
for(int j = 0;j 5;j++ ){
if(grid[i+1][j+1]==0)
diamondsButton[i][j].setVisible(false);
}
}
}
public void estimateEven(int placeX,int placeY,JButton bz) {
if(pressInformation==false) {
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
pressInformation=true;
}
else {
x0=x;
y0=y;
fristMsg=secondMsg;
firstButton=secondButton;
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
if(fristMsg==secondMsg secondButton!=firstButton){
xiao();
}
}
}
public void xiao() { //相同的情况下能不能消去。仔细分析,不一条条注释
if((x0==x (y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)(y0==y))){ //判断是否相邻
remove();
}
else{
for (j=0;j7;j++ ) {
if (grid[x0][j]==0){ //判断第一个按钮同行哪个按钮为空
if (yj) { //如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边
for (i=y-1;i=j;i-- ){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0) {
k=0;
break;
}
else{ k=1; } //K=1说明通过了第一次验证
}
if (k==1) {
linePassOne();
}
}
if (yj){ //如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边
for (i=y+1;i=j ;i++ ){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0){
k=0;
break;
}
else { k=1; }
}
if (k==1){
linePassOne();
}
}
if (y==j ) {
linePassOne();
}
}
if (k==2) {
if (x0==x) {
remove();
}
if (x0x) {
for (n=x0;n=x-1;n++ ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 n==x-1) {
remove();
}
}
}
if (x0x) {
for (n=x0;n=x+1 ;n-- ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 n==x+1) {
remove();
}
}
}
}
}
for (i=0;i8;i++ ) { //列
if (grid[i][y0]==0) {
if (xi) {
for (j=x-1;j=i ;j-- ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (xi) {
for (j=x+1;j=i;j++ ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (x==i) {
rowPassOne();
}
}
if (k==2){
if (y0==y) {
remove();
}
if (y0y) {
for (n=y0;n=y-1 ;n++ ) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 n==y-1) {
remove();
}
}
}
if (y0y) {
for (n=y0;n=y+1 ;n--) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 n==y+1) {
remove();
}
}
}
}
}
}
}
public void linePassOne(){
if (y0j){ //第一按钮同行空按钮在左边
for (i=y0-1;i=j ;i-- ){ //判断第一按钮同左侧空按钮之间有没按钮
if (grid[x0][i]!=0) {
k=0;
break;
}
else { k=2; } //K=2说明通过了第二次验证
}
}
if (y0j){ //第一按钮同行空按钮在与第二按钮之间
for (i=y0+1;i=j ;i++){
if (grid[x0][i]!=0) {
k=0;
break;
}
else{ k=2; }
}
}
}
public void rowPassOne(){
if (x0i) {
for (j=x0-1;j=i ;j-- ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
if (x0i) {
for (j=x0+1;j=i ;j++ ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
}
public void remove(){
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation=false;
k=0;
grid[x0][y0]=0;
grid[x][y]=0;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==newlyButton){
int grid[][] = new int[8][7];
this.grid = grid;
randomBuild();
mainFrame.setVisible(false);
pressInformation=false;
init();
}
if(e.getSource()==exitButton)
System.exit(0);
if(e.getSource()==resetButton)
reload();
for(int cols = 0;cols 6;cols++){
for(int rows = 0;rows 5;rows++ ){
if(e.getSource()==diamondsButton[cols][rows])
estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);
}
}
}
public static void main(String[] args) {
lianliankan llk = new lianliankan();
llk.randomBuild();
llk.init();
}
}
//old 998 lines
//new 318 lines
相关问答
Q1: 急求简单的手机java小游戏代码 能运行的 简单的就可以
package test;
import java.util.Random;
import java.util.Scanner;
public class TestGame {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int r = new Random().nextInt(999999);
int count =0 ;
while(true){
System.out.println("猜数字游戏,请输入一个数0到999999,输入-1结束游戏:");
int i = sc.nextInt() ;
if(i==-1){
break ;
}
count ++ ;
if(ir){
System.out.print("你猜小了。");
System.out.println("你已经猜了"+count+"次");
}else if(ir){
System.out.println("你猜大了。");
System.out.println("你已经猜了"+count+"次");
}else{
System.out.println("恭喜你大对了,但是没奖励!");
}
}
System.out.println("游戏结束");
}
}
Q2: java编写的飞行射击游戏代码
(无忧手机网),不需要输入手机号码认证什么的直接下载就可以了,有很多支持下列系统格式的软件和游戏,根据下面的手机型号归属系统下载相应的软件就可以了。大部分都是已破解的,里面也有很多java的游戏(只要支持java 就可以)
Q3: 如何制作简单的射击类(比CS还要简单的)游戏
射击类游戏(一)
第二部分开始类的编写:
1、首先打开Flash CS4 (CS3、CS4或CS5没什么大的区别)新建项目,按确定。打开项目面板。图:新建项目
2、点击右边的第一个三角按钮(项目),在下拉菜单中选"新建项目"。在弹出新建项目面板项目名称:填入项目名称。在根文件夹:项中点击右边的浏览按钮,浏览到准备存放项目的目录,按确定。在ActionScript版本:选ActionScript 3.0 。单击"创建项目"按钮,这样项目就建好了。图:项目面板
3、把AirRaid.fla文件复制到项目文件夹下(省去制作界面)。点击下面的"创建类"图标,在弹出的创建类面板类:的文本框中填入文件名:AirRaid 单击创建类按钮。这样类就创建好了并为我们写好了类的框架,我们可以在框架中添加我们的代码了。图:创建类
图:填入类名
图:类的框架
4、编写AirRaid.as类
AirRaid.as类:文档类 AirRaid类是游戏的控制器,绑定AirRaid.fla 。负责生成界面上的飞机、火炮、子弹。显示得分,子弹的剩余数量。注册了键盘按下,释放侦听器,接收用户的键盘操作。注册了进入帧事件侦听器,进行碰撞检测。定义一个飞机计时器,随机生成飞机。检测剩余子弹的数目,当子弹数为零时也就是主角死亡了,移除界面上的飞机,火炮,跳转到重玩按钮。
package {
public class AirRaid extends MovieClip {
private var aagun:AAGun;//火炮
private var airplanes:Array;//飞机数组
private var bullets:Array;//子弹数组
public var leftArrow, rightArrow:Boolean;
private var nextPlane:Timer;//不定时生成飞机的计时器
private var shotsLeft:int;//得分文本
private var shotsHit:int;//子弹数文本
// 初始化得分数和子弹数
// 生成炮加入到舞台上
// 生成飞机、子弹数组
// 键盘按下、释放事件侦听器
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);
// 进入帧事件侦听器,检测子弹击中飞机的碰撞检测。
addEventListener(Event.ENTER_FRAME,checkForHits);
// 生成下一架飞机
setNextPlane();
}
//不定时生成飞机
public function setNextPlane() {
//1000毫秒至2000毫秒之间生成一架飞机
nextPlane.start();
}
public function newPlane(event:TimerEvent) {
// 随机的边、速度和高度
// 生成飞机
var p:Airplane = new Airplane(side,speed,altitude);
// 碰撞检测
}
}
if ((shotsLeft == 0) (bullets.length == 0)) {
endGame();
}
}
// 按下键盘
public function keyDownFunction(event:KeyboardEvent) {
if (event.keyCode == 37) {
leftArrow = true;
} else if (event.keyCode == 39) {
rightArrow = true;
} else if (event.keyCode == 32) {
fireBullet();
}
}
// 释放键盘
public function keyUpFunction(event:KeyboardEvent) {
if (event.keyCode == 37) {
leftArrow = false;
} else if (event.keyCode == 39) {
rightArrow = false;
}
}
// 生成新的子弹
public function fireBullet() {
if (shotsLeft = 0) return;
var b:Bullet = new Bullet(aagun.x,aagun.y,-300);
addChild(b);
bullets.push(b);
shotsLeft--;
showGameScore();
}
public function showGameScore() {
showScore.text = String("得分: "+shotsHit);
showShots.text = String("剩余子弹: "+shotsLeft);
}
// 从数组获取飞机
public function removePlane(plane:Airplane) {
for(var i in airplanes) {
if (airplanes[i] == plane) {
airplanes.splice(i,1);
break;
}
}
}
// 获取数组的一个子弹
public function removeBullet(bullet:Bullet) {
for(var i in bullets) {
if (bullets[i] == bullet) {
bullets.splice(i,1);
break;
}
}
}
// 游戏结束,移除界面上的东西。
public function endGame() {
// 移除飞机
for(var i:int=airplanes.length-1;i=0;i--) {
airplanes[i].deletePlane();
}
airplanes = null;
aagun.deleteGun();
aagun = null;
// 移除侦听器
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);
stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpFunction);
removeEventListener(Event.ENTER_FRAME,checkForHits);
nextPlane.stop();
nextPlane = null;
gotoAndStop("gameover");
}
}
}
5、编写AAGun.as类
创建类的方法同第3步。首先选中库中的AAGun元件,打开项目面板创建类。在"将类邦定到库元件"前面打勾,点下"使用所选库元件"的单选按钮。图:邦定库元件
AAGun类:邦定库中的炮元件,向左移动向右移动,检测边界,自动消毁。
package {
import flash.display.*;
import flash.events.*;
import flash.utils.getTimer;
public class AAGun extends MovieClip {
static const speed:Number = 150.0;
private var lastTime:int; // 控制移动的Timer
public function AAGun() {
// 枪的初始位置
this.x = 275;
this.y = 340;
//运动
addEventListener(Event.ENTER_FRAME,moveGun);
}
public function moveGun(event:Event) {
// 得到时差
var timePassed:int = getTimer() - lastTime;
lastTime += timePassed;
// 现在的位置
var newx = this.x;
// 移动到左边
if (MovieClip(parent).leftArrow) {
newx -= speed*timePassed / 1000;
}
// 移动到右边
if (MovieClip(parent).rightArrow) {
newx += speed*timePassed / 1000;
}
// 检测边界
if (newx 10) newx = 10;
if (newx 540) newx = 540;
// 更新位置
this.x = newx;
}
// 移除屏幕上的炮和事件
public function deleteGun() {
parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME,moveGun);
}
}
}
新建文件Airplane.as
Airplane类:邦定库中的飞机元件。随机产生飞机,随机的速度,产生5种飞机样式,检测是否飞出边界,检测被子弹击中。自动毁灭和爆炸效果。
// 飞机的速度、方向
// 控制移动的Timer
//左右移动
//检测边界
// 检测碰撞,子弹击中飞机跳转到帧标签"explode"演示爆炸动画。
// 移除舞台上的飞机和事件
新建文件Bullet.as
Bullet类:邦定库中的子弹元件。控制子弹的飞行方向,检测子弹是否飞出屏幕。
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.getTimer;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Bullet extends MovieClip {
private var dy:Number; // 子弹的速度、方向。
private var lastTime:int;
public function Bullet(x,y:Number, speed: Number) {
// 初始位置
this.x = x;
this.y = y;
// 得到速度
dy = speed;
// 动画
lastTime = getTimer();
addEventListener(Event.ENTER_FRAME,moveBullet);
public function moveBullet(event:Event) {
// 得到时差
var timePassed:int = getTimer()-lastTime;
lastTime += timePassed;
// 子弹运动
this.y += dy*timePassed/1000;
// 子弹越过屏幕的顶端
if (this.y 0) {
deleteBullet();
// 移除舞台的子弹和事件
public function deleteBullet() {
MovieClip(parent).removeBullet(this);
parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME,moveBullet);
所有的类文件完成保存后,回到AirRaid.fla场景,按Ctrl+Enter组合键测试,看看你的成果吧!
简单的学个Flash,很好学,还可以做动画,很有用。大型游戏要学VB或者C++,编得游戏画质不行。3DMAX是做特效的,你想做个游戏,不是一两年搞得了的,有专门教这个的班。
Q4: 求一个简单的JAVA游戏代码,100行左右,谢谢!
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Painter extends JFrame{
/**
*
*/
private static final long serialVersionUID = 8160427604782702376L;
CanvasPanel canvas = new CanvasPanel();;
public Painter() {
super("Star");
this.add(canvas);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Painter();
}
}
class CanvasPanel extends JPanel implements ActionListener{
/**
*
*/
private static final long serialVersionUID = -4642528854538741028L;
private JButton[] btn = new JButton[4];
private String[] btn_name = {"+", "-", "R", "L"};
private int center_x = 200, center_y = 200, radius = 100, degree = 0;
public CanvasPanel() {
this.setPreferredSize(new Dimension(400, 500));
this.setLayout(null);
for(int i = 0; i 4; i++) {
btn[i] = new JButton(btn_name[i]);
btn[i].setBounds(160 + i * 60, 425, 50, 50);
btn[i].addActionListener(this);
this.add(btn[i]);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i = 0; i 5; i++) {
g.drawLine( (int) (center_x + radius * Math.sin(Math.toRadians(degree + 72 * i))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree + 72 * i))),
(int) (center_x + radius * Math.sin(Math.toRadians(degree + 72 * i + 144))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree + 72 * i + 144))));
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand() == "+") {
if(radius 200)
radius += 2;
repaint();
} else if(e.getActionCommand() == "-") {
if(radius 0)
radius -= 2;
repaint();
} else if(e.getActionCommand() == "R") {
degree = (degree + 2) % 360;
repaint();
} else if(e.getActionCommand() == "L") {
degree = (degree - 2) % 360;
repaint();
}
}
}
Q5: Java 求一段射击小游戏的代码(让一个小图片随机出现)
1、在JFrame上用网格布局批量实例化一些JLabel 2、建立一个线程获得一个随机数,根据随机数给某个JLabel设置icon 3、线程sleep(1000); 4、移除这个JLabel的icon 5、线程循环







