Java游戏编程序switch游戏推荐

导读:就爱阅读网友为您分享以下“编写Java程序,使用switch语句实现判断月份i有几天。不用考虑闰年”资讯,希望对您有所帮助,感谢您对92to.com的支持!编写Java程序,使用switch语句实现判断月份i有几天。不用考虑闰年。 importjava.util.S publicclassChargeMouth { publicstaticvoid main(String[]
执行结果: Scanner scan = newScanner(System.in); System.out.println("请输入想要判断月份的i");
c = scan.nextInt(); switch(c){
case 1: case 3:
case 12: System.out.println("本月31天"); case 4:
case 11: System.out.println("本月30天"); case 2: System.out.println("本月28天"); default: System.out.println("error"); } 百度搜索“就爱阅读”,专业资料,生活学习,尽在就爱阅读网92to.com,您的在线图书馆
欢迎转载:
相关推荐:Java 在switch case中的一段程序,case 3 和case 5后的程序都可以运行,但是case4运行的时候_百度知道
Java 在switch case中的一段程序,case 3 和case 5后的程序都可以运行,但是case4运行的时候
判定传入的4输入不正确,导致case4里的程序无法运行
我有更好的答案
方便贴出代码么
你的变量key7值一直为0,没有进行录入重新赋值,不满足1&=key7&=4,
采纳率:32%
你问的是key2那个case4无法运行,还是key7的case4无法运行?另,麻烦贴一下完整的代码。
本回答被网友采纳
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。《Java小游戏实现》:贪吃蛇
《Java小游戏实现》:贪吃蛇
在完成坦克大战之后,就想到了贪吃蛇这个小游戏,因为这两个游戏太像了,因此,就决定把这个游戏来尝试的写下。接下来的几篇博文就是来记录这个小游戏实现的全过程。
突然,想起,一年前(时间是日),我刚学习Java的时候看过别人写的这个游戏源代码,还专门写了篇博文,连接如下:
确实好巧,今天我自己就从零开始来完成这个小游戏,完成的方式也是一步一步的添加功能这样的方式来实现。
第一步完成的功能:写一个界面
大家见到的贪吃蛇小游戏,界面肯定是少不了的。因此,第一步就是写一个小界面。
实现代码如下:
public class SnakeFrame extends Frame{
//方格的宽度和长度
public static final int BLOCK_WIDTH = 15 ;
public static final int BLOCK_HEIGHT = 15 ;
//界面的方格的行数和列数
public static final int ROW = 40;
public static final int COL = 40;
public static void main(String[] args) {
new SnakeFrame().launch();
public void launch(){
this.setTitle("Snake");
this.setSize(ROW*BLOCK_HEIGHT, COL*BLOCK_WIDTH);
this.setLocation(300, 400);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
this.setResizable(false);
this.setVisible(true);
第二步完成的功能:在界面上画成一格一格的
我们见过的贪吃蛇游戏,是有一个格子一个格子构成,然后蛇在这个里面运动。
重写paint方法,单元格就是横着画几条线竖着画几条线即可。
代码如下:
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.GRAY);
* 将界面画成由ROW*COL的方格构成,两个for循环即可解决
for(int i = 0;i&ROW;i++){
g.drawLine(0, i*BLOCK_HEIGHT, COL*BLOCK_WIDTH,i*BLOCK_HEIGHT );
for(int i=0;i&COL;i++){
g.drawLine(i*BLOCK_WIDTH, 0 , i*BLOCK_WIDTH ,ROW*BLOCK_HEIGHT);
g.setColor(c);
效果如下:
第三步完成的功能:建立另外的线程来控制重画
由于,蛇的运动就是改变蛇所在的位置,然后进行重画,就是我们所看到的运动。因此,在这里,我们单独用一个线程来控制重画。
1、新建一个MyPaintThread类,实现了Runnable接口
private class MyPaintThread implements Runnable{
public void run() {
while(true){
repaint();
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
2、在SnakeFrame的launchFrame方法中添加代码:new Thread(new MyPaintThread()).start();即可。
完成功能:利用双缓冲来解决闪烁的问题
private Image offScreenImage = null;
public void update(Graphics g) {
if(offScreenImage==null){
offScreenImage = this.createImage(ROW*BLOCK_HEIGHT, COL*BLOCK_WIDTH);
Graphics offg = offScreenImage.getGraphics();
paint(offg);
g.drawImage(offScreenImage, 0, 0, null);
第四步完成的功能:在界面上画一个蛇出来
贪吃蛇游戏中的蛇就是用一系列的点来表示,这里我们来模拟一个链表。链表上的每个元素代表一个节点。
首先,我们先新建一个Node类来表示构成蛇的节点,用面向对象的思想,发现,这个类应该有如下的属性和方法:
2、大小,即长度、宽度
4、构造方法
5、draw方法
Node类的代码如下:
public class Node {
private static final int BLOCK_WIDTH = SnakeFrame.BLOCK_WIDTH;
private static final int BLOCK_HEIGHT = SnakeFrame.BLOCK_HEIGHT;
private int
private int
public Node(int row, int col, Direction dir) {
this.row =
this.col =
this.dir =
public void draw(Graphics g){
Color c = g.getColor();
g.setColor(Color.BLACK);
g.fillRect(col*BLOCK_WIDTH, row*BLOCK_HEIGHT, BLOCK_WIDTH, BLOCK_HEIGHT);
g.setColor(c);
Direction是一个enum,具体如下:
public enum Direction {
而在Snake类中,用面向对象的思维,可以发现,Snake类中应该有如下的属性和方法
3、构造函数
3、draw方法
具体代码如下:
public class Snake {
private Node head = null;
private Node tail = null;
private SnakeF
private Node node = new Node(3,4,Direction.D);
private int size = 0;
public Snake(SnakeFrame sf) {
public void draw(Graphics g){
if(head==null){
for(Node node =node!=null;node = node.next){
node.draw(g);
在SnakeFrame类中new一个Snake对象,然后调用Snake对象的draw方法即可。
效果如下:
第五步完成的功能:通过键盘控制蛇的上下左右移动
首先想到的是这样:在Snake类中添加一个keyPressed方法,然后在SnakeFrame的键盘事件中调用Snake对象的keyPressed方法。
注意:蛇的移动是通过在头部添加一个单元格,在尾部删除一个单元格这样的思想来实现。
具体如下:
Snake类中添加一个keyPressed方法,主要是根据键盘的上下左右键来确定蛇的头结点的方向,然后move方法再根据头结点的方向来在头部添加一个单元格。
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_LEFT :
if(head.dir!=Direction.R){
head.dir = Direction.L;
case KeyEvent.VK_UP :
if(head.dir!=Direction.D){
head.dir = Direction.U;
case KeyEvent.VK_RIGHT :
if(head.dir!=Direction.L){
head.dir = Direction.R;
case KeyEvent.VK_DOWN :
if(head.dir!=Direction.U){
head.dir = Direction.D;
public void move() {
addNodeInHead();
deleteNodeInTail();
private void deleteNodeInTail() {
Node node = tail.
tail = null;
node.next = null;
private void addNodeInHead() {
Node node = null;
switch(head.dir){
node = new Node(head.row,head.col-1,head.dir);
node = new Node(head.row-1,head.col,head.dir);
node = new Node(head.row,head.col+1,head.dir);
node = new Node(head.row+1,head.col,head.dir);
node.next =
head.pre =
public void draw(Graphics g){
if(head==null){
for(Node node =node!=null;node = node.next){
node.draw(g);
这样就实现了通过键盘来实现蛇的移动。
完成的功能:蛇吃蛋
首先我们新建一个蛋Egg的类。
类的属性和方法有:
1、位置、大小
2、构造方法
3、draw方法
4、getRect方法:用于碰撞检测
5、reAppear方法:用于重新产生蛋的方法
代码如下:
public class Egg {
private int
private int
private static final int BLOCK_WIDTH = SnakeFrame.BLOCK_WIDTH;
private static final int BLOCK_HEIGHT = SnakeFrame.BLOCK_HEIGHT;
private static final Random r = new Random();
private Color color = Color.RED;
public Egg(int row, int col) {
this.row =
this.col =
public Egg() {
this((r.nextInt(SnakeFrame.ROW-2))+2,(r.nextInt(SnakeFrame.COL-2))+2);
public void reAppear(){
this.row = (r.nextInt(SnakeFrame.ROW-2))+2;
this.col = (r.nextInt(SnakeFrame.COL-2))+2;
public void draw(Graphics g){
Color c= g.getColor();
g.setColor(color);
g.fillOval(col*BLOCK_WIDTH, row*BLOCK_HEIGHT, BLOCK_WIDTH, BLOCK_HEIGHT);
g.setColor(c);
if(color==Color.RED){
color = Color.BLUE;
color = Color.RED;
public Rectangle getRect(){
return new Rectangle(col*BLOCK_WIDTH, row*BLOCK_HEIGHT, BLOCK_WIDTH, BLOCK_HEIGHT);
蛇吃蛋,怎么样才能判断蛇吃到蛋了呢,这就需要用到碰撞检测了。
这里我们在Snake类中添加一个eatEgg方法。当蛇吃到蛋之后,就需要将蛇的长度+1,这里处理的是在蛇的头部添加一个节点,当蛋被吃掉之后,就需要再重新随机产生一个蛋。
代码如下:
public Rectangle getRect(){
return new Rectangle(head.col*BLOCK_WIDTH, head.row*BLOCK_HEIGHT, BLOCK_WIDTH, BLOCK_HEIGHT);
public boolean eatEgg(Egg egg){
if(this.getRect().intersects(egg.getRect())){
addNodeInHead();
egg.reAppear();
return true;
return false;
以上就完成了蛇吃蛋的功能。
完成的功能:添加边界处理
在我们熟悉的贪吃蛇游戏中,我们一般都知道,当蛇撞到墙或者是撞到自己身体的某一部分,则游戏就结束。下面我们就来实现这一功能。
在Snake类中,添加checkDead方法
private void checkDead() {
//头结点的边界检查
if(head.row&2||head.row&SnakeFrame.ROW||head.col&0||head.col&SnakeFrame.COL){
this.sf.gameOver()
//头结点与其它结点相撞也是死忙
for(Node node =head.next
if(head.row==node.row&&head.col == node.col){
this.sf.gameOver()
如果蛇撞墙或是撞到自己本身的某一个部分。则调用SnakeFrame类中的gameOver()方法来进行一定的处理。
本游戏的处理方法为:通过设置一个boolean 变量,来停止游戏并提示相关信息。
具体代码如下:
private boolean b_gameOver = false;
public void gameOver(){
b_gameOver = true;
public void update(Graphics g) {
if(b_gameOver){
g.drawString("游戏结束!!!", ROW/2*BLOCK_HEIGHT, COL/2*BLOCK_WIDTH);
以上就完成了蛇是否撞墙或是撞到自身一部分的功能。
以上基本上实现了贪吃蛇的基本功能。剩下的一些功能不再介绍,例如:添加得分记录、通过键盘某按键来控制游戏的停止、重新开始、再来一局等。
以上的功能虽然没有介绍,但是在代码中,我有实现这些相应的功能。
完整代码可以在这里获取:
没有更多推荐了,
不良信息举报
举报内容:
《Java小游戏实现》:贪吃蛇
举报原因:
原文地址:
原因补充:
最多只允许输入30个字
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!用Java编写2048,玩着自己编写的游戏就是爽用Java编写2048,玩着自己编写的游戏就是爽LaoGuo说技术百家号2048作为一款益智类的游戏,相信很多人都玩过,小编今天就教大家用Java的eclipse来编写2048,接下来我将展示代码。1、首先设置一些调用程序importjava.awt.Cimportjava.awt.EventQimportjava.awt.BorderLimportjava.awt.FlowLimportjava.awt.Fimportjava.awt.event.*;importjava.util.Rimportjavax.swing.BorderFimportjavax.swing.Iimportjavax.swing.ImageIimportjavax.swing.JFimportjavax.swing.JLimportjavax.swing.JPimportjavax.swing.SwingCimportjavax.swing.border.*;importjavax.swing.JTextFpublicclassCopy2048extendsJFrame{privateJPanelscoresPprivateJPanelmainPprivateJLabellabelMaxSprivateJLabellabelSprivateJL//提示操作标签privateJTextFieldtextMaxSprivateJLabeltextSprivateJLabel[][]privateIconicon2;privateinttimes=16;//记录剩余空方块数目privateintscores=0;//记录分数privateintl1,l2,l3,l4,l5;//用于判断游戏是否失败2、继续写Fontfont=newFont("",Font.BOLD,14);//设置字体类型和大小Fontfont2=newFont("",Font.BOLD,30);Randomrandom=newRandom();publicstaticvoidmain(String[]args){EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{Copy2048frame=newCopy2048();frame.setVisible(true);//Threadthread=newThread(frame);//thread.start();catch(Exceptione1){e1.printStackTrace();});3、继续/***构造方法*/publicCopy2048(){super();setResizable(false);//禁止调整窗体大小getContentPane().setLayout(null);//设置空布局setBounds(500,50,500,615);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setTitle("2048PC版");//设置窗体标题scoresPane=newJPanel();//创建分数显示面板scoresPane.setBackground(Color.green);//设置分数显示面板的背景色scoresPane.setBounds(20,20,460,40);scoresPane.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.YELLOW));//设置得分面板的边框getContentPane().add(scoresPane);//将得分面板添加到窗体scoresPane.setLayout(null);//设置面板空布局labelMaxScores=newJLabel("最高分:");//最高分标签labelMaxScores.setFont(font);//设置字体类型和大小labelMaxScores.setBounds(10,5,50,30);//设置最懂啊分标签的位置尺寸scoresPane.add(labelMaxScores);//将最高分标签添加到得分容器中textMaxScores=newJTextField("暂不可用");//得分标签textMaxScores.setBounds(60,5,150,30);textMaxScores.setFont(font);textMaxScores.setEditable(false);scoresPane.add(textMaxScores);//将得分标签添加到分数面板中labelScores=newJLabel("得分:");labelScores.setFont(font);//设置字体类型和大小labelScores.setBounds(240,5,50,30);scoresPane.add(labelScores);textScores=newJLabel(String.valueOf(scores));textScores.setFont(font);textScores.setBounds(290,5,150,30);scoresPane.add(textScores);mainPane=newJPanel();//创建游戏主面板mainPane.setBounds(20,70,460,500);//设置主面板位置尺寸this.getContentPane().add(mainPane);mainPane.setLayout(null);//设置空布局texts=newJLabel[4][4];//创建文本框二维数组for(inti=0;ifor(intj=0;jtexts[i][j]=newJLabel();//创建标签texts[i][j].setFont(font2);texts[i][j].setHorizontalAlignment(SwingConstants.CENTER);texts[i][j].setText("");texts[i][j].setBounds(120*j,120*i,100,100);//设置方块的大小位置setColor(i,j,"");4、继续texts[i][j].setOpaque(true);texts[i][j].setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.green));//设置方块边框颜色mainPane.add(texts[i][j]);//将创建的文本框放在tips=newJLabel("Tips:使用上、下、左、右键或者W、S、A、D键控制");tips.setFont(font);tips.setBounds(60,480,400,20);mainPane.add(tips);textMaxScores.addKeyListener(newKeyAdapter(){//为最高分标签添加按键监听器publicvoidkeyPressed(KeyEvente){do_label_keyPressed(e);//调用时间处理方法});Create2();Create2();/***按键输入事件的处理方法*@parame*/protectedvoiddo_label_keyPressed(finalKeyEvente){intcode=e.getKeyCode();//获取按键代码5、继续//a的引入是为了防止连加的情况出现SStringstr1;switch(code){caseKeyEvent.VK_LEFT:caseKeyEvent.VK_A://如果按键代码是左方向键或者A键for(inti=0;ia=5;for(intk=0;kfor(intj=1;jstr=texts[i][j].getText();//获取当前方块标签文本字符str1=texts[i][j-1].getText();//获取当前左1方块标签文本字符if(str1.compareTo("")==0){//如果左1方块文本为空字符texts[i][j-1].setText(str);//字符左移setColor(i,j-1,str);texts[i][j].setText("");//当前方块字符置空setColor(i,j,"");}elseif((str.compareTo(str1)==0)&&(j!=a)&&(j!=a-1)){//如果当前方块和左1方块文本字符相等num=Integer.parseInt(str);scores+=times++;str=String.valueOf(2*num);texts[i][j-1].setText(str);//左1方块文本字符变为两方块之和setColor(i,j-1,str);texts[i][j].setText("");//当前方块字符置空6、继续setColor(i,j,"");a=j;l1=1;//用于判断游戏是否失败Create2();caseKeyEvent.VK_RIGHT:caseKeyEvent.VK_D:for(inti=0;ia=5;for(intk=0;kfor(intj=2;j>=0;j--){str=texts[i][j].getText();str1=texts[i][j+1].getText();if(str1.compareTo("")==0){texts[i][j+1].setText(str);setColor(i,j+1,str);texts[i][j].setText("");setColor(i,j,"");elseif(str.compareTo(str1)==0&&j!=a&&j!=a+1){num=Integer.parseInt(str);scores+=times++;str=String.valueOf(2*num);texts[i][j+1].setText(str);setColor(i,j+1,str);texts[i][j].setText("");setColor(i,j,"");a=j;l2=1;Create2();caseKeyEvent.VK_UP:caseKeyEvent.VK_W:for(intj=0;ja=5;for(intk=0;kfor(inti=1;istr=texts[i][j].getText();str1=texts[i-1][j].getText();if(str1.compareTo("")==0){texts[i-1][j].setText(str);setColor(i-1,j,str);texts[i][j].setText("");setColor(i,j,"");elseif(str.compareTo(str1)==0&&i!=a&&i!=a-1){num=Integer.parseInt(str);scores+=times++;str=String.valueOf(2*num);texts[i-1][j].setText(str);setColor(i-1,j,str);texts[i][j].setText("");setColor(i,j,"");a=i;l3=1;Create2();caseKeyEvent.VK_DOWN:caseKeyEvent.VK_S:for(intj=0;ja=5;for(intk=0;kfor(inti=2;i>=0;i--){str=texts[i][j].getText();str1=texts[i+1][j].getText();if(str1.compareTo("")==0){texts[i+1][j].setText(str);setColor(i+1,j,str);texts[i][j].setText("");setColor(i,j,"");elseif(str.compareTo(str1)==0&&i!=a&&i!=a+1){num=Integer.parseInt(str);scores+=times++;str=String.valueOf(2*num);texts[i+1][j].setText(str);setColor(i+1,j,str);texts[i][j].setText("");setColor(i,j,"");a=i;l4=1;Create2();default:textScores.setText(String.valueOf(scores));/***在随机位置产生一个2号方块的方法*@parami,j*/publicvoidCreate2(){inti,j;booleanr=Sif(times>0){while(!r){i=random.nextInt(4);j=random.nextInt(4);str=texts[i][j].getText();if((str.compareTo("")==0)){texts[i][j].setIcon(icon2);texts[i][j].setText("2");setColor(i,j,"2");times--;r=l1=l2=l3=l4=0;elseif(l1>0&&l2>0&&l3>0&&l4>0){//l1到l4同时被键盘赋值为1说明任何方向键都不能产生新的数字2,说明游戏失败tips.setText("GAMEOVER!");/***设置标签颜色*@parami,j,str*/publicvoidsetColor(inti,intj,Stringstr){switch(str){case"2":texts[i][j].setBackground(Color.yellow);case"4":texts[i][j].setBackground(Color.red);case"8":texts[i][j].setBackground(Color.pink);case"16":texts[i][j].setBackground(Color.orange);case"32":texts[i][j].setBackground(Color.magenta);case"64":texts[i][j].setBackground(Color.LIGHT_GRAY);case"128":texts[i][j].setBackground(Color.green);case"256":texts[i][j].setBackground(Color.gray);case"512":texts[i][j].setBackground(Color.DARK_GRAY);case"1024":texts[i][j].setBackground(Color.cyan);case"2048":texts[i][j].setBackground(Color.blue);case"":case"4096":texts[i][j].setBackground(Color.white);default:好了全部代码都在这了,一起来玩2048吧一起爽吧!!!!!本文仅代表作者观点,不代表百度立场。系作者授权百家号发表,未经许可不得转载。LaoGuo说技术百家号最近更新:简介:这里是你最想看到的。作者最新文章相关文章

我要回帖

更多关于 switch游戏发售表 的文章

 

随机推荐