我正在为一个java项目(Swing GUI)创建一个小游戏,我们必须使用MVC模式,我得到了基本模型并向下查看。我无法理解如何使用键盘键 - 例如 - 增加我的角色的X位置。我应该使用什么库?
这是我的代码(这是我第一次编码):
/*A Thing type object can stand on a Tile,the characters and items class implement this interface.*/
public interface Thing {
public void attacked(int x);
}
public abstract class Character implements Thing{
protected int health;
protected int position[]=new int[2];
protected int direction;
protected void setHealth(int hp){
this.health=hp;
}
public int getHealth(){
return this.health;
}
public void setPositionX(int x){
this.position[0]=x;
}
public void setPositionY(int y){
this.position[1]=y;
}
public int getPositionX(){
return this.position[0];
}
public int getPositionY(){
return this.position[1];
}
public void setDirection(int x){
this.direction=x;
}
public int getDirection(){
return this.direction;
}
public void moveUp(){
int posy=this.getPositionY();
this.setPositionY(posy+1);
}
public void moveDown(){
int posy=this.getPositionY();
this.setPositionY(posy-1);
}
public void moveLeft(){
int posx=this.getPositionX();
this.setPositionX(posx-1);
}
public void moveRight(){
int posx=this.getPositionX();
this.setPositionX(posx+1);
}
public void attack(){
}
public void heavyAttack(){
}
}
/*Only one type of character,just to have a really basic code to test then improve it*/
public class Mage extends Character{
public Mage(){
this.setHealth(5);
this.setPositionX(3);
this.setPositionY(3);
}
public void attacked(int x){
this.setHealth(this.getHealth()-x);
}
}
/*A Tile is a space on which a character/item (Thing) can stand,the level is a matrice of tiles*/
public class Tile {
private Thing[] occupant;
public Tile(){
this.occupant=new Thing[1];
}
public Thing getOccupant(){
return this.occupant[0];
}
public void addThing(Thing a){
this.occupant[0]=a;
}
public void removeThing(){
this.occupant[0]=null;
}
}
public class Terrain {
private String name;
private Tile[][] checker;
private int sizeX;
private int sizeY;
public Terrain(int x,int y,String Name){
this.name=Name;
this.sizeX=x;
this.sizeY=y;
this.checker= new Tile[y][x];
for (int i=0; i<y; i++){
for(int j=0; j<x; j++){
this.checker[i][j]=new Tile();
}
}
}
public int getSizeX(){
return sizeX;
}
public int getSizeY(){
return sizeY;
}
public Thing getTileOccupant(int x,int y){
return this.checker[y][x].getOccupant();
}
public void Spawn(Thing t,int x,int y){
this.checker[y][x].addThing(t);
}
}
/* The View,This was really just to test it out so it only shows the tiles and the mage sprite*/
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
public class myPanel extends JPanel {
private Terrain terrain;
public myPanel(Terrain checker){
this.terrain=checker;
this.requestFocusInWindow();
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
File file=new File("D:\\workspace\\Projet INFO-H-200\\src\\Images\\soltest.png");
BufferedImage sol=null;
try{
sol=ImageIO.read(file);
}
catch (IOException e){
e.printStackTrace();
}
file=new File("D:\\workspace\\Projet INFO-H-200\\src\\Images\\mage.png");
BufferedImage mage=null;
try{
mage=ImageIO.read(file);
}
catch (IOException e){
e.printStackTrace();
}
for (int i=0;i<terrain.getSizeY();i++){
for (int j=0;j<terrain.getSizeX();j++){
g.drawImage(sol, 33*j,33*i, null);
if (terrain.getTileOccupant(j,i)!=null){
g.drawImage(mage, 33*j, 33*i, null);
}
}
}
}
}
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args){
Mage blubu=new Mage();
Terrain terrain=new Terrain(10,10,"Test");
terrain.Spawn(blubu, blubu.getPositionX(), blubu.getPositionY());
Controler controler=new Controler(blubu);
JFrame mainWindow=new JFrame();
mainWindow.setSize(new Dimension(1000,800));
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setLocationRelativeTo(null); //au centre
mainWindow.setLayout(new BorderLayout());//gérer les placements
mainWindow.add(new myPanel(terrain), BorderLayout.CENTER); //on ajoute le panel au centre
mainWindow.setVisible(true);
mainWindow.toFront();
}
}