質問内容
質問を評価する
(0ポイント)
|
スカッシュゲームを書き換えてボールを飛ばなくしました。次にボールを跳ね返す板の動きを視覚的に見るためのx軸を挿入しようとしたのですが、うまくいきません。どこをどう書き換えればよろしいでしょうか? プログラムは import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*;
public class Squash extends JFrame implements ActionListener,KeyListener{ JButton rbtn; JButton gbtn; JButton bbtn; MyPanel mypnl; public Squash() { setDefaultCloseOperation(EXIT_ON_CLOSE); setTitle("Spuash"); mypnl = new MyPanel(400,400); rbtn = new JButton("enter"); gbtn = new JButton("LEFT"); bbtn = new JButton("RIGHT"); JPanel btnpnl = new JPanel(); btnpnl.setLayout(new GridLayout(1,3,0,0)); btnpnl.add(rbtn); btnpnl.add(gbtn); btnpnl.add(bbtn); setLayout(new BorderLayout()); add(mypnl, BorderLayout.CENTER); add(btnpnl,BorderLayout.SOUTH); rbtn.addActionListener(this); gbtn.addActionListener(this); bbtn.addActionListener(this); rbtn.addKeyListener(this); gbtn.addKeyListener(this); bbtn.addKeyListener(this); pack(); setVisible(true); ; } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == rbtn) { rbtn.setEnabled(false); //rbtn.setEnabled(true); rbtn.setText("busy"); MoveDisk disk = new MoveDisk(mypnl,rbtn); disk.start(); Bar bar = new Bar(mypnl,rbtn); bar.start(); } if (e.getSource() == gbtn) { mypnl.greenct++; } if (e.getSource() == bbtn) { mypnl.bluect++; } } @Override public void keyPressed(KeyEvent e){ if(e.getKeyCode()==KeyEvent.VK_ENTER ){ rbtn.doClick(); } if(e.getKeyCode()==KeyEvent.VK_LEFT ){ mypnl.greenct++; } if(e.getKeyCode()==KeyEvent.VK_RIGHT ){ mypnl.bluect++; } } @Override public void keyReleased(KeyEvent e){ } @Override public void keyTyped(KeyEvent e){ } public static void main(String[] args){ Squash myframe = new Squash(); }
}
class MyPanel extends JPanel{ BufferedImage buffimg; Graphics2D bfg; Color bgcolor= new Color(255,255,191); int greenct = 0; int bluect = 0; boolean isnaname = false; public MyPanel(int width, int height){ setPreferredSize(new Dimension(width,height)); int r=2; buffimg = new BufferedImage( width*r,height*r,BufferedImage.TYPE_INT_RGB); bfg = buffimg.createGraphics(); bfg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); bfg.setColor(bgcolor); bfg.fillRect(0, 0, buffimg.getWidth(), buffimg.getHeight()); } @Override public void paintComponent(Graphics myg){ int pnlw = getSize().width; int imgh = buffimg.getHeight() * pnlw / buffimg.getWidth(); myg.drawImage(buffimg, 0, 0, pnlw, imgh, this); } public Color randomColor(char rgb){ int r=0; int g=0; int b=0; int dc=256; int x = (int)(dc*Math.random()); if (x > 255){ x = 0; } if (rgb=='r') r=x; if (rgb=='g') g=x; if (rgb=='b') b=x; Color c = new Color(r,g,b); return c; } } class MoveDisk extends Thread { int x; int y; int d, dx, dy; int xmax, ymax; public static int disksyoki; public static int dxsyoki; public static int end=0; Graphics2D thg; MyPanel mypnl; BufferedImage buffimg; int greenct = 0; int bluect = 0; boolean isnaname; int score = 0; JButton btn; public MoveDisk(MyPanel mypnl,JButton btn) { this.mypnl = mypnl; this.buffimg = mypnl.buffimg; this.btn = btn; thg = buffimg.createGraphics(); thg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); xmax = buffimg.getWidth(); ymax = buffimg.getHeight(); thg.setColor(mypnl.bgcolor); thg.fillRect(0,0,xmax,ymax); x = (int)(xmax*Math.random()); y = ymax/4; d = xmax/40; dxsyoki = (int)(xmax*(Math.random()/Math.random())); dx = dxsyoki; dy = (int)(ymax*(Math.random()/Math.random())); if (dx>xmax/200){ dx=xmax/200; } if (dy>ymax/200){ dy=ymax/200; } this.greenct = mypnl.greenct; this.bluect = mypnl.bluect; this.isnaname = mypnl.isnaname; } @Override public void run() { while ( true ){ end = 0; thg.setColor(mypnl.bgcolor); thg.fillRect(x-d/2,y-d/2,d,d); if (y<Bar.y && y>Bar.y-d && x<Bar.x+Bar.d*10 && x>Bar.x){ if (score==3){ dx=dx+xmax/800; dy=dy+ymax/800; } if (score==6){ dx=dx+xmax/800; dy=dy+ymax/800; } if (score==9){ dx=dx+xmax/800; dy=dy+ymax/800; } if (score==12){ dx=dx+xmax/800; dy=dy+ymax/800; } if (score==15){ dx=dx+xmax/800; dy=dy+ymax/800; } if (score==20){ dx=dx+xmax/800; dy=dy+ymax/800; } dy=-dy; score++; } if(x==Bar.x && y > Bar.y-d && y < Bar.y ){ dx=-dx; score++; } if(x==Bar.x+Bar.d*10 && y > Bar.y-d && y < Bar.y){ dx=-dx; score++; } x+=dx; if (x >xmax){ x = 2*xmax-x; dx = -dx; } if (0 > x){ x = -x; dx = -dx; } y+=dy; if (0 > y){ y = -y; dy = -dy; } } } } class Bar extends Thread { public static int x; public static int y; int dx, dy; public static int d,xmax, ymax; Graphics2D bar ; MyPanel mypnl; BufferedImage buffimg; int greenct = 0; int bluect = 0; boolean isnaname; public Bar(MyPanel mypnl,JButton btn) { this.mypnl = mypnl; this.buffimg = mypnl.buffimg; bar = buffimg.createGraphics(); bar.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); xmax = buffimg.getWidth(); ymax = buffimg.getHeight(); x = xmax/2; y = (ymax/8)*7; d = xmax/40; dx = xmax/400; this.greenct = mypnl.greenct; this.bluect = mypnl.bluect; this.isnaname = mypnl.isnaname; } @Override public void run() { while ( true ){ bar.setColor(mypnl.bgcolor); bar.fillRect(x-d/2-(d*5),y-d/2,d*10+d/2,d); //bar.fillRect(x-d*2, y-d, d*20, y*10); if (greenct != mypnl.greenct){ x=x-(dx*10); greenct = mypnl.greenct; } if ( bluect != mypnl.bluect ) { x = x+(dx*10); bluect = mypnl.bluect; } if (x+d/2 >(xmax-d*9+d*5)){ x = 2*(xmax-d*9+d*5)-x; } if (0+d/2 > x){ x = -x+d/2; } bar.setColor(Color.black); bar.fillRect(x-d/2-(d*5),y-d/2,d*10,d); mypnl.repaint(); try { Thread.sleep(10); } catch(InterruptedException ex) { System.err.println(ex); } } } } です。 よろしくお願いいたします。
|