我是一名初学者,正在开发一个游戏程序,玩家需要通过移动一个绿色方块来避开红色方块到达目的地。但是我不知道如何创建另一个移动的红色方块而不必每次都创建一个新的enemyMovement2()
方法,这样似乎效率很低,因为如果我想创建多个敌人怎么办?有没有办法将这种行为作为一个arraylist
以实现复用?
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Game extends JPanel implements KeyListener, ActionListener {
private static boolean dead = false;
private static boolean won = false;
boolean isPaused=false;
boolean hasStarted=false;
int level = 1;
boolean powerUpIsEaten;
boolean keepPowerUpOffScreen;
int powerupx;
int powerupy;
int powerupwidth=20;
int powerupheight=20;
int winningx;
int winningy;
int winningwidth;
int winningheight;
int losingx;
int losingy;
int losingwidth;
int losingheight;
int losing2x;
int losing2y;
int losing2width;
int losing2height;
int losing3x;
int losing3y;
int losing3width;
int losing3height;
int losing4x;
int losing4y;
int losing4width;
int losing4height;
int losing5x;
int losing5y;
int losing5width;
int losing5height;
int enemywidth;
int enemyheight;
int userwidth;
int userheight;
ArrayList<Integer> badRectangles = new ArrayList<Integer>();
int x = 1, y = 1;
int enemyX=400, enemyY=100;
int velx=0, vely=0;
boolean hasWon = false, hasDied = false;
int movementcounter=0;
private void move() throws InterruptedException {
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
if (vely > 0){
if(y>700-userheight *2){
}
else{
y = y + 5 + movementcounter/300;
}
}
if (vely < 0){
if(y<0){
}
else{
y = y - 5 - movementcounter/300;
}
}
if (velx > 0){
if(x>1200-userwidth){
}
else{
x = x + 5 + movementcounter/300;
}
}
if (velx < 0){
if(x<0){
}
else{
x = x - 5 - movementcounter/300;
}
}
if(isInARectangle()){
hasDied=true;
}
if(isInWinningArea()){
x=0;
y=0;
hasWon=true;
}
movementcounter++;
}
@Override
public void paint(Graphics g) {
super.paint(g);
getValues(level);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setFont(new Font("TimesRoman", Font.PLAIN, 30));
if(level==5){
g2d.drawString("Congratulations", 550, 350);
this.won=true;
}
else if(hasStarted
&& !isPaused){
eatPowerup();
g2d.setColor(Color.RED);
for(int i=1; i<=level; i++){
g2d.fillRect(badRectangles.get(4 * i - 4),badRectangles.get(4 * i - 3),badRectangles.get(4 * i - 2),badRectangles.get(4 * i - 1));
}
g2d.setColor(Color.GREEN);
g2d.fillRect(winningx, winningy, winningwidth, winningheight); //WINNING RECTANGLE
g2d.setColor(Color.RED);
g2d.fillRect(enemyX, enemyY, enemywidth, enemyheight); //ENEMY
enemyMovement();
if(!hasDied){
g2d.setColor(Color.GREEN);
g2d.fillRect(x, y, userwidth, userheight); //USER
}
if(hasWon
&& !hasDied){
level++;
if(level==2){
enemyX=1000;
enemyY=0;
}
if(level==3){
enemyX=700;
enemyY=0;
}
if(level==4){
enemyX=400;
enemyY=100;
}
powerUpIsEaten=false;
hasWon=false;
keepPowerUpOffScreen=false;
}
if(hasDied){
g2d.setColor(Color.RED);
g2d.drawString("You died", 500, 300); //LOSING SEQUENCE
this.dead=hasDied;
}
if(!powerUpIsEaten
&& level>1
&& !keepPowerUpOffScreen){
g2d.setColor(Color.BLUE);
g2d.fillRect(powerupx, powerupy, powerupwidth, powerupheight);
}
else{
keepPowerUpOffScreen=true;
powerUpIsEaten=false;
}
}
if(isPaused){
g2d.drawString("PAUSED", 450, 300);
}
if(!hasStarted){
g2d.drawString("PRESS UP TO START", 450, 300);
}
}
public void eatPowerup(){
if(powerupx<x + userwidth + 5
&& powerupx>x
&& powerupy<y + userheight + 5
&& powerupy>y){
powerUpIsEaten=true;
powerupx=0;
powerupy=1000;
enemyX=0;
enemyY=0;
}
else{
powerUpIsEaten=false;
}
}
public boolean isInWinningArea(){
if(x<winningx + winningwidth - userwidth
&& x>winningx
&& y<winningy + winningheight-userheight
&& y>winningy){
return true;
}
else{
return false;
}
}
public boolean isInARectangle(){
if((x > losingx-userwidth
&& x < losingx + losingwidth
&& y > losingy-userheight
&& y < losingy + losingheight)
|| x > losing2x-userwidth
&& x < losing2x + losing2width
&& y > losing2y-userheight
&& y < losing2y + losing2height
|| x > losing3x-userwidth
&& x < losing3x + losing3width
&& y > losing3y-userheight
&& y < losing3y + losing3height
|| x > losing4x-userwidth
&& x < losing4x + losing4width
&& y > losing4y-userheight
&& y < losing4y + losing4height){
return true;
}
else{
return false;
}
}
public void getValues(int level){
if(level==1){
winningx=550;
winningy=550;
winningwidth=100;
winningheight=100;
losingx=200;
losingy=100;
losingwidth=200;
losingheight=200;
enemywidth=60;
enemyheight=60;
userwidth=50;
userheight=50;
}
if(level==2){
winningx=1000;
winningy=500;
winningwidth=200;
winningheight=200;
losingx=200;
losingy=100;
losingwidth=200;
losingheight=200;
losing2x=700;
losing2y=500;
losing2width=300;
losing2height=200;
enemywidth=60;
enemyheight=60;
userwidth=50;
userheight=50;
powerupx=900;
powerupy=100;
}
if(level==3){
winningx=1000;
winningy=500;
winningwidth=200;
winningheight=200;
losingx=200;
losingy=100;
losingwidth=200;
losingheight=200;
losing2x=700;
losing2y=500;
losing2width=300;
losing2height=200;
losing3x=100;
losing3y=100;
losing3width=100;
losing3height=450;
enemywidth=60;
enemyheight=60;
userwidth=50;
userheight=50;
powerupx=100;
powerupy=600;
}
if(level==4){
winningx=1000;
winningy=500;
winningwidth=200;
winningheight=200;
losingx=200;
losingy=100;
losingwidth=200;
losingheight=200;
losing2x=700;
losing2y=500;
losing2width=300;
losing2height=200;
losing3x=100;
losing3y=100;
losing3width=100;
losing3height=450;
losing4x=900;
losing4y=300;
losing4width=500;
losing4height=100;
enemywidth=60;
enemyheight=60;
userwidth=50;
userheight=50;
powerupx=600;
powerupy=500;
}
if (level==5){
}
badRectangles.clear();
badRectangles.add(losingx);
badRectangles.add(losingy);
badRectangles.add(losingwidth);
badRectangles.add(losingheight);
badRectangles.add(losing2x);
badRectangles.add(losing2y);
badRectangles.add(losing2width);
badRectangles.add(losing2height);
badRectangles.add(losing3x);
badRectangles.add(losing3y);
badRectangles.add(losing3width);
badRectangles.add(losing3height);
badRectangles.add(losing4x);
badRectangles.add(losing4y);
badRectangles.add(losing4width);
badRectangles.add(losing4height);
badRectangles.add(losing5x);
badRectangles.add(losing5y);
badRectangles.add(losing5width);
badRectangles.add(losing5height);
}
public void enemyMovement(){
if(enemyX<x){
enemyX= enemyX+1 + movementcounter/300;
}
if(enemyX>x){
enemyX=enemyX-1 - movementcounter/300;
}
if(enemyY<y){
enemyY=enemyY + 1 + movementcounter/300;
}
if(enemyY>y){
enemyY=enemyY -1 - movementcounter/300;
}
if(x<enemyX+enemywidth
&& x>enemyX-userwidth
&& y<enemyY+ enemyheight
&& y>enemyY-userheight){
hasDied=true;
}
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch( key ) {
case KeyEvent.VK_UP:
isPaused=false;
hasStarted=true;
vely = -1;
break;
case KeyEvent.VK_DOWN:
vely = 1;
break;
case KeyEvent.VK_LEFT:
velx = -1;
break;
case KeyEvent.VK_RIGHT :
velx = 1;
break;
case KeyEvent.VK_P:
isPaused=true;
}
}
@Override
public void keyReleased(KeyEvent e) {
velx = 0;
vely = 0;
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Square Game");
Game game = new Game();
frame.add(game);
frame.setSize(1200, 700);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground( Color.BLUE );
while (!dead
&& !won) {
game.move();
game.repaint();
Thread.sleep(10);
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
paint
,而考虑使用paintComponent
。避免使用KeyListener
,而考虑使用Key Bindings API。 - MadProgrammerThread
时要小心,考虑使用 Swing 的Timer
。请参阅Swing 中的并发和 如何使用 Swing 定时器获取更多详细信息。 - MadProgrammer