Java贪吃蛇小游戏,我家小AD从小水蛇成长为水中巨蟒!

老程序员花二天时间做的Java贪吃蛇

引言:

每一个人童年时都有一个乃至数个伟大的梦想,我小时候的梦想:

1.孙悟空,因为我想成为一个降妖除魔、72变,最重要的是我想去哪就去哪,一个跟斗就起飞!
2.葫芦娃,因为分开时个个独挡一面、身怀绝技;合体时,完美无敌,想大就大、想小就小!
3.军事家,我从小比较喜欢看抗日电视剧,感觉我们国家从苦难中走出来很不容易,我那时希望自己成为军事家,能够保卫好我们的国家!
4.科学家,老师跟我们说科学家是国家栋梁!我也希望能够成为杰出的科学家,提升我们国家的科技。
5.工程师,成为一名优秀的工程师,能建设好我们的国家!

带着这些梦想,我通过十几年的努力学习,我从农村来到了城市,从学校走出来到了社会,成为了一名 IT工程师
这样

  • 215
    点赞
  • 400
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 142
    评论
以下是一个简单的 Java 贪吃蛇小游戏的示例代码: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SnakeGame extends JFrame implements KeyListener { private static final long serialVersionUID = 1L; private final int WIDTH = 300; private final int HEIGHT = 300; private final int DOT_SIZE = 10; private final int ALL_DOTS = 900; private final int RAND_POS = 29; private int DELAY = 140; private int x[] = new int[ALL_DOTS]; private int y[] = new int[ALL_DOTS]; private int dots; private int apple_x; private int apple_y; private boolean leftDirection = false; private boolean rightDirection = true; private boolean upDirection = false; private boolean downDirection = false; private boolean inGame = true; private Timer timer; private Image ball; private Image apple; private Image head; public SnakeGame() { addKeyListener(this); setTitle("Snake Game"); setBackground(Color.black); setResizable(false); setSize(WIDTH, HEIGHT); ImageIcon iid = new ImageIcon(getClass().getResource("dot.png")); ball = iid.getImage(); ImageIcon iia = new ImageIcon(getClass().getResource("apple.png")); apple = iia.getImage(); ImageIcon iih = new ImageIcon(getClass().getResource("head.png")); head = iih.getImage(); initGame(); } public void initGame() { dots = 3; for (int z = 0; z < dots; z++) { x[z] = 50 - z * 10; y[z] = 50; } locateApple(); timer = new Timer(DELAY, new GameCycle()); timer.start(); } public void paint(Graphics g) { super.paint(g); if (inGame) { g.drawImage(apple, apple_x, apple_y, this); for (int z = 0; z < dots; z++) { if (z == 0) g.drawImage(head, x[z], y[z], this); else g.drawImage(ball, x[z], y[z], this); } Toolkit.getDefaultToolkit().sync(); } else { gameOver(g); } } public void gameOver(Graphics g) { String msg = "Game Over"; Font small = new Font("Helvetica", Font.BOLD, 14); FontMetrics metr = getFontMetrics(small); g.setColor(Color.white); g.setFont(small); g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2, HEIGHT / 2); } public void checkApple() { if ((x[0] == apple_x) && (y[0] == apple_y)) { dots++; locateApple(); } } public void move() { for (int z = dots; z > 0; z--) { x[z] = x[(z - 1)]; y[z] = y[(z - 1)]; } if (leftDirection) { x[0] -= DOT_SIZE; } if (rightDirection) { x[0] += DOT_SIZE; } if (upDirection) { y[0] -= DOT_SIZE; } if (downDirection) { y[0] += DOT_SIZE; } } public void checkCollision() { for (int z = dots; z > 0; z--) { if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) { inGame = false; } } if (y[0] >= HEIGHT) { inGame = false; } if (y[0] < 0) { inGame = false; } if (x[0] >= WIDTH) { inGame = false; } if (x[0] < 0) { inGame = false; } if (!inGame) { timer.stop(); } } public void locateApple() { int r = (int) (Math.random() * RAND_POS); apple_x = ((r * DOT_SIZE)); r = (int) (Math.random() * RAND_POS); apple_y = ((r * DOT_SIZE)); } public void keyReleased(KeyEvent e) { } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) { leftDirection = true; upDirection = false; downDirection = false; } if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) { rightDirection = true; upDirection = false; downDirection = false; } if ((key == KeyEvent.VK_UP) && (!downDirection)) { upDirection = true; rightDirection = false; leftDirection = false; } if ((key == KeyEvent.VK_DOWN) && (!upDirection)) { downDirection = true; rightDirection = false; leftDirection = false; } } public void keyTyped(KeyEvent e) { } class GameCycle implements ActionListener { public void actionPerformed(ActionEvent e) { if (inGame) { checkApple(); checkCollision(); move(); } repaint(); } } public static void main(String[] args) { new SnakeGame().setVisible(true); } } ``` 在代码中,`initGame()` 方法初始化游戏,`paint(Graphics g)` 方法绘制游戏界面,`checkApple()` 方法检查是否吃到了苹果,`move()` 方法移动贪吃蛇,`checkCollision()` 方法检查是否发生碰撞,`locateApple()` 方法随机生成苹果的位置,`keyPressed(KeyEvent e)` 方法处理键盘事件。运行代码后,即可开始玩贪吃蛇小游戏

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 142
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程界小明哥

请博主喝瓶水,博主持续输出!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值