JavaSwing游戏开发之Camera原理

embedded/2025/1/16 20:52:46/

在这里插入图片描述
在这里插入图片描述

java">package org.timer;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;public class GameCameraWithObjects extends JPanel implements KeyListener {// 游戏世界大小private final int worldWidth = 2000;private final int worldHeight = 1500;// 摄像机位置private int cameraX = 0;private int cameraY = 0;// 摄像机视野大小private final int viewWidth = 800;private final int viewHeight = 600;// 玩家位置private int playerX = 400;private int playerY = 300;// 世界中的对象private final List<GameObject> gameObjects = new ArrayList<>();private final Random random = new Random();public GameCameraWithObjects() {setPreferredSize(new Dimension(viewWidth, viewHeight));addKeyListener(this);setFocusable(true);generateGameObjects(50); // 生成 50 个随机对象}// 生成游戏世界中的随机对象private void generateGameObjects(int count) {for (int i = 0; i < count; i++) {int x = random.nextInt(worldWidth);int y = random.nextInt(worldHeight);int size = 40; // 随机大小 10-40
//            int size = random.nextInt(30) + 10; // 随机大小 10-40Color color = new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));gameObjects.add(new GameObject(x, y, size, color));}}@Overrideprotected void paintComponent(Graphics g) {super.paintComponent(g);// 填充背景g.setColor(Color.LIGHT_GRAY);g.fillRect(0, 0, viewWidth, viewHeight);// 绘制世界边界g.setColor(Color.BLACK);g.drawRect(-cameraX, -cameraY, worldWidth, worldHeight);// 绘制所有对象for (GameObject obj : gameObjects) {int screenX = obj.x - cameraX;int screenY = obj.y - cameraY;// 只绘制在视野内的对象if (screenX + obj.size > 0 && screenX < viewWidth && screenY + obj.size > 0 && screenY < viewHeight) {g.setColor(obj.color);g.fillRect(screenX, screenY, obj.size, obj.size);g.drawString("("+obj.x+","+obj.y+")", screenX, screenY);}}// 绘制玩家g.setColor(Color.RED);int screenX = playerX - cameraX;int screenY = playerY - cameraY;g.fillRect(screenX - 10, screenY - 10, 20, 20);// 显示调试信息g.setColor(Color.BLACK);g.drawString("Camera: (" + cameraX + ", " + cameraY + ")", 10, 20);g.drawString("Player: (" + playerX + ", " + playerY + ")", 10, 40);g.drawString("Objects: " + gameObjects.size(), 10, 60);}// 更新摄像机位置private void updateCamera() {
//        cameraX = playerX - viewWidth / 2;cameraX = Math.max(0, Math.min(playerX - viewWidth / 2, worldWidth - viewWidth));
//        cameraY = Math.max(0, Math.min(playerY - viewHeight / 2, worldHeight - viewHeight));cameraY = Math.max(0, Math.min(playerY - viewHeight / 2, worldHeight - viewHeight));}@Overridepublic void keyPressed(KeyEvent e) {int speed = 10;switch (e.getKeyCode()) {case KeyEvent.VK_UP -> playerY = Math.max(0, playerY - speed);case KeyEvent.VK_DOWN -> playerY = Math.min(worldHeight, playerY + speed);case KeyEvent.VK_LEFT -> playerX = Math.max(0, playerX - speed);case KeyEvent.VK_RIGHT -> playerX = Math.min(worldWidth, playerX + speed);}// 更新摄像机updateCamera();repaint();}@Overridepublic void keyReleased(KeyEvent e) {}@Overridepublic void keyTyped(KeyEvent e) {}public static void main(String[] args) {JFrame frame = new JFrame("Game Camera with Objects");GameCameraWithObjects gamePanel = new GameCameraWithObjects();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.add(gamePanel);frame.pack();frame.setVisible(true);}// 内部类表示游戏对象private static class GameObject {int x, y, size;Color color;public GameObject(int x, int y, int size, Color color) {this.x = x;this.y = y;this.size = size;this.color = color;}}
}

http://www.ppmy.cn/embedded/154480.html

相关文章

js解决 Number失精度问题

const updatePromises adinfo.rows.map(async item > {const cwf await uniCloud.httpclient.request("https://api.oceanengine.com/open_api/v3.0/project/list/", {method: GET,data: {advertiser_id: item.account_id},// 1. 指定text数据格式dataType: tex…

R语言基础| 方差分析

写在前面 方差分析&#xff08;ANOVA&#xff0c;Analysis of Variance&#xff09;是统计学中一种用来比较三个或三个以上样本均值是否存在显著差异的方法。方差分析基于样本数据的方差来判断整体均值之间的差异&#xff0c;是处理多个样本组数据对比问题的常用技术。 方差分析…

HTML5 网站模板

HTML5 网站模板 参考 HTML5 Website Templates

网络安全实验之钓鱼网站的制作及技巧

在红队攻击中&#xff0c;除漏洞以外最简洁高效的攻击方式无疑是钓鱼攻击&#xff0c;通过不同的钓鱼方式可达到不同的攻击效果&#xff0c;本次我会分享最常见的钓鱼手段之一的网站钓鱼技术&#xff0c;同时对可实现的攻击操作进行演示。 更多网站钓鱼实验科普&#xff0c;可…

【人工智能】用Python进行对象检测:从OpenCV到YOLO的全面指南

对象检测是计算机视觉领域的核心任务之一&#xff0c;广泛应用于视频监控、自动驾驶、智能安防等多个场景。随着深度学习技术的发展&#xff0c;基于传统方法的对象检测逐渐被基于神经网络的先进模型所取代。本文将系统地介绍如何使用Python进行对象检测&#xff0c;重点探讨了…

基于单片机的无线气象仪系统设计(论文+源码)

1系统方案设计 如图2.1所示为无线气象仪系统设计框架。系统设计采用STM32单片机作为主控制器&#xff0c;结合DHT11温湿度传感器、光敏传感器、BMP180气压传感器、PR-3000-FS-N01风速传感器实现气象环境的温度、湿度、光照、气压、风速等环境数据的检测&#xff0c;并通过OLED1…

查看nginx已安装的模块

一、查看nginx已经安装了哪些模块 1、使用nginx -V [rootjxq-c2-16-1 auto]# /alidata/nginx/sbin/nginx -V nginx version: nginx/1.11.13 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled conf…

ASP.NET Core - IStartupFilter 与 IHostingStartup

ASP.NET Core - IStartupFilter 与 IHostingStartup 1. IStartupFilter2 IHostingStartup2.5.1 创建外部程序集2.5.2 激活外部程序集 1. IStartupFilter 上面讲到的方式虽然能够根据不同环境将Startup中的启动逻辑进行分离&#xff0c;但是有些时候我们还会可以根据应用中的功能…