帮我找一个200行的Java游戏代码
时间: 2023-11-04 19:04:19 浏览: 110
JAVA游戏代码
好的,以下是一个简单的Java游戏代码,共计约200行左右,可以供您参考:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JPanel implements KeyListener{
private static final long serialVersionUID = 1L;
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
private static final int PLAYER_SIZE = 20;
private static final int ENEMY_SIZE = 10;
private static final int PLAYER_SPEED = 5;
private static final int ENEMY_SPEED = 3;
private static final int MAX_ENEMIES = 10;
private int playerX = 250;
private int playerY = 250;
private int playerDX = 0;
private int playerDY = 0;
private List<Enemy> enemies = new ArrayList<Enemy>();
public Game() {
JFrame frame = new JFrame("Game");
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.addKeyListener(this);
frame.add(this);
frame.setVisible(true);
startGame();
}
public void startGame() {
Thread gameThread = new Thread() {
public void run() {
while(true) {
update();
repaint();
try { Thread.sleep(10); } catch(Exception e) {}
}
}
};
gameThread.start();
}
public void update() {
playerX += playerDX;
playerY += playerDY;
if(playerX < 0) playerX = 0;
if(playerX > WIDTH - PLAYER_SIZE) playerX = WIDTH - PLAYER_SIZE;
if(playerY < 0) playerY = 0;
if(playerY > HEIGHT - PLAYER_SIZE) playerY = HEIGHT - PLAYER_SIZE;
for(int i = 0; i < enemies.size(); i++) {
Enemy enemy = enemies.get(i);
enemy.x += enemy.dx;
enemy.y += enemy.dy;
if(enemy.x < 0 || enemy.x > WIDTH - ENEMY_SIZE) enemy.dx *= -1;
if(enemy.y < 0 || enemy.y > HEIGHT - ENEMY_SIZE) enemy.dy *= -1;
if(collides(enemy.x, enemy.y, ENEMY_SIZE, ENEMY_SIZE, playerX, playerY, PLAYER_SIZE, PLAYER_SIZE)) {
System.out.println("Game Over!");
System.exit(0);
}
}
if(enemies.size() < MAX_ENEMIES) {
int x = (int)(Math.random() * WIDTH);
int y = (int)(Math.random() * HEIGHT);
int dx = (int)(Math.random() * ENEMY_SPEED * 2) - ENEMY_SPEED;
int dy = (int)(Math.random() * ENEMY_SPEED * 2) - ENEMY_SPEED;
enemies.add(new Enemy(x, y, dx, dy));
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.WHITE);
g.fillRect(playerX, playerY, PLAYER_SIZE, PLAYER_SIZE);
for(int i = 0; i < enemies.size(); i++) {
Enemy enemy = enemies.get(i);
g.setColor(Color.RED);
g.fillRect(enemy.x, enemy.y, ENEMY_SIZE, ENEMY_SIZE);
}
}
public boolean collides(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) {
return x1 < x2 + w2 &&
x1 + w1 > x2 &&
y1 < y2 + h2 &&
y1 + h1 > y2;
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_LEFT) playerDX = -PLAYER_SPEED;
if(e.getKeyCode() == KeyEvent.VK_RIGHT) playerDX = PLAYER_SPEED;
if(e.getKeyCode() == KeyEvent.VK_UP) playerDY = -PLAYER_SPEED;
if(e.getKeyCode() == KeyEvent.VK_DOWN) playerDY = PLAYER_SPEED;
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_LEFT) playerDX = 0;
if(e.getKeyCode() == KeyEvent.VK_RIGHT) playerDX = 0;
if(e.getKeyCode() == KeyEvent.VK_UP) playerDY = 0;
if(e.getKeyCode() == KeyEvent.VK_DOWN) playerDY = 0;
}
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
new Game();
}
private class Enemy {
public int x, y, dx, dy;
public Enemy(int x, int y, int dx, int dy) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
}
}
}
```
这是一个简单的游戏,玩家需要通过键盘控制小方块移动,同时躲避随机生成的敌人方块。玩家和敌人之间碰撞时游戏结束。
阅读全文