在JFrame中制作游戏

时间:2013-06-21 07:26:35

标签: java swing

I want to make the game like the one here

我做了JFrame和背景以及球员,但我不能移动球员请帮助我! 这是我的来源:他们是1个包中的3个clases。 主要类是真的吗?! 这是我的uni项目,我很困惑........

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();
        }   
}

2 个答案:

答案 0 :(得分:1)

我没有在main中获得for循环(比如1 nanosec运行时?)但是无论如何。首先你需要一个合适的游戏循环。改变玩家的位置后,你必须重新渲染帧。寻找写游戏循环,我的建议。也许还可以搜索DoubleBufferStrategys,以便获得流畅的渲染。

<强> Example

答案 1 :(得分:0)

对于游戏应用程序,请使用定时器服务来制作动画和图形的 Read the Java Swing tutorial

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