1.用JAVA编写一个小游戏
前天写的猜数字游戏,yong i控制猜测次数,有详细解析,用黑窗口可以直接运行,我试验过了,没问题 import javax.swing.Icon; import javax.swing.JOptionPane; public class CaiShuZi4JOptionPane { /** * @param args */ public static void main(String[] args) { Icon icon = null; boolean bl = false; int put = 0; int c = (int) (((Math.random())*100)+1); //获取一个1-100的随机数 System.out.println("你获取的随机数是:"+c); //打印你的随机数字 String str1 = (String) JOptionPane.showInputDialog(null,"请输入你的猜测数字(1-100):\n","猜数字游戏",JOptionPane.PLAIN_MESSAGE,icon,null,"在这输入"); //第一次输入你的猜测数字 if(str1==null){ JOptionPane.showMessageDialog(null, "你已经取消了本次游戏"); //如果你点取消那么本次游戏结束 }else{ bl = num(str1); //判断是输入的是不是数字或者是整数 if(true==bl){ //如果是数字的话进入与随机数比较的程序 System.out.println("你输入的数字是:"+str1); //打印你输入的数字 put = Integer.valueOf(str1); for(int i = 4;i > 0;i--){ //i是你可以猜测的次数 if(put==c){ JOptionPane.showMessageDialog(null, "恭喜你猜对了,正确答案是:"+c+"。
"); //如果你猜对了就直接结束循环 break; }else if(put>c){ //如果输大了就让你再次从新输入 str1 = (String) JOptionPane.showInputDialog(null,"你的输入过大。你还有"+i+"次机会,请重新输入:\n","猜数字游戏",JOptionPane.PLAIN_MESSAGE,icon,null,"在这输入"); if(str1==null){ JOptionPane.showMessageDialog(null, "你已经取消了本次输入"); break; }else{ bl =num(str1); if(true==bl){ put = Integer.valueOf(str1); }else{ JOptionPane.showMessageDialog(null, "你的输入不正确,请重新输入"); } } }else if(put 你还有"+i+"次机会,请重新输入:\n","猜数字游戏",JOptionPane.PLAIN_MESSAGE,icon,null,"在这输入"); if(str1==null){ JOptionPane.showMessageDialog(null, "你已经取消了本次输入"); break; }else{ bl =num(str1); if(true==bl){ put = Integer.valueOf(str1); }else{ JOptionPane.showMessageDialog(null, "你的输入不正确,请重新输入"); } } } } }else if(bl==false){ //这个 是你第一次如果填写的不是数字的话也会结束本次游戏 JOptionPane.showMessageDialog(null, "请您下次按要求填写。本次游戏结束"); } if(true==bl && c!=put){ //如果你i次都没猜对,那么就直接告诉你这个数十什么 JOptionPane.showMessageDialog(null, "很遗憾你没能猜对,这个数字是:"+c+"."); } } } public static boolean num(String value){ //一个静态方法,判断你输入的是不是数字 try { Integer.parseInt(value); return true; } catch (Exception e) { return false; } } }。 //打字游戏给你吧 import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.Random; class WordPanel extends JPanel implements Runnable{ private Thread thread = null; private int level = 1; private Font font = new Font("宋体",Font.ITALIC+Font.BOLD,24); private Color color = Color.BLUE; public static final int x = 10; private int y = 0; private char word;//下落的字母 private static Random rand = new Random(); public void setY(int y){ this.y = y; } public void setWord(char word){ this.word = word; } public char getWord(){ return this.word; } public static char newChar(){ return (char)(97+rand.nextInt(26)); } public WordPanel(){ word = newChar(); thread = new Thread(this); thread.start(); } public void paintComponent(Graphics g) { super.paintComponent(g); g.setFont(font); g.setColor(color); g.drawString(String.valueOf(word),x,y); } public void run(){ while (true){ try { Thread.sleep(1000); this.repaint(); if (y>=this.getHeight()){ y = 0; word = this.newChar(); }else y+=20; } catch (Exception ex) { ex.printStackTrace(); } } } } public class WordGame extends JFrame{ private WordPanel[] words = new WordPanel[10]; class Listener extends KeyAdapter{ public void keyTyped(KeyEvent e) { char input = e.getKeyChar(); for (int i = 0; i。 贪吃蛇程序:GreedSnake.java (也是程序入口):import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Iterator; import java.util.LinkedList; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class GreedSnake implements KeyListener { JFrame mainFrame; Canvas paintCanvas; JLabel labelScore;// 计分牌 SnakeModel snakeModel = null;// 蛇 public static final int canvasWidth = 200; public static final int canvasHeight = 300; public static final int nodeWidth = 10; public static final int nodeHeight = 10; // ---------------------------------------------------------------------- // GreedSnake():初始化游戏界面 // ---------------------------------------------------------------------- public GreedSnake() { // 设置界面元素 mainFrame = new JFrame("GreedSnake"); Container cp = mainFrame.getContentPane(); labelScore = new JLabel("Score:"); cp.add(labelScore, BorderLayout.NORTH); paintCanvas = new Canvas(); paintCanvas.setSize(canvasWidth + 1, canvasHeight + 1); paintCanvas.addKeyListener(this); cp.add(paintCanvas, BorderLayout.CENTER); JPanel panelButtom = new JPanel(); panelButtom.setLayout(new BorderLayout()); JLabel labelHelp;// 帮助信息 labelHelp = new JLabel("PageUp, PageDown for speed;", JLabel.CENTER); panelButtom.add(labelHelp, BorderLayout.NORTH); labelHelp = new JLabel("ENTER or R or S for start;", JLabel.CENTER); panelButtom.add(labelHelp, BorderLayout.CENTER); labelHelp = new JLabel("SPACE or P for pause", JLabel.CENTER); panelButtom.add(labelHelp, BorderLayout.SOUTH); cp.add(panelButtom, BorderLayout.SOUTH); mainFrame.addKeyListener(this); mainFrame.pack(); mainFrame.setResizable(false); mainFrame.(JFrame.EXIT_ON_CLOSE); mainFrame.setVisible(true); begin(); } // ---------------------------------------------------------------------- // keyPressed():按键检测 // ---------------------------------------------------------------------- public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); if (snakeModel.running) switch (keyCode) { case KeyEvent.VK_UP: snakeModel.changeDirection(SnakeModel.UP); break; case KeyEvent.VK_DOWN: snakeModel.changeDirection(SnakeModel.DOWN); break; case KeyEvent.VK_LEFT: snakeModel.changeDirection(SnakeModel.LEFT); break; case KeyEvent.VK_RIGHT: snakeModel.changeDirection(SnakeModel.RIGHT); break; case KeyEvent.VK_ADD: case KeyEvent.VK_PAGE_UP: snakeModel.speedUp();// 加速 break; case KeyEvent.VK_SUBTRACT: case KeyEvent.VK_PAGE_DOWN: snakeModel.speedDown();// 减速 break; case KeyEvent.VK_SPACE: case KeyEvent.VK_P: snakeModel.changePauseState();// 暂停或继续 break; default: } // 重新开始 if (keyCode == KeyEvent.VK_R || keyCode == KeyEvent.VK_S || keyCode == KeyEvent.VK_ENTER) { snakeModel.running = false; begin(); } } // ---------------------------------------------------------------------- // keyReleased():空函数 // ---------------------------------------------------------------------- public void keyReleased(KeyEvent e) { } // ---------------------------------------------------------------------- // keyTyped():空函数 // ---------------------------------------------------------------------- public void keyTyped(KeyEvent e) { } // ---------------------------------------------------------------------- // repaint():绘制游戏界面(包括蛇和食物) // ---------------------------------------------------------------------- void repaint() { Graphics g = paintCanvas.getGraphics(); // draw background g.setColor(Color.WHITE); g.fillRect(0, 0, canvasWidth, canvasHeight); // draw the snake g.setColor(Color.BLACK); LinkedList na = snakeModel.nodeArray; Iterator it = na.iterator(); while (it.hasNext()) { Node n = (Node) it.next(); drawNode(g, n); } // draw the food g.setColor(Color.RED); Node n = snakeModel.food; drawNode(g, n); updateScore(); } // ---------------------------------------------------------------------- // drawNode():绘画某一结点(蛇身或食物) // ---------------------------------------------------------------------- private void drawNode(Graphics g, Node n) { g.fillRect(n.x * nodeWidth, n.y * nodeHeight, nodeWidth - 1, nodeHeight - 1); } // ---------------------------------------------------------------------- // updateScore():改变计分牌 // ---------------------------------------------------------------------- public void updateScore() { String s = "Score: " + snakeModel.score; labelScore.setText(s); } // ---------------------------------------------------------------------- // begin():游戏开始,放置贪吃蛇 // ---------------------------------------。 我给你个华容道的游戏参照下吧。 可以自己照着做下。多看例子想想就行了。 找个游戏做的不好,不要笑话啊。 import java.awt.*; import javax.swing.JApplet.*; import java.awt.event.*; import javax.swing.*; class People extends JButton implements FocusListener { Rectangle rect=null; int left_x,left_y;//按钮左上角坐标. int width,height; //按钮的宽和高. String name; int number; public People(int number,String s,int x,int y,int w,int h,HuaRongRoad road) { super(s); name=s; this.number=number; left_x=x; left_y=y; width=w; height=h; setBackground(Color.GREEN); road.add(this); addKeyListener(road); setBounds(x,y,w,h); addFocusListener(this); rect=new Rectangle(x,y,w,h); } public void focusGained(FocusEvent e) { setBackground(Color.red); } public void focusLost(FocusEvent e) { setBackground(Color.GREEN); } } public class HuaRongRoad extends JApplet implements KeyListener,ActionListener { People people[]=new People[10]; Rectangle left,right,above,below;//华容道的边界 JButton restart=new JButton("restart"); public void init() { getContentPane().setLayout(null); getContentPane().add(restart); restart.setBounds(5,5,80,25); restart.addActionListener(this); getContentPane().setBackground(Color.white); people[0]=new People(0,"曹操",154,54,200,200,this); people[1]=new People(1,"关羽",154,254,200,100,this); people[2]=new People(2,"张飞",54,254,100,200,this); people[3]=new People(3,"刘备",354,254,100,200,this); people[4]=new People(4,"张辽",54,54,100,200,this); people[5]=new People(5,"曹仁",354,54,100,200,this); people[6]=new People(6,"兵 ",54,454,100,100,this); people[7]=new People(7,"兵 ",354,454,100,100,this); people[8]=new People(8,"兵 ",154,354,100,100,this); people[9]=new People(9,"兵 ",254,354,100,100,this); people[9].requestFocus(); people[0].setForeground(Color.white); left=new Rectangle(49,49,5,510); right=new Rectangle(454,49,5,510); above=new Rectangle(49,49,410,5); below=new Rectangle(49,554,410,5); } public void paint(Graphics g) { //华容道的边界 super.paint(g); g.setColor(Color.cyan); g.fillRect(49,49,5,510); g.fillRect(454,49,5,510); g.fillRect(49,49,410,5); g.fillRect(49,554,410,5); // g.drawString("单击,按方向箭头移动",100,20); g.setColor(Color.red); g.drawString("曹操到达该位置",110,300); } public void keyPressed(KeyEvent e) { People man=(People)e.getSource(); man.rect.setLocation(man.getBounds().x,man.getBounds().y); if(e.getKeyCode()==KeyEvent.VK_DOWN) { man.left_y=man.left_y+100; //向下前进50个单位 man.setLocation(man.left_x,man.left_y); man.rect.setLocation(man.left_x,man.left_y); //判断是否和其他人或边界重叠,出现就退回50个单位 for(int i=0;i<10;i++) { if((man.rect.intersects(people[i].rect))&&(man.number!=i)) { man.left_y=man.left_y-100; man.setLocation(man.left_x,man.left_y); man.rect.setLocation(man.left_x,man.left_y); } } if(man.rect.intersects(below)) { man.left_y=man.left_y-100; man.setLocation(man.left_x,man.left_y); man.rect.setLocation(man.left_x,man.left_y); } } if(e.getKeyCode()==KeyEvent.VK_UP) { man.left_y=man.left_y-100; //向上前进50个单位 man.setLocation(man.left_x,man.left_y); man.rect.setLocation(man.left_x,man.left_y); //判断是否和其他人或边界重叠,出现就退回50个单位 for(int i=0;i<10;i++) { if((man.rect.intersects(people[i].rect))&&(man.number!=i)) { man.left_y=man.left_y+100; man.setLocation(man.left_x,man.left_y); man.rect.setLocation(man.left_x,man.left_y); } } if(man.rect.intersects(above)) { man.left_y=man.left_y+100; man.setLocation(man.left_x,man.left_y); man.rect.setLocation(man.left_x,man.left_y); } } if(e.getKeyCode()==KeyEvent.VK_LEFT) { man.left_x=man.left_x-100; //向左前进50个单位 man.setLocation(man.left_x,man.left_y); man.rect.setLocation(man.left_x,man.left_y); //判断是否和其他人或边界重叠,出现就退回50个单位 for(int i=0;i<10;i++) { if((man.rect.intersects(people[i].rect))&&(man.number!=i)) { man.left_x=man.left_x+100; man.setLocation(man.left_x,man.left_y); man.rect.setLocation(man.left_x,man.left_y); } } if(man.rect.intersects(left)) { man.left_x=man.left_x+100; man.setLocation(man.left_x,man.left_y); man.rect.setLocation(man.left_x,man.left_y); } } if(e.getKeyCode()==KeyEvent.VK_RIGHT) { man.left_x=man.left_x+100; //向右进50个单位 man.setLocation(man.left_x,man.left_y); man.rect.setLocation(man.left_x,man.left_y); //判断是否和其他人或边界重叠,出现就退回50个单位 for(int i=0;i<10;i++) { if((man.rect.intersects(people[i].rect))&&(man.number!=i)) { man.left_x=man.left_x-100; man.setLocation(man.left_x,man.left_y); man。 package FishGame; import java.io.IOException; import java.util.*; import javax.microedition.lcdui.*; public class Fish_Canvas extends Canvas implements Runnable{ /**屏幕宽*/ int width; /**屏幕高*/ int height; /**玩家得分*/ int score; /**随机数对象*/ Random r; /**游戏和菜单之间的控制变量,true代表游戏状态,false代表不在游戏状态*/ boolean isGame; /**true代表暂停,false代表不暂停*/ boolean isPaused; /**选择状态值 */ int status = 1; /**菜单画面 1*/ final int MENU = 1; /**开始画面 2*/ final int START = 2; /**游戏简介 3*/ final int SYNOPSIS = 3; /**游戏帮助 4*/ final int HELP = 4; /**退出游戏 5*/ final int QUIT = 5; /**菜单数组*/ String[] menu = { "\u5F00\u59CB\u6E38\u620F", "\u6E38\u620F\u7B80\u4ECB", "\u6E38\u620F\u5E2E\u52A9", "\u9000\u51FA\u6E38\u620F" }; String[] menu2= { "\u7EE7\u7EED\u6E38\u620F", "\u6E38\u620F\u7B80\u4ECB", "\u6E38\u620F\u5E2E\u52A9", "\u9000\u51FA\u6E38\u620F" }; /**选择框的宽*/ final int CASE_WIDTH = 45; /**选择框的高*/ final int CASE_HEIGHT = 15; /**当前选中的菜单项的序号*/ int index; /**菜单第一个字的x*/ final int MENUX = 60; /**菜单第一个字的y*/ final int MENUY = 60; /**菜单之间的间隔*/ final int MENU_SPACE = 20; /**帮助字符串数组*/ String[] help = { "\u4E0A\u79FB\uFF1A\u63092\u6216\u4E0A\u65B9\u5411\u952E", "\u4E0B\u79FB\uFF1A\u63098\u6216\u4E0B\u65B9\u5411\u952E", "\u5DE6\u79FB\uFF1A\u63094\u6216\u5DE6\u65B9\u5411\u952E", "\u53F3\u79FB\uFF1A\u63096\u6216\u53F3\u65B9\u5411\u952E", "\u6682\u505C\uFF1A\u63095\u6216OK\u952E", "\u9000\u51FA\uFF1A\u6309\u5DE6\u8F6F\u952E" }; /**帮助第一个字的x*/ final int HELPX = 30; /**帮助第一个字的y*/ final int HELPY = 40; /**帮助字符串数组之间的间隔*/ final int HELP_SPACE = 20; /**简介字符串数组*/ String[] synopsis = { "\u6E38\u620F\u7C7B\u578B\uFF1A\u76CA\u667A\u7C7B", "\u4F5C\u8005\uFF1A\u738B\u5C11\u6C5F", "\u7248\u672C\uFF1Av9.0", "QQ: 349126394", "Email: shaojiang216@163.com" }; /**简介第一个字的x*/ final int SYNOPSISX = 20; /**简介第一个字的y*/ final int SYNOPSISY = 40; /**简介字符串数组之间的间隔*/ final int SYNOPSIS_SPACE = 20; /**游戏结束字符串*/ String gameOver = "GAME OVER!"; /**游戏结束字符串的宽*/ int gameOver_width; /**字符串的高*/ int gameOver_height; /**游戏结束字符串左上x*/ int gameOver_x; /**游戏结束字符串左上y*/ int gameOver_y; /** 按键状态 */ int key; /**二进制倒数第一位的数字*/ final int UP = 1; /**二进制倒数第二位的数字*/ final int DOWN = 1 << 1; /**二进制倒数第三位的数字*/ final int LEFT = 1 << 2; /**二进制倒数第四位的数字*/ final int RIGHT = 1 << 3; /**箭头上键*/ final int KEY_UP = -1; /**箭头下键*/ final int KEY_DOWN = -2; /**箭头左键*/ final int KEY_LEFT = -3; /**箭头右键*/ final int KEY_RIGHT = -4; /**确定键*/ final int KEY_FIRE = -5; /**右软键(返回键或暂停键)*/ final int KEY_BACK = -7; /**左软键(退出键)*/ final int KEY_QUIT = -6; /**玩家鱼左上x*/ int player_fishx; /**玩家鱼左上y*/ int player_fishy; /**玩家鱼宽*/ int playerfish_width; /**玩家鱼高*/ int playerfish_height; /**玩家鱼的类型0~9*/ int playerfish_size; /**玩家鱼的方向,0代表向左,1代表向右*/ int playerfish_direct; /**玩家鱼的速度*/ final int PLAYER_SPEED = 10; /**敌人鱼左上x*/ int[] enemyFish_fishx; /**敌人鱼左上y*/ int[] enemyFish_fishy; /**敌人鱼宽*/ int enemyFish_width; /**敌人鱼高*/ int enemyFish_height; /**敌人鱼的类型0~10*/ int[] enemyFish_size; /**敌人鱼的方向,0代表向左,1代表向右*/ int[] enemyFish_direct; /**敌人鱼从左端出来*/ final int DIRECT_LEFT = 1; /**敌人鱼从右端出来*/ final int DIRECT_RIGHT = 0; /**敌人鱼的最大数量*/ final int MAX_NUM = 6; /**玩家鱼的速度*/ int enemy_speed = 8; /**敌人鱼序号*/ int enemyFish_index; /**敌人鱼是否可见*/ boolean[] isVisible; /**鱼的尾巴状态,0代表向内,1代表向外*/ int fish_tail; /**控制尾巴摆动的变量*/ int tmp_tail; /**游戏是否结束,true代表结束,false代表未结束*/ boolean isGameOver; /**背景图片对象*/ Image backGround_ima; /**玩家鱼向左图片对象数组*/ Image[][][] playerFish_ima; /**敌人鱼图片对象数组*/ Image[][][] enemyFish_ima; /**左上锚点*/ final int TOP_LEFT = Graphics.TOP|Graphics.LEFT; /**左下锚点*/ final int BOTTOM_LEFT = Graphics.BOTTOM|Graphics.LEFT; /**右下锚点*/ final int BOTTOM_RIGHT = Graphics.BOTTOM|Graphics.RIGHT; /**创建字体对象*/ Font f; /**线程对象*/ Thread thread; /** * 构造方法 */ public Fish_Canvas(){ //初始字体对象 f = Font.getDefaultFont(); //初始随机数对象。 import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.*;import javax.swing.Timer;public class Tetris extends JFrame { public Tetris() { Tetrisblok a = new Tetrisblok(); addKeyListener(a); add(a); } public static void main(String[] args) { Tetris frame = new Tetris(); JMenuBar menu = new JMenuBar(); frame.setJMenuBar(menu); JMenu game = new JMenu("游戏"); JMenuItem newgame = game.add("新游戏"); JMenuItem pause = game.add("暂停"); JMenuItem goon = game.add("继续"); JMenuItem exit = game.add("退出"); JMenu help = new JMenu("帮助"); JMenuItem about = help.add("关于"); menu.add(game); menu.add(help); frame.setLocationRelativeTo(null); frame.(JFrame.EXIT_ON_CLOSE); frame.setSize(220, 275); frame.setTitle("Tetris内测版"); // frame.setUndecorated(true); frame.setVisible(true); frame.setResizable(false); }}// 创建一个俄罗斯方块类class Tetrisblok extends JPanel implements KeyListener { // blockType 代表方块类型 // turnState代表方块状态 private int blockType; private int score = 0; private int turnState; private int x; private int y; private int i = 0; int j = 0; int flag = 0; // 定义已经放下的方块x=0-11,y=0-21; int[][] map = new int[13][23]; // 方块的形状 第一组代表方块类型有S、Z、L、J、I、O、T 7种 第二组 代表旋转几次 第三四组为 方块矩阵 private final int shapes[][][] = new int[][][] { // i { { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 }, { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 } }, // s { { 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, { 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 } }, // z { { 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 } }, // j { { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, // o { { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, // l { { 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, { 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, // t { { 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 } } }; // 生成新方块的方法 public void newblock() { blockType = (int) (Math.random() * 1000) % 7; turnState = (int) (Math.random() * 1000) % 4; x = 4; y = 0; if (gameover(x, y) == 1) { newmap(); drawwall(); score = 0; JOptionPane.showMessageDialog(null, "GAME OVER"); } } // 画围墙 public void drawwall() { for (i = 0; i < 12; i++) { map[i][21] = 2; } for (j = 0; j < 22; j++) { map[11][j] = 2; map[0][j] = 2; } } // 初始化地图 public void newmap() { for (i = 0; i < 12; i++) { for (j = 0; j < 22; j++) { map[i][j] = 0; } } } // 初始化构造方法 Tetrisblok() { newblock(); newmap(); drawwall(); Timer timer = new Timer(1000, new TimerListener()); timer.start(); } // 旋转的方法 public void turn() { int tempturnState = turnState; turnState = (turnState + 1) % 4; if (blow(x, y, blockType, turnState) == 1) { } if (blow(x, y, blockType, turnState) == 0) { turnState = tempturnState; } repaint(); } // 左移的方法 public void left() { if (blow(x - 1, y, blockType, turnState) == 1) { x = x - 1; } ; repaint(); } // 右移的方法 public void right() { if (blow(x + 1, y, blockType, turnState) == 1) { x = x + 1; } ; repaint(); } // 下落的方法 public void down() { if (blow(x, y + 1, blockType, turnState) == 1) { y = y + 1; delline(); } ; if (blow(x, y + 1, blockType, turnState) == 0) { add(x, y, blockType, turnState); newblock(); delline(); } ; repaint(); } // 是否合法的方法 public int blow(int x, int y, int blockType, int turnState) { for (int a = 0; a < 4; a++) { for (int b = 0; b < 4; b++) { if (((shapes[blockType][turnState][a * 4 + b] == 1) && (map[x + b + 1][y + a] == 1)) || ((shapes[blockType][turnState][a * 4 + b] == 1) && (map[x + b + 1][y + a] == 2))) { return 0; } } } return 1; } // 消行的方法 public void delline() { int c = 0; for (int b = 0; b < 22; b++) { for (int a = 0; a < 12; a++) { if (map[a][b] == 1) { c = c + 1; if (c == 10) { score += 10; for (int d = b; d > 0; d--) { for (int e = 0; e < 11; e++) { map[e][d] = map[e][d - 1]; } } } } } c = 0; } } // 判断你挂的方法 public int gameover(int x, int y) { if (blow(x, y, blockType, turnState) == 0) { return 1; 。 首先得看你是什么专业的,非计算机相关专业自学起来就是瞎子摸象了。如果是计算机相关专业,学过C,C++之类的编程语言并且知道什么是面向过程、面向对象。那自学起来,用点功夫,天天图书馆、看点视频。自己捣鼓捣鼓,一个月javaSE的东西就可以入门了。至于你的小游戏嘛,掌握Java面向对象的概念就可以做出像飞机大战,俄罗斯方块,贪吃蛇之类的小游戏了。重要的是逻辑设计,编码要求不高。界面的话你要用到Swing之类的用户图形界面工具类。图书馆大把这类的书,就看你学不学的来了。 ——每个认真的人,都值得被认真对待。 //超精简计算机小程序 //: HelloWorld.java //论文自己写吧,就5行程序而已 /**定义一个JAVA类,名字叫 HelloWorld */ public class HelloWorld { /** *程序入口,如果该程序能启动,必须有这个方法,而且格式和参数类型都不能变 * @param args 程序初始化参数组 */ public static void main(String[] args) { // TODO Auto-generated method stub //打印字符串 'HelloWorld' 到标准输设备. System.out.println("HelloWorld."); } } 转载请注明出处众文网 » 用java编写小游戏毕业论文(用JAVA编写一个小游戏)2.用JAVA编写一个完整的游戏程序
3.用JAVA编一个小游戏或者其他程序
4.用java开发小游戏
5.用java制作一个小游戏 急~~~~~~~~~
6.在线求一份罗斯方块游戏的java程序,最好是一个整体的毕业论文
7.毕业设计两个月内可以自学做出java小游戏吗
8.急求java小程序论文