Created
January 8, 2015 09:03
-
-
Save takanakahiko/af8ac1d5fec16bd45c7e to your computer and use it in GitHub Desktop.
ブロック崩し作ったけどガバガバ仕様(1.8)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
class myPanel extends JPanel implements ActionListener, KeyListener{ | |
int x = 30, y = 20; | |
int dx = 12, dy = 5, d = 30; | |
int padY = 55; | |
int pd = 0; | |
Timer pTimer; | |
final int width, hight; | |
int block[][] = { | |
{1,1,1}, | |
{1,1,1}, | |
{1,1,1}, | |
{1,1,1}, | |
{1,1,1}, | |
{1,1,1}, | |
{1,1,1} | |
}; | |
public myPanel(int w, int h){ | |
width = w; | |
hight = h; | |
setPreferredSize(new Dimension(w, h)); | |
setBackground(Color.black); | |
pTimer = new Timer(100, this); | |
addKeyListener(this); | |
} | |
public void start(){ | |
pTimer.start(); | |
} | |
@Override | |
public void paintComponent(Graphics g){ | |
super.paintComponent(g); | |
g.setColor(Color.white); | |
g.fillRect(30, 0, width, 5); | |
g.fillRect(width-5, 0, 5, hight); | |
g.fillRect(30, hight-5, width, 5); | |
g.fillOval(x, y, 10, 10); | |
g.fillRect(20, padY, 8, 30); | |
for(int i = 0; i < block.length; i++){ | |
for(int j = 0; j < block[i].length; j++){ | |
if(block[i][j]==1){ | |
g.fillRect(200 + j * 30, 5 + i * 20, 30, 20); | |
} | |
} | |
} | |
g.drawString(""+((x-200)/30)+","+((y-5)/20),50,50); | |
} | |
@Override | |
public void actionPerformed(ActionEvent e){ | |
padY += pd; | |
if(padY < 0) | |
padY = 0; | |
else if(padY > hight - 30) | |
padY = hight - 30; | |
x += dx; | |
y += dy; | |
if(x > width - 15){ | |
dx *= -1; | |
x = width - 15; | |
} | |
if(y < 5){ | |
dy *= -1; | |
y = 5; | |
}else if(y > hight - 15){ | |
dy *= -1; | |
y = hight - 15; | |
} | |
if((x < 30)&&(y >= padY)&&(y <= padY+30)){ | |
dx *= -1; | |
x = 30; | |
} | |
if(x>200){ | |
System.out.print((x-200)/30); | |
if(block[(y-5)/20][(x-200)/30]==1){ | |
block[(y-5)/20][(x-200)/30]=0; | |
x -= (x-200)%30; | |
dx *= -1; | |
} | |
} | |
repaint(); | |
} | |
@Override | |
public void keyPressed(KeyEvent e){ | |
switch(e.getKeyCode()){ | |
case KeyEvent.VK_UP: | |
pd = -10; | |
break; | |
case KeyEvent.VK_DOWN: | |
pd = 10; | |
break; | |
} | |
} | |
@Override | |
public void keyReleased(KeyEvent e){ | |
switch(e.getKeyCode()){ | |
case KeyEvent.VK_UP: | |
case KeyEvent.VK_DOWN: | |
pd = 0; | |
break; | |
} | |
} | |
@Override | |
public void keyTyped(KeyEvent e){ } | |
@Override | |
public boolean isFocusable(){ | |
return true; | |
} | |
} | |
public class TinyPong{ | |
public static void main(String[] args){ | |
JFrame f = new JFrame(); | |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
f.setLayout(new BorderLayout()); | |
f.setSize(300,200); | |
f.setTitle("TingPong"); | |
f.setResizable(false); | |
Container contentPane = f.getContentPane(); | |
myPanel p = new myPanel(300,150); | |
contentPane.add(p,"Center"); | |
p.start(); | |
f.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment