返回

臻房博客

弹出
首页 > 能用java做的小游戏,用java写的游戏 >>正文

能用java做的小游戏,用java写的游戏

“能用Java做的小游戏”指的是使用Java编程语言来开发和实现的游戏。Java是一种广泛应用的计算机编程语言,特别适用于开发跨平台、可扩展性强的软件。对于想利用Java技能的小游戏开发者来说,这不仅意味着能够创造有趣、吸引人的游戏体验,还能让他们掌握Java编程的精髓。通过编写简单的Java程序,如控制台小游戏或图形界面游戏,开发者可以锻炼逻辑思维和问题解决能力,同时享受编程带来的乐趣。

用java写的游戏

用java写的游戏

在Java中,你可以创建各种类型的游戏,包括2D平台游戏、动作冒险游戏、策略游戏等。以下是一个简单的Java 2D平台游戏的示例代码,使用了Java的Swing库来创建图形用户界面(GUI)。

```java

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class SimplePlatformGame extends JFrame {

private static final int WIDTH = 800;

private static final int HEIGHT = 600;

private static final int BLOCK_SIZE = 40;

private Player player;

private obstacles[] obstacles;

public SimplePlatformGame() {

setTitle("Simple Platform Game");

setSize(WIDTH, HEIGHT);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

player = new Player(WIDTH / 2, HEIGHT - BLOCK_SIZE);

obstacles = new obstacles[10];

for (int i = 0; i < obstacles.length; i++) {

obstacles[i] = new obstacles(i * BLOCK_SIZE, (i + 1) * BLOCK_SIZE);

}

add(player);

for (obstacles[i] : obstacles) {

add(obstacles[i]);

}

}

private class Player extends JPanel {

public Player(int x) {

setBounds(x, HEIGHT - BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);

setFocusable(true);

requestFocus();

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.setColor(Color.BLUE);

g.fillRect(getX(), getY(), getWidth(), getHeight());

}

}

private class Obstacles extends JPanel {

public Obstacles(int x, int y) {

setBounds(x, y, BLOCK_SIZE, BLOCK_SIZE);

setFocusable(true);

requestFocus();

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.setColor(Color.GRAY);

g.fillRect(getX(), getY(), BLOCK_SIZE, BLOCK_SIZE);

}

}

private class GameLoop implements ActionListener {

private long lastTime;

private static final int DELAY = 1000 / 60;

@Override

public void actionPerformed(ActionEvent e) {

long currentTime = System.currentTimeMillis();

if (currentTime - lastTime >= DELAY) {

update();

repaint();

lastTime = currentTime;

}

}

private void update() {

player.moveLeft();

player.moveRight();

for (int i = 0; i < obstacles.length; i++) {

obstacles[i].moveUp();

obstacles[i].moveDown();

}

}

}

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> {

SimplePlatformGame game = new SimplePlatformGame();

game.setVisible(true);

GameLoop gameLoop = new GameLoop();

game.addKeyListener(new java.awt.event.KeyAdapter() {

@Override

public void keyPressed(java.awt.event.KeyEvent evt) {

player.setSpeed(evt.getKeyCode() == KeyEvent.VK_LEFT ? -1 : 1);

}

});

game.addKeyListener(new java.awt.event.KeyAdapter() {

@Override

public void keyReleased(java.awt.event.KeyEvent evt) {

player.setSpeed(evt.getKeyCode() == KeyEvent.VK_LEFT ? 1 : -1);

}

});

gameLoop.start();

});

}

}

```

这个示例代码创建了一个简单的2D平台游戏,玩家可以通过键盘控制一个蓝色的方块移动。游戏中还包含了一些灰色的方块作为障碍物,玩家需要避免碰撞到这些障碍物。

要运行此代码,请将其复制到一个名为`SimplePlatformGame.java`的文件中,并使用以下命令编译和运行:

```bash

javac SimplePlatformGame.java

java SimplePlatformGame

```

请注意,这只是一个简单的示例,你可以根据需要扩展和改进这个游戏,例如添加更多的关卡、角色、敌人等。

能用java做的小游戏

能用java做的小游戏

当然可以!Java 是一种功能强大的编程语言,可以用来开发各种类型的小游戏。以下是一些简单的 Java 小游戏示例:

1. 贪吃蛇游戏:

贪吃蛇是一个经典的游戏,玩家通过键盘控制蛇的移动,吃到食物后蛇会变长,撞到墙或自己游戏结束。

2. 井字棋游戏:

井字棋是一个简单的两人对弈游戏,玩家轮流在 3x3 的棋盘上放置自己的标记,先赢得游戏的玩家获胜。

3. 猜数字游戏:

游戏规则是计算机随机生成一个 1 到 100 之间的整数,玩家需要猜测这个数字,每次猜测后计算机会给出提示(太大或太小)。

4. 扫雷游戏:

扫雷是一个经典的益智游戏,玩家需要在一张有地雷的网格中点击非地雷格子,如果没有触发地雷则得分,否则游戏结束。

5. 接苹果游戏:

玩家控制一个不断上升的苹果,通过点击屏幕让苹果掉下来,如果苹果碰到障碍物或者玩家没有及时接住,游戏结束。

6. 角色扮演游戏(RPG):

虽然 RPG 游戏比较复杂,但 Java 可以用来实现一个简单的角色扮演游戏,包括角色创建、战斗系统、物品系统等。

下面是一个简单的贪吃蛇游戏的示例代码:

```java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class SnakeGame extends JPanel implements ActionListener {

private static final int WIDTH = 600;

private static final int HEIGHT = 600;

private static final int UNIT_SIZE = 20;

private static final int GAME_UNITS = (WIDTH * HEIGHT) / (UNIT_SIZE * UNIT_SIZE);

private static final int DELAY = 100;

private int x1, y1, x2, y2;

private int body[] = new int[GAME_UNITS];

private int appleX, appleY;

private boolean running = false;

public SnakeGame() {

setPreferredSize(new Dimension(WIDTH, HEIGHT));

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent e) {

int key = e.getKeyCode();

if ((key == KeyEvent.VK_UP) && (y1 > 0)) {

y1 -= UNIT_SIZE;

} else if ((key == KeyEvent.VK_DOWN) && (y1 < HEIGHT - 1)) {

y1 += UNIT_SIZE;

} else if ((key == KeyEvent.VK_LEFT) && (x1 > 0)) {

x1 -= UNIT_SIZE;

} else if ((key == KeyEvent.VK_RIGHT) && (x1 < WIDTH - 1)) {

x1 += UNIT_SIZE;

}

}

});

Timer timer = new Timer(DELAY, this);

timer.start();

}

public void run() {

createGame();

while (running) {

checkApple();

checkSelf();

repaint();

try {

Thread.sleep(DELAY);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

public void createGame() {

body[0] = new int[] {100, 50};

body[1] = new int[] {120, 50};

body[2] = new int[] {100, 70};

body[3] = new int[] {120, 70};

x1 = 100;

y1 = 50;

x2 = 120;

y2 = 50;

appleX = (int) (Math.random() * (WIDTH / UNIT_SIZE)) * UNIT_SIZE;

appleY = (int) (Math.random() * (HEIGHT / UNIT_SIZE)) * UNIT_SIZE;

running = true;

}

public void checkApple() {

if ((x1 == appleX) && (y1 == appleY)) {

body[4] = new int[] {x1, y1};

appleX = (int) (Math.random() * (WIDTH / UNIT_SIZE)) * UNIT_SIZE;

appleY = (int) (Math.random() * (HEIGHT / UNIT_SIZE)) * UNIT_SIZE;

}

}

public void checkSelf() {

for (int i = 0; i < body.length - 1; i++) {

if ((body[i] == new int[] {x1, y1}) && (i != 4)) {

running = false;

}

}

}

public void paintComponent(Graphics g) {

super.paintComponent(g);

if (running) {

g.setColor(Color.red);

g.fillRect(appleX, appleY, UNIT_SIZE, UNIT_SIZE);

for (int i = 0; i < body.length; i++) {

g.setColor(Color.green);

g.fillRect(body[i][0], body[i][1], UNIT_SIZE, UNIT_SIZE);

}

} else {

g.setColor(Color.red);

g.drawString("Game Over", WIDTH / 2 - 50, HEIGHT / 2);

}

}

public static void main(String[] args) {

JFrame frame = new JFrame("Snake Game");

SnakeGame game = new SnakeGame();

frame.add(game);

frame.pack();

frame.setVisible(true);

game.run();

}

}

```

这个示例代码实现了一个简单的贪吃蛇游戏。你可以在此基础上进行扩展和优化,添加更多的功能和更好的图形界面。

温馨提示:以上内容和图片整理于网络,仅供参考,希望对您有帮助!本文仅代表作者观点,不代表本站立场。
博主精选BLOG HIGHLIGHTS
  • 婚礼上的歌是什么歌,婚礼那首歌叫什么
  • 3个男人捡瓶子是什么歌
  • 70后的清新打扮是什么歌,70后年轻时尚的发型
  • 楼顶上唱什么歌好听女生
  • 带涵的装修公司取名,以涵为名取个公司名称
  • 起名万瑾有什么寓意
  • 服装材料公司起名
  • cf端游打字为什么只有拼音
  • 能用java做的小游戏,用java写的游戏
  • 腾讯游戏大厅有什么手游,腾讯的大型游戏
  • 热门标签