Java小游戏飞机大战源代码 要有登录注册功能
时间: 2023-08-01 10:06:10 浏览: 112
好的,以下是一个简单的Java小游戏飞机大战的实现思路和部分关键代码,包含登录注册功能,供您参考:
1. 确定需求:游戏需要支持注册、登录、游戏主界面、游戏结束等功能。
2. 设计数据库:根据需求设计数据库,包括用户表、游戏记录表等。
3. 编写后端代码:使用Java语言和Spring框架编写后端代码,包括数据访问层、业务逻辑层和控制层。
```java
// 数据访问层
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
@Repository
public interface GameRecordRepository extends JpaRepository<GameRecord, Long> {
}
// 业务逻辑层
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User register(User user) {
return userRepository.save(user);
}
public User login(String username, String password) {
User user = userRepository.findByUsername(username);
if (user == null || !user.getPassword().equals(password)) {
return null;
}
return user;
}
}
@Service
public class GameRecordService {
@Autowired
private GameRecordRepository gameRecordRepository;
public void save(GameRecord gameRecord) {
gameRecordRepository.save(gameRecord);
}
public List<GameRecord> getGameRecordsByUserId(Long userId) {
return gameRecordRepository.findAllByUserId(userId);
}
}
// 控制层
@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("")
public User register(@RequestBody User user) {
return userService.register(user);
}
@PostMapping("/login")
public User login(@RequestParam String username, @RequestParam String password) {
return userService.login(username, password);
}
}
@RestController
@RequestMapping("/api/gameRecords")
public class GameRecordController {
@Autowired
private GameRecordService gameRecordService;
@PostMapping("")
public void save(@RequestBody GameRecord gameRecord) {
gameRecordService.save(gameRecord);
}
@GetMapping("/{userId}")
public List<GameRecord> getGameRecordsByUserId(@PathVariable Long userId) {
return gameRecordService.getGameRecordsByUserId(userId);
}
}
```
4. 编写前端页面:使用HTML、CSS和JavaScript等技术编写前端页面,包括登录页面、注册页面、游戏主界面、游戏结束页面等。
```html
<!-- 登录页面 -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="/api/users/login" method="post">
<label for="username">用户名:</label>
<input type="text" id="username" name="username"><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password"><br>
<button type="submit">登录</button>
</form>
</body>
</html>
<!-- 注册页面 -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="/api/users" method="post">
<label for="username">用户名:</label>
<input type="text" id="username" name="username"><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password"><br>
<button type="submit">注册</button>
</form>
</body>
</html>
<!-- 游戏主界面 -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>飞机大战</title>
<style>
#game-board {
width: 600px;
height: 800px;
margin: 0 auto;
border: 1px solid #000;
}
</style>
</head>
<body>
<div id="game-board"></div>
<script src="/js/game.js"></script>
</body>
</html>
<!-- 游戏结束页面 -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>游戏结束</title>
</head>
<body>
<h1>游戏结束</h1>
<p>您的得分是:{{ score }}</p>
<form action="/api/gameRecords" method="post">
<input type="hidden" name="userId" value="{{ userId }}">
<input type="hidden" name="score" value="{{ score }}">
<button type="submit">保存记录</button>
</form>
<a href="/game">重新开始</a>
</body>
</html>
```
5. 编写游戏代码:使用Java语言和Swing框架编写游戏代码,包括游戏主界面、游戏逻辑等。
```java
public class GameBoard extends JPanel implements ActionListener {
private Timer timer;
private List<EnemyPlane> enemyPlanes;
private PlayerPlane playerPlane;
public GameBoard() {
setPreferredSize(new Dimension(600, 800));
setBackground(Color.WHITE);
setFocusable(true);
addKeyListener(new GameKeyListener());
playerPlane = new PlayerPlane(300, 700);
enemyPlanes = new ArrayList<>();
for (int i = 0; i < 5; i++) {
enemyPlanes.add(new EnemyPlane(50 + i * 100, 50));
}
timer = new Timer(10, this);
timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
update();
repaint();
}
private void update() {
playerPlane.move();
for (EnemyPlane enemyPlane : enemyPlanes) {
enemyPlane.move();
}
checkCollision();
}
private void checkCollision() {
Rectangle playerRect = playerPlane.getRect();
for (EnemyPlane enemyPlane : enemyPlanes) {
Rectangle enemyRect = enemyPlane.getRect();
if (playerRect.intersects(enemyRect)) {
gameOver();
}
}
}
private void gameOver() {
timer.stop();
String userId = JOptionPane.showInputDialog("请输入您的用户名");
int score = playerPlane.getScore();
GameRecord gameRecord = new GameRecord(userId, score);
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest httpRequest = HttpRequest.newBuilder()
.uri(URI.create("/api/gameRecords"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(new Gson().toJson(gameRecord)))
.build();
try {
HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
if (httpResponse.statusCode() == 200) {
String message = "游戏结束,您的得分是:" + score;
JOptionPane.showMessageDialog(null, message);
} else {
JOptionPane.showMessageDialog(null, "保存游戏记录失败");
}
} catch (IOException | InterruptedException e) {
JOptionPane.showMessageDialog(null, "保存游戏记录失败");
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
playerPlane.draw(g);
for (EnemyPlane enemyPlane : enemyPlanes) {
enemyPlane.draw(g);
}
}
private class GameKeyListener extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
playerPlane.setMoveLeft(true);
break;
case KeyEvent.VK_RIGHT:
playerPlane.setMoveRight(true);
break;
case KeyEvent.VK_UP:
playerPlane.setMoveUp(true);
break;
case KeyEvent.VK_DOWN:
playerPlane.setMoveDown(true);
break;
case KeyEvent.VK_SPACE:
playerPlane.fire();
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
playerPlane.setMoveLeft(false);
break;
case KeyEvent.VK_RIGHT:
playerPlane.setMoveRight(false);
break;
case KeyEvent.VK_UP:
playerPlane.setMoveUp(false);
break;
case KeyEvent.VK_DOWN:
playerPlane.setMoveDown(false);
break;
}
}
}
}
```
以上是一个简单的Java小游戏飞机大战的实现思路和部分关键代码,供您参考。请注意,这只是一个简单的示例,实际项目中可能需要更多的功能和代码实现。
阅读全文