首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单的小程序游戏错误

简单的小程序游戏错误
EN

Stack Overflow用户
提问于 2013-05-14 02:47:44
回答 1查看 95关注 0票数 0

我的applet出了点问题。它的目的是创建一款游戏,你可以在游戏中移动屏幕底部的积木,以适应从上面掉下来的积木上的洞。我已经实现了块,但是当我按下一个键的时候,我不能让块移动。以下是我的源代码:

代码语言:javascript
复制
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MathewBorumGame extends JApplet
{
    public void init()
    {
        Dimension dim = getSize();
        MathewBorumGamePanel avoidance = new MathewBorumGamePanel(dim);
        add(avoidance);
        Thread game = new Thread(avoidance);
        game.start();
    }
}// end of the extended JApplet class

class MathewBorumGamePanel extends JPanel implements Runnable{
    protected int x = 225;
    protected int y = 450;
    private int speed = 0;
    private boolean isPlaying = false;
    private boolean hasHit = false;
    private String instructions = "Press 'Enter' to start survive as long as"
        + " as possible.";
    private String keyInstructions = "Use arrow keys to move.";
    private String test;
    private Dimension dim = null;
    private Thread game = null;
    private Timer timer = new Timer(1000, new TimerListener());
    public MathewBorumGamePanel(Dimension paramDimension) {
        this.dim = paramDimension;
        setBackground(new Color(255, 255, 204));
        setForeground(Color.black);
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                        switch (e.getKeyCode()) {
                    case KeyEvent.VK_LEFT:
                        if(x >= 10) x = x - 10;
                        repaint();
                        break;
                    case KeyEvent.VK_RIGHT:
                        if(x <= 490) x = x + 10;
                        repaint();
                        break;
                    default:
                        repaint();
                        break;
                }
            }
        });
    }
    public void run() {
        for (;;) {
            repaint();
        }
    }
    public void paintComponent(Graphics paramGraphics) {
        super.paintComponent(paramGraphics);
        paramGraphics.fillRect(x, y, 50, 15);
        paramGraphics.setFont(new Font("Serif", Font.BOLD, 15));
        paramGraphics.drawString(this.instructions, 30, 30);
        paramGraphics.drawString(this.keyInstructions, 30, 50);
        test = test.valueOf(x);
        paramGraphics.drawString(test, 30, 70);
    }

    private class TimerListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            repaint();
        }
    }
}//end of the extended JPanel class

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-14 21:49:07

有关更多提示,请参阅代码中的注释。

代码语言:javascript
复制
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// <applet code='MathewBorumGame' width=400 height=400></applet>
public class MathewBorumGame extends JApplet
{
    public void init()
    {
        Dimension dim = getSize();
        MathewBorumGamePanel avoidance = new MathewBorumGamePanel(dim);
        add(avoidance);
        /*
        Thread game = new Thread(avoidance);
        game.start();*/
    }
}// end of the extended JApplet class

class MathewBorumGamePanel extends JPanel { //implements Runnable{
    protected int x = 225;
    protected int y = 450;
    private int speed = 0;
    private boolean isPlaying = false;
    private boolean hasHit = false;
    private String instructions = "Press 'Enter' to start survive as long as"
        + " as possible.";
    private String keyInstructions = "Use arrow keys to move.";
    private String test;
    private Dimension dim = null;
    private Thread game = null;
    private Timer timer = new Timer(1000, new TimerListener());
    public MathewBorumGamePanel(Dimension paramDimension) {
        this.dim = paramDimension;
        setBackground(new Color(255, 255, 204));
        setForeground(Color.black);
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                        switch (e.getKeyCode()) {
                    case KeyEvent.VK_LEFT:
                        System.out.println("Go left");
                        if(x >= 10) x = x - 10;
                        repaint();
                        break;
                    case KeyEvent.VK_RIGHT:
                        System.out.println("Go right");
                        if(x <= 490) x = x + 10;
                        repaint();
                        break;
                    default:
                        //repaint();
                        break;
                }
            }
        });
        // Must be focusable!
        setFocusable(true);
        // very important!  Must have focus..
        requestFocusInWindow();
        timer.start();
    }

    /*  THIS IS NOT HOW TO DO ANIMATION!
    @Override
    public void run() {
        for (;;) {
            repaint();
        }
    }*/

    public void paintComponent(Graphics paramGraphics) {
        super.paintComponent(paramGraphics);
        paramGraphics.fillRect(x, y, 50, 15);
        paramGraphics.setFont(new Font("Serif", Font.BOLD, 15));
        paramGraphics.drawString(this.instructions, 30, 30);
        paramGraphics.drawString(this.keyInstructions, 30, 50);
        test = test.valueOf(x);
        paramGraphics.drawString(test, 30, 70);
    }

    private class TimerListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            repaint();
        }
    }
}//end of the extended JPanel class
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16529005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档