我尝试打乒乓球但是我的球不动,所以如何让球移动?
这是我的代码
package test;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class pingpong1 extends JFrame implements Runnable {
public static void main(String[] args) {
pingpong1 ping = new pingpong1("PingPong Hard Game");
new Thread(ping).start();
ping.setSize(600, 300);
ping.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
ping.setVisible(true);
} private int width, height;
private User user;
private Computer computer;
private Ball ball;
static int UserScore = 0;
int ComputerScore=0;
public pingpong1(String title){
}
@Override
public void paint(Graphics g){
super.paint(g);
Image img2;
ImageIcon img = new ImageIcon("pingpong.png");
img2 = img.getImage();
g.drawImage(img2,0,0, this);
ball.paint(g);
}
@Override
public void run(){
while(true){
ball.moveBall();
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
Logger.getLogger( getName()).log(Level.SEVERE,null,e);
}
}
}
public void paintComponent(Graphics g) {
if (user == null) {
width = getWidth();
height = getHeight();
user = new User();
computer = new Computer();
ball = new Ball();
}
ball.draw(g);
}
public class User{
}
public class Computer{
}
public class Ball{
private int x,y;
private int centerX , centerY;
private Color color;
boolean go;
Ball(){
go=false;
}
public void paint(Graphics g) {
// TODO Auto-generated method stub
}
public Ball(int x,int y,Color color){
this.x=x;
this.y=y;
this.color=color;
this.centerX=5;
this.centerY=5;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public void moveBall(){
centerX=5;
x+=centerX;
y+=centerY;
} void draw(Graphics g){
Image img2;
ImageIcon img = new ImageIcon("pingpong.png");
img2 = img.getImage();
g.drawImage(img2, centerX - -35, centerY -10 , null);
}
}
}
答案 0 :(得分:1)
首先你的移动代码,需要有一些输入来实际移动球。现在它除了添加x,y并没有做任何事情,并且没有重新绘制它,所以你基本上告诉它什么都不做。 如果你正在寻找这样的用户控制的东西会起作用吗?
public void moveIt(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_DOWN:
myY += 5;
break;
case KeyEvent.VK_UP:
myY -= 5;
break;
case KeyEvent.VK_LEFT:
myX -= 5;
break;
case KeyEvent.VK_RIGHT:
myX += 5;
break;
}
}
如果您正在寻找一种自动移动球的方法,那么您需要查看代码中的一些内容。因为你没有考虑速度/方向等...
这是移动球http://introcs.cs.princeton.edu/java/34nbody/Ball.java.html
的基本示例我会发表评论,但我的代表不到50岁。
答案 1 :(得分:0)
在你的代码中,你正确地更新了球在游戏循环中的位置:
class pingpong1
.filter((Q(for_field__is_null=True) | Q(for_field__bool=True))
但你实际上从未在更新的位置重新绘制球:
while(true){
ball.moveBall(); // You update movement here
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
System.err.println("Interrupted.");
}
}
因为public void paint(Graphics g){
super.paint(g);
Image img2;
ImageIcon img = new ImageIcon("pingpong.png");
img2 = img.getImage();
g.drawImage(img2,0,0, this);
ball.paint(g); // this method is empty
}
方法ball
为空:
班级球
paint
要纠正错误:
public void paint(Graphics g) {
// TODO Auto-generated method stub
}
或者只是致电public void paint(Graphics g) {
ImageIcon icon = new ImageIcon("ball.png");
Image image = icon.getImage();
g.drawImage(image, x, y, null);
}
,但您仍需要更正ball.draw()
和x
,因为它们目前是常量,请更改:
y
要:
g.drawImage(img2, centerX + 35, centerY - 10, null);