Free Running Timer ?

8 篇文章 1 订阅
7 篇文章 0 订阅
A free running timer keeps repeatedly running from end to end without being reloaded or stopped at intermediate states. It counts the input pulses from zero to the maximum count and on reaching the full count, sets a flag (which can be used to generate an interrupt) and resets itself to zero and continues the counting process. A free running timer can be used to generate interrupts at regular intervals and to generate accurate delays
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的贪吃蛇小游戏的Java实现,具体代码如下: ```java import java.awt.*; import java.awt.event.*; import java.util.Random; import javax.swing.*; public class SnakeGame extends JPanel implements ActionListener { private static final int WIDTH = 300; private static final int HEIGHT = 300; private static final int UNIT_SIZE = 10; private static final int GAME_UNITS = (WIDTH * HEIGHT) / UNIT_SIZE; private static final int DELAY = 75; private final int[] x = new int[GAME_UNITS]; private final int[] y = new int[GAME_UNITS]; private int bodyParts = 6; private int applesEaten; private int appleX; private int appleY; private char direction = 'R'; private boolean running = false; private final Timer timer; public SnakeGame() { this.setPreferredSize(new Dimension(WIDTH, HEIGHT)); this.setBackground(Color.black); this.setFocusable(true); this.addKeyListener(new MyKeyAdapter()); startGame(); timer = new Timer(DELAY, this); timer.start(); } public void startGame() { newApple(); running = true; } public void paintComponent(Graphics g) { super.paintComponent(g); draw(g); } public void draw(Graphics g) { if (running) { // Draw the apple g.setColor(Color.red); g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE); // Draw the snake for (int i = 0; i < bodyParts; i++) { if (i == 0) { g.setColor(Color.green); } else { g.setColor(new Color(45, 180, 0)); } g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE); } // Draw the score g.setColor(Color.red); g.setFont(new Font("Ink Free", Font.BOLD, 25)); FontMetrics metrics = getFontMetrics(g.getFont()); g.drawString("Score: " + applesEaten, (WIDTH - metrics.stringWidth("Score: " + applesEaten)) / 2, g.getFont().getSize()); } else { gameOver(g); } } public void newApple() { appleX = new Random().nextInt(WIDTH / UNIT_SIZE) * UNIT_SIZE; appleY = new Random().nextInt(HEIGHT / UNIT_SIZE) * UNIT_SIZE; } public void move() { for (int i = bodyParts; i > 0; i--) { x[i] = x[i - 1]; y[i] = y[i - 1]; } switch (direction) { case 'U': y[0] = y[0] - UNIT_SIZE; break; case 'D': y[0] = y[0] + UNIT_SIZE; break; case 'L': x[0] = x[0] - UNIT_SIZE; break; case 'R': x[0] = x[0] + UNIT_SIZE; break; } } public void checkApple() { if ((x[0] == appleX) && (y[0] == appleY)) { bodyParts++; applesEaten++; newApple(); } } public void checkCollisions() { // Check if head collides with body for (int i = bodyParts; i > 0; i--) { if ((x[0] == x[i]) && (y[0] == y[i])) { running = false; } } // Check if head touches left border if (x[0] < 0) { running = false; } // Check if head touches right border if (x[0] > WIDTH) { running = false; } // Check if head touches top border if (y[0] < 0) { running = false; } // Check if head touches bottom border if (y[0] > HEIGHT) { running = false; } // Stop timer if game is over if (!running) { timer.stop(); } } public void gameOver(Graphics g) { g.setColor(Color.red); g.setFont(new Font("Ink Free", Font.BOLD, 40)); FontMetrics metrics1 = getFontMetrics(g.getFont()); g.drawString("Game Over", (WIDTH - metrics1.stringWidth("Game Over")) / 2, HEIGHT / 2); g.setColor(Color.red); g.setFont(new Font("Ink Free", Font.BOLD, 25)); FontMetrics metrics2 = getFontMetrics(g.getFont()); g.drawString("Score: " + applesEaten, (WIDTH - metrics2.stringWidth("Score: " + applesEaten)) / 2, g.getFont().getSize()); } @Override public void actionPerformed(ActionEvent e) { if (running) { move(); checkApple(); checkCollisions(); } repaint(); } public class MyKeyAdapter extends KeyAdapter { @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_LEFT: if (direction != 'R') { direction = 'L'; } break; case KeyEvent.VK_RIGHT: if (direction != 'L') { direction = 'R'; } break; case KeyEvent.VK_UP: if (direction != 'D') { direction = 'U'; } break; case KeyEvent.VK_DOWN: if (direction != 'U') { direction = 'D'; } break; } } } public static void main(String[] args) { JFrame frame = new JFrame("Snake Game"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new SnakeGame()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } } ``` 您可以通过运行该代码创建一个简单的贪吃蛇游戏,并使用W、A、S、D键来控制蛇的方向。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值