用JAVA实现吃豆人小游戏
用JAVA实现吃豆人小游戏
- 游戏运行效果
- Model.java
- Pacman.java
- 完整的游戏代码及资源文件
游戏运行效果
Model.java
package pacman;import javax.swing.*;
import javax.swing.Timer;
import javax.swing.JPanel;import java.awt.*;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;public class Model extends JPanel implements ActionListener {private Dimension d; //窗口的宽度private final Font smallFont = new Font("Arial",Font.BOLD,14); //游戏内文字字体及字号private boolean inGame = false; //判断游戏是否开始private boolean dying = false; //判断Pacman是否存活private final int BLOCK_SIZE = 24; //豆 横排个数: 24private final int N_BLOCK = 15; //豆 竖排个数: 15private final int SCREEN_SIZE = N_BLOCK * N_BLOCK; //共计:24 * 15 = 360 (个)private final int MAX_GHOSTS = 12; //游戏内敌人的数量private final int PACMAN_SPEED = 6; //Pacman移动的速度private int N_GHOSTS = 6; //敌人数量private int lives,score; //生命值,关卡得分private int [] dx, dy;private int [] ghost_x, ghost_y, ghost_dx, ghost_dy, ghostSpeed;private Image heart, ghost;private Image up, down, left, right;private int pacman_x, pacman_y, pacmand_x, pacmand_y;private int req_dx, req_dy;private final int validSpeeds[] = {1,2,3,4,6,8};private final int maxSpeed = 6;private int currentSpeed = 3;private short [] screenDate;private Timer timer;/*** 0 = blue蓝色 4 = right border右边款* 1 = left border左边框 8 = bottom border底边框* 2 = top border顶边框 16 = white dots白点豆*/private final short levelData[] = {19, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 22,17, 16, 16, 16, 16, 24, 16, 16, 16, 16, 16, 16, 16, 16, 20,25, 24, 24, 24, 28, 0, 17, 16, 16, 16, 16, 16, 16, 16, 20,0, 0, 0, 0, 0, 0, 17, 16, 16, 16, 16, 16, 16, 16, 20,19, 18, 18, 18, 18, 18, 16, 16, 16, 16, 24, 24, 24, 24, 20,17, 16, 16, 16, 16, 16, 16, 16, 16, 20, 0, 0, 0, 0, 21,17, 16, 16, 16, 16, 16, 16, 16, 16, 20, 0, 0, 0, 0, 21,17, 16, 16, 16, 24, 16, 16, 16, 16, 20, 0, 0, 0, 0, 21,17, 16, 16, 20, 0, 17, 16, 16, 16, 16, 18, 18, 18, 18, 20,17, 24, 24, 28, 0, 25, 24, 24, 16, 16, 16, 16, 16, 16, 20,21, 0, 0, 0, 0, 0, 0, 0, 17, 16, 16, 16, 16, 16, 20,17, 18, 18, 22, 0, 19, 18, 18, 16, 16, 16, 16, 16, 16, 20,17, 16, 16, 20, 0, 17, 16, 16, 16, 16, 16, 16, 16, 16, 20,17, 16, 16, 20, 0, 17, 16, 16, 16, 16, 16, 16, 16, 16, 20,25, 24, 24, 24, 26, 24, 24, 24, 24, 24, 24, 24, 24, 24, 28};public Model() {loadImages();initVariables();addKeyListener(new TAdapter());setFocusable(true);initGame();}/*导入图片—吃豆人的每一个动作图*/private void loadImages() {down = new ImageIcon("images/down.gif").getImage();up = new ImageIcon("images/up.gif").getImage();left = new ImageIcon("images/left.gif").getImage();right = new ImageIcon("images/right.gif").getImage();ghost = new ImageIcon("images/ghost.gif").getImage();heart = new ImageIcon("images/heart.png").getImage();}/*游戏标题文字*/public void showIntroScreen(Graphics2D graphics2D) {String start = "Press SPACE to start"; //游戏开始文字graphics2D.setColor(Color.yellow); //文字颜色graphics2D.drawString(start,(SCREEN_SIZE) / 4, 150); //文字在窗体的位置}/*分数模块*/public void drawScore(Graphics2D graphics2D) {graphics2D.setFont(smallFont); //分数字体 : 小号字体graphics2D.setColor(new Color(5,151,79)); //分数文字颜色 :重构颜色RGB格式String s = "Score: " + score;graphics2D.drawString(s,SCREEN_SIZE / 2 + 96,SCREEN_SIZE + 16);for (int i = 0; i < lives; i++) {graphics2D.drawImage(heart,i * 28 + 8, SCREEN_SIZE + 1,this);}}private void initVariables() {screenDate = new short[N_BLOCK * N_BLOCK];d = new Dimension(400,400);ghost_x = new int[MAX_GHOSTS];ghost_dx = new int[MAX_GHOSTS];ghost_y = new int[MAX_GHOSTS];ghost_dy = new int[MAX_GHOSTS];dx = new int[4];dy = new int[4];timer = new Timer(60,this); //每40ms 绘制一次图形及游戏运行速度(可以作为游戏难度调整功能)timer.start();}/*游戏初始化*/private void initGame() {lives = 3; //初始化生命值数量score = 0; //初始化分数为0initLevel();N_GHOSTS = 6; //初始化敌人数量currentSpeed = 3; //初始化移动速度}/*游戏关卡范围*/private void initLevel() {int i;for (i = 0; i < N_BLOCK * N_BLOCK; i++) {screenDate[i] = levelData[i];}//continueLevel();}//游戏图形化初始private void playGame(Graphics2D graphics2D) {if (dying) {death();} else {movePacman();drawPacman(graphics2D);moveGhost(graphics2D);checkMaze();}}public void movePacman() {int pos;short ch;if (pacman_x % BLOCK_SIZE == 0 && pacman_y % BLOCK_SIZE == 0) {pos = pacman_x / BLOCK_SIZE + N_BLOCK * (int) (pacman_y / BLOCK_SIZE);ch = screenDate[pos];if ((ch & 16 ) != 0) { //判断pacman吃到豆子(16)screenDate[pos] = (short) (ch & 15);score++;}if (req_dx != 0 || req_dy != 0 ) {if (!((req_dx == -1 && req_dy == 0 && (ch & 1) != 0)|| (req_dy == 1 && req_dy == 0 && (ch & 4) != 0)|| (req_dx == 0 && req_dy == -1 && (ch & 2) != 0)|| (req_dx == 0 && req_dy == 1 && (ch & 8) != 0))) {pacmand_y = req_dy;pacmand_x = req_dx;}}if (( pacmand_x == -1 && pacmand_y == 0 && (ch & 1) != 0)|| (pacmand_x == 1 && pacmand_y == 0 && (ch & 4) != 0)|| (pacmand_x == 0 && pacmand_y == -1 && (ch & 2) != 0)|| (pacmand_x == 0 && pacmand_y == 1 && (ch & 8) != 0)) {pacmand_x = 0;pacmand_y = 0;}}pacman_x = pacman_x + PACMAN_SPEED * pacmand_x;pacman_y = pacman_y + PACMAN_SPEED * pacmand_y;}/*绘制Pacman移动模型*/public void drawPacman(Graphics2D graphics2D) {if (req_dx == -1) {graphics2D.drawImage(left,pacman_x + 1, pacman_y + 1,this);}else if (req_dx == 1) {graphics2D.drawImage(right,pacman_x + 1, pacman_y + 1,this);}else if (req_dy == -1) {graphics2D.drawImage(up,pacman_x + 1, pacman_y + 1,this);}else {graphics2D.drawImage(down,pacman_x + 1,pacman_y + 1,this);}}/*Ghost移动模型算法*/public void moveGhost(Graphics2D graphics2D) {int pos;int count;for (int i = 0; i < N_GHOSTS; i++) {if (ghost_x[i] % BLOCK_SIZE == 0 && ghost_y[i] % BLOCK_SIZE == 0) {pos = ghost_x[i] / BLOCK_SIZE + N_BLOCK * (int) (ghost_y[i] / BLOCK_SIZE);count = 0;if ((screenDate[pos] & 1 ) == 0 && ghost_dx[i] != 1 ) {dx[count] = -1;dy[count] = 0;count++;}if ((screenDate[pos] & 2 ) == 0 && ghost_dy[i] != 1 ) {dx[count] = 0;dy[count] = -1;count++;}if ((screenDate[pos] & 4 ) == 0 && ghost_dx[i] != -1 ) {dx[count] = 1;dy[count] = 0;count++;}if ((screenDate[pos] & 8 ) == 0 && ghost_dy[i] != -1 ) {dx[count] = 0;dy[count] = 1;count++;}if (count == 0) {if ((screenDate[pos] & 15) == 15) {ghost_dy[i] = 0;ghost_dx[i] = 0;}else {ghost_dy[i] = -ghost_dy[i];ghost_dx[i] = -ghost_dx[i];}} else {count = (int) (Math.random() * count);if (count > 3) {count = 3;}ghost_dx[i] = dx[count];ghost_dy[i] = dy[count];}}ghost_x[i] = ghost_x[i] + (ghost_dx[i] * ghostSpeed[i]);ghost_y[i] = ghost_y[i] + (ghost_dy[i] * ghostSpeed[i]);drawGhost(graphics2D,ghost_x[i] + 1,ghost_y[i] + 1);if (pacman_x > (ghost_x[i] - 12) && pacman_x < (ghost_x[i] + 12)&& pacman_y > (ghost_y[i] - 12) && pacman_y < (ghost_y[i] + 12)&& inGame) {dying = true;}}}/*绘制Ghost移动模型*/public void drawGhost(Graphics2D graphics2D,int x, int y) {graphics2D.drawImage(ghost,x,y,this);}public void checkMaze() {int i = 0;boolean finished = true;while (i < N_BLOCK * N_BLOCK && finished ) {if ((screenDate[i] & 48 ) != 0 ) {finished = false;}} i++;if (finished) {score += 50;if (N_GHOSTS < MAX_GHOSTS) {N_GHOSTS++;}if (currentSpeed < maxSpeed) {currentSpeed++;}} initLevel();}/*死亡机制判定*/private void death() {lives--; //生命值自减一if (lives == 0) { //判断当生命值等于0的时候,使游戏结束inGame = false;}continueLevel();}/*角色随机移动速度算法*/private void continueLevel() {int dx = 1;int random;for (int i = 0; i < N_GHOSTS; i++) {ghost_y[i] = 4 * BLOCK_SIZE;ghost_x[i] = 4 * BLOCK_SIZE;ghost_dy[i] = 0;ghost_dx[i] = dx;dx = -dx;random = (int) (Math.random() * (currentSpeed +1));if (random > currentSpeed) {random = currentSpeed;}ghostSpeed[i] = validSpeeds[random];}pacman_x = 7 * BLOCK_SIZE;pacman_y = 11 * BLOCK_SIZE;pacmand_x = 0;pacmand_y = 0;req_dx = 0;req_dy = 0;dying = false;}public void drawMaze(Graphics2D graphics2D) {short i = 0;int x,y;for (y = 0; y < SCREEN_SIZE; y += BLOCK_SIZE) {for (x = 0; x < SCREEN_SIZE; x += BLOCK_SIZE) {graphics2D.setColor(new Color(0,72,251));graphics2D.setStroke(new BasicStroke(5));if ((screenDate[i] == 0)) {graphics2D.fillRect(x,y, BLOCK_SIZE,BLOCK_SIZE);}if ((screenDate[i] & 1) != 0) {graphics2D.drawLine(x,y,x,y + BLOCK_SIZE - 1);}if ((screenDate[i] & 2) != 0) {graphics2D.drawLine(x,y,x + BLOCK_SIZE - 1,y);}if ((screenDate[i] & 4) != 0) {graphics2D.drawLine(x + BLOCK_SIZE - 1,y,x + BLOCK_SIZE - 1,y + BLOCK_SIZE - 1);}if ((screenDate[i] & 8) != 0) {graphics2D.drawLine(x,y + BLOCK_SIZE - 1,x + BLOCK_SIZE - 1,y + BLOCK_SIZE - 1);}if ((screenDate[i] & 4) != 0) {graphics2D.setColor(new Color(255,255,255));graphics2D.fillOval(x + 10, y + 10, 6, 6);}i++;/** 数值说明:* 0 = blue 4 = right border* 1 = left border 8 = bottom border* 2 = top border 16 = white dots* */}}}public void paintComponent(Graphics graphics) {super.paintComponent(graphics);Graphics2D graphics2D = (Graphics2D) graphics;graphics2D.setColor(Color.black);graphics2D.fillRect(0,0, d.width,d.height);drawMaze(graphics2D);drawScore(graphics2D);if (inGame) {playGame(graphics2D);}else {showIntroScreen(graphics2D);}Toolkit.getDefaultToolkit().sync();}/*键盘控制部分*/class TAdapter extends KeyAdapter {public void keyPressed(KeyEvent e) {int key = e.getKeyCode();if (inGame) { //首先判断是否游戏开始if (key == KeyEvent.VK_LEFT) { //监听到键盘方向左键触发 向左移动一格子,Y轴不变req_dx = -1;req_dy = 0;} else if (key == KeyEvent.VK_RIGHT) { //监听到键盘方向右键触发 向右移动一格子,Y轴不变req_dx = 1;req_dy = 0;} else if (key == KeyEvent.VK_UP) { //监听到键盘方向上键触发 向上移动一格子,Z轴不变req_dx = 0;req_dy = -1;} else if (key == KeyEvent.VK_DOWN) { //监听到键盘方向下键触发 向下移动一格子,Z轴不变req_dx = 0;req_dy = 1;} else if (key == KeyEvent.VK_ESCAPE && timer.isRunning()) { //监听到键盘ESC时或者时间结束,退出游戏inGame = false;} else {if (key == KeyEvent.VK_SPACE) { //监听到键盘SPACE时,开始游戏inGame = true;initGame();}}}}}@Overridepublic void actionPerformed(ActionEvent e) { repaint(); }
}
Pacman.java
package pacman;import javax.swing.*;public class Pacman extends JFrame {public Pacman() {add(new Model());}public static void main(String[] args) {Pacman pacman = new Pacman();pacman.setVisible(true);pacman.setTitle("Pacman Game"); //窗体标题pacman.setSize(380,420); //窗体的大小pacman.setDefaultCloseOperation(EXIT_ON_CLOSE);pacman.setLocationRelativeTo(null);}
}
完整的游戏代码及资源文件
下载地址https://download.csdn.net/download/weixin_50679163/78951807
用JAVA实现吃豆人小游戏相关推荐
- 基于C++控制台(Windows平台)的一个吃豆人小游戏
PacManX --南京大学2019秋季学期 "高级程序设计 "课程设计一 基于C++控制台(Windows平台)的一个吃豆人小游戏 已实现的目标: 地图支持自定义编辑(可编辑地图 ...
- 整活~使用webAI做一个网页AR吃豆人小游戏
一个好习惯,先给结论 使用网页端深度学习框架识别人脸,做一个AR吃豆人小游戏.吃豆人会随着人脸在镜头内的移动而移动,吃完全部豆子即为获胜. 在线体验地址:点我预览 代码地址:点我github 本文首发 ...
- 可怜的博主跟小豆人杠起来啦!Python制作的吃豆人小游戏,快来围观!!
相关文件 关注小编,私信小编领取哟! 当然别忘了一件三连哟~~ 对了大家可以关注小编的公众号哟~~ Python日志 开发环境 Python版本:3.6.4 相关模块: pygame模块: 以及一些P ...
- JS实现简单的吃豆人小游戏
吃豆人小游戏 今天练习了一下JS,写了一个吃豆人的小demo Html以及CSS部分 首先定义一个div,用来存放吃豆人的一些元素,我们给他加一个id="game",然后我们在这d ...
- C语言实现吃豆人小游戏(转载)
c语言实现吃豆人小游戏(转载) 游戏内还有一些bug,凑活着来玩一玩吧! #include <stdio.h> #include <iostream> #include < ...
- Dev-C++5.11游戏创作之吃豆人小游戏(转载)
Hi!大家好,我是你们的编程小王子!今天为大家转载了一个小游戏, 蒟蒻一枚https://blog.csdn.net/yueyuedog原创 代码我不过多解释,还是比较"简单"的 ...
- python 吃豆人_pacman 人工智能编程 吃豆人小游戏 可实现智能寻路 吃豆 通关 - 下载 - 搜珍网...
Berkeley人工智能吃豆人作业-Python/ Berkeley人工智能吃豆人作业-Python/再附带一份完整工程源码吧/ Berkeley人工智能吃豆人作业-Python/再附带一份完整工程源 ...
- 前端实现简易吃豆人小游戏
1.首先先将html文件里的初始代码设置完毕 2.规定范围 规定人物的活动范围,豆子的起始位置,人物的大小,并且逐次进行样式的编写 技术范围:css.html. 难点:需要在脑海中构思下一步的操作. ...
- C语言零基础项目:吃豆人小游戏!详细思路+源码分享
每天一个C语言小项目,提升你的编程能力! <吃豆游戏>是一款休闲小游戏,和贪吃蛇,球球大作战吃食物都是有差不多的游戏逻辑. 效果展示: 这个游戏本身很简单,一共3关,吃掉画面上全部小豆子就 ...
最新文章
- 无盘中用户数据的保存
- 解析Python中的条件语句和循环语句
- 腾讯机器狗,站起来了!
- 在Spring Framework中通过JNDI进行配置
- 【转】3:C#异步WaitAll的使用
- 机器学习领域综述大列表:真的很大, 你忍一忍
- mysql优化 my.cnf_MySQL性能调优my.cnf详解
- CDH使用Solr实现HBase二级索引
- linux运维零基础学习,没有基础怎么学习Linux运维?Linux学习
- 基于聚类算法与随机森林算法的手机终端换机推荐
- pycharm 代码跳转
- Golang 内建类型和内建函数 builtin包 注释翻译
- iOS前后台切换和监听
- 盗版windows7危害大
- 微信小程序实现多语言方案|中英互译
- 【满分】【华为OD机试真题2023 JAVAJS】查找重复代码
- 电子计算机司法鉴定客体特征,电子证据司法鉴定的含义和特点是什么?
- POJ3349-Snowflake Snow Snowflakes
- 华中科技大学计算机课程设计,华中科技大学计算机学院操作系统课程设计资料报告材料[1].doc...
- 今天,就让坏得很的糟老头子来告诉你顺序表的基操,零基础也可get!
热门文章
- 周易之阿拉伯几何原本谭
- 计算平均分 (5分)
- YAMMER使用手册
- com.google.protobuf.ServiceException: java.lang.NoClassDefFoundError: com/yammer/metrics/core/Gauge
- oracle全文搜索
- 三层交换机到底怎么配置才算高级?老杨手把手示范给你看
- windows无法连接到打印机 错误代码为0x00000533,解决办法
- Flash/Flex学习笔记(51):3维旋转与透视变换(PerspectiveProjection)
- npm update 作用,它做了那些事情?
- 要有成为真正JNCIE的勇气