我做了一个在键盘上移动玩家的游戏,通过随机吃硬币来增加分数。当你触摸tnt时,hp降低,当你触摸咖啡时,hp升高。使用图形g在屏幕上打印。现在我要让游戏开始屏幕。我已经为另一个类创建了一个开始按钮,当你按下按钮时,会出现一个新的游戏窗口。但我看不到游戏屏幕,而且它落后了。即使是关闭按钮也不能很好地按下,所以我们要通过待办事项管理器来强制它。我能知道问题出在哪里吗?如果游戏不是作为一个新窗口运行,而是从同一个窗口出来,那就没关系了。请帮帮我。我将依次向您展示start button类和game类。我的英语不好,所以谢谢你看了这个长问题。
★★★ 开始按钮类★★★
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class gamestart {
public static void main(String[] args) {
JFrame frame = new JFrame("Start up frame");
JButton newGameButton = new JButton("New Game");
frame.setLayout(new FlowLayout());
frame.add(newGameButton);
frame.setVisible(true);
newGameButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFrame newGameWindow = new JFrame("A new game!");
newGameWindow.setVisible(true);
newGameWindow.add(new CoinEat1());
newGameWindow.pack();
}
});
frame.pack();
}
}
★★★ 游戏类★★★
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class CoinEat1 extends JFrame {
private Image bufferImage;
private Graphics screenGraphic;
private Clip clip;
private Image backgroundImage = new ImageIcon("src/images/배경.jpg").getImage();
private Image player = new ImageIcon("src/images/1P.png").getImage();
private Image coin = new ImageIcon("src/images/코인.png").getImage();
private Image TNT = new ImageIcon("src/images/폭탄.png").getImage();
private Image Coffee = new ImageIcon("src/images/커피.png").getImage();
private int playerX,playerY;
private int playerWidth = player.getWidth(null);
private int playerHeight = player.getHeight(null);
private int coinX,coinY;
private int coinWidth = coin.getWidth(null);
private int coinHeight = coin.getHeight(null);
private int TNTX,TNTY;
private int TNTWidth = TNT.getWidth(null);
private int TNTHeight = TNT.getHeight(null);
private int CoffeeX,CoffeeY;
private int CoffeeWidth = Coffee.getWidth(null);
private int CoffeeHeight = Coffee.getHeight(null);
private int score;
private int HP;
private boolean playerup, down, left, right;
public CoinEat1() {
setTitle("동전JAVA");
setVisible(true);
setSize(Main.SCREEN_WIDTH,Main.SCREEN_HEIGHT);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_UP:
playerup=true;
break;
case KeyEvent.VK_DOWN:
down=true;
break;
case KeyEvent.VK_LEFT:
left=true;
break;
case KeyEvent.VK_RIGHT:
right=true;
break;
}
}
public void keyReleased(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_UP:
playerup=false;
break;
case KeyEvent.VK_DOWN:
down=false;
break;
case KeyEvent.VK_LEFT:
left=false;
break;
case KeyEvent.VK_RIGHT:
right=false;
break;
}
}
});
Init();
while(true){
try {
Thread.sleep(20);
} catch(InterruptedException e) {
e.printStackTrace();
}
keyProcess();
CoinCrashCheck();
TNTCrashCheck();
CoffeeCrashCheck();
}
}
public void Init() {
score = 0;
HP = 100;
playerX = (Main.SCREEN_WIDTH-playerWidth)/2;
playerY = (Main.SCREEN_HEIGHT-playerHeight)/2;
coinX = (int)(Math.random()*(Main.SCREEN_WIDTH+1-playerWidth));
coinY = (int)(Math.random()*(Main.SCREEN_HEIGHT+1-playerHeight-30))+30;
TNTX = (int)(Math.random()*(Main.SCREEN_WIDTH+1-playerWidth));
TNTY = (int)(Math.random()*(Main.SCREEN_HEIGHT+1-playerHeight-30))+30;
CoffeeX = (int)(Math.random()*(Main.SCREEN_WIDTH+1-playerWidth));
CoffeeY = (int)(Math.random()*(Main.SCREEN_HEIGHT+1-playerWidth))+30;
playsound("src/music/BGM.wav",true);
}
public void keyProcess() {
if (playerup && playerY - 3 > 30) playerY-=3;
if (down && playerY + playerHeight + 3 < Main.SCREEN_HEIGHT) playerY+=3;
if (left && playerX -3 > 0) playerX-=3;
if (right && playerX + playerWidth + 3 < Main.SCREEN_WIDTH) playerX+=3;
}
public void CoinCrashCheck(){
if (playerX+playerWidth > coinX && coinX+coinWidth > playerX && playerY+playerHeight > coinY && coinY+coinHeight > playerY){
score += 100;
playsound("src/music/coin.wav",false);
coinX = (int)(Math.random()*(Main.SCREEN_WIDTH+1-playerWidth));
coinY = (int)(Math.random()*(Main.SCREEN_HEIGHT+1-playerHeight-30))+30;
}
}
public void TNTCrashCheck() {
if (playerX+playerWidth > TNTX && TNTX+TNTWidth > playerX && playerY+playerHeight > TNTY && TNTY+TNTHeight > playerY){
HP -= 25;
TNTX = (int)(Math.random()*(Main.SCREEN_WIDTH+1-playerWidth));
TNTY = (int)(Math.random()*(Main.SCREEN_HEIGHT+1-playerHeight-30))+30;
}
}
public void CoffeeCrashCheck() {
if (playerX+playerWidth > CoffeeX && CoffeeX+CoffeeWidth > playerX && playerY+playerHeight > CoffeeY && CoffeeY+CoffeeHeight > playerY){
HP += 25;
CoffeeX = (int)(Math.random()*(Main.SCREEN_WIDTH+1-playerWidth));
CoffeeY = (int)(Math.random()*(Main.SCREEN_HEIGHT+1-playerHeight-30))+30;
}
}
public void playsound(String pathName, boolean isLoop) {
try {
clip = AudioSystem.getClip();
File audioFile = new File(pathName);
AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
clip.open(audioStream);
clip.start();
if(isLoop)
clip.loop(Clip.LOOP_CONTINUOUSLY);
}catch (LineUnavailableException e) {
e.printStackTrace();
}
catch (UnsupportedAudioFileException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
bufferImage = createImage(Main.SCREEN_WIDTH,Main.SCREEN_HEIGHT);
screenGraphic = bufferImage.getGraphics();
screenDraw(screenGraphic);
g.drawImage(bufferImage,0,0,null);
}
public void screenDraw(Graphics g){//이미지를 출력해줄 paint 메소드
g.drawImage(backgroundImage,0,0,null);
g.drawImage(player,playerX,playerY,null);
g.drawImage(coin,coinX,coinY,null);
g.drawImage(TNT,TNTX,TNTY,null);
g.drawImage(Coffee,CoffeeX,CoffeeY,null);
g.setColor(Color.white);
g.setFont(new Font("Arial",Font.BOLD,35));
g.drawString("SCORE : "+score, 30, 80);
g.drawString("HP : " + HP, 30, 120);
this.repaint();
}
public static void main(String[] args) {
new CoinEat1();
}
}
暂无答案!
目前还没有任何答案,快来回答吧!