我正在制作一个游戏,我从我的电脑上拍摄的图像没有在屏幕上移动,有什么不对请帮帮我
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;
public class Player extends Entity{
int velX=0,velY=0, speed =5;
public Player(int x, int y) {
super(x, y);
}
public void Update(){
y+=velY;
x+=velX;
checkCollision();
}
public void draw(Graphics2D g2d){
g2d.drawImage(getPlayerImage(),x,y, null);
g2d.draw(getbounds());
}
public Image getPlayerImage(){
ImageIcon ic = new ImageIcon("D:/abc.png");
return ic.getImage();
}
public void KeyPressed(KeyEvent e){
int key = e.getKeyCode();
if(key== KeyEvent.VK_UP) velY=-speed;
else if(key==KeyEvent.VK_DOWN) velY=speed;
if(key==KeyEvent.VK_LEFT) velX-=speed;
else if(key==KeyEvent.VK_RIGHT) velX=speed;
else if(key==KeyEvent.VK_SPACE) GameFrame.addMissile(new Missile(x,y));
}
public void KeyReleased(KeyEvent e){
int key = e.getKeyCode();
if(key== KeyEvent.VK_UP)velY=0;
else if(key==KeyEvent.VK_DOWN) velY=0;
if(key==KeyEvent.VK_LEFT) velX=0;
else if(key==KeyEvent.VK_RIGHT) velX=0;
}
public void checkCollision(){
ArrayList<Enemy> enemies = GameFrame.getEnemyList();
for(int i=0; i<enemies.size(); i++){
Enemy tempEnemy = enemies.get(i);
if(getbounds().intersects(enemies.get(i).getbounds())) System.exit(0);
}
}
public Rectangle getbounds(){
return new Rectangle(x,y,getPlayerImage().getWidth(null),getPlayerImage().getHeight(null));
}
}
还有另外7个课程,但是图像移动工作在这里,还有一个KeyAdapt课程,它只是作为一个关键适配器工作
答案 0 :(得分:0)
你应该看一下当你按下鼠标时,你想要移动,但这取决于你向左或向右移动你应该减少或增加。与向上或向下相同。
所以当你上下移动时,你沿着Y轴向前移动,以0向北(向上)开始,以Y向南(向下)的最大值结束。所以如果你想向上移动,你可以做velY - =速度,如果你想下去你做velY + =速度。假设此时velY为0且速度大于0。
X轴也是如此,左边是0,右边是X的最大值。当你向左移动时,你从0(velX:-5)减少,当你向右时从0增加(velX:+5 )。
另一件事是,当你多次需要你的ImageIcon时,最好将它作为私有属性,因为这样它只被实例化一次。这将提高您的性能,因为每次使用位于硬盘驱动器上的映像实例化ImageIcon时,您都会进行磁盘IO操作,从而减慢所有操作。
命名在Java中也很重要,您使用大写字母编写方法名称是不正确的。方法名称应以小写字母开头并使用camelcase。
我用switch case语句重写了你的代码,使其更具可读性。
public class Player extends Entity {
private int velX=0,velY=0, speed =5;
private ImageIcon ic = new ImageIcon("D:/abc.png");
public Player(int x, int y) {
super(x, y);
}
@Override
public void update(){
y+=velY;
x+=velX;
checkCollision();
}
public void draw(Graphics2D g2d){
g2d.drawImage(getPlayerImage(),x,y, null);
g2d.draw(getbounds());
}
public Image getPlayerImage(){
return ic.getImage();
}
public void keyPressed(KeyEvent e){
switch (e.getKeyCode()) {
case KeyEvent.VK_UP: velY -= speed; break;
case KeyEvent.VK_DOWN: velY += speed; break;
case KeyEvent.VK_LEFT: velX -= speed; break;
case KeyEvent.VK_RIGHT: velX += speed; break;
case KeyEvent.VK_SPACE: GameFrame.addMissile(new Missile(x,y)); break;
default: break;
}
}
public void keyReleased(KeyEvent e){
switch (e.getKeyCode()) {
case KeyEvent.VK_UP: velY = 0; break;
case KeyEvent.VK_DOWN: velY = 0; break;
case KeyEvent.VK_LEFT: velX = 0; break;
case KeyEvent.VK_RIGHT: velX = 0; break;
default: break;
}
}
public void checkCollision(){
ArrayList<Enemy> enemies = GameFrame.getEnemyList();
for(int i=0; i<enemies.size(); i++){
Enemy tempEnemy = enemies.get(i);
if(getbounds().intersects(enemies.get(i).getbounds())) System.exit(0);
}
}
public Rectangle getbounds(){
return new Rectangle(x,y,getPlayerImage().getWidth(null),getPlayerImage().getHeight(null));
}
}