Java 简单的2D游戏,尝试理解多个类
我只是在创建一个简单的2D游戏,我在理解如何与其他类交互时遇到了困难 例如,在此之前,我将所有的播放器图片和坐标都存储在程序的主循环中,但现在我希望将其放到一个新类之外。只需将该播放器称为类似于Java 简单的2D游戏,尝试理解多个类,java,Java,我只是在创建一个简单的2D游戏,我在理解如何与其他类交互时遇到了困难 例如,在此之前,我将所有的播放器图片和坐标都存储在程序的主循环中,但现在我希望将其放到一个新类之外。只需将该播放器称为类似于player的播放器即可 改变它。。e、 面向对象编程 我的全部代码 public int x, y; public boolean up, down, left, right, jump; public Image offScreen; public Graphics d; public B
player的播放器即可
改变它。。e、 面向对象编程
我的全部代码
public int x, y;
public boolean up, down, left, right, jump;
public Image offScreen;
public Graphics d;
public BufferedImage walkAnimations[] = new BufferedImage[5];
public BufferedImage background;
public BufferedImage player;
public void run() {
// Set Window Size
x = 100;
y = 100;
try {
// Import the images to use in the game
background = ImageIO.read(new File("img/background.jpg"));
walkAnimations[0] = ImageIO.read(new File("img/walkleft.png"));
walkAnimations[1] = ImageIO.read(new File("img/walkright.png"));
walkAnimations[2] = ImageIO.read(new File("img/walkup.png"));
walkAnimations[3] = ImageIO.read(new File("img/walkdown.png"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
player = walkAnimations[3];
while (true) {
// Create the Gravity (If player !jumping; and if player is below or above the ground, then set player to ground.
if (!jump) {
if (y < Character.GROUND_LEVEL || y > Character.GROUND_LEVEL) {
y = Character.GROUND_LEVEL;
}
}
// If Keys are Pressed, Move the object
if (left) {
x -= 2;
} else if (right) {
x += 2;
} else if (up) {
y -= 2;
} else if (down) {
y += 2;
}
// Repaint the window to update the changes
repaint();
try {
// Make the thread sleep
Thread.sleep(20);
} catch (InterruptedException e) {
// Catch any errors with the Thread
e.printStackTrace();
}
}
}
public int x,y;
公共布尔向上、向下、左、右、跳跃;
屏幕外的公众形象;
公共图形d;
公共BuffereImage walkAnimations[]=新BuffereImage[5];
公共缓冲区图像背景;
公共缓存图像播放器;
公开募捐{
//设置窗口大小
x=100;
y=100;
试一试{
//导入要在游戏中使用的图像
background=ImageIO.read(新文件(“img/background.jpg”);
walkAnimations[0]=ImageIO.read(新文件(“img/walkleft.png”);
walkAnimations[1]=ImageIO.read(新文件(“img/walkright.png”);
walkAnimations[2]=ImageIO.read(新文件(“img/walkup.png”);
walkAnimations[3]=ImageIO.read(新文件(“img/walkdown.png”);
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
播放器=步行动画[3];
while(true){
//创造重力(如果玩家!跳跃;如果玩家低于或高于地面,则将玩家设置为地面)。
如果(!跳转){
if(yCharacter.GROUND|u LEVEL){
y=字符地面水平;
}
}
//如果按了键,则移动对象
如果(左){
x-=2;
}否则,如果(右){
x+=2;
}否则,如果(向上){
y-=2;
}否则,如果(向下){
y+=2;
}
//重新绘制窗口以更新更改
重新油漆();
试一试{
//使线程睡眠
睡眠(20);
}捕捉(中断异常e){
//捕获线程中的任何错误
e、 printStackTrace();
}
}
}
(对于那些不想麻烦访问我的GitHub获取代码的人);我基本上想获取x/y、图像和其他东西,并将它们放入一个名为“Player”的新类中,但很难让它工作(例如,通过这样做.Player.y或Player.y)
简单地说,如果您查看Alpha 0.003更新,您可以在两个类中看到所有更新。
我已经尝试将所有播放器的内容移动到一个名为player
因此,我希望能够将其称为:player.x=0;
而不是执行x=0;
设置动画也一样
我只想做this.player=walkAnimations[0]
但是我不明白如何正确地做。做Player=newplayer();
错了吗
"this.player = walkAnimations[0]"
此.player
是一个player类型变量
walkAnimations[0]
是BuffereImage类型数组的单元格
因此,这句话不会被编译。基本上,你所做的是邪恶的。不是“我谋杀了一个婴儿,喝了它的血邪恶”,只是一种和蔼可亲的邪恶,比如“我在广告间隙去小便”
所以要回答这个问题,对象应该如何交互
他们应该通过互相发送信息进行互动。
在爪哇™ 您可以通过调用方法进行交互
例如,根据我和另一位助教最近的一项强制性任务,我们有一个示例:
@Override
public void setDirection(Direction dir) {
this.faces = dir;
}
因此,如果我想告诉玩家对象朝南,我可以向它发送一条消息,告诉它朝南:
player.setDirection(Direction.SOUTH);
现在,你可以问,为什么不这样称呼它
p.faces = Direction.SOUTH;
那不管用吗
是的,在这种情况下,只有在这种情况下。
然而,在做这类事情时可能还有其他问题。可能玩家对象在他转身时必须做一些事情。可能它必须告诉音响系统播放一些噪音。可能它必须在某处添加一个数字,记录你采取了多少步骤。可能它必须向NSA发送电子邮件。谁知道呢。以及何时发送消息是,你不必在意。玩家对象负责所有这些。你不是
这就是“我应该如何与我的对象交互”问题的答案
“我所做的为什么不起作用?”的答案是类型不匹配。你试图让玩家成为一个BuffereImage对象,这不是你想要的。你的问题不是很清楚,但我将尝试为你提供关于如何进行OO游戏编程的一般指导,因为我认为基本上这就是你的要求
面向对象编程的关键是实体由类表示,类同时保存实体的状态(与其相关的数据)和操作。
例如,一辆汽车可以用一个car
类来表示。这个类将包含汽车的特征——重量、颜色、价格等,还将包含它可以做的事情——启动、驱动、停止等
这意味着游戏中的每个实体(无论是玩家、怪物还是其他什么)都应该有自己的类。该类应该包含实体的所有信息——例如,它在屏幕上的位置(x,y)、它的图像——以及它可以做的任何事(例如,在屏幕上移动、跳跃、发射导弹)
下面是一个非常简单的实体
类,例如java伪代码中的类。所有游戏实体类都应从该类继承:
class Entity{
private int x, y; // position on screen
private int dx, dy; // this is how much the entity moves each game-loop cycle.
private Image image;
private Rectangle boundingBox;
private int healthPoints;
public Entity(int x, int y){
// ths position of the thing is set through the constructor
this.x = x;
this.y = y;
dx = 0;
dy = 0;
healthPoints = 100;
Image = new Image(/*some file*/); // this is pseudo code.
boundingBox = new Rectangle(image); // pseudo code. A rectangle the size of the image.
}
public int getX(){ return x; }
public int getY() { return y; }
public Image getImage() { return image; }
// user input can affect this from the outside, to move the entity. or possibly an ai mechanism.
public void setDX(int dx){ this.dx = dx; }
public void setDY(int dy){ this.dy = dy; }
public void move(){
x += dx;
y += dy;
}
public Rectangle getBoundingBox(){ return boundingBox; }
}
从这个类派生的任何对象都将为自己保存它的所有数据和操作
现在,您的gameloop控件从外部管理实体并访问它们的数据——移动它们、检查碰撞、显示它们的图像等等
例如,一个非常简化的包含游戏循环的游戏类
Class GameClass{
ArrayList entities;
public GameClass(){
entities = new ArrayList();
startGameLoop();
}
private void startGameLoop(){
while(true){ // this is bad, don't do this.
displayEntities(); // step 1 of gameloop.
moveEntities(); // step 2
checkForCollisions(); // step 3
}
}
private void displayEntities(){
for(int i=0; i<entities.size(); i++){
Entity entity = entities.get(i);
displayImage(entity.getImage(),entity.getX(),entity.getY());
}
}
private void updateEntities(){
for(int i=0; i<entities.size(); i++){
Entity entity = entities.get(i);
entity.move();
}
}
private void checkForCollisions(){
for(int i=0; i<entities.size(); i++){
Entity entity = entities.get(i);
for(int j=0; j<entities.size(); j++){
entities.remove(i);
}
}
}
}