【发布时间】:2015-04-24 01:34:50
【问题描述】:
我正在尝试在 Java Swing GUI 中为我的突围游戏创建一些动画游戏文本。
预期行为:每次击中砖块时,它的点都会撞击屏幕,暂停 0.25 秒,然后消失。
我做了什么: 在一个名为AlertText 的类的方法中使用了一个计时器。当GameLogic 类中的砖块被击中时,会创建一个新的AlertText,并且它的计时器开始运行。现在在游戏类中,我有了绘画类。
问题:那么我如何调用在GameLogic 中创建的AlertText 的特定实例以使用setter 和getter 方法在@987654327 中设置我的g.drawString @ 班级。我觉得这应该是一种常见的技术?有名字吗?
我让它与全局变量一起用于一种风格的砖块,所以我知道动画正在工作,但我需要 100 多个全局变量来制作每种砖块。
游戏类
public class Game extends JPanel
{
public static final int HEIGHT = 720;
public static final int WIDTH = 600;
public Color color;
private GameLogic gl = new GameLogic();
private KeyboardController controller;
public Paddle player = new Paddle(110, HEIGHT-30, 100, 10, 10, color.black, controller);
public Ball gameBall = new Ball(300, 300, 15, color.black);
private boolean PaddleUpdateComplete = false;
private List<AlertText> activeAlerts = new ArrayList<AlertText>();
Game game = new Game();
public void spawnNewAlert(Brick b){
AlertText alert = new AlertText();
alert.setxPos(b.getXPosition());
alert.setyPos(b.getYPosition());
alert.setText(b.getPoints()+"");
alert.setColor(b.getColor());
activeAlerts.add(alert);
alert.fireText();
}
@Override
public void paint(Graphics g)
{
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.WHITE);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, (int)AlertText.staticAlertSize));
g.setColor(new Color(AlertText.staticRed, AlertText.staticGreen, AlertText.staticBlue));
g.drawString(AlertText.staticAlertOne, AlertText.staticAlertXPos, AlertText.staticAlertYPos);
player.draw((Graphics2D)g);
gameBall.draw((Graphics2D)g);
gl.drawBricks(g);
// Draw GameObjects and anything else here
g.setFont(scoreFont);
g.drawString("Score: " + player.getScore(), 10, 25);
g.drawString("LIVES: " + player.getLives(), 150, 25);
if(gl.gameOver(player) &&
gameBall.getYPosition() >= HEIGHT){
g.setColor(Color.black);
g.setFont(endFont);
g.drawString("Game Over! Score: " + player.getScore(), (WIDTH/2) - 85, (HEIGHT/2));
}
if(gl.empty()){
g.setColor(Color.black);
g.setFont(endFont);
g.drawString("You won! Score: " + player.getScore(), (WIDTH/2) - 85, (HEIGHT/2));
timer.stop();
}
if(PowerUps.isMegaPaddle){
g.setColor(Color.orange);
g.setFont(TimeFont);
g.drawString(PowerUps.megaPaddlecount+"", 300, 500);
}
if(PowerUps.isMegaBall){
g.setColor(Color.red);
g.setFont(TimeFont);
g.drawString(PowerUps.megaBallcount+"", 250, 400);
}
if(!game.activeAlerts.isEmpty()){
for(AlertText alert: game.activeAlerts){
g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, alert.getTextSize()));
g.setColor(alert.getColor());
g.drawString(alert.getText(), alert.getxPos(), alert.getxPos());
if(alert.count<=0){
game.activeAlerts.remove(alert);
}
}
}
}
public void updateGameState()
{
gameBall.move();
player.move(controller);
gl.checkCollisions(gameBall, player, timer, WIDTH, HEIGHT, game);
gl.removeBrick();
// Move GameObjects and check for collisions here
if(Paddle.paddleHits==1 && !PaddleUpdateComplete){
gameBall.setXVelocity(10);
gameBall.setYVelocity(gameBall.getYVelocity()-6);
PaddleUpdateComplete = true;
}
}
public final void setupGame()
{
gameBall.setXVelocity(0);
gameBall.setYVelocity(-10);
player.setLives(5);
gl.makeBricks();
// Instantiate instance variables here
}
// Constructor method should not be modified
public Game()
{
// Set the size of the Panel to the correct size
this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
// Set the background color of the Panel to black
this.setBackground(Color.BLACK);
// Instantiate a KeyboardController and listen for input with it
controller = new KeyboardController();
this.addKeyListener(controller);
// Call the setupGame method to initialize instance variables
this.setupGame();
// Get focus in the window
this.setFocusable(true);
this.requestFocusInWindow();
}
// Start method should not be modified
public void start()
{
// Set up a new Timer to repeat every 20 milliseconds (50 FPS)
timer = new Timer(20, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
repaint();
updateGameState();
}
});
timer.setRepeats(true);
timer.start();
}
Timer timer;
}
警报类方法fireText()
public void fireText(){
count = 50;
textSize=0;
Firing=true;
Timer time = new Timer(50, null);
time.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(count>47){
textSize+=10;
xPos-=2;
count--;
}
else if(count>42){
count--;
}
else {
yPos -= 1;
xPos += 1;
textSize -= 1;
count--;
if(count<=0) {
text ="";
count=-1;
Firing =false;
time.stop();
}
}
}
});
time.start();
}
游戏逻辑方法
public void checkCollisions(Ball ball, Paddle player, Timer time, int WIDTH, int HEIGHT, Game game) {
if(hitPaddle(ball, player)){
ball.setYVelocity(ball.getYVelocity() * -1);
Paddle.paddleHits++;
return;
}
//check if ball hit any walls
if(ball.getXPosition() >= (WIDTH - ball.getDiameter()) || ball.getXPosition() <= 0){
ball.setXVelocity(ball.getXVelocity() * -1);
}
if(ball.getYPosition() > (player.getYPosition() + player.getHeight() + 10)){
resetBall(ball, player, time, WIDTH, HEIGHT);
}
if(ball.getYPosition() <= 0){
ball.setYVelocity(ball.getYVelocity() * -1);
}
//handle collisions between bricks
int brickRowsActive = 0;
for(ArrayList<Brick> alb : bricks){
if(alb.size() == horizontalCount){
++brickRowsActive;
}
}
for(int i = (brickRowsActive==0) ? 0 : (brickRowsActive - 1); i < bricks.size(); ++i){
for(Brick b : bricks.get(i)){
if(brickHitByBall(ball, b)){
checkPowerUps(b, player, ball);
game.spawnNewAlert(b);
player.setScore(player.getScore() + b.getPoints());
b.decrementType();
}
}
}
}
【问题讨论】:
-
你能显示一些相关的代码吗?
-
Swing 还是 Android?不能两者兼有。