由Java实现的一个射击游戏

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

public class ShootingGame extends JPanel implements ActionListener {
    private int playerX = 250;
    private int bulletX, bulletY;
    private boolean isShooting = false;
    private List<Enemy> enemies = new ArrayList<>();
    private List<SpecialWeapon> specialWeapons = new ArrayList<>();
    private int score = 0;
    private int lives = 3;
    private boolean gameOver = false;

    public ShootingGame() {
        setPreferredSize(new Dimension(500, 500));
        setBackground(Color.BLACK);

        Timer timer = new Timer(10, this);
        timer.start();

        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                    playerX -= 5;
                    if (playerX < 0)
                        playerX = 0;
                } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                    playerX += 5;
                    if (playerX > 450)
                        playerX = 450;
                } else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                    if (!isShooting) {
                        bulletX = playerX + 20;
                        bulletY = 470;
                        isShooting = true;
                    }
                }
            }
        });
        setFocusable(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.RED);
        g.fillRect(playerX, 470, 40, 20);

        if (isShooting) {
            g.setColor(Color.YELLOW);
            g.fillOval(bulletX, bulletY, 5, 10);
        }

        for (Enemy enemy : enemies) {
            g.setColor(enemy.getColor());
            g.fillRect(enemy.getX(), enemy.getY(), 40, 20);
        }

        for (SpecialWeapon weapon : specialWeapons) {
            g.setColor(weapon.getColor());
            g.fillOval(weapon.getX(), weapon.getY(), 20, 20);
        }

        g.setColor(Color.WHITE);
        g.setFont(new Font("Arial", Font.BOLD, 20));
        g.drawString("Score: " + score, 10, 30);
        g.drawString("Lives: " + lives, 10, 60);

        if (gameOver) {
            g.setFont(new Font("Arial", Font.BOLD, 40));
            g.drawString("Game Over", 150, 250);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (gameOver) {
            return;
        }

        if (isShooting) {
            bulletY -= 5;
            if (bulletY < 0)
                isShooting = false;
        }

        Iterator<Enemy> enemyIterator = enemies.iterator();
        while (enemyIterator.hasNext()) {
            Enemy enemy = enemyIterator.next();
            enemy.move();

            if (isShooting && bulletX >= enemy.getX() && bulletX <= enemy.getX() + 40
                    && bulletY >= enemy.getY() && bulletY <= enemy.getY() + 20) {
                enemyIterator.remove();
                isShooting = false;
                score += 10;
            }

            if (enemy.getY() >= 470) {
                enemyIterator.remove();
                lives--;
                if (lives <= 0) {
                    gameOver = true;
                }
            }
        }

        Iterator<SpecialWeapon> weaponIterator = specialWeapons.iterator();
        while (weaponIterator.hasNext()) {
            SpecialWeapon weapon = weaponIterator.next();
            weapon.move();

            if (weapon.getX() >= playerX && weapon.getX() <= playerX + 40
                    && weapon.getY() >= 470 && weapon.getY() <= 490) {
                weaponIterator.remove();
                score += 20;
            }
        }

        if (enemies.size() < 5) {
            Random random = new Random();
            int enemyX = random.nextInt(460);
            int enemyType = random.nextInt(3);
            Color enemyColor;
            switch (enemyType) {
                case 0:
                    enemyColor = Color.GREEN;
                    break;
                case 1:
                    enemyColor = Color.BLUE;
                    break;
                case 2:
                    enemyColor = Color.ORANGE;
                    break;
                default:
                    enemyColor = Color.GREEN;
            }
            enemies.add(new Enemy(enemyX, -20, enemyColor));
        }

        if (specialWeapons.size() < 1) {
            Random random = new Random();
            int weaponX = random.nextInt(460);
            specialWeapons.add(new SpecialWeapon(weaponX, -20));
        }

        repaint();
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Shooting Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new ShootingGame());
        frame.pack();
        frame.setVisible(true);
    }

    private static class Enemy {
        private int x;
        private int y;
        private Color color;

        public Enemy(int x, int y, Color color) {
            this.x = x;
            this.y = y;
            this.color = color;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        public Color getColor() {
            return color;
        }

        public void move() {
            y += 2;
        }
    }

    private static class SpecialWeapon {
        private int x;
        private int y;
        private Color color;

        public SpecialWeapon(int x, int y) {
            this.x = x;
            this.y = y;
            this.color = Color.YELLOW;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        public Color getColor() {
            return color;
        }

        public void move() {
            y += 2;
        }
    }
}

  • 22
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值