2016-09-22 3 views
1

점수 보드가 화면에서 업데이트되지 않는 이유에 대해 매우 혼란 스럽습니다. 점수가 증가하고 있습니다 (디버거로 확인했는데 공이 중앙에 위치 함).퐁 (Pong)에서 점수가 업데이트되지 않음

퐁 클래스

package com.dheraxysgames.pong; 

import java.awt.Dimension; 

import javax.swing.JFrame; 

public class Pong extends JFrame{ 

    private static final long serialVersionUID = 1L; 

    //Set Resolution 
    public static final int width = 300, 
          height = width/4 * 3, 
          scale = 3; 

    public Pong() { 
     Dimension size = new Dimension(width * scale, height * scale); 
     setSize(size); 
     setTitle("PONG"); 
     setResizable(false); 
     setVisible(true); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     add(new GamePanel()); 
    } 

    public static int getWindowWidth(){ 
     return width * scale; 
    } 

    public static int getWindowHeight(){ 
     return height * scale; 
    } 

    public static void main(String[] args) { 
     new Pong(); 
    } 

} 

의 GamePanel 클래스

package com.dheraxysgames.pong; 

import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 

import javax.swing.JPanel; 
import javax.swing.Timer; 

public class GamePanel extends JPanel implements ActionListener, KeyListener { 

    private static final long serialVersionUID = 1L; 

    Ball ball = new Ball(); 
    Player player = new Player(); 
    AI ai = new AI(this); 

    public GamePanel(){ 
     Timer time = new Timer(50, this); 
     time.start(); 

     this.addKeyListener(this); 
     this.setFocusable(true); 
    } 

    private void update(){ 
     player.update(); 
     ai.update(); 
     ball.update(); 

     ball.checkCollision(player); 
     ball.checkCollision(ai); 
    } 

    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 

     g.setColor(Color.BLACK); 
     g.fillRect(0, 0, Pong.getWindowWidth(), Pong.getWindowHeight()); 

     g.setColor(Color.WHITE); 
     Font font = new Font("IMMORTAL", Font.BOLD, 50); 
     g.setFont(font); 
     g.drawString(player.score + " : " + ai.score, Pong.getWindowWidth()/2 - 60, 50); 

     player.paint(g); 
     ai.paint(g); 
     ball.paint(g); 
    } 

    public Ball getBall(){ 
     return ball; 
    } 

    public void actionPerformed(ActionEvent e) { 
     update(); 
     repaint(); 
    } 

    //Keyboard 
    public void keyPressed(KeyEvent e) { 
     if (e.getKeyCode() == KeyEvent.VK_UP) player.setYVelocity(-10); 
     if (e.getKeyCode() == KeyEvent.VK_DOWN) player.setYVelocity(10); 
    } 

    public void keyReleased(KeyEvent e) { 
     if (e.getKeyCode() == KeyEvent.VK_UP) player.setYVelocity(0); 
     if (e.getKeyCode() == KeyEvent.VK_DOWN) player.setYVelocity(0); 
    } 

    public void keyTyped(KeyEvent e) { 

    } 

} 

플레이어 클래스

package com.dheraxysgames.pong; 

import java.awt.Color; 
import java.awt.Graphics; 

public class Player { 

    public int score = 0; 

    private static int width = 50, 
         height = 150; 

    private int x = 800, y = (Pong.getWindowHeight()/2) - (height/2), 
       yV = 0; 

    public Player() { 

    } 

    public void update(){ 
     y += yV; 
    } 

    public void paint(Graphics g){ 
     g.setColor(Color.GREEN); 
     g.fillRect(x, y, width, height); 
    } 

    public int getX(){ 
     return x; 
    } 

    public int getY(){ 
     return y; 
    } 

    public int getWidth(){ 
     return width; 
    } 

    public int getHeight(){ 
     return height; 
    } 

    public void setYVelocity(int speed){ 
     yV = speed; 
    } 

} 

AI 클래스 :하지만 점수 판은 지속적으로 "0 0"을 말한다 전혀 업데이트되지 않는다

package com.dheraxysgames.pong; 

import java.awt.Color; 
import java.awt.Graphics; 

public class AI { 

    public int score = 0; 

    private static int width = 50, 
         height = 150; 

    private GamePanel field; 

    private int x = 50, y = (Pong.getWindowHeight()/2) - (height/2), 
       yV = 0; 

    public AI(GamePanel game) { 
     this.field = game; 
    } 

    public AI(){ 

    } 

    public void update(){ 
     if(field.getBall().getY() < this.y) yV = -8; 
     if(field.getBall().getY() > this.y) yV = 8; 
     y += yV; 
    } 

    public void paint(Graphics g){ 
     g.setColor(Color.GREEN); 
     g.fillRect(x, y, width, height); 
    } 

    public int getX(){ 
     return x; 
    } 

    public int getY(){ 
     return y; 
    } 

    public int getWidth(){ 
     return width; 
    } 

    public int getHeight(){ 
     return height; 
    } 

    public void setYVelocity(int speed){ 
     yV = speed; 
    } 

} 

볼 클래스

package com.dheraxysgames.pong; 

import java.awt.Color; 
import java.awt.Graphics; 

public class Ball { 

    private int x = Pong.getWindowWidth()/2, y = Pong.getWindowHeight()/2, 
       xV = 10, yV = 10; 

    private static int size = 40; 

    Player player = new Player(); 
    AI ai = new AI(); 

    public void update() { 
     x += xV; 
     y += yV; 

     if (x < 0){ 
      reverseXDirection(); 
      player.score++; 
      x = Pong.getWindowWidth()/2; 
      y = Pong.getWindowHeight()/2; 
     } 
     if (x > Pong.getWindowWidth() - size){ 
      reverseXDirection(); 
      ai.score++; 
      x = Pong.getWindowWidth()/2; 
      y = Pong.getWindowHeight()/2; 
     } 
     if (y < 0 || y > Pong.getWindowHeight() - size - 38) reverseYDirection(); 
    } 

    public void paint(Graphics g){ 
     g.setColor(Color.GREEN); 
     g.fillOval(x, y, size, size); 
    } 

    private void reverseXDirection(){ 
     xV = -xV; 
    } 

    private void reverseYDirection(){ 
     yV = -yV; 
    } 

    public void checkCollision(Player p) { 
     if (this.x + size > p.getX() && this.x + size < p.getX() + p.getWidth()){ 
      if (this.y + size > p.getY() && this.y + size < p.getY() + p.getHeight()){ 
       reverseXDirection(); 
      } 
     } 
    } 

    public void checkCollision(AI ai) { 
     if (this.x > ai.getX() && this.x < ai.getX() + ai.getWidth()){ 
      if (this.y > ai.getY() && this.y < ai.getY() + ai.getHeight()){ 
       reverseXDirection(); 
      } 
     } 
    } 

    public int getX(){ 
     return x; 
    } 

    public int getY(){ 
     return y; 
    } 

} 
+0

콘텐츠를 추가하기 전에 표시해야합니까? – Li357

+0

나는 그것이 실제로 효과가 있다는 것을 눈치 채지 못했습니다. – Dheraxys

답변

0

당신이 업데이트하고 점수가의 GamePanel 클래스에서 확인되고있는 동일한 점수 아니다 볼 클래스이야에서. 두 클래스에서 당신이 볼에

Player player = new Player(); 
AI ai = new AI(); 

플레이어와 AI를 호출하는 방법을

주의의 GamePanel의 플레이어와 인공 지능에서 별도의 인스턴스입니다.

GamePanel 클래스에서 플레이어와 AI를 가져 와서 Ball 클래스에 전달해야합니다. 이에

Ball ball = new Ball(); 
Player player = new Player(); 
AI ai = new AI(this); 

: 그것은을이었다

Player player = new Player(); 
AI ai = new AI(this); 
Ball ball = new Ball(player, ai); 

이 작업을 수행하는 가장 쉬운 방법은 아마 볼 그렇게

Player player; 
AI ai; 
public Ball(Player player, AI ai) { 
    this.player = player; 
    this.ai = ai; 
} 

같은 그리고 당신의 GamePanel 수준의 변화 생성자를 제공하는 것 Java를 사용 했으므로 아마도이 작업을 수행하는 간단한 방법을 잊어 버릴 수 있지만 문제가 해결되어야합니다.

+0

그게 효과가! 무리 감사! – Dheraxys