claude-sonnet-4-20250514-thinking-32k· Anthropic LMArena Human Preference 140K
帮我写一个愤怒的小鸟的Java代码,同时小鸟用红色的圆表示,猪用绿色的圆表示,要用eclipse能够运行
- User
帮我写一个愤怒的小鸟的Java代码,同时小鸟用红色的圆表示,猪用绿色的圆表示,要用eclipse能够运行
- Assistant
我来为你写一个简单的愤怒的小鸟Java游戏,可以在Eclipse中直接运行:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import java.util.Iterator; public class AngryBirdsGame extends JFrame { private GamePanel gamePanel; public AngryBirdsGame() { setTitle("愤怒的小鸟"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); gamePanel = new GamePanel(); add(gamePanel); pack(); setLocationRelativeTo(null); setVisible(true); // 启动游戏循环 Timer gameTimer = new Timer(16, e -> gamePanel.update()); gameTimer.start(); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> new AngryBirdsGame()); } } class GamePanel extends JPanel implements MouseListener { private static final int PANEL_WIDTH = 800; private static final int PANEL_HEIGHT = 600; private static final int GROUND_HEIGHT = 100; private ArrayList<Bird> birds; private ArrayList<Pig> pigs; private Point launchPoint; private boolean aiming; private Point mousePoint; private int score; private boolean gameWon; public GamePanel() { setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT)); setBackground(Color.CYAN); addMouseListener(this); initGame(); } private void initGame() { birds = new ArrayList<>(); pigs = new ArrayList<>(); launchPoint = new Point(100, PANEL_HEIGHT - GROUND_HEIGHT - 50); score = 0; gameWon = false; // 创建几只猪 pigs.add(new Pig(600, PANEL_HEIGHT - GROUND_HEIGHT - 30)); pigs.add(new Pig(650, PANEL_HEIGHT - GROUND_HEIGHT - 30)); pigs.add(new Pig(700, PANEL_HEIGHT - GROUND_HEIGHT - 30)); pigs.add(new Pig(625, PANEL_HEIGHT - GROUND_HEIGHT - 80)); } public void update() { // 更新所有小鸟 Iterator<Bird> birdIter = birds.iterator(); while (birdIter.hasNext()) { Bird bird = birdIter.next(); bird.update(); // 检查小鸟是否碰撞猪 Iterator<Pig> pigIter = pigs.iterator(); while (pigIter.hasNext()) { Pig pig = pigIter.next(); if (bird.collidesWith(pig)) { pigIter.remove(); score += 100; } } // 移除落地或飞出屏幕的小鸟 if (bird.y > PANEL_HEIGHT - GROUND_HEIGHT || bird.x > PANEL_WIDTH + 50) { birdIter.remove(); } } // 检查是否获胜 if (pigs.isEmpty() && !gameWon) { gameWon = true; } repaint(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 绘制地面 g2d.setColor(Color.GREEN); g2d.fillRect(0, PANEL_HEIGHT - GROUND_HEIGHT, PANEL_WIDTH, GROUND_HEIGHT); // 绘制发射点 g2d.setColor(Color.BROWN); g2d.fillRect(launchPoint.x - 10, launchPoint.y - 10, 20, 50); // 绘制瞄准线 if (aiming && mousePoint != null) { g2d.setColor(Color.RED); g2d.setStroke(new BasicStroke(2)); g2d.drawLine(launchPoint.x, launchPoint.y, mousePoint.x, mousePoint.y); } // 绘制小鸟 for (Bird bird : birds) { bird.draw(g2d); } // 绘制猪 for (Pig pig : pigs) { pig.draw(g2d); } // 绘制UI g2d.setColor(Color.BLACK); g2d.setFont(new Font("Arial", Font.BOLD, 20)); g2d.drawString("得分: " + score, 10, 30); g2d.drawString("剩余猪: " + pigs.size(), 10, 60); if (gameWon) { g2d.setColor(Color.RED); g2d.setFont(new Font("Arial", Font.BOLD, 36)); g2d.drawString("恭喜获胜!", PANEL_WIDTH/2 - 100, PANEL_HEIGHT/2); } // 绘制说明 g2d.setColor(Color.BLACK); g2d.setFont(new Font("Arial", Font.PLAIN, 12)); g2d.drawString("点击并拖拽发射小鸟", 10, PANEL_HEIGHT - 10); } @Override public void mousePressed(MouseEvent e) { aiming = true; mousePoint = e.getPoint(); } @Override public void mouseReleased(MouseEvent e) { if (aiming) { // 计算发射力度和方向 int deltaX = launchPoint.x - e.getX(); int deltaY = launchPoint.y - e.getY(); // 创建新小鸟 Bird bird = new Bird(launchPoint.x, launchPoint.y); bird.setVelocity(deltaX * 0.3, deltaY * 0.3); birds.add(bird); aiming = false; mousePoint = null; } } @Override public void mouseDragged(MouseEvent e) { if (aiming) { mousePoint = e.getPoint(); } } @Override public void mouseClicked(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) {} @Override public void mouseExited(MouseEvent e) {} } class Bird { public double x, y; private double vx, vy; private static final double GRAVITY = 0.5; private static final int RADIUS = 15; public Bird(double x, double y) { this.x = x; this.y = y; this.vx = 0; this.vy = 0; } public void setVelocity(double vx, double vy) { this.vx = vx; this.vy = vy; } public void update() { x += vx; y += vy; vy += GRAVITY; // 重力效果 // 简单的反弹效果 if (y > 500 - RADIUS) { y = 500 - RADIUS; vy *= -0.6; vx *= 0.8; } } public void draw(Graphics2D g2d) { g2d.setColor(Color.RED); g2d.fillOval((int)(x - RADIUS), (int)(y - RADIUS), RADIUS * 2, RADIUS * 2); // 画眼睛 g2d.setColor(Color.WHITE); g2d.fillOval((int)(x - 8), (int)(y - 8), 6, 6); g2d.fillOval((int)(x + 2), (int)(y - 8), 6, 6); g2d.setColor(Color.BLACK); g2d.fillOval((int)(x - 6), (int)(y - 6), 3, 3); g2d.fillOval((int)(x + 4), (int)(y - 6), 3, 3); } public boolean collidesWith(Pig pig) { double distance = Math.sqrt(Math.pow(x - pig.x, 2) + Math.pow(y - pig.y, 2)); return distance < RADIUS + pig.getRadius(); } } class Pig { public double x, y; private static final int RADIUS = 20; public Pig(double x, double y) { this.x = x; this.y = y; } public void draw(Graphics2D g2d) { g2d.setColor(Color.GREEN); g2d.fillOval((int)(x - RADIUS), (int)(y - RADIUS), RADIUS * 2, RADIUS * 2); // 画鼻子 g2d.setColor(Color.DARK_GRAY); g2d.fillOval((int)(x - 3), (int)(y - 2), 6, 4); // 画眼睛 g2d.setColor(Color.WHITE); g2d.fillOval((int)(x - 10), (int)(y - 10), 8, 8); g2d.fillOval((int)(x + 2), (int)(y - 10), 8, 8); g2d.setColor(Color.BLACK); g2d.fillOval((int)(x - 8), (int)(y - 8), 4, 4); g2d.fillOval((int)(x + 4), (int)(y - 8), 4, 4); } public int getRadius() { return RADIUS; } }这个愤怒的小鸟游戏包含以下特性:
主要功能:
- 红色圆形小鸟,带有眼睛细节
- 绿色圆形猪,带有鼻子和眼睛
- 点击并拖拽鼠标来瞄准和发射小鸟
- 重力物理效果
- 碰撞检测
- 得分系统
游戏玩法:
- 用鼠标点击并拖拽来瞄准
- 松开鼠标发射小鸟
- 击中所有绿色的猪即可获胜
- 显示当前得分和剩余猪的数量
在Eclipse中运行:
- 创建新的Java项目
- 创建新的Java类,复制粘贴上面的代码
- 运行主类
AngryBirdsGame
游戏窗口会显示一个简单但完整的愤怒的小鸟游戏,你可以根据需要进一步扩展功能,比如添加更多关卡、障碍物或特殊能力。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.