我对这个问题很感兴趣。
基本上我有一个汽车图像,一旦它从x轴上的屏幕上消失,我需要回到起点。我以为我已经通过checkOFFscreen修复了它,但它没有用,所以我很生气并要求就如何解决这个问题提供一些指导。
Mainpanel.java
import java.awt.Color;
import java.awt.Dimension;
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.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MainPanel extends JPanel {
Player myPlayer;
Player myOtherPlayer;
private int WIDTH = 1000;
private int HEIGHT = 1000;
private int WALLWIDTH = 100;
private int WALLHEIGHT = 100;
private ArrayList<Wall> walls = new ArrayList<Wall>();
Timer myTimer = new Timer(500, new timerListener());
JLabel myTimeLabel;
int time =1;
public MainPanel()
{
setPreferredSize(new Dimension(WIDTH,HEIGHT));
JLabel myLabel= new JLabel ("Game ends once 30 seconds is receahed:");
myLabel.setFont(new Font("Serif", Font.BOLD,32));
myLabel.setForeground(Color.WHITE);
myTimeLabel= new JLabel (Integer.toString(time));
myTimeLabel.setFont(new Font("Serif", Font.BOLD,32));
myTimer.start();
add(myLabel);
add(myTimeLabel);
myPlayer = new Player(0,100, "toad.png", KeyEvent.VK_UP, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT,this, 50, 38);
myOtherPlayer = new Player(200,200, "toad.png", KeyEvent.VK_W, KeyEvent.VK_S, KeyEvent.VK_A, KeyEvent.VK_D,this, 50, 38);
createWalls();
}
public ArrayList<Wall> getWalls() {
return walls;
}
public void createWalls()
{
int j = 0;
for(int i = 0; i < HEIGHT/WALLHEIGHT; i++)
{
for(int k = 0; k < WIDTH/WALLWIDTH; k++)
{
if(i == 0 || i == (HEIGHT/WALLHEIGHT-1))
{
walls.add(new Wall(k*WALLWIDTH,j,"road.png", 100, 100));
}
}
j+=WALLHEIGHT;
}
}
private class timerListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
time++;
myTimeLabel.setText(Integer.toString(time));
myTimeLabel.setForeground(Color.WHITE);
if(time >= 30)
{
myTimer.stop();
}
repaint();
}
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
page.drawImage(myPlayer.getImageIcon().getImage(), myPlayer.getX(), myPlayer.getY(), null);
page.drawImage(myOtherPlayer.getImageIcon().getImage(), myOtherPlayer.getX(), myOtherPlayer.getY(), null);
for(int i = 0; i < walls.size(); i++)
{
page.drawImage(walls.get(i).getImageIcon().getImage(), walls.get(i).getX(), walls.get(i).getY(), null);
}
page.setFont(new Font("Arial", Font.PLAIN,32));
page.drawString("Player 1 Score: " + myPlayer.getScore(), 100, 800);
page.drawString("Player 2 Score: " + myOtherPlayer.getScore(), 100, 850);
if(time > 30)
{
page.drawString("GAME OVER", WIDTH/2-100, HEIGHT/2);
JOptionPane.showMessageDialog(null, "Please enter your email");
}
}
}
Player.java
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
public class Player extends GameObject implements KeyListener{
private int Up;
private int Down;
private int Left;
private int Right;
private MainPanel myPanel;
private Movement myMovement = new Movement();
private int score = 0;
public Player(int x, int y, String imagePath, int Up, int Down, int Left, int Right, MainPanel myPanel, int HEIGHT, int WIDTH)
{
super(x,y,imagePath, HEIGHT, WIDTH);
this.Up = Up;
this.Down = Down;
this.Left = Left;
this.Right = Right;
this.myPanel = myPanel;
myPanel.addKeyListener(this);
myPanel.setFocusable(true);
checkOffScreen();
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void checkOffScreen(){
if (x >=1050){
x=0;
}
}
public int getScore()
{
return score;
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
int key = arg0.getKeyCode();
if(key == Left)
{
x-=myMovement.getDistanceLeft();
if(checkWalls())
{
x+=10;
}
}
else if(key == Right)
{
x+=myMovement.getDistanceRight();
if(checkWalls())
{
x-=10;
}
}
myPanel.repaint();
}
public boolean checkWalls()
{
ArrayList<Wall> walls = myPanel.getWalls();
for(int i = 0 ; i < walls.size(); i++)
{
if(areRectsColliding(x,x+HEIGHT,y,y+WIDTH,walls.get(i).getX(), walls.get(i).getX()+ walls.get(i).getHEIGHT(),
walls.get(i).getY(),walls.get(i).getY()+walls.get(i).getWIDTH()))
{
return true;
}
}
return false;
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
private boolean areRectsColliding(int r1TopLeftX, int r1BottomRightX,
int r1TopLeftY, int r1BottomRightY, int r2TopLeftX,
int r2BottomRightX, int r2TopLeftY, int r2BottomRightY) {
if (r1TopLeftX < r2BottomRightX && r1BottomRightX > r2TopLeftX
&& r1TopLeftY < r2BottomRightY && r1BottomRightY > r2TopLeftY) {
return true;
} else {
return false;
}
}
@Override
public ImageIcon getImageIcon() {
// TODO Auto-generated method stub
return new ImageIcon(imagePath);
}
}
答案 0 :(得分:1)
checkOffScreen看起来就像是你玩家的初始化,而不是游戏的主循环。重新绘制时需要检查x,就像移动汽车图像时一样,每次打电话移动汽车图像时,都需要检查x轴后的x轴。被感动了 伪代码
Player.checkOffScreen();
myPanel.repaint();
这应该在它达到给定的参数后将汽车移回到开头。