实例介绍
【实例简介】
【实例截图】
【核心代码】
package com.softeem.game; import java.awt.Font; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.swing.JFrame; import javax.swing.JOptionPane; /** * 游戏画板 * @author Mr.Zhou * */ public class GameFrame extends JFrame implements KeyListener,Runnable{ private List<BackGround> allBG = new ArrayList<BackGround>(); private Mario mario = null; private BackGround nowBG = null; private Thread t = null; //是否已经开始游戏 private boolean isStart = false; public static void main(String[] args){ GameFrame frame = new GameFrame(); frame.setTitle("超级玛丽v1.0~周哥"); MP3Player mp3 = new MP3Player("music/bg.mp3"); mp3.play(); } //初始化 public GameFrame(){ // this.setTitle("玛丽奥"); this.setSize(900, 600); // int width = Toolkit.getDefaultToolkit().getScreenSize().width; // int height = Toolkit.getDefaultToolkit().getScreenSize().height; // this.setLocation((width-900)/2, (height-600)/2); this.setLocationRelativeTo(null); this.setResizable(false); //初始化图片 StaticValue.init(); //创建全部场景 for(int i=1;i<=7;i ){ this.allBG.add(new BackGround(i, i==7?true:false)); } //将第一个场景设置为当前场景 this.nowBG = this.allBG.get(0); //初始化玛丽奥 this.mario = new Mario(0); //将玛丽奥放入场景中 this.mario.setBg(nowBG); this.repaint(); this.addKeyListener(this); this.t = new Thread(this); t.start(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } //设置游戏标题 public void setTitle(String title){ super.setTitle(title); } //设置游戏窗体尺寸 public void setSize(int width,int height){ super.setSize(width, height); } //将玛丽奥添加到游戏中 public void setRole(Mario mario){ mario.setBg(nowBG); } public void paint(Graphics g) { //双缓存 BufferedImage image = new BufferedImage(900, 600, BufferedImage.TYPE_3BYTE_BGR); Graphics g2 = image.getGraphics(); Font font = new Font("楷体",Font.BOLD,25); if(this.isStart){ //绘制背景 g2.drawImage(this.nowBG.getBgImage(), 0, 0, this); //绘制生命 g2.setFont(font); g2.drawString("生命:" this.mario.getLife(), 620, 50); //绘制分数 g2.drawString("分数:" this.mario.getScore(), 750, 50); //绘制怪物敌人 Iterator<Enemy> iterEnemy = this.nowBG.getAllEnemy().iterator(); while(iterEnemy.hasNext()){ Enemy e = iterEnemy.next(); g2.drawImage(e.getShowImage(), e.getX(), e.getY(), this); } //绘制障碍物 Iterator<Obstruction> iter = this.nowBG.getAllObstruction().iterator(); while(iter.hasNext()){ Obstruction ob = iter.next(); g2.drawImage(ob.getShowImage(), ob.getX(), ob.getY(), this); } //绘制玛丽奥 g2.drawImage(this.mario.getShowImage(), this.mario.getX(), this.mario.getY(), this); }else{ g2.drawImage(StaticValue.startImage, 0, 0, this); } //把缓存图片绘制进去 g.drawImage(image, 0, 0, this); } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { if(this.isStart){ //玛丽奥的移动控制 if(e.getKeyCode()==39){ this.mario.rightMove(); } if(e.getKeyCode()==37){ this.mario.leftMove(); } //跳跃控制 if(e.getKeyCode()==38){ this.mario.jump(); } }else if(e.getKeyCode()==32){ this.isStart = true; this.nowBG.enemyStartMove(); this.mario.setScore(0); this.mario.setLife(3); } } public void keyReleased(KeyEvent e) { if(this.isStart){ //控制玛丽奥的停止 if(e.getKeyCode()==39){ this.mario.rightStop();; } if(e.getKeyCode()==37){ this.mario.leftStop();; } } } public void run() { while(true){ this.repaint(); try { Thread.sleep(50); if(this.mario.getX() >= 840){ //切换场景 this.nowBG = this.allBG.get(this.nowBG.getSort()); //将场景放入玛丽奥中 this.mario.setBg(nowBG); this.nowBG.enemyStartMove(); //修改马里奥坐标 this.mario.setX(0); } if(this.mario.isDead()){ JOptionPane.showMessageDialog(this, "游戏结束"); System.exit(0); } if(this.mario.isClear()){ JOptionPane.showMessageDialog(this, "恭喜游戏通关!"); System.exit(0); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论