-2

我想让游戏像这里的一样

我做了一个JFrame和背景和球员,但我不能移动球员请帮帮我!这是我的来源:它们是 1 个包装中的 3 个类。主课是真的吗?!这是我的大学项目,我很困惑........

package sportshead;

import javax.swing.*;
import java.awt.*;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;

public class screen extends JFrame{
     private Image back;
     private Image player1;
     private Image player2;
     private Image goal1;
     private Image goal2;
     private Image ball;

    Player1 p = new Player1();

public screen(){
    setTitle("The title");
    setSize(1500, 800);
    setLocation(0,0);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
     }
public void loadPics(){
        player1 = new ImageIcon("C:\\Test\\player1.png").getImage();
        player2 = new ImageIcon("C:\\Test\\player2.png").getImage();
        goal1 = new ImageIcon("C:\\Test\\goal1.png").getImage();
        goal2 = new ImageIcon("C:\\Test\\goal2.png").getImage();
        ball = new ImageIcon("C:\\Test\\ball.png").getImage();
        back = new ImageIcon("C:\\Test\\Background.jpg").getImage();
        repaint();
       }
public void paint(Graphics g){
         g.drawImage(back, 0, 0, null);
         p.paint(g);
         g.drawImage(player2, -400, 200, null);
         g.drawImage(goal1, 400, 145, null);
         g.drawImage(goal2, -500, 140, null);
         g.drawImage(ball, -20, -200, null);
         g.setColor(Color.BLACK);
         g.drawLine(0, 603, 1400, 603);
     }
}


package sportshead;

import java.awt.*; 
import java.awt.event.*;
import javax.swing.*; 
import sun.java2d.pipe.DrawImage;

public class Player1 extends JFrame {

    private Image image;
    private int x=0;
    private int y=0;
    public int yMove =200;  
    public int xMove = 200;   

public Player1(){
         image = new ImageIcon("C:\\Test\\player1.png").getImage();
         addKeyListener( new KeyList());
       }

public void paint(Graphics g) {  
        g.drawImage(image, xMove, yMove, this);  
    } 
public void moveLeft() {  
        x = xMove--;  
    }  

public void moveRight() {  
        y = xMove++;  
    }  
public void moveReset(int value) {  
        xMove = value;  
    }

    // inner class
private class KeyList extends KeyAdapter {  
        public void keyPressed(KeyEvent k) {  
            if (k.getKeyCode() == KeyEvent.VK_LEFT){  
                moveLeft();  
            }  
            if (k.getKeyCode() == KeyEvent.VK_RIGHT){  
                moveRight();  
            }  
        }  
    } 
}

package sportshead;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;

public class Main extends JFrame{

     public static void main(String [] args){

          int i =0;

          screen s = new screen();
          s.loadPics();
          Player1  p = new Player1();
          for(i=0;i<50;i+=5){
            p.xMove+=i;
            p.yMove+=i;
            p.repaint();
            s.loadPics();
        }   
}
4

2 回答 2

1

我没有在 main 中得到 for 循环(比如 1 纳秒运行时间?),但无论如何。首先,您需要一个适当的游戏循环。更改播放器的位置后,您必须重新渲染帧。搜索写游戏循环,我的建议。也许还可以搜索 DoubleBufferStrategys,这样您就可以得到流畅的渲染。

例子

于 2013-06-21T07:35:52.953 回答
0

对于游戏应用程序,使用 Timer 服务进行动画和图形。阅读 Java Swing 教程

用于丰富的动画游戏。使用 JavaFX。

于 2013-06-21T07:34:47.437 回答