Java马里奥游戏敌我碰撞
我是java游戏编程的begginer,我对游戏有一个小问题,实际上是一个大问题。 我试着在敌人和障碍物之间制造碰撞,但它不起作用,我不知道为什么。它应该可以工作,但它只是以每秒一帧的速度减慢游戏速度,什么也不做 我有一门叫做游戏的主课 使用此主Init()函数 在LoadImageLevel函数中,我逐像素读取level.png,并根据不同的颜色设置每个对象的位置Java马里奥游戏敌我碰撞,java,collision-detection,collision,2d-games,Java,Collision Detection,Collision,2d Games,我是java游戏编程的begginer,我对游戏有一个小问题,实际上是一个大问题。 我试着在敌人和障碍物之间制造碰撞,但它不起作用,我不知道为什么。它应该可以工作,但它只是以每秒一帧的速度减慢游戏速度,什么也不做 我有一门叫做游戏的主课 使用此主Init()函数 在LoadImageLevel函数中,我逐像素读取level.png,并根据不同的颜色设置每个对象的位置 private void LoadImageLevel (BufferedImage image){ int w = im
private void LoadImageLevel (BufferedImage image){
int w = image.getWidth();
int h = image.getHeight();
//System.out.println(w + " , " + h);
for(int xx = 0; xx < h; xx++){
for(int yy = 0; yy < w ; yy++){
int pixel = image.getRGB(xx, yy);
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
if(red == 255 && green == 255 && blue == 255)
handler.addObject(new Block(xx*32,yy*32,1,ObjectId.Block));
if(red == 0 && green == 0 && blue == 255)
handler.addObject(new Player(xx*32,yy*32,1,handler,ObjectId.Player));
if(red == 0 && green == 255 && blue == 0)
handler.addObject(new Enemy(xx*32,yy*32,handler,ObjectId.Enemy));
}
}
}
private void LoadImageLevel(buffereImage图像){
int w=image.getWidth();
int h=image.getHeight();
//系统输出println(w+“,”+h);
对于(int xx=0;xx>16)和0xff;
绿色整数=(像素>>8)&0xff;
蓝色整数=(像素)&0xff;
如果(红色==255&&绿色==255&&蓝色==255)
addObject(新块(xx*32,yy*32,1,ObjectId.Block));
如果(红色==0&&绿色==0&&蓝色==255)
handler.addObject(新播放器(xx*32,yy*32,1,handler,ObjectId.Player));
如果(红色==0&&绿色==255&&蓝色==0)
handler.addObject(新敌人(xx*32,yy*32,handler,ObjectId.敌军));
}
}
}
类内播放器是两个重要的函数:tick和collision,其中In tick被称为collison
public class Player extends GameObject{
private float width = 32, // 48
height = 64; // 96
private float gravity = 0.5f;
private final float MAX_SPEED = 10;
private int facing = 1;
private int last = 0; // last position left or right
private Handler handler;
Texture tex = Game.getInstance();
private int type;
private Animation playerWalk, playerWalkLeft,jump;
public Player(float x, float y,int type , Handler handler ,ObjectId id) {
super(x, y, id);
this.handler = handler;
this.type = type;
playerWalk = new Animation(2,tex.player[2],tex.player[3],
tex.player[4],tex.player[5]);
playerWalkLeft = new Animation(2,tex.player[7],tex.player[8],
tex.player[9],tex.player[10]);
jump = new Animation(2,tex.player[11],tex.player[12]);
}
public void tick(LinkedList<GameObject> object) {
x += velX;
y += velY;
if(velX < 0) facing = -1;
else if(velX > 0) facing = 1;
if(falling || jumping){
velY += gravity;
if(velY > MAX_SPEED){
velY = MAX_SPEED;
}
}
Collision(object);
//System.out.println(velX + " " + velY);
playerWalk.runAnimation();
playerWalkLeft.runAnimation();
jump.runAnimation();
}
private void Collision(LinkedList<GameObject> object){
for(int i = 0; i < handler.object.size();i++){
GameObject tempObject = handler.object.get(i);
if(tempObject.getId() == ObjectId.Block ){
if(getBoundsTop().intersects(tempObject.getBounds())){
y = tempObject.getY() + 32;
velY = 0;
}
if(getBounds().intersects(tempObject.getBounds())){
y = tempObject.getY() - height;
velY = 0;
falling = false;
jumping = false;
}else
falling = true;
if(getBoundsRight().intersects(tempObject.getBounds())){
x = tempObject.getX() - width;
}
if(getBoundsLeft().intersects(tempObject.getBounds())){
x = tempObject.getX() + 35;
}
}
/* new */
}
}
public void render(Graphics g) {
/*
g.setColor(Color.blue);
g.fillRect((int)x,(int)y,(int)width,(int)height);
Graphics2D g2d = (Graphics2D) g;
g.setColor(Color.red);
g2d.draw(getBounds());
g2d.draw(getBoundsRight());
g2d.draw(getBoundsLeft());
g2d.draw(getBoundsTop());
*/
if(velX != 0){
if (facing == 1){
playerWalk.drawAnimation(g,(int) x, (int)y,32,64);
last = 1;
}
else{
playerWalkLeft.drawAnimation(g,(int) x, (int)y,32,64);
last = 0;
}
}
else
if (last == 1)
g.drawImage(tex.player[1], (int)x,(int) y,32,64,null);
else
g.drawImage(tex.player[6], (int)x,(int) y,32,64,null); // 6 ,32,64
//System.out.println("Y: " + y); // 513 je max
if (y >= 513){
g.setColor(Color.red);
g.drawString("Game Over", (int) x, 200);
}
}
public Rectangle getBounds() {
return new Rectangle((int) ((int)x+(width/2)-((width/2)/2)),(int) ((int)y+(height/2)),(int)width/2,(int)height/2);
}
public Rectangle getBoundsTop() {
return new Rectangle((int) ((int)x+(width/2)-((width/2)/2)),(int)y,(int)width/2,(int)height/2);
}
public Rectangle getBoundsRight() {
return new Rectangle((int) ((int)x+width-5),(int)y+5,(int)5,(int)height-10);
}
public Rectangle getBoundsLeft() {
return new Rectangle((int)x,(int)y+5,(int)5,(int)height-10);
}
公共类玩家扩展游戏对象{
专用浮点宽度=32,//48
高度=64;//96
私人浮子重力=0.5f;
私人最终浮动最大速度=10;
私人int=1;
private int last=0;//左或右最后一个位置
私人经办人;
纹理tex=Game.getInstance();
私有int型;
私人动画播放者漫游,播放者漫游,跳跃;
公共播放器(float x、float y、int类型、处理程序、ObjectId id){
super(x,y,id);
this.handler=handler;
this.type=type;
playerWalk=新动画(2,tex.player[2],tex.player[3],
tex.player[4],tex.player[5]);
playerWalkLeft=新动画(2,tex.player[7],tex.player[8],
tex.player[9],tex.player[10];
跳跃=新动画(2,tex.player[11],tex.player[12]);
}
公共无效勾号(LinkedList对象){
x+=velX;
y+=0;
如果(velX<0)面向=-1;
否则,如果(velX>0)面=1;
如果(坠落| |跳跃){
重力+=重力;
如果(速度>最大速度){
Vly=最大速度;
}
}
碰撞(物体);
//System.out.println(velX+“”+velY);
playerWalk.runAnimation();
playerWalkLeft.runAnimation();
jump.runAnimation();
}
私有无效冲突(LinkedList对象){
对于(int i=0;i=513){
g、 setColor(Color.red);
g、 抽绳(“游戏结束”(int)x200);
}
}
公共矩形getBounds(){
返回新矩形((int)((int)x+(宽度/2)-(宽度/2)/2)),(int)((int)y+(高度/2)),(int)宽度/2,(int)高度/2);
}
公共矩形getBoundsTop(){
返回新矩形((int)((int)x+(宽度/2)-(宽度/2)/2)),(int)y,(int)宽度/2,(int)高度/2);
}
公共矩形getBoundsRight(){
返回新矩形((int)((int)x+width-5),(int)y+5,(int)5,(int)height-10);
}
公共矩形getBoundsLeft(){
返回新矩形((int)x,(int)y+5,(int)5,(int)height-10);
}
玩家没有任何阻挡碰撞的问题
public class Block extends GameObject {
Texture tex = Game.getInstance();
private int type;
public Block(float x, float y,int type,ObjectId id) {
super(x, y, id);
this.type = type;
}
public void tick(LinkedList<GameObject> object) {
}
public void render(Graphics g) {
if(type == 0)
g.drawImage(tex.block[0], (int) x, (int) y ,null);
if(type == 1)
g.drawImage(tex.block[1], (int) x, (int) y ,null);
}
public Rectangle getBounds() {
return new Rectangle((int)x,(int)y,32,32);
}
}
公共类块扩展游戏对象{
纹理tex=Game.getInstance();
私有int型;
公共块(浮点x、浮点y、int类型、ObjectId){
super(x,y,id);
this.type=type;
}
公共无效勾号(LinkedList对象){
}
公共空间渲染(图形g){
如果(类型==0)
g、 drawImage(tex.block[0],(int)x,(int)y,null);
如果(类型==1)
g、 drawImage(tex.block[1],(int)x,(int)y,null);
}
公共矩形getBounds(){
返回新矩形((int)x,(int)y,32,32);
}
}
但当我尝试创建敌方类并使其与玩家类相同时,我的意思是碰撞,它只会使游戏变慢,而不是其他
public class Enemy extends GameObject{
private Handler handler;
public Enemy(float x, float y,Handler handler, ObjectId id) {
super(x, y, id);
this.handler = handler;
}
public void tick(LinkedList<GameObject> object) {
for(int i = 0; i < handler.object.size();i++){
GameObject tempObject = handler.object.get(i);
if(tempObject.getId() == ObjectId.Block ){
if(getBoundsTop().intersects(tempObject.getBounds())){
}
if(getBounds().intersects(tempObject.getBounds())){
}
if(getBoundsRight().intersects(tempObject.getBounds())){
}
if(getBoundsLeft().intersects(tempObject.getBounds())){
}
}
}
}
public void render(Graphics g) {
g.setColor(Color.red);
g.fillRect((int)x,(int) y, 32, 32);
}
public Rectangle getBoundsTop() {
return new Rectangle((int)x,(int)y,32,32);
}
public Rectangle getBoundsLeft() {
return new Rectangle((int)x,(int)y,32,32);
}
public Rectangle getBoundsRight() {
return new Rectangle((int)x,(int)y,32,32);
}
public Rectangle getBounds() {
return new Rectangle((int)x,(int)y,32,32);
}}
公共类敌人扩展游戏对象{
私人经办人;
公敌(浮点数x、浮点数y、处理程序处理程序、对象id){
super(x,y,id);
this.handler=handler;
}
公共无效勾号(LinkedList对象){
对于(int i=0;ipublic class Enemy extends GameObject{
private Handler handler;
public Enemy(float x, float y,Handler handler, ObjectId id) {
super(x, y, id);
this.handler = handler;
}
public void tick(LinkedList<GameObject> object) {
for(int i = 0; i < handler.object.size();i++){
GameObject tempObject = handler.object.get(i);
if(tempObject.getId() == ObjectId.Block ){
if(getBoundsTop().intersects(tempObject.getBounds())){
}
if(getBounds().intersects(tempObject.getBounds())){
}
if(getBoundsRight().intersects(tempObject.getBounds())){
}
if(getBoundsLeft().intersects(tempObject.getBounds())){
}
}
}
}
public void render(Graphics g) {
g.setColor(Color.red);
g.fillRect((int)x,(int) y, 32, 32);
}
public Rectangle getBoundsTop() {
return new Rectangle((int)x,(int)y,32,32);
}
public Rectangle getBoundsLeft() {
return new Rectangle((int)x,(int)y,32,32);
}
public Rectangle getBoundsRight() {
return new Rectangle((int)x,(int)y,32,32);
}
public Rectangle getBounds() {
return new Rectangle((int)x,(int)y,32,32);
}}