พึ่งเคยใช้งานครั้งแรกโปรดอ่านที่นี่! howtouse!
x
  • Register
หางานด้าน IT อยู่เหรอ?

ควบคุมทิศทางไม่ได้รับ

0 votes

ผมพยายามจะควบคุมทิศทางแต่ มันเหมือนกับไม่ได้อ่านโค้ดอ่าครับ แล้วเวอร์ชั่นของ eclipe มันมีผลต่อโค้ดมัียครับ

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
public class Main {
public static void main(String[] args){
Frame app = new Frame("Pac Man");
app.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we){
 
System.exit(0);
}
});
PacModel world =new PacModel();
PacView screen =new PacView(world);
 
app.add(screen);
app.pack();
app.setVisible(true);
 
screen.addKeyListener(new PacListener(world));
 
 
while(!world.gameOver()){
world.tick();
screen.repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
}}
 
import java.util.ArrayList;;
public class PacModel {
 private String startboard =
"####################\n"
+"#0.....#...#...#..0#\n"
   +"#.####.#...........#\n"
   +"#......#...#...#...#\n"
   +"#..##......#.......#\n"
   +"#..................#\n"
   +"#......##.###......#\n"
   +"#......#....#......#\n"
   +"#......######......#\n"
   +"#..................#\n"
   +"#..................#\n"
   +"#..................#\n"
   +"#..................#\n"
   +"#..................#\n"
   +"#..................#\n"
   +"#..................#\n"
   +"#..................#\n"
   +"#0................0#\n"
   +"####################";
 
 private ArrayList<ArrayList<Character>>board = new ArrayList<ArrayList<Character>>();
public float pacx=5,pacy=6;
public  static int STILL=0,UP=1,RIGHT=2,DOWN=3,LEFT=4;
public int pacdir=RIGHT;
public int time=0;
 
 public PacModel(){
 for (String row :startboard.split("\n")){
ArrayList<Character> r= new ArrayList<Character>();
for (int i =0;i<row.length();i++)
r.add(row.charAt(i));
 
board.add(r);
 }
 
 }
public int getHeight() {
return board.size();
}
public int getWidth() {
return board.get(0).size();
}
public char getCell(int x, int y) {
 
return board.get(y).get(x);
 
}
public void tick(){
time+=1;
if(pacdir==RIGHT)
pacx+=.01;
else if(pacdir==LEFT)
pacx-=.01;
else if(pacdir==DOWN)
pacy-=.01;
else if(pacdir==UP)
pacy+=.01;
}
public boolean gameOver() {
return false;
}
 
}
 
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
 
public class PacView extends Canvas{
private static final long serialVersionUID = 1L;
private PacModel world;
 
private final int T=40;
    public PacView(PacModel world) {
this.world=world;
setSize(world.getWidth()*T,world.getHeight()*T);
}
    public void update(Graphics g){
    Image im = new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_RGB);
    paint(im.getGraphics());
    g.drawImage(im, 0, 0,null);
   
    }
public void paint(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(),getHeight());
g.setColor(Color.BLACK);
for(int x=0;x<world.getWidth();x++){
for(int y=0;y<world.getHeight();y++){
char c =world.getCell(x,y);
switch (c){
case '.':
if((world.time/8)%2==0)
g.drawOval(x*T+T/3,y*T+T/3,T/3,T/3);break;
case '#':g.fillRect(x*T, y*T,T,T);break;
case '0':g.drawOval(x*T+1,y*T+1,T-2,T-2);break;
default:System.out.println("what is a'"+c+"'");
 
}
}
}
g.setColor(Color.YELLOW);
g.fillArc((int)(world.pacx*T),(int)(world.pacy*T), T,T,30,300);
}
}
 
 
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
 
public class PacListener implements KeyListener {
  PacModel world;
public PacListener(PacModel world) {
this.world =world;
}
 
 
 
@Override
public void keyPressed(KeyEvent ke) {
int code=ke.getKeyCode();
if(code==KeyEvent.VK_LEFT){
world.pacdir=world.LEFT;
}
else if(code==KeyEvent.VK_RIGHT){
world.pacdir=world.RIGHT;
}
else if(code==KeyEvent.VK_UP){
world.pacdir=world.UP;
}
else if(code==KeyEvent.VK_DOWN){
 
world.pacdir=world.DOWN;
}
 
}
 
@Override
public void keyReleased(KeyEvent arg0) {}
 
@Override
public void keyTyped(KeyEvent arg0) {}
 
}
 

 

ถามเมื่อ Nov 19, 2015 in programing-ทั่วไป โดย POP (102 คะแนน)
อยากได้คำตอบเร็วๆไหม?? แชร์คำถามนี้ให้เพื่อนๆคุณซิ:    

Please log in or register to answer this question.

Related questions

0 votes
1 คำตอบ
0 votes
0 คำตอบ
530 views ถามเมื่อ Apr 30, 2015 in Java โดย minkuki (102 คะแนน)
0 votes
0 คำตอบ
–1 vote
0 คำตอบ
0 votes
0 คำตอบ
...